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; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 39 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 40 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 41 | return 0; |
| 42 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 43 | if (unformat |
| 44 | (line_input, "%U %u", unformat_vnet_sw_interface, im->vnet_main, |
| 45 | &sw_if_index, &spd_id)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 46 | ; |
| 47 | else if (unformat (line_input, "del")) |
| 48 | is_add = 0; |
| 49 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 50 | { |
| 51 | error = clib_error_return (0, "parse error: '%U'", |
| 52 | format_unformat_error, line_input); |
| 53 | goto done; |
| 54 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 55 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 56 | ipsec_set_interface_spd (vm, sw_if_index, spd_id, is_add); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 57 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 58 | done: |
| 59 | unformat_free (line_input); |
| 60 | |
| 61 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 64 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 65 | VLIB_CLI_COMMAND (set_interface_spd_command, static) = { |
| 66 | .path = "set interface ipsec spd", |
| 67 | .short_help = |
| 68 | "set interface ipsec spd <int> <id>", |
| 69 | .function = set_interface_spd_command_fn, |
| 70 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 71 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 72 | |
| 73 | static clib_error_t * |
| 74 | ipsec_sa_add_del_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 75 | unformat_input_t * input, |
| 76 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 77 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 78 | unformat_input_t _line_input, *line_input = &_line_input; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 79 | ip46_address_t tun_src = { }, tun_dst = |
| 80 | { |
| 81 | }; |
| 82 | ipsec_crypto_alg_t crypto_alg; |
| 83 | ipsec_integ_alg_t integ_alg; |
| 84 | ipsec_protocol_t proto; |
| 85 | ipsec_sa_flags_t flags; |
| 86 | clib_error_t *error; |
Kingwel Xie | d3d1205 | 2019-03-07 06:34:30 -0500 | [diff] [blame] | 87 | ipsec_key_t ck = { 0 }; |
| 88 | ipsec_key_t ik = { 0 }; |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 89 | u32 id, spi, salt, sai; |
| 90 | u16 udp_src, udp_dst; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 91 | int is_add, rv; |
Benoît Ganne | bdd8b57 | 2020-07-28 15:56:15 +0200 | [diff] [blame] | 92 | u32 m_args = 0; |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame^] | 93 | ip_dscp_t dscp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 94 | |
Neale Ranns | 8dc75c0 | 2019-12-10 01:08:19 +0000 | [diff] [blame] | 95 | salt = 0; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 96 | error = NULL; |
| 97 | is_add = 0; |
| 98 | flags = IPSEC_SA_FLAG_NONE; |
| 99 | proto = IPSEC_PROTOCOL_ESP; |
Neale Ranns | e6be702 | 2019-06-04 15:37:34 +0000 | [diff] [blame] | 100 | integ_alg = IPSEC_INTEG_ALG_NONE; |
| 101 | crypto_alg = IPSEC_CRYPTO_ALG_NONE; |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 102 | udp_src = udp_dst = IPSEC_UDP_PORT_NONE; |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame^] | 103 | dscp = IP_DSCP_CS0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 104 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 105 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 106 | return 0; |
| 107 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 108 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 109 | { |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 110 | if (unformat (line_input, "add %u", &id)) |
Benoît Ganne | bdd8b57 | 2020-07-28 15:56:15 +0200 | [diff] [blame] | 111 | { |
| 112 | is_add = 1; |
| 113 | m_args |= 1 << 0; |
| 114 | } |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 115 | else if (unformat (line_input, "del %u", &id)) |
Benoît Ganne | bdd8b57 | 2020-07-28 15:56:15 +0200 | [diff] [blame] | 116 | { |
| 117 | is_add = 0; |
| 118 | m_args |= 1 << 0; |
| 119 | } |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 120 | else if (unformat (line_input, "spi %u", &spi)) |
Benoît Ganne | bdd8b57 | 2020-07-28 15:56:15 +0200 | [diff] [blame] | 121 | m_args |= 1 << 1; |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 122 | else if (unformat (line_input, "salt 0x%x", &salt)) |
Neale Ranns | 80f6fd5 | 2019-04-16 02:41:34 +0000 | [diff] [blame] | 123 | ; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 124 | else if (unformat (line_input, "esp")) |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 125 | proto = IPSEC_PROTOCOL_ESP; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 126 | else if (unformat (line_input, "ah")) |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 127 | proto = IPSEC_PROTOCOL_AH; |
| 128 | else if (unformat (line_input, "crypto-key %U", |
| 129 | unformat_ipsec_key, &ck)) |
| 130 | ; |
| 131 | else if (unformat (line_input, "crypto-alg %U", |
| 132 | unformat_ipsec_crypto_alg, &crypto_alg)) |
| 133 | ; |
| 134 | else if (unformat (line_input, "integ-key %U", unformat_ipsec_key, &ik)) |
| 135 | ; |
| 136 | else if (unformat (line_input, "integ-alg %U", |
| 137 | unformat_ipsec_integ_alg, &integ_alg)) |
| 138 | ; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 139 | else if (unformat (line_input, "tunnel-src %U", |
Kingwel Xie | 72b3bce | 2019-02-12 06:45:08 -0500 | [diff] [blame] | 140 | unformat_ip46_address, &tun_src, IP46_TYPE_ANY)) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 141 | { |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 142 | flags |= IPSEC_SA_FLAG_IS_TUNNEL; |
| 143 | if (!ip46_address_is_ip4 (&tun_src)) |
| 144 | flags |= IPSEC_SA_FLAG_IS_TUNNEL_V6; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 145 | } |
| 146 | else if (unformat (line_input, "tunnel-dst %U", |
Kingwel Xie | 72b3bce | 2019-02-12 06:45:08 -0500 | [diff] [blame] | 147 | unformat_ip46_address, &tun_dst, IP46_TYPE_ANY)) |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 148 | ; |
Christian Hopps | 9997538 | 2020-07-17 09:53:18 -0400 | [diff] [blame] | 149 | else if (unformat (line_input, "inbound")) |
| 150 | flags |= IPSEC_SA_FLAG_IS_INBOUND; |
| 151 | else if (unformat (line_input, "use-anti-replay")) |
| 152 | flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY; |
| 153 | else if (unformat (line_input, "use-esn")) |
| 154 | flags |= IPSEC_SA_FLAG_USE_ESN; |
Radu Nicolau | 717de09 | 2018-08-03 10:37:24 +0100 | [diff] [blame] | 155 | else if (unformat (line_input, "udp-encap")) |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 156 | flags |= IPSEC_SA_FLAG_UDP_ENCAP; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 157 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 158 | { |
| 159 | error = clib_error_return (0, "parse error: '%U'", |
| 160 | format_unformat_error, line_input); |
| 161 | goto done; |
| 162 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 163 | } |
Christian Hopps | 9997538 | 2020-07-17 09:53:18 -0400 | [diff] [blame] | 164 | if ((flags & IPSEC_SA_FLAG_IS_INBOUND) |
| 165 | && !(flags & IPSEC_SA_FLAG_IS_TUNNEL)) |
| 166 | { |
| 167 | error = clib_error_return (0, "inbound specified on non-tunnel SA"); |
| 168 | goto done; |
| 169 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 170 | |
Benoît Ganne | bdd8b57 | 2020-07-28 15:56:15 +0200 | [diff] [blame] | 171 | if (!(m_args & 1)) |
| 172 | { |
| 173 | error = clib_error_return (0, "missing id"); |
| 174 | goto done; |
| 175 | } |
| 176 | |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 177 | if (is_add) |
Benoît Ganne | bdd8b57 | 2020-07-28 15:56:15 +0200 | [diff] [blame] | 178 | { |
| 179 | if (!(m_args & 2)) |
| 180 | { |
| 181 | error = clib_error_return (0, "missing spi"); |
| 182 | goto done; |
| 183 | } |
| 184 | rv = ipsec_sa_add_and_lock (id, spi, proto, crypto_alg, |
| 185 | &ck, integ_alg, &ik, flags, |
| 186 | 0, clib_host_to_net_u32 (salt), |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame^] | 187 | &tun_src, &tun_dst, |
| 188 | TUNNEL_ENCAP_DECAP_FLAG_NONE, dscp, |
| 189 | &sai, udp_src, udp_dst); |
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 | e6be702 | 2019-06-04 15:37:34 +0000 | [diff] [blame] | 197 | error = clib_error_return (0, "failed"); |
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; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 282 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 283 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 284 | return 0; |
| 285 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 286 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 287 | { |
| 288 | if (unformat (line_input, "add")) |
| 289 | is_add = 1; |
| 290 | else if (unformat (line_input, "del")) |
| 291 | is_add = 0; |
Neale Ranns | b1fd80f | 2020-05-12 13:33:56 +0000 | [diff] [blame] | 292 | else if (unformat (line_input, "ip6")) |
| 293 | p.is_ipv6 = 1; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 294 | else if (unformat (line_input, "spd %u", &p.id)) |
| 295 | ; |
| 296 | else if (unformat (line_input, "inbound")) |
Neale Ranns | 9f231d4 | 2019-03-19 10:06:00 +0000 | [diff] [blame] | 297 | is_outbound = 0; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 298 | else if (unformat (line_input, "outbound")) |
Neale Ranns | 9f231d4 | 2019-03-19 10:06:00 +0000 | [diff] [blame] | 299 | is_outbound = 1; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 300 | else if (unformat (line_input, "priority %d", &p.priority)) |
| 301 | ; |
| 302 | else if (unformat (line_input, "protocol %u", &tmp)) |
| 303 | p.protocol = (u8) tmp; |
| 304 | else |
| 305 | if (unformat |
| 306 | (line_input, "action %U", unformat_ipsec_policy_action, |
| 307 | &p.policy)) |
| 308 | { |
| 309 | if (p.policy == IPSEC_POLICY_ACTION_RESOLVE) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 310 | { |
| 311 | error = clib_error_return (0, "unsupported action: 'resolve'"); |
| 312 | goto done; |
| 313 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 314 | } |
| 315 | else if (unformat (line_input, "sa %u", &p.sa_id)) |
| 316 | ; |
| 317 | else if (unformat (line_input, "local-ip-range %U - %U", |
| 318 | unformat_ip4_address, &p.laddr.start.ip4, |
| 319 | unformat_ip4_address, &p.laddr.stop.ip4)) |
Neale Ranns | b1fd80f | 2020-05-12 13:33:56 +0000 | [diff] [blame] | 320 | local_range_set = 1; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 321 | else if (unformat (line_input, "remote-ip-range %U - %U", |
| 322 | unformat_ip4_address, &p.raddr.start.ip4, |
| 323 | unformat_ip4_address, &p.raddr.stop.ip4)) |
Neale Ranns | b1fd80f | 2020-05-12 13:33:56 +0000 | [diff] [blame] | 324 | remote_range_set = 1; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 325 | else if (unformat (line_input, "local-ip-range %U - %U", |
| 326 | unformat_ip6_address, &p.laddr.start.ip6, |
| 327 | unformat_ip6_address, &p.laddr.stop.ip6)) |
| 328 | { |
| 329 | p.is_ipv6 = 1; |
Neale Ranns | b1fd80f | 2020-05-12 13:33:56 +0000 | [diff] [blame] | 330 | local_range_set = 1; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 331 | } |
| 332 | else if (unformat (line_input, "remote-ip-range %U - %U", |
| 333 | unformat_ip6_address, &p.raddr.start.ip6, |
| 334 | unformat_ip6_address, &p.raddr.stop.ip6)) |
| 335 | { |
| 336 | p.is_ipv6 = 1; |
Neale Ranns | b1fd80f | 2020-05-12 13:33:56 +0000 | [diff] [blame] | 337 | remote_range_set = 1; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 338 | } |
| 339 | else if (unformat (line_input, "local-port-range %u - %u", &tmp, &tmp2)) |
| 340 | { |
| 341 | p.lport.start = tmp; |
| 342 | p.lport.stop = tmp2; |
| 343 | } |
| 344 | else |
| 345 | if (unformat (line_input, "remote-port-range %u - %u", &tmp, &tmp2)) |
| 346 | { |
| 347 | p.rport.start = tmp; |
| 348 | p.rport.stop = tmp2; |
| 349 | } |
| 350 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 351 | { |
| 352 | error = clib_error_return (0, "parse error: '%U'", |
| 353 | format_unformat_error, line_input); |
| 354 | goto done; |
| 355 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 356 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 357 | |
Neale Ranns | b1fd80f | 2020-05-12 13:33:56 +0000 | [diff] [blame] | 358 | if (!remote_range_set) |
| 359 | { |
| 360 | if (p.is_ipv6) |
| 361 | clib_memset (&p.raddr.stop.ip6, 0xff, 16); |
| 362 | else |
| 363 | clib_memset (&p.raddr.stop.ip4, 0xff, 4); |
| 364 | } |
| 365 | if (!local_range_set) |
| 366 | { |
| 367 | if (p.is_ipv6) |
| 368 | clib_memset (&p.laddr.stop.ip6, 0xff, 16); |
| 369 | else |
| 370 | clib_memset (&p.laddr.stop.ip4, 0xff, 4); |
| 371 | } |
| 372 | |
Neale Ranns | 9f231d4 | 2019-03-19 10:06:00 +0000 | [diff] [blame] | 373 | rv = ipsec_policy_mk_type (is_outbound, p.is_ipv6, p.policy, &p.type); |
| 374 | |
| 375 | if (rv) |
| 376 | { |
| 377 | error = clib_error_return (0, "unsupported policy type for:", |
| 378 | " outboud:%s %s action:%U", |
| 379 | (is_outbound ? "yes" : "no"), |
| 380 | (p.is_ipv6 ? "IPv4" : "IPv6"), |
| 381 | format_ipsec_policy_action, p.policy); |
| 382 | goto done; |
| 383 | } |
| 384 | |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 385 | rv = ipsec_add_del_policy (vm, &p, is_add, &stat_index); |
| 386 | |
| 387 | if (!rv) |
| 388 | vlib_cli_output (vm, "policy-index:%d", stat_index); |
| 389 | else |
| 390 | vlib_cli_output (vm, "error:%d", rv); |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 391 | |
| 392 | done: |
| 393 | unformat_free (line_input); |
| 394 | |
| 395 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 398 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 399 | VLIB_CLI_COMMAND (ipsec_policy_add_del_command, static) = { |
| 400 | .path = "ipsec policy", |
| 401 | .short_help = |
| 402 | "ipsec policy [add|del] spd <id> priority <n> ", |
| 403 | .function = ipsec_policy_add_del_command_fn, |
| 404 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 405 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 406 | |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 407 | static void |
Neale Ranns | 670027a | 2019-08-27 12:47:17 +0000 | [diff] [blame] | 408 | 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] | 409 | { |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 410 | u32 sai; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 411 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 412 | /* *INDENT-OFF* */ |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 413 | pool_foreach_index (sai, im->sad, ({ |
Neale Ranns | 670027a | 2019-08-27 12:47:17 +0000 | [diff] [blame] | 414 | vlib_cli_output(vm, "%U", format_ipsec_sa, sai, |
| 415 | (detail ? IPSEC_FORMAT_DETAIL : IPSEC_FORMAT_BRIEF)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 416 | })); |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 417 | /* *INDENT-ON* */ |
| 418 | } |
| 419 | |
| 420 | static void |
| 421 | ipsec_spd_show_all (vlib_main_t * vm, ipsec_main_t * im) |
| 422 | { |
| 423 | u32 spdi; |
| 424 | |
| 425 | /* *INDENT-OFF* */ |
| 426 | pool_foreach_index (spdi, im->spds, ({ |
| 427 | vlib_cli_output(vm, "%U", format_ipsec_spd, spdi); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 428 | })); |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 429 | /* *INDENT-ON* */ |
| 430 | } |
| 431 | |
| 432 | static void |
| 433 | ipsec_spd_bindings_show_all (vlib_main_t * vm, ipsec_main_t * im) |
| 434 | { |
| 435 | u32 spd_id, sw_if_index; |
Neale Ranns | d83c4a8 | 2019-06-14 06:48:27 -0700 | [diff] [blame] | 436 | ipsec_spd_t *spd; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 437 | |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 438 | vlib_cli_output (vm, "SPD Bindings:"); |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 439 | |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 440 | /* *INDENT-OFF* */ |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 441 | 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] | 442 | spd = pool_elt_at_index (im->spds, spd_id); |
| 443 | vlib_cli_output (vm, " %d -> %U", spd->id, |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 444 | format_vnet_sw_if_index_name, im->vnet_main, |
| 445 | sw_if_index); |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 446 | })); |
| 447 | /* *INDENT-ON* */ |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 448 | } |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 449 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 450 | static walk_rc_t |
| 451 | ipsec_tun_protect_show_one (index_t itpi, void *ctx) |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 452 | { |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 453 | vlib_cli_output (ctx, "%U", format_ipsec_tun_protect_index, itpi); |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 454 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 455 | return (WALK_CONTINUE); |
| 456 | } |
| 457 | |
| 458 | static void |
| 459 | ipsec_tunnel_show_all (vlib_main_t * vm) |
| 460 | { |
| 461 | ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm); |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | static clib_error_t * |
| 465 | show_ipsec_command_fn (vlib_main_t * vm, |
| 466 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 467 | { |
| 468 | ipsec_main_t *im = &ipsec_main; |
| 469 | |
Neale Ranns | 670027a | 2019-08-27 12:47:17 +0000 | [diff] [blame] | 470 | ipsec_sa_show_all (vm, im, 0); |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 471 | ipsec_spd_show_all (vm, im); |
| 472 | ipsec_spd_bindings_show_all (vm, im); |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 473 | ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm); |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 474 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 475 | vlib_cli_output (vm, "IPSec async mode: %s", |
| 476 | (im->async_mode ? "on" : "off")); |
| 477 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 478 | return 0; |
| 479 | } |
| 480 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 481 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 482 | VLIB_CLI_COMMAND (show_ipsec_command, static) = { |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 483 | .path = "show ipsec all", |
| 484 | .short_help = "show ipsec all", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 485 | .function = show_ipsec_command_fn, |
| 486 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 487 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 488 | |
| 489 | static clib_error_t * |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 490 | show_ipsec_sa_command_fn (vlib_main_t * vm, |
| 491 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 492 | { |
| 493 | ipsec_main_t *im = &ipsec_main; |
| 494 | u32 sai = ~0; |
Neale Ranns | 670027a | 2019-08-27 12:47:17 +0000 | [diff] [blame] | 495 | u8 detail = 0; |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 496 | |
| 497 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 498 | { |
| 499 | if (unformat (input, "%u", &sai)) |
| 500 | ; |
Neale Ranns | 670027a | 2019-08-27 12:47:17 +0000 | [diff] [blame] | 501 | if (unformat (input, "detail")) |
| 502 | detail = 1; |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 503 | else |
| 504 | break; |
| 505 | } |
| 506 | |
| 507 | if (~0 == sai) |
Neale Ranns | 670027a | 2019-08-27 12:47:17 +0000 | [diff] [blame] | 508 | ipsec_sa_show_all (vm, im, detail); |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 509 | else |
Christian E. Hopps | 01d61e7 | 2019-09-27 14:43:22 -0400 | [diff] [blame] | 510 | vlib_cli_output (vm, "%U", format_ipsec_sa, sai, |
| 511 | IPSEC_FORMAT_DETAIL | IPSEC_FORMAT_INSECURE); |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 512 | |
| 513 | return 0; |
| 514 | } |
| 515 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 516 | static clib_error_t * |
| 517 | clear_ipsec_sa_command_fn (vlib_main_t * vm, |
| 518 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 519 | { |
| 520 | ipsec_main_t *im = &ipsec_main; |
| 521 | u32 sai = ~0; |
| 522 | |
| 523 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 524 | { |
| 525 | if (unformat (input, "%u", &sai)) |
| 526 | ; |
| 527 | else |
| 528 | break; |
| 529 | } |
| 530 | |
| 531 | if (~0 == sai) |
| 532 | { |
| 533 | /* *INDENT-OFF* */ |
| 534 | pool_foreach_index (sai, im->sad, ({ |
| 535 | ipsec_sa_clear(sai); |
| 536 | })); |
| 537 | /* *INDENT-ON* */ |
| 538 | } |
| 539 | else |
| 540 | { |
| 541 | if (pool_is_free_index (im->sad, sai)) |
| 542 | return clib_error_return (0, "unknown SA index: %d", sai); |
| 543 | else |
| 544 | ipsec_sa_clear (sai); |
| 545 | } |
| 546 | |
| 547 | return 0; |
| 548 | } |
| 549 | |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 550 | /* *INDENT-OFF* */ |
| 551 | VLIB_CLI_COMMAND (show_ipsec_sa_command, static) = { |
| 552 | .path = "show ipsec sa", |
| 553 | .short_help = "show ipsec sa [index]", |
| 554 | .function = show_ipsec_sa_command_fn, |
| 555 | }; |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 556 | |
| 557 | VLIB_CLI_COMMAND (clear_ipsec_sa_command, static) = { |
| 558 | .path = "clear ipsec sa", |
| 559 | .short_help = "clear ipsec sa [index]", |
| 560 | .function = clear_ipsec_sa_command_fn, |
| 561 | }; |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 562 | /* *INDENT-ON* */ |
| 563 | |
| 564 | static clib_error_t * |
| 565 | show_ipsec_spd_command_fn (vlib_main_t * vm, |
| 566 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 567 | { |
| 568 | ipsec_main_t *im = &ipsec_main; |
| 569 | u8 show_bindings = 0; |
| 570 | u32 spdi = ~0; |
| 571 | |
| 572 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 573 | { |
| 574 | if (unformat (input, "%u", &spdi)) |
| 575 | ; |
| 576 | else if (unformat (input, "bindings")) |
| 577 | show_bindings = 1; |
| 578 | else |
| 579 | break; |
| 580 | } |
| 581 | |
| 582 | if (show_bindings) |
| 583 | ipsec_spd_bindings_show_all (vm, im); |
| 584 | else if (~0 != spdi) |
| 585 | vlib_cli_output (vm, "%U", format_ipsec_spd, spdi); |
| 586 | else |
| 587 | ipsec_spd_show_all (vm, im); |
| 588 | |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | /* *INDENT-OFF* */ |
| 593 | VLIB_CLI_COMMAND (show_ipsec_spd_command, static) = { |
| 594 | .path = "show ipsec spd", |
| 595 | .short_help = "show ipsec spd [index]", |
| 596 | .function = show_ipsec_spd_command_fn, |
| 597 | }; |
| 598 | /* *INDENT-ON* */ |
| 599 | |
| 600 | static clib_error_t * |
| 601 | show_ipsec_tunnel_command_fn (vlib_main_t * vm, |
| 602 | unformat_input_t * input, |
| 603 | vlib_cli_command_t * cmd) |
| 604 | { |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 605 | ipsec_tunnel_show_all (vm); |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 606 | |
| 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | /* *INDENT-OFF* */ |
| 611 | VLIB_CLI_COMMAND (show_ipsec_tunnel_command, static) = { |
| 612 | .path = "show ipsec tunnel", |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 613 | .short_help = "show ipsec tunnel", |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 614 | .function = show_ipsec_tunnel_command_fn, |
| 615 | }; |
| 616 | /* *INDENT-ON* */ |
| 617 | |
| 618 | static clib_error_t * |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 619 | ipsec_show_backends_command_fn (vlib_main_t * vm, |
| 620 | unformat_input_t * input, |
| 621 | vlib_cli_command_t * cmd) |
| 622 | { |
| 623 | ipsec_main_t *im = &ipsec_main; |
| 624 | u32 verbose = 0; |
| 625 | |
| 626 | (void) unformat (input, "verbose %u", &verbose); |
| 627 | |
| 628 | vlib_cli_output (vm, "IPsec AH backends available:"); |
| 629 | u8 *s = format (NULL, "%=25s %=25s %=10s\n", "Name", "Index", "Active"); |
| 630 | ipsec_ah_backend_t *ab; |
| 631 | /* *INDENT-OFF* */ |
| 632 | pool_foreach (ab, im->ah_backends, { |
| 633 | s = format (s, "%=25s %=25u %=10s\n", ab->name, ab - im->ah_backends, |
| 634 | ab - im->ah_backends == im->ah_current_backend ? "yes" : "no"); |
| 635 | if (verbose) { |
| 636 | vlib_node_t *n; |
| 637 | n = vlib_get_node (vm, ab->ah4_encrypt_node_index); |
| 638 | s = format (s, " enc4 %s (next %d)\n", n->name, ab->ah4_encrypt_next_index); |
| 639 | n = vlib_get_node (vm, ab->ah4_decrypt_node_index); |
| 640 | s = format (s, " dec4 %s (next %d)\n", n->name, ab->ah4_decrypt_next_index); |
| 641 | n = vlib_get_node (vm, ab->ah6_encrypt_node_index); |
| 642 | s = format (s, " enc6 %s (next %d)\n", n->name, ab->ah6_encrypt_next_index); |
| 643 | n = vlib_get_node (vm, ab->ah6_decrypt_node_index); |
| 644 | s = format (s, " dec6 %s (next %d)\n", n->name, ab->ah6_decrypt_next_index); |
| 645 | } |
| 646 | }); |
| 647 | /* *INDENT-ON* */ |
| 648 | vlib_cli_output (vm, "%v", s); |
| 649 | _vec_len (s) = 0; |
| 650 | vlib_cli_output (vm, "IPsec ESP backends available:"); |
| 651 | s = format (s, "%=25s %=25s %=10s\n", "Name", "Index", "Active"); |
| 652 | ipsec_esp_backend_t *eb; |
| 653 | /* *INDENT-OFF* */ |
| 654 | pool_foreach (eb, im->esp_backends, { |
| 655 | s = format (s, "%=25s %=25u %=10s\n", eb->name, eb - im->esp_backends, |
| 656 | eb - im->esp_backends == im->esp_current_backend ? "yes" |
| 657 | : "no"); |
| 658 | if (verbose) { |
| 659 | vlib_node_t *n; |
| 660 | n = vlib_get_node (vm, eb->esp4_encrypt_node_index); |
| 661 | s = format (s, " enc4 %s (next %d)\n", n->name, eb->esp4_encrypt_next_index); |
| 662 | n = vlib_get_node (vm, eb->esp4_decrypt_node_index); |
| 663 | s = format (s, " dec4 %s (next %d)\n", n->name, eb->esp4_decrypt_next_index); |
| 664 | n = vlib_get_node (vm, eb->esp6_encrypt_node_index); |
| 665 | s = format (s, " enc6 %s (next %d)\n", n->name, eb->esp6_encrypt_next_index); |
| 666 | n = vlib_get_node (vm, eb->esp6_decrypt_node_index); |
| 667 | s = format (s, " dec6 %s (next %d)\n", n->name, eb->esp6_decrypt_next_index); |
| 668 | } |
| 669 | }); |
| 670 | /* *INDENT-ON* */ |
| 671 | vlib_cli_output (vm, "%v", s); |
| 672 | |
| 673 | vec_free (s); |
| 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | /* *INDENT-OFF* */ |
| 678 | VLIB_CLI_COMMAND (ipsec_show_backends_command, static) = { |
| 679 | .path = "show ipsec backends", |
| 680 | .short_help = "show ipsec backends", |
| 681 | .function = ipsec_show_backends_command_fn, |
| 682 | }; |
| 683 | /* *INDENT-ON* */ |
| 684 | |
| 685 | static clib_error_t * |
| 686 | ipsec_select_backend_command_fn (vlib_main_t * vm, |
| 687 | unformat_input_t * input, |
| 688 | vlib_cli_command_t * cmd) |
| 689 | { |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 690 | unformat_input_t _line_input, *line_input = &_line_input; |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 691 | ipsec_main_t *im = &ipsec_main; |
| 692 | clib_error_t *error; |
| 693 | u32 backend_index; |
| 694 | |
| 695 | error = ipsec_rsc_in_use (im); |
| 696 | |
| 697 | if (error) |
| 698 | return error; |
| 699 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 700 | /* Get a line of input. */ |
| 701 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 702 | return 0; |
| 703 | |
| 704 | if (unformat (line_input, "ah")) |
| 705 | { |
| 706 | if (unformat (line_input, "%u", &backend_index)) |
| 707 | { |
| 708 | if (ipsec_select_ah_backend (im, backend_index) < 0) |
| 709 | { |
| 710 | return clib_error_return (0, "Invalid AH backend index `%u'", |
| 711 | backend_index); |
| 712 | } |
| 713 | } |
| 714 | else |
| 715 | { |
| 716 | return clib_error_return (0, "Invalid backend index `%U'", |
| 717 | format_unformat_error, line_input); |
| 718 | } |
| 719 | } |
| 720 | else if (unformat (line_input, "esp")) |
| 721 | { |
| 722 | if (unformat (line_input, "%u", &backend_index)) |
| 723 | { |
| 724 | if (ipsec_select_esp_backend (im, backend_index) < 0) |
| 725 | { |
| 726 | return clib_error_return (0, "Invalid ESP backend index `%u'", |
| 727 | backend_index); |
| 728 | } |
| 729 | } |
| 730 | else |
| 731 | { |
| 732 | return clib_error_return (0, "Invalid backend index `%U'", |
| 733 | format_unformat_error, line_input); |
| 734 | } |
| 735 | } |
| 736 | else |
| 737 | { |
| 738 | return clib_error_return (0, "Unknown input `%U'", |
| 739 | format_unformat_error, line_input); |
| 740 | } |
| 741 | |
| 742 | return 0; |
| 743 | } |
| 744 | |
| 745 | /* *INDENT-OFF* */ |
| 746 | VLIB_CLI_COMMAND (ipsec_select_backend_command, static) = { |
| 747 | .path = "ipsec select backend", |
| 748 | .short_help = "ipsec select backend <ah|esp> <backend index>", |
| 749 | .function = ipsec_select_backend_command_fn, |
| 750 | }; |
| 751 | |
| 752 | /* *INDENT-ON* */ |
| 753 | |
| 754 | static clib_error_t * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 755 | clear_ipsec_counters_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 756 | unformat_input_t * input, |
| 757 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 758 | { |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 759 | vlib_clear_combined_counters (&ipsec_spd_policy_counters); |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 760 | vlib_clear_combined_counters (&ipsec_sa_counters); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 761 | |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 762 | return (NULL); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 763 | } |
| 764 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 765 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 766 | VLIB_CLI_COMMAND (clear_ipsec_counters_command, static) = { |
| 767 | .path = "clear ipsec counters", |
| 768 | .short_help = "clear ipsec counters", |
| 769 | .function = clear_ipsec_counters_command_fn, |
| 770 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 771 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 772 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 773 | static u32 |
| 774 | ipsec_tun_mk_local_sa_id (u32 ti) |
| 775 | { |
| 776 | return (0x80000000 | ti); |
| 777 | } |
| 778 | |
| 779 | static u32 |
| 780 | ipsec_tun_mk_remote_sa_id (u32 ti) |
| 781 | { |
| 782 | return (0xc0000000 | ti); |
| 783 | } |
| 784 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 785 | static clib_error_t * |
| 786 | create_ipsec_tunnel_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 787 | unformat_input_t * input, |
| 788 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 789 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 790 | unformat_input_t _line_input, *line_input = &_line_input; |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 791 | ip46_address_t local_ip = ip46_address_initializer; |
| 792 | ip46_address_t remote_ip = ip46_address_initializer; |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 793 | ip_address_t nh = IP_ADDRESS_V4_ALL_0S; |
Damjan Marion | 6844985 | 2020-03-16 17:53:38 +0100 | [diff] [blame] | 794 | ipsec_crypto_alg_t crypto_alg = IPSEC_CRYPTO_ALG_NONE; |
| 795 | ipsec_integ_alg_t integ_alg = IPSEC_INTEG_ALG_NONE; |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 796 | ipsec_sa_flags_t flags; |
Benoît Ganne | bdd8b57 | 2020-07-28 15:56:15 +0200 | [diff] [blame] | 797 | u32 local_spi, remote_spi, salt = 0, table_id, fib_index; |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 798 | u32 instance = ~0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 799 | int rv; |
Benoît Ganne | bdd8b57 | 2020-07-28 15:56:15 +0200 | [diff] [blame] | 800 | u32 m_args = 0; |
Kingwel Xie | 72b3bce | 2019-02-12 06:45:08 -0500 | [diff] [blame] | 801 | u8 ipv4_set = 0; |
| 802 | u8 ipv6_set = 0; |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 803 | u8 is_add = 1; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 804 | clib_error_t *error = NULL; |
Kingwel Xie | d3d1205 | 2019-03-07 06:34:30 -0500 | [diff] [blame] | 805 | ipsec_key_t rck = { 0 }; |
| 806 | ipsec_key_t lck = { 0 }; |
| 807 | ipsec_key_t lik = { 0 }; |
| 808 | ipsec_key_t rik = { 0 }; |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 809 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 810 | table_id = 0; |
| 811 | flags = IPSEC_SA_FLAG_NONE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 812 | |
| 813 | /* Get a line of input. */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 814 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 815 | return 0; |
| 816 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 817 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 818 | { |
| 819 | if (unformat |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 820 | (line_input, "local-ip %U", unformat_ip46_address, &local_ip, |
Kingwel Xie | 72b3bce | 2019-02-12 06:45:08 -0500 | [diff] [blame] | 821 | IP46_TYPE_ANY)) |
| 822 | { |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 823 | ip46_address_is_ip4 (&local_ip) ? (ipv4_set = 1) : (ipv6_set = 1); |
Benoît Ganne | bdd8b57 | 2020-07-28 15:56:15 +0200 | [diff] [blame] | 824 | m_args |= 1 << 0; |
Kingwel Xie | 72b3bce | 2019-02-12 06:45:08 -0500 | [diff] [blame] | 825 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 826 | else |
| 827 | if (unformat |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 828 | (line_input, "remote-ip %U", unformat_ip46_address, &remote_ip, |
Kingwel Xie | 72b3bce | 2019-02-12 06:45:08 -0500 | [diff] [blame] | 829 | IP46_TYPE_ANY)) |
| 830 | { |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 831 | ip46_address_is_ip4 (&remote_ip) ? (ipv4_set = 1) : (ipv6_set = 1); |
Benoît Ganne | bdd8b57 | 2020-07-28 15:56:15 +0200 | [diff] [blame] | 832 | m_args |= 1 << 1; |
Kingwel Xie | 72b3bce | 2019-02-12 06:45:08 -0500 | [diff] [blame] | 833 | } |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 834 | else if (unformat (line_input, "local-spi %u", &local_spi)) |
Benoît Ganne | bdd8b57 | 2020-07-28 15:56:15 +0200 | [diff] [blame] | 835 | m_args |= 1 << 2; |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 836 | else if (unformat (line_input, "remote-spi %u", &remote_spi)) |
Benoît Ganne | bdd8b57 | 2020-07-28 15:56:15 +0200 | [diff] [blame] | 837 | m_args |= 1 << 3; |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 838 | else if (unformat (line_input, "salt 0x%x", &salt)) |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 839 | ; |
Radu Nicolau | 717de09 | 2018-08-03 10:37:24 +0100 | [diff] [blame] | 840 | else if (unformat (line_input, "udp-encap")) |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 841 | flags |= IPSEC_SA_FLAG_UDP_ENCAP; |
Kingwel Xie | 2baf942 | 2019-02-04 02:07:06 -0800 | [diff] [blame] | 842 | else if (unformat (line_input, "use-esn")) |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 843 | flags |= IPSEC_SA_FLAG_USE_ESN; |
Kingwel Xie | 2baf942 | 2019-02-04 02:07:06 -0800 | [diff] [blame] | 844 | else if (unformat (line_input, "use-anti-replay")) |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 845 | flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY; |
| 846 | else if (unformat (line_input, "instance %u", &instance)) |
| 847 | ; |
| 848 | else if (unformat (line_input, "tx-table %u", &table_id)) |
Pierre Pfister | 4c422f9 | 2018-12-10 11:19:08 +0100 | [diff] [blame] | 849 | ; |
Neale Ranns | fd06084 | 2019-03-04 13:44:42 +0000 | [diff] [blame] | 850 | else |
| 851 | if (unformat |
| 852 | (line_input, "local-crypto-key %U", unformat_ipsec_key, &lck)) |
| 853 | ; |
| 854 | else |
| 855 | if (unformat |
| 856 | (line_input, "remote-crypto-key %U", unformat_ipsec_key, &rck)) |
| 857 | ; |
| 858 | else if (unformat (line_input, "crypto-alg %U", |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 859 | unformat_ipsec_crypto_alg, &crypto_alg)) |
Neale Ranns | fd06084 | 2019-03-04 13:44:42 +0000 | [diff] [blame] | 860 | ; |
| 861 | else |
| 862 | if (unformat |
| 863 | (line_input, "local-integ-key %U", unformat_ipsec_key, &lik)) |
| 864 | ; |
| 865 | else |
| 866 | if (unformat |
Simon Zhang | e8e950a | 2019-04-23 23:04:07 +0800 | [diff] [blame] | 867 | (line_input, "remote-integ-key %U", unformat_ipsec_key, &rik)) |
Neale Ranns | fd06084 | 2019-03-04 13:44:42 +0000 | [diff] [blame] | 868 | ; |
| 869 | else if (unformat (line_input, "integ-alg %U", |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 870 | unformat_ipsec_integ_alg, &integ_alg)) |
Neale Ranns | fd06084 | 2019-03-04 13:44:42 +0000 | [diff] [blame] | 871 | ; |
Kingwel Xie | 2baf942 | 2019-02-04 02:07:06 -0800 | [diff] [blame] | 872 | else if (unformat (line_input, "del")) |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 873 | is_add = 0; |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 874 | else if (unformat (line_input, "nh &U", unformat_ip_address, &nh)) |
| 875 | ; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 876 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 877 | { |
| 878 | error = clib_error_return (0, "unknown input `%U'", |
| 879 | format_unformat_error, line_input); |
| 880 | goto done; |
| 881 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 882 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 883 | |
Benoît Ganne | bdd8b57 | 2020-07-28 15:56:15 +0200 | [diff] [blame] | 884 | if (0xf != m_args) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 885 | { |
| 886 | error = clib_error_return (0, "mandatory argument(s) missing"); |
| 887 | goto done; |
| 888 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 889 | |
Kingwel Xie | 72b3bce | 2019-02-12 06:45:08 -0500 | [diff] [blame] | 890 | if (ipv4_set && ipv6_set) |
| 891 | return clib_error_return (0, "both IPv4 and IPv6 addresses specified"); |
| 892 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 893 | fib_index = fib_table_find (fib_ip_proto (ipv6_set), table_id); |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 894 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 895 | if (~0 == fib_index) |
| 896 | { |
| 897 | rv = VNET_API_ERROR_NO_SUCH_FIB; |
| 898 | goto done; |
| 899 | } |
Neale Ranns | fd06084 | 2019-03-04 13:44:42 +0000 | [diff] [blame] | 900 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 901 | if (is_add) |
| 902 | { |
| 903 | // remote = input, local = output |
| 904 | u32 sw_if_index; |
Neale Ranns | fd06084 | 2019-03-04 13:44:42 +0000 | [diff] [blame] | 905 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 906 | /* create an ip-ip tunnel, then the two SA, then bind them */ |
| 907 | rv = |
| 908 | ipip_add_tunnel (ipv6_set ? IPIP_TRANSPORT_IP6 : IPIP_TRANSPORT_IP4, |
Neale Ranns | 9534696 | 2019-11-25 13:04:44 +0000 | [diff] [blame] | 909 | instance, &local_ip, &remote_ip, fib_index, |
Neale Ranns | 59ff918 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 910 | TUNNEL_ENCAP_DECAP_FLAG_NONE, IP_DSCP_CS0, |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 911 | TUNNEL_MODE_P2P, &sw_if_index); |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 912 | rv |= |
| 913 | ipsec_sa_add_and_lock (ipsec_tun_mk_local_sa_id (sw_if_index), |
| 914 | local_spi, IPSEC_PROTOCOL_ESP, crypto_alg, |
| 915 | &lck, integ_alg, &lik, flags, table_id, |
| 916 | clib_host_to_net_u32 (salt), &local_ip, |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame^] | 917 | &remote_ip, TUNNEL_ENCAP_DECAP_FLAG_NONE, |
| 918 | IP_DSCP_CS0, NULL, |
| 919 | IPSEC_UDP_PORT_NONE, IPSEC_UDP_PORT_NONE); |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 920 | rv |= |
| 921 | ipsec_sa_add_and_lock (ipsec_tun_mk_remote_sa_id (sw_if_index), |
| 922 | remote_spi, IPSEC_PROTOCOL_ESP, crypto_alg, |
| 923 | &rck, integ_alg, &rik, |
| 924 | (flags | IPSEC_SA_FLAG_IS_INBOUND), table_id, |
| 925 | clib_host_to_net_u32 (salt), &remote_ip, |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame^] | 926 | &local_ip, TUNNEL_ENCAP_DECAP_FLAG_NONE, |
| 927 | IP_DSCP_CS0, NULL, |
| 928 | IPSEC_UDP_PORT_NONE, IPSEC_UDP_PORT_NONE); |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 929 | rv |= |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 930 | ipsec_tun_protect_update_one (sw_if_index, &nh, |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 931 | ipsec_tun_mk_local_sa_id (sw_if_index), |
| 932 | ipsec_tun_mk_remote_sa_id |
| 933 | (sw_if_index)); |
| 934 | } |
| 935 | else |
| 936 | rv = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 937 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 938 | switch (rv) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 939 | { |
| 940 | case 0: |
| 941 | break; |
| 942 | case VNET_API_ERROR_INVALID_VALUE: |
Neale Ranns | d14fccd | 2019-11-15 15:03:27 +0000 | [diff] [blame] | 943 | error = clib_error_return (0, |
| 944 | "IPSec tunnel interface already exists..."); |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 945 | goto done; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 946 | default: |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 947 | error = clib_error_return (0, "ipsec_register_interface returned %d", |
| 948 | rv); |
| 949 | goto done; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 950 | } |
| 951 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 952 | done: |
| 953 | unformat_free (line_input); |
| 954 | |
| 955 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 956 | } |
| 957 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 958 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 959 | VLIB_CLI_COMMAND (create_ipsec_tunnel_command, static) = { |
| 960 | .path = "create ipsec tunnel", |
Pierre Pfister | 4c422f9 | 2018-12-10 11:19:08 +0100 | [diff] [blame] | 961 | .short_help = "create ipsec tunnel local-ip <addr> local-spi <spi> " |
Kingwel Xie | 2baf942 | 2019-02-04 02:07:06 -0800 | [diff] [blame] | 962 | "remote-ip <addr> remote-spi <spi> [instance <inst_num>] [udp-encap] [use-esn] [use-anti-replay] " |
Pierre Pfister | 4c422f9 | 2018-12-10 11:19:08 +0100 | [diff] [blame] | 963 | "[tx-table <table-id>]", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 964 | .function = create_ipsec_tunnel_command_fn, |
| 965 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 966 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 967 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 968 | static clib_error_t * |
| 969 | ipsec_tun_protect_cmd (vlib_main_t * vm, |
| 970 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 971 | { |
| 972 | unformat_input_t _line_input, *line_input = &_line_input; |
| 973 | 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] | 974 | ip_address_t peer = { }; |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 975 | vnet_main_t *vnm; |
| 976 | |
| 977 | is_del = 0; |
| 978 | sw_if_index = ~0; |
| 979 | vnm = vnet_get_main (); |
| 980 | |
| 981 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 982 | return 0; |
| 983 | |
| 984 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 985 | { |
| 986 | if (unformat (line_input, "del")) |
| 987 | is_del = 1; |
| 988 | else if (unformat (line_input, "add")) |
| 989 | is_del = 0; |
| 990 | else if (unformat (line_input, "sa-in %d", &sa_in)) |
| 991 | vec_add1 (sa_ins, sa_in); |
| 992 | else if (unformat (line_input, "sa-out %d", &sa_out)) |
| 993 | ; |
| 994 | else if (unformat (line_input, "%U", |
| 995 | unformat_vnet_sw_interface, vnm, &sw_if_index)) |
| 996 | ; |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 997 | else if (unformat (line_input, "%U", unformat_ip_address, &peer)) |
| 998 | ; |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 999 | else |
| 1000 | return (clib_error_return (0, "unknown input '%U'", |
| 1001 | format_unformat_error, line_input)); |
| 1002 | } |
| 1003 | |
| 1004 | if (!is_del) |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 1005 | ipsec_tun_protect_update (sw_if_index, &peer, sa_out, sa_ins); |
Eric Kinzie | 609d579 | 2020-10-13 20:02:11 -0400 | [diff] [blame] | 1006 | else |
| 1007 | ipsec_tun_protect_del (sw_if_index, &peer); |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1008 | |
| 1009 | unformat_free (line_input); |
| 1010 | return NULL; |
| 1011 | } |
| 1012 | |
| 1013 | /** |
| 1014 | * Protect tunnel with IPSEC |
| 1015 | */ |
| 1016 | /* *INDENT-OFF* */ |
| 1017 | VLIB_CLI_COMMAND (ipsec_tun_protect_cmd_node, static) = |
| 1018 | { |
| 1019 | .path = "ipsec tunnel protect", |
| 1020 | .function = ipsec_tun_protect_cmd, |
Eric Kinzie | 609d579 | 2020-10-13 20:02:11 -0400 | [diff] [blame] | 1021 | .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] | 1022 | // this is not MP safe |
| 1023 | }; |
| 1024 | /* *INDENT-ON* */ |
| 1025 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1026 | |
| 1027 | static clib_error_t * |
| 1028 | ipsec_tun_protect_show (vlib_main_t * vm, |
| 1029 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 1030 | { |
| 1031 | ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm); |
| 1032 | |
| 1033 | return NULL; |
| 1034 | } |
| 1035 | |
| 1036 | /** |
| 1037 | * show IPSEC tunnel protection |
| 1038 | */ |
| 1039 | /* *INDENT-OFF* */ |
| 1040 | VLIB_CLI_COMMAND (ipsec_tun_protect_show_node, static) = |
| 1041 | { |
| 1042 | .path = "show ipsec protect", |
| 1043 | .function = ipsec_tun_protect_show, |
| 1044 | .short_help = "show ipsec protect", |
| 1045 | }; |
| 1046 | /* *INDENT-ON* */ |
| 1047 | |
Neale Ranns | 7b4e52f | 2020-05-24 16:17:50 +0000 | [diff] [blame] | 1048 | static int |
Neale Ranns | 302b25a | 2020-10-19 13:23:33 +0000 | [diff] [blame] | 1049 | 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] | 1050 | { |
| 1051 | ipsec4_tunnel_kv_t *ikv = (ipsec4_tunnel_kv_t *) kv; |
| 1052 | vlib_main_t *vm = arg; |
| 1053 | |
| 1054 | vlib_cli_output (vm, " %U", format_ipsec4_tunnel_kv, ikv); |
| 1055 | |
| 1056 | return (BIHASH_WALK_CONTINUE); |
| 1057 | } |
| 1058 | |
| 1059 | static int |
Neale Ranns | 302b25a | 2020-10-19 13:23:33 +0000 | [diff] [blame] | 1060 | 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] | 1061 | { |
| 1062 | ipsec6_tunnel_kv_t *ikv = (ipsec6_tunnel_kv_t *) kv; |
| 1063 | vlib_main_t *vm = arg; |
| 1064 | |
| 1065 | vlib_cli_output (vm, " %U", format_ipsec6_tunnel_kv, ikv); |
| 1066 | |
| 1067 | return (BIHASH_WALK_CONTINUE); |
| 1068 | } |
| 1069 | |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 1070 | static clib_error_t * |
| 1071 | ipsec_tun_protect_hash_show (vlib_main_t * vm, |
| 1072 | unformat_input_t * input, |
| 1073 | vlib_cli_command_t * cmd) |
| 1074 | { |
| 1075 | ipsec_main_t *im = &ipsec_main; |
| 1076 | |
| 1077 | { |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 1078 | vlib_cli_output (vm, "IPv4:"); |
| 1079 | |
Neale Ranns | 302b25a | 2020-10-19 13:23:33 +0000 | [diff] [blame] | 1080 | clib_bihash_foreach_key_value_pair_8_16 |
Neale Ranns | 7b4e52f | 2020-05-24 16:17:50 +0000 | [diff] [blame] | 1081 | (&im->tun4_protect_by_key, ipsec_tun_protect4_hash_show_one, vm); |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 1082 | |
| 1083 | vlib_cli_output (vm, "IPv6:"); |
| 1084 | |
Neale Ranns | 302b25a | 2020-10-19 13:23:33 +0000 | [diff] [blame] | 1085 | clib_bihash_foreach_key_value_pair_24_16 |
Neale Ranns | 7b4e52f | 2020-05-24 16:17:50 +0000 | [diff] [blame] | 1086 | (&im->tun6_protect_by_key, ipsec_tun_protect6_hash_show_one, vm); |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | return NULL; |
| 1090 | } |
| 1091 | |
| 1092 | /** |
| 1093 | * show IPSEC tunnel protection hash tables |
| 1094 | */ |
| 1095 | /* *INDENT-OFF* */ |
| 1096 | VLIB_CLI_COMMAND (ipsec_tun_protect_hash_show_node, static) = |
| 1097 | { |
| 1098 | .path = "show ipsec protect-hash", |
| 1099 | .function = ipsec_tun_protect_hash_show, |
| 1100 | .short_help = "show ipsec protect-hash", |
| 1101 | }; |
| 1102 | /* *INDENT-ON* */ |
| 1103 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1104 | clib_error_t * |
| 1105 | ipsec_cli_init (vlib_main_t * vm) |
| 1106 | { |
| 1107 | return 0; |
| 1108 | } |
| 1109 | |
| 1110 | VLIB_INIT_FUNCTION (ipsec_cli_init); |
| 1111 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1112 | static clib_error_t * |
| 1113 | set_async_mode_command_fn (vlib_main_t * vm, unformat_input_t * input, |
| 1114 | vlib_cli_command_t * cmd) |
| 1115 | { |
| 1116 | unformat_input_t _line_input, *line_input = &_line_input; |
| 1117 | int async_enable = 0; |
| 1118 | |
| 1119 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 1120 | return 0; |
| 1121 | |
| 1122 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 1123 | { |
| 1124 | if (unformat (line_input, "on")) |
| 1125 | async_enable = 1; |
| 1126 | else if (unformat (line_input, "off")) |
| 1127 | async_enable = 0; |
| 1128 | else |
| 1129 | return (clib_error_return (0, "unknown input '%U'", |
| 1130 | format_unformat_error, line_input)); |
| 1131 | } |
| 1132 | |
| 1133 | vnet_crypto_request_async_mode (async_enable); |
| 1134 | ipsec_set_async_mode (async_enable); |
| 1135 | |
| 1136 | unformat_free (line_input); |
| 1137 | return (NULL); |
| 1138 | } |
| 1139 | |
| 1140 | /* *INDENT-OFF* */ |
| 1141 | VLIB_CLI_COMMAND (set_async_mode_command, static) = { |
| 1142 | .path = "set ipsec async mode", |
| 1143 | .short_help = "set ipsec async mode on|off", |
| 1144 | .function = set_async_mode_command_fn, |
| 1145 | }; |
| 1146 | /* *INDENT-ON* */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1147 | |
| 1148 | /* |
| 1149 | * fd.io coding-style-patch-verification: ON |
| 1150 | * |
| 1151 | * Local Variables: |
| 1152 | * eval: (c-set-style "gnu") |
| 1153 | * End: |
| 1154 | */ |