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> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 23 | |
| 24 | #include <vnet/ipsec/ipsec.h> |
| 25 | |
| 26 | static clib_error_t * |
| 27 | set_interface_spd_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 28 | unformat_input_t * input, |
| 29 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 30 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 31 | unformat_input_t _line_input, *line_input = &_line_input; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 32 | ipsec_main_t *im = &ipsec_main; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 33 | u32 sw_if_index = (u32) ~ 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 34 | u32 spd_id; |
| 35 | int is_add = 1; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 36 | clib_error_t *error = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 37 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 38 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 39 | return 0; |
| 40 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 41 | if (unformat |
| 42 | (line_input, "%U %u", unformat_vnet_sw_interface, im->vnet_main, |
| 43 | &sw_if_index, &spd_id)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 44 | ; |
| 45 | else if (unformat (line_input, "del")) |
| 46 | is_add = 0; |
| 47 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 48 | { |
| 49 | error = clib_error_return (0, "parse error: '%U'", |
| 50 | format_unformat_error, line_input); |
| 51 | goto done; |
| 52 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 53 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 54 | ipsec_set_interface_spd (vm, sw_if_index, spd_id, is_add); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 55 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 56 | done: |
| 57 | unformat_free (line_input); |
| 58 | |
| 59 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 62 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 63 | VLIB_CLI_COMMAND (set_interface_spd_command, static) = { |
| 64 | .path = "set interface ipsec spd", |
| 65 | .short_help = |
| 66 | "set interface ipsec spd <int> <id>", |
| 67 | .function = set_interface_spd_command_fn, |
| 68 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 69 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 70 | |
| 71 | static clib_error_t * |
| 72 | ipsec_sa_add_del_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 73 | unformat_input_t * input, |
| 74 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 75 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 76 | unformat_input_t _line_input, *line_input = &_line_input; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 77 | ip46_address_t tun_src = { }, tun_dst = |
| 78 | { |
| 79 | }; |
| 80 | ipsec_crypto_alg_t crypto_alg; |
| 81 | ipsec_integ_alg_t integ_alg; |
| 82 | ipsec_protocol_t proto; |
| 83 | ipsec_sa_flags_t flags; |
| 84 | clib_error_t *error; |
| 85 | ipsec_key_t ck, ik; |
| 86 | int is_add, rv; |
| 87 | u32 id, spi; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 88 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 89 | error = NULL; |
| 90 | is_add = 0; |
| 91 | flags = IPSEC_SA_FLAG_NONE; |
| 92 | proto = IPSEC_PROTOCOL_ESP; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 93 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 94 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 95 | return 0; |
| 96 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 97 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 98 | { |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 99 | if (unformat (line_input, "add %u", &id)) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 100 | is_add = 1; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 101 | else if (unformat (line_input, "del %u", &id)) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 102 | is_add = 0; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 103 | else if (unformat (line_input, "spi %u", &spi)) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 104 | ; |
| 105 | else if (unformat (line_input, "esp")) |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 106 | proto = IPSEC_PROTOCOL_ESP; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 107 | else if (unformat (line_input, "ah")) |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 108 | proto = IPSEC_PROTOCOL_AH; |
| 109 | else if (unformat (line_input, "crypto-key %U", |
| 110 | unformat_ipsec_key, &ck)) |
| 111 | ; |
| 112 | else if (unformat (line_input, "crypto-alg %U", |
| 113 | unformat_ipsec_crypto_alg, &crypto_alg)) |
| 114 | ; |
| 115 | else if (unformat (line_input, "integ-key %U", unformat_ipsec_key, &ik)) |
| 116 | ; |
| 117 | else if (unformat (line_input, "integ-alg %U", |
| 118 | unformat_ipsec_integ_alg, &integ_alg)) |
| 119 | ; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 120 | else if (unformat (line_input, "tunnel-src %U", |
Kingwel Xie | 72b3bce | 2019-02-12 06:45:08 -0500 | [diff] [blame] | 121 | unformat_ip46_address, &tun_src, IP46_TYPE_ANY)) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 122 | { |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 123 | flags |= IPSEC_SA_FLAG_IS_TUNNEL; |
| 124 | if (!ip46_address_is_ip4 (&tun_src)) |
| 125 | flags |= IPSEC_SA_FLAG_IS_TUNNEL_V6; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 126 | } |
| 127 | else if (unformat (line_input, "tunnel-dst %U", |
Kingwel Xie | 72b3bce | 2019-02-12 06:45:08 -0500 | [diff] [blame] | 128 | unformat_ip46_address, &tun_dst, IP46_TYPE_ANY)) |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 129 | ; |
Radu Nicolau | 717de09 | 2018-08-03 10:37:24 +0100 | [diff] [blame] | 130 | else if (unformat (line_input, "udp-encap")) |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 131 | flags |= IPSEC_SA_FLAG_UDP_ENCAP; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 132 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 133 | { |
| 134 | error = clib_error_return (0, "parse error: '%U'", |
| 135 | format_unformat_error, line_input); |
| 136 | goto done; |
| 137 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 138 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 139 | |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 140 | if (is_add) |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 141 | rv = ipsec_sa_add (id, spi, proto, crypto_alg, |
| 142 | &ck, integ_alg, &ik, flags, |
| 143 | 0, &tun_src, &tun_dst, NULL); |
| 144 | else |
| 145 | rv = ipsec_sa_del (id); |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 146 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 147 | if (rv) |
| 148 | clib_error_return (0, "failed"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 149 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 150 | done: |
| 151 | unformat_free (line_input); |
| 152 | |
| 153 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 156 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 157 | VLIB_CLI_COMMAND (ipsec_sa_add_del_command, static) = { |
| 158 | .path = "ipsec sa", |
| 159 | .short_help = |
| 160 | "ipsec sa [add|del]", |
| 161 | .function = ipsec_sa_add_del_command_fn, |
| 162 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 163 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 164 | |
| 165 | static clib_error_t * |
| 166 | ipsec_spd_add_del_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 167 | unformat_input_t * input, |
| 168 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 169 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 170 | unformat_input_t _line_input, *line_input = &_line_input; |
Damjan Marion | 3f54b18 | 2016-08-16 11:27:02 +0200 | [diff] [blame] | 171 | u32 spd_id = ~0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 172 | int is_add = ~0; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 173 | clib_error_t *error = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 174 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 175 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 176 | return 0; |
| 177 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 178 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 179 | { |
| 180 | if (unformat (line_input, "add")) |
| 181 | is_add = 1; |
| 182 | else if (unformat (line_input, "del")) |
| 183 | is_add = 0; |
| 184 | else if (unformat (line_input, "%u", &spd_id)) |
| 185 | ; |
| 186 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 187 | { |
| 188 | error = clib_error_return (0, "parse error: '%U'", |
| 189 | format_unformat_error, line_input); |
| 190 | goto done; |
| 191 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 192 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 193 | |
Damjan Marion | 3f54b18 | 2016-08-16 11:27:02 +0200 | [diff] [blame] | 194 | if (spd_id == ~0) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 195 | { |
| 196 | error = clib_error_return (0, "please specify SPD ID"); |
| 197 | goto done; |
| 198 | } |
Damjan Marion | 3f54b18 | 2016-08-16 11:27:02 +0200 | [diff] [blame] | 199 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 200 | ipsec_add_del_spd (vm, spd_id, is_add); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 201 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 202 | done: |
| 203 | unformat_free (line_input); |
| 204 | |
| 205 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 208 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 209 | VLIB_CLI_COMMAND (ipsec_spd_add_del_command, static) = { |
| 210 | .path = "ipsec spd", |
| 211 | .short_help = |
| 212 | "ipsec spd [add|del] <id>", |
| 213 | .function = ipsec_spd_add_del_command_fn, |
| 214 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 215 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 216 | |
| 217 | |
| 218 | static clib_error_t * |
| 219 | ipsec_policy_add_del_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 220 | unformat_input_t * input, |
| 221 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 222 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 223 | unformat_input_t _line_input, *line_input = &_line_input; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 224 | ipsec_policy_t p; |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 225 | int rv, is_add = 0; |
| 226 | u32 tmp, tmp2, stat_index; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 227 | clib_error_t *error = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 228 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 229 | clib_memset (&p, 0, sizeof (p)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 230 | p.lport.stop = p.rport.stop = ~0; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 231 | p.laddr.stop.ip4.as_u32 = p.raddr.stop.ip4.as_u32 = (u32) ~ 0; |
| 232 | p.laddr.stop.ip6.as_u64[0] = p.laddr.stop.ip6.as_u64[1] = (u64) ~ 0; |
| 233 | p.raddr.stop.ip6.as_u64[0] = p.raddr.stop.ip6.as_u64[1] = (u64) ~ 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 234 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 235 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 236 | return 0; |
| 237 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 238 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 239 | { |
| 240 | if (unformat (line_input, "add")) |
| 241 | is_add = 1; |
| 242 | else if (unformat (line_input, "del")) |
| 243 | is_add = 0; |
| 244 | else if (unformat (line_input, "spd %u", &p.id)) |
| 245 | ; |
| 246 | else if (unformat (line_input, "inbound")) |
| 247 | p.is_outbound = 0; |
| 248 | else if (unformat (line_input, "outbound")) |
| 249 | p.is_outbound = 1; |
| 250 | else if (unformat (line_input, "priority %d", &p.priority)) |
| 251 | ; |
| 252 | else if (unformat (line_input, "protocol %u", &tmp)) |
| 253 | p.protocol = (u8) tmp; |
| 254 | else |
| 255 | if (unformat |
| 256 | (line_input, "action %U", unformat_ipsec_policy_action, |
| 257 | &p.policy)) |
| 258 | { |
| 259 | if (p.policy == IPSEC_POLICY_ACTION_RESOLVE) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 260 | { |
| 261 | error = clib_error_return (0, "unsupported action: 'resolve'"); |
| 262 | goto done; |
| 263 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 264 | } |
| 265 | else if (unformat (line_input, "sa %u", &p.sa_id)) |
| 266 | ; |
| 267 | else if (unformat (line_input, "local-ip-range %U - %U", |
| 268 | unformat_ip4_address, &p.laddr.start.ip4, |
| 269 | unformat_ip4_address, &p.laddr.stop.ip4)) |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 270 | ; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 271 | else if (unformat (line_input, "remote-ip-range %U - %U", |
| 272 | unformat_ip4_address, &p.raddr.start.ip4, |
| 273 | unformat_ip4_address, &p.raddr.stop.ip4)) |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 274 | ; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 275 | else if (unformat (line_input, "local-ip-range %U - %U", |
| 276 | unformat_ip6_address, &p.laddr.start.ip6, |
| 277 | unformat_ip6_address, &p.laddr.stop.ip6)) |
| 278 | { |
| 279 | p.is_ipv6 = 1; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 280 | } |
| 281 | else if (unformat (line_input, "remote-ip-range %U - %U", |
| 282 | unformat_ip6_address, &p.raddr.start.ip6, |
| 283 | unformat_ip6_address, &p.raddr.stop.ip6)) |
| 284 | { |
| 285 | p.is_ipv6 = 1; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 286 | } |
| 287 | else if (unformat (line_input, "local-port-range %u - %u", &tmp, &tmp2)) |
| 288 | { |
| 289 | p.lport.start = tmp; |
| 290 | p.lport.stop = tmp2; |
| 291 | } |
| 292 | else |
| 293 | if (unformat (line_input, "remote-port-range %u - %u", &tmp, &tmp2)) |
| 294 | { |
| 295 | p.rport.start = tmp; |
| 296 | p.rport.stop = tmp2; |
| 297 | } |
| 298 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 299 | { |
| 300 | error = clib_error_return (0, "parse error: '%U'", |
| 301 | format_unformat_error, line_input); |
| 302 | goto done; |
| 303 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 304 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 305 | |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 306 | /* Check if SA is for IPv6/AH which is not supported. Return error if TRUE. */ |
| 307 | if (p.sa_id) |
| 308 | { |
| 309 | uword *p1; |
| 310 | ipsec_main_t *im = &ipsec_main; |
| 311 | ipsec_sa_t *sa = 0; |
| 312 | p1 = hash_get (im->sa_index_by_sa_id, p.sa_id); |
Klement Sekera | c2fc57e | 2018-06-28 14:20:12 +0200 | [diff] [blame] | 313 | if (!p1) |
| 314 | { |
| 315 | error = |
| 316 | clib_error_return (0, "SA with index %u not found", p.sa_id); |
| 317 | goto done; |
| 318 | } |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 319 | sa = pool_elt_at_index (im->sad, p1[0]); |
| 320 | if (sa && sa->protocol == IPSEC_PROTOCOL_AH && is_add && p.is_ipv6) |
| 321 | { |
| 322 | error = clib_error_return (0, "AH not supported for IPV6: '%U'", |
| 323 | format_unformat_error, line_input); |
| 324 | goto done; |
| 325 | } |
| 326 | } |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 327 | rv = ipsec_add_del_policy (vm, &p, is_add, &stat_index); |
| 328 | |
| 329 | if (!rv) |
| 330 | vlib_cli_output (vm, "policy-index:%d", stat_index); |
| 331 | else |
| 332 | vlib_cli_output (vm, "error:%d", rv); |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 333 | |
| 334 | done: |
| 335 | unformat_free (line_input); |
| 336 | |
| 337 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 340 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 341 | VLIB_CLI_COMMAND (ipsec_policy_add_del_command, static) = { |
| 342 | .path = "ipsec policy", |
| 343 | .short_help = |
| 344 | "ipsec policy [add|del] spd <id> priority <n> ", |
| 345 | .function = ipsec_policy_add_del_command_fn, |
| 346 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 347 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 348 | |
| 349 | static clib_error_t * |
| 350 | set_ipsec_sa_key_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 351 | unformat_input_t * input, |
| 352 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 353 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 354 | unformat_input_t _line_input, *line_input = &_line_input; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 355 | clib_error_t *error = NULL; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 356 | ipsec_key_t ck, ik; |
| 357 | u32 id; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 358 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 359 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 360 | return 0; |
| 361 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 362 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 363 | { |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 364 | if (unformat (line_input, "%u", &id)) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 365 | ; |
| 366 | else |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 367 | if (unformat (line_input, "crypto-key %U", unformat_ipsec_key, &ck)) |
| 368 | ; |
| 369 | else if (unformat (line_input, "integ-key %U", unformat_ipsec_key, &ik)) |
| 370 | ; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 371 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 372 | { |
| 373 | error = clib_error_return (0, "parse error: '%U'", |
| 374 | format_unformat_error, line_input); |
| 375 | goto done; |
| 376 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 377 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 378 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 379 | ipsec_set_sa_key (id, &ck, &ik); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 380 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 381 | done: |
| 382 | unformat_free (line_input); |
| 383 | |
| 384 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 387 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 388 | VLIB_CLI_COMMAND (set_ipsec_sa_key_command, static) = { |
| 389 | .path = "set ipsec sa", |
| 390 | .short_help = |
| 391 | "set ipsec sa <id> crypto-key <key> integ-key <key>", |
| 392 | .function = set_ipsec_sa_key_command_fn, |
| 393 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 394 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 395 | |
| 396 | static clib_error_t * |
| 397 | show_ipsec_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 398 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 399 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 400 | ipsec_main_t *im = &ipsec_main; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 401 | u32 spd_id, sw_if_index, sai; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 402 | vnet_hw_interface_t *hi; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 403 | ipsec_tunnel_if_t *t; |
Klement Sekera | ea84130 | 2018-05-11 12:59:05 +0200 | [diff] [blame] | 404 | u8 *protocol = NULL; |
| 405 | u8 *policy = NULL; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 406 | u32 i; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 407 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 408 | /* *INDENT-OFF* */ |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 409 | pool_foreach_index (sai, im->sad, ({ |
| 410 | vlib_cli_output(vm, "%U", format_ipsec_sa, sai); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 411 | })); |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 412 | pool_foreach_index (i, im->spds, ({ |
| 413 | vlib_cli_output(vm, "%U", format_ipsec_spd, i); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 414 | })); |
| 415 | |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 416 | vlib_cli_output (vm, "SPD Bindings:"); |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 417 | |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 418 | hash_foreach(sw_if_index, spd_id, im->spd_index_by_sw_if_index, ({ |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 419 | vlib_cli_output (vm, " %d -> %U", spd_id, |
| 420 | format_vnet_sw_if_index_name, im->vnet_main, |
| 421 | sw_if_index); |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 422 | })); |
| 423 | /* *INDENT-ON* */ |
| 424 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 425 | vlib_cli_output (vm, "tunnel interfaces"); |
| 426 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 427 | pool_foreach (t, im->tunnel_interfaces, ({ |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 428 | if (t->hw_if_index == ~0) |
| 429 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 430 | hi = vnet_get_hw_interface (im->vnet_main, t->hw_if_index); |
Pierre Pfister | 4c422f9 | 2018-12-10 11:19:08 +0100 | [diff] [blame] | 431 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 432 | vlib_cli_output(vm, " %s", hi->name); |
Pierre Pfister | 4c422f9 | 2018-12-10 11:19:08 +0100 | [diff] [blame] | 433 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 434 | vlib_cli_output(vm, " out-bound sa"); |
| 435 | vlib_cli_output(vm, " %U", format_ipsec_sa, t->output_sa_index); |
| 436 | |
| 437 | vlib_cli_output(vm, " in-bound sa"); |
| 438 | vlib_cli_output(vm, " %U", format_ipsec_sa, t->input_sa_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 439 | })); |
Klement Sekera | ea84130 | 2018-05-11 12:59:05 +0200 | [diff] [blame] | 440 | vec_free(policy); |
| 441 | vec_free(protocol); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 442 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 443 | return 0; |
| 444 | } |
| 445 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 446 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 447 | VLIB_CLI_COMMAND (show_ipsec_command, static) = { |
| 448 | .path = "show ipsec", |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 449 | .short_help = "show ipsec [backends]", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 450 | .function = show_ipsec_command_fn, |
| 451 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 452 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 453 | |
| 454 | static clib_error_t * |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 455 | ipsec_show_backends_command_fn (vlib_main_t * vm, |
| 456 | unformat_input_t * input, |
| 457 | vlib_cli_command_t * cmd) |
| 458 | { |
| 459 | ipsec_main_t *im = &ipsec_main; |
| 460 | u32 verbose = 0; |
| 461 | |
| 462 | (void) unformat (input, "verbose %u", &verbose); |
| 463 | |
| 464 | vlib_cli_output (vm, "IPsec AH backends available:"); |
| 465 | u8 *s = format (NULL, "%=25s %=25s %=10s\n", "Name", "Index", "Active"); |
| 466 | ipsec_ah_backend_t *ab; |
| 467 | /* *INDENT-OFF* */ |
| 468 | pool_foreach (ab, im->ah_backends, { |
| 469 | s = format (s, "%=25s %=25u %=10s\n", ab->name, ab - im->ah_backends, |
| 470 | ab - im->ah_backends == im->ah_current_backend ? "yes" : "no"); |
| 471 | if (verbose) { |
| 472 | vlib_node_t *n; |
| 473 | n = vlib_get_node (vm, ab->ah4_encrypt_node_index); |
| 474 | s = format (s, " enc4 %s (next %d)\n", n->name, ab->ah4_encrypt_next_index); |
| 475 | n = vlib_get_node (vm, ab->ah4_decrypt_node_index); |
| 476 | s = format (s, " dec4 %s (next %d)\n", n->name, ab->ah4_decrypt_next_index); |
| 477 | n = vlib_get_node (vm, ab->ah6_encrypt_node_index); |
| 478 | s = format (s, " enc6 %s (next %d)\n", n->name, ab->ah6_encrypt_next_index); |
| 479 | n = vlib_get_node (vm, ab->ah6_decrypt_node_index); |
| 480 | s = format (s, " dec6 %s (next %d)\n", n->name, ab->ah6_decrypt_next_index); |
| 481 | } |
| 482 | }); |
| 483 | /* *INDENT-ON* */ |
| 484 | vlib_cli_output (vm, "%v", s); |
| 485 | _vec_len (s) = 0; |
| 486 | vlib_cli_output (vm, "IPsec ESP backends available:"); |
| 487 | s = format (s, "%=25s %=25s %=10s\n", "Name", "Index", "Active"); |
| 488 | ipsec_esp_backend_t *eb; |
| 489 | /* *INDENT-OFF* */ |
| 490 | pool_foreach (eb, im->esp_backends, { |
| 491 | s = format (s, "%=25s %=25u %=10s\n", eb->name, eb - im->esp_backends, |
| 492 | eb - im->esp_backends == im->esp_current_backend ? "yes" |
| 493 | : "no"); |
| 494 | if (verbose) { |
| 495 | vlib_node_t *n; |
| 496 | n = vlib_get_node (vm, eb->esp4_encrypt_node_index); |
| 497 | s = format (s, " enc4 %s (next %d)\n", n->name, eb->esp4_encrypt_next_index); |
| 498 | n = vlib_get_node (vm, eb->esp4_decrypt_node_index); |
| 499 | s = format (s, " dec4 %s (next %d)\n", n->name, eb->esp4_decrypt_next_index); |
| 500 | n = vlib_get_node (vm, eb->esp6_encrypt_node_index); |
| 501 | s = format (s, " enc6 %s (next %d)\n", n->name, eb->esp6_encrypt_next_index); |
| 502 | n = vlib_get_node (vm, eb->esp6_decrypt_node_index); |
| 503 | s = format (s, " dec6 %s (next %d)\n", n->name, eb->esp6_decrypt_next_index); |
| 504 | } |
| 505 | }); |
| 506 | /* *INDENT-ON* */ |
| 507 | vlib_cli_output (vm, "%v", s); |
| 508 | |
| 509 | vec_free (s); |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | /* *INDENT-OFF* */ |
| 514 | VLIB_CLI_COMMAND (ipsec_show_backends_command, static) = { |
| 515 | .path = "show ipsec backends", |
| 516 | .short_help = "show ipsec backends", |
| 517 | .function = ipsec_show_backends_command_fn, |
| 518 | }; |
| 519 | /* *INDENT-ON* */ |
| 520 | |
| 521 | static clib_error_t * |
| 522 | ipsec_select_backend_command_fn (vlib_main_t * vm, |
| 523 | unformat_input_t * input, |
| 524 | vlib_cli_command_t * cmd) |
| 525 | { |
| 526 | u32 backend_index; |
| 527 | ipsec_main_t *im = &ipsec_main; |
| 528 | |
| 529 | if (pool_elts (im->sad) > 0) |
| 530 | { |
| 531 | return clib_error_return (0, |
| 532 | "Cannot change IPsec backend, while %u SA entries are configured", |
| 533 | pool_elts (im->sad)); |
| 534 | } |
| 535 | |
| 536 | unformat_input_t _line_input, *line_input = &_line_input; |
| 537 | /* Get a line of input. */ |
| 538 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 539 | return 0; |
| 540 | |
| 541 | if (unformat (line_input, "ah")) |
| 542 | { |
| 543 | if (unformat (line_input, "%u", &backend_index)) |
| 544 | { |
| 545 | if (ipsec_select_ah_backend (im, backend_index) < 0) |
| 546 | { |
| 547 | return clib_error_return (0, "Invalid AH backend index `%u'", |
| 548 | backend_index); |
| 549 | } |
| 550 | } |
| 551 | else |
| 552 | { |
| 553 | return clib_error_return (0, "Invalid backend index `%U'", |
| 554 | format_unformat_error, line_input); |
| 555 | } |
| 556 | } |
| 557 | else if (unformat (line_input, "esp")) |
| 558 | { |
| 559 | if (unformat (line_input, "%u", &backend_index)) |
| 560 | { |
| 561 | if (ipsec_select_esp_backend (im, backend_index) < 0) |
| 562 | { |
| 563 | return clib_error_return (0, "Invalid ESP backend index `%u'", |
| 564 | backend_index); |
| 565 | } |
| 566 | } |
| 567 | else |
| 568 | { |
| 569 | return clib_error_return (0, "Invalid backend index `%U'", |
| 570 | format_unformat_error, line_input); |
| 571 | } |
| 572 | } |
| 573 | else |
| 574 | { |
| 575 | return clib_error_return (0, "Unknown input `%U'", |
| 576 | format_unformat_error, line_input); |
| 577 | } |
| 578 | |
| 579 | return 0; |
| 580 | } |
| 581 | |
| 582 | /* *INDENT-OFF* */ |
| 583 | VLIB_CLI_COMMAND (ipsec_select_backend_command, static) = { |
| 584 | .path = "ipsec select backend", |
| 585 | .short_help = "ipsec select backend <ah|esp> <backend index>", |
| 586 | .function = ipsec_select_backend_command_fn, |
| 587 | }; |
| 588 | |
| 589 | /* *INDENT-ON* */ |
| 590 | |
| 591 | static clib_error_t * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 592 | clear_ipsec_counters_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 593 | unformat_input_t * input, |
| 594 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 595 | { |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 596 | vlib_clear_combined_counters (&ipsec_spd_policy_counters); |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 597 | vlib_clear_combined_counters (&ipsec_sa_counters); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 598 | |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 599 | return (NULL); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 602 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 603 | VLIB_CLI_COMMAND (clear_ipsec_counters_command, static) = { |
| 604 | .path = "clear ipsec counters", |
| 605 | .short_help = "clear ipsec counters", |
| 606 | .function = clear_ipsec_counters_command_fn, |
| 607 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 608 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 609 | |
| 610 | static clib_error_t * |
| 611 | create_ipsec_tunnel_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 612 | unformat_input_t * input, |
| 613 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 614 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 615 | unformat_input_t _line_input, *line_input = &_line_input; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 616 | ipsec_add_del_tunnel_args_t a; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 617 | int rv; |
| 618 | u32 num_m_args = 0; |
Kingwel Xie | 72b3bce | 2019-02-12 06:45:08 -0500 | [diff] [blame] | 619 | u8 ipv4_set = 0; |
| 620 | u8 ipv6_set = 0; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 621 | clib_error_t *error = NULL; |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 622 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 623 | clib_memset (&a, 0, sizeof (a)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 624 | a.is_add = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 625 | |
| 626 | /* Get a line of input. */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 627 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 628 | return 0; |
| 629 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 630 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 631 | { |
| 632 | if (unformat |
Kingwel Xie | 72b3bce | 2019-02-12 06:45:08 -0500 | [diff] [blame] | 633 | (line_input, "local-ip %U", unformat_ip46_address, &a.local_ip, |
| 634 | IP46_TYPE_ANY)) |
| 635 | { |
| 636 | ip46_address_is_ip4 (&a.local_ip) ? (ipv4_set = 1) : (ipv6_set = 1); |
| 637 | num_m_args++; |
| 638 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 639 | else |
| 640 | if (unformat |
Kingwel Xie | 72b3bce | 2019-02-12 06:45:08 -0500 | [diff] [blame] | 641 | (line_input, "remote-ip %U", unformat_ip46_address, &a.remote_ip, |
| 642 | IP46_TYPE_ANY)) |
| 643 | { |
| 644 | ip46_address_is_ip4 (&a.remote_ip) ? (ipv4_set = 1) : (ipv6_set = |
| 645 | 1); |
| 646 | num_m_args++; |
| 647 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 648 | else if (unformat (line_input, "local-spi %u", &a.local_spi)) |
| 649 | num_m_args++; |
| 650 | else if (unformat (line_input, "remote-spi %u", &a.remote_spi)) |
| 651 | num_m_args++; |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 652 | else if (unformat (line_input, "instance %u", &a.show_instance)) |
| 653 | a.renumber = 1; |
Radu Nicolau | 717de09 | 2018-08-03 10:37:24 +0100 | [diff] [blame] | 654 | else if (unformat (line_input, "udp-encap")) |
| 655 | a.udp_encap = 1; |
Kingwel Xie | 2baf942 | 2019-02-04 02:07:06 -0800 | [diff] [blame] | 656 | else if (unformat (line_input, "use-esn")) |
| 657 | a.esn = 1; |
| 658 | else if (unformat (line_input, "use-anti-replay")) |
| 659 | a.anti_replay = 1; |
Pierre Pfister | 4c422f9 | 2018-12-10 11:19:08 +0100 | [diff] [blame] | 660 | else if (unformat (line_input, "tx-table %u", &a.tx_table_id)) |
| 661 | ; |
Kingwel Xie | 2baf942 | 2019-02-04 02:07:06 -0800 | [diff] [blame] | 662 | else if (unformat (line_input, "del")) |
| 663 | a.is_add = 0; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 664 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 665 | { |
| 666 | error = clib_error_return (0, "unknown input `%U'", |
| 667 | format_unformat_error, line_input); |
| 668 | goto done; |
| 669 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 670 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 671 | |
| 672 | if (num_m_args < 4) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 673 | { |
| 674 | error = clib_error_return (0, "mandatory argument(s) missing"); |
| 675 | goto done; |
| 676 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 677 | |
Kingwel Xie | 72b3bce | 2019-02-12 06:45:08 -0500 | [diff] [blame] | 678 | if (ipv6_set) |
| 679 | return clib_error_return (0, "currently only IPv4 supported"); |
| 680 | |
| 681 | if (ipv4_set && ipv6_set) |
| 682 | return clib_error_return (0, "both IPv4 and IPv6 addresses specified"); |
| 683 | |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 684 | rv = ipsec_add_del_tunnel_if (&a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 685 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 686 | switch (rv) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 687 | { |
| 688 | case 0: |
| 689 | break; |
| 690 | case VNET_API_ERROR_INVALID_VALUE: |
| 691 | if (a.is_add) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 692 | error = clib_error_return (0, |
| 693 | "IPSec tunnel interface already exists..."); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 694 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 695 | error = clib_error_return (0, "IPSec tunnel interface not exists..."); |
| 696 | goto done; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 697 | default: |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 698 | error = clib_error_return (0, "ipsec_register_interface returned %d", |
| 699 | rv); |
| 700 | goto done; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 701 | } |
| 702 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 703 | done: |
| 704 | unformat_free (line_input); |
| 705 | |
| 706 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 707 | } |
| 708 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 709 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 710 | VLIB_CLI_COMMAND (create_ipsec_tunnel_command, static) = { |
| 711 | .path = "create ipsec tunnel", |
Pierre Pfister | 4c422f9 | 2018-12-10 11:19:08 +0100 | [diff] [blame] | 712 | .short_help = "create ipsec tunnel local-ip <addr> local-spi <spi> " |
Kingwel Xie | 2baf942 | 2019-02-04 02:07:06 -0800 | [diff] [blame] | 713 | "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] | 714 | "[tx-table <table-id>]", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 715 | .function = create_ipsec_tunnel_command_fn, |
| 716 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 717 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 718 | |
| 719 | static clib_error_t * |
| 720 | set_interface_key_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 721 | unformat_input_t * input, |
| 722 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 723 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 724 | unformat_input_t _line_input, *line_input = &_line_input; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 725 | ipsec_main_t *im = &ipsec_main; |
| 726 | ipsec_if_set_key_type_t type = IPSEC_IF_SET_KEY_TYPE_NONE; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 727 | u32 hw_if_index = (u32) ~ 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 728 | u32 alg; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 729 | u8 *key = 0; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 730 | clib_error_t *error = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 731 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 732 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 733 | return 0; |
| 734 | |
| 735 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 736 | { |
| 737 | if (unformat (line_input, "%U", |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 738 | unformat_vnet_hw_interface, im->vnet_main, &hw_if_index)) |
| 739 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 740 | else |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 741 | if (unformat |
| 742 | (line_input, "local crypto %U", unformat_ipsec_crypto_alg, &alg)) |
| 743 | type = IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO; |
| 744 | else |
| 745 | if (unformat |
| 746 | (line_input, "remote crypto %U", unformat_ipsec_crypto_alg, &alg)) |
| 747 | type = IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO; |
| 748 | else |
| 749 | if (unformat |
| 750 | (line_input, "local integ %U", unformat_ipsec_integ_alg, &alg)) |
| 751 | type = IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG; |
| 752 | else |
| 753 | if (unformat |
| 754 | (line_input, "remote integ %U", unformat_ipsec_integ_alg, &alg)) |
| 755 | type = IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG; |
| 756 | else if (unformat (line_input, "%U", unformat_hex_string, &key)) |
| 757 | ; |
| 758 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 759 | { |
| 760 | error = clib_error_return (0, "parse error: '%U'", |
| 761 | format_unformat_error, line_input); |
| 762 | goto done; |
| 763 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 764 | } |
| 765 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 766 | if (type == IPSEC_IF_SET_KEY_TYPE_NONE) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 767 | { |
| 768 | error = clib_error_return (0, "unknown key type"); |
| 769 | goto done; |
| 770 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 771 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 772 | if (alg > 0 && vec_len (key) == 0) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 773 | { |
| 774 | error = clib_error_return (0, "key is not specified"); |
| 775 | goto done; |
| 776 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 777 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 778 | if (hw_if_index == (u32) ~ 0) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 779 | { |
| 780 | error = clib_error_return (0, "interface not specified"); |
| 781 | goto done; |
| 782 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 783 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 784 | ipsec_set_interface_key (im->vnet_main, hw_if_index, type, alg, key); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 785 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 786 | done: |
| 787 | vec_free (key); |
| 788 | unformat_free (line_input); |
| 789 | |
| 790 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 791 | } |
| 792 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 793 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 794 | VLIB_CLI_COMMAND (set_interface_key_command, static) = { |
| 795 | .path = "set interface ipsec key", |
| 796 | .short_help = |
| 797 | "set interface ipsec key <int> <local|remote> <crypto|integ> <key type> <key>", |
| 798 | .function = set_interface_key_command_fn, |
| 799 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 800 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 801 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 802 | clib_error_t * |
| 803 | ipsec_cli_init (vlib_main_t * vm) |
| 804 | { |
| 805 | return 0; |
| 806 | } |
| 807 | |
| 808 | VLIB_INIT_FUNCTION (ipsec_cli_init); |
| 809 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 810 | |
| 811 | /* |
| 812 | * fd.io coding-style-patch-verification: ON |
| 813 | * |
| 814 | * Local Variables: |
| 815 | * eval: (c-set-style "gnu") |
| 816 | * End: |
| 817 | */ |