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> |
| 22 | |
| 23 | #include <vnet/ipsec/ipsec.h> |
| 24 | |
| 25 | static clib_error_t * |
| 26 | set_interface_spd_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 27 | unformat_input_t * input, |
| 28 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 29 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 30 | unformat_input_t _line_input, *line_input = &_line_input; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 31 | ipsec_main_t *im = &ipsec_main; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 32 | u32 sw_if_index = (u32) ~ 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 33 | u32 spd_id; |
| 34 | int is_add = 1; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 35 | clib_error_t *error = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 36 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 37 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 38 | return 0; |
| 39 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 40 | if (unformat |
| 41 | (line_input, "%U %u", unformat_vnet_sw_interface, im->vnet_main, |
| 42 | &sw_if_index, &spd_id)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 43 | ; |
| 44 | else if (unformat (line_input, "del")) |
| 45 | is_add = 0; |
| 46 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 47 | { |
| 48 | error = clib_error_return (0, "parse error: '%U'", |
| 49 | format_unformat_error, line_input); |
| 50 | goto done; |
| 51 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 52 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 53 | ipsec_set_interface_spd (vm, sw_if_index, spd_id, is_add); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 54 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 55 | done: |
| 56 | unformat_free (line_input); |
| 57 | |
| 58 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 61 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 62 | VLIB_CLI_COMMAND (set_interface_spd_command, static) = { |
| 63 | .path = "set interface ipsec spd", |
| 64 | .short_help = |
| 65 | "set interface ipsec spd <int> <id>", |
| 66 | .function = set_interface_spd_command_fn, |
| 67 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 68 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 69 | |
| 70 | static clib_error_t * |
| 71 | ipsec_sa_add_del_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 72 | unformat_input_t * input, |
| 73 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 74 | { |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 75 | ipsec_main_t *im = &ipsec_main; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 76 | unformat_input_t _line_input, *line_input = &_line_input; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 77 | ipsec_sa_t sa; |
| 78 | int is_add = ~0; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 79 | u8 *ck = 0, *ik = 0; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 80 | clib_error_t *error = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 81 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 82 | memset (&sa, 0, sizeof (sa)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 83 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 84 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 85 | return 0; |
| 86 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 87 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 88 | { |
| 89 | if (unformat (line_input, "add %u", &sa.id)) |
| 90 | is_add = 1; |
| 91 | else if (unformat (line_input, "del %u", &sa.id)) |
| 92 | is_add = 0; |
| 93 | else if (unformat (line_input, "spi %u", &sa.spi)) |
| 94 | ; |
| 95 | else if (unformat (line_input, "esp")) |
| 96 | sa.protocol = IPSEC_PROTOCOL_ESP; |
| 97 | else if (unformat (line_input, "ah")) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 98 | { |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 99 | sa.protocol = IPSEC_PROTOCOL_AH; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 100 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 101 | else |
| 102 | if (unformat (line_input, "crypto-key %U", unformat_hex_string, &ck)) |
| 103 | sa.crypto_key_len = vec_len (ck); |
| 104 | else |
| 105 | if (unformat |
| 106 | (line_input, "crypto-alg %U", unformat_ipsec_crypto_alg, |
| 107 | &sa.crypto_alg)) |
| 108 | { |
Radu Nicolau | 89c2781 | 2018-05-04 12:51:53 +0100 | [diff] [blame] | 109 | if (sa.crypto_alg < IPSEC_CRYPTO_ALG_NONE || |
Radu Nicolau | 6929ea9 | 2016-11-29 11:00:30 +0000 | [diff] [blame] | 110 | sa.crypto_alg >= IPSEC_CRYPTO_N_ALG) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 111 | { |
| 112 | error = clib_error_return (0, "unsupported crypto-alg: '%U'", |
| 113 | format_ipsec_crypto_alg, |
| 114 | sa.crypto_alg); |
| 115 | goto done; |
| 116 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 117 | } |
| 118 | else |
| 119 | if (unformat (line_input, "integ-key %U", unformat_hex_string, &ik)) |
| 120 | sa.integ_key_len = vec_len (ik); |
| 121 | else if (unformat (line_input, "integ-alg %U", unformat_ipsec_integ_alg, |
| 122 | &sa.integ_alg)) |
| 123 | { |
Radu Nicolau | 89c2781 | 2018-05-04 12:51:53 +0100 | [diff] [blame] | 124 | if (sa.integ_alg < IPSEC_INTEG_ALG_NONE || |
Radu Nicolau | 6929ea9 | 2016-11-29 11:00:30 +0000 | [diff] [blame] | 125 | sa.integ_alg >= IPSEC_INTEG_N_ALG) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 126 | { |
| 127 | error = clib_error_return (0, "unsupported integ-alg: '%U'", |
| 128 | format_ipsec_integ_alg, |
| 129 | sa.integ_alg); |
| 130 | goto done; |
| 131 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 132 | } |
| 133 | else if (unformat (line_input, "tunnel-src %U", |
| 134 | unformat_ip4_address, &sa.tunnel_src_addr.ip4)) |
| 135 | sa.is_tunnel = 1; |
| 136 | else if (unformat (line_input, "tunnel-dst %U", |
| 137 | unformat_ip4_address, &sa.tunnel_dst_addr.ip4)) |
| 138 | sa.is_tunnel = 1; |
| 139 | else if (unformat (line_input, "tunnel-src %U", |
| 140 | unformat_ip6_address, &sa.tunnel_src_addr.ip6)) |
| 141 | { |
| 142 | sa.is_tunnel = 1; |
| 143 | sa.is_tunnel_ip6 = 1; |
| 144 | } |
| 145 | else if (unformat (line_input, "tunnel-dst %U", |
| 146 | unformat_ip6_address, &sa.tunnel_dst_addr.ip6)) |
| 147 | { |
| 148 | sa.is_tunnel = 1; |
| 149 | sa.is_tunnel_ip6 = 1; |
| 150 | } |
| 151 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 152 | { |
| 153 | error = clib_error_return (0, "parse error: '%U'", |
| 154 | format_unformat_error, line_input); |
| 155 | goto done; |
| 156 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 157 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 158 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 159 | if (sa.crypto_key_len > sizeof (sa.crypto_key)) |
| 160 | sa.crypto_key_len = sizeof (sa.crypto_key); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 161 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 162 | if (sa.integ_key_len > sizeof (sa.integ_key)) |
| 163 | sa.integ_key_len = sizeof (sa.integ_key); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 164 | |
| 165 | if (ck) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 166 | strncpy ((char *) sa.crypto_key, (char *) ck, sa.crypto_key_len); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 167 | |
| 168 | if (ik) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 169 | strncpy ((char *) sa.integ_key, (char *) ik, sa.integ_key_len); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 170 | |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 171 | if (is_add) |
| 172 | { |
| 173 | ASSERT (im->cb.check_support_cb); |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 174 | error = im->cb.check_support_cb (&sa); |
| 175 | if (error) |
| 176 | goto done; |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 179 | ipsec_add_del_sa (vm, &sa, is_add, 0 /* enable nat traversal */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 180 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 181 | done: |
| 182 | unformat_free (line_input); |
| 183 | |
| 184 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 187 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 188 | VLIB_CLI_COMMAND (ipsec_sa_add_del_command, static) = { |
| 189 | .path = "ipsec sa", |
| 190 | .short_help = |
| 191 | "ipsec sa [add|del]", |
| 192 | .function = ipsec_sa_add_del_command_fn, |
| 193 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 194 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 195 | |
| 196 | static clib_error_t * |
| 197 | ipsec_spd_add_del_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 198 | unformat_input_t * input, |
| 199 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 200 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 201 | unformat_input_t _line_input, *line_input = &_line_input; |
Damjan Marion | 3f54b18 | 2016-08-16 11:27:02 +0200 | [diff] [blame] | 202 | u32 spd_id = ~0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 203 | int is_add = ~0; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 204 | clib_error_t *error = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 205 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 206 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 207 | return 0; |
| 208 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 209 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 210 | { |
| 211 | if (unformat (line_input, "add")) |
| 212 | is_add = 1; |
| 213 | else if (unformat (line_input, "del")) |
| 214 | is_add = 0; |
| 215 | else if (unformat (line_input, "%u", &spd_id)) |
| 216 | ; |
| 217 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 218 | { |
| 219 | error = clib_error_return (0, "parse error: '%U'", |
| 220 | format_unformat_error, line_input); |
| 221 | goto done; |
| 222 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 223 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 224 | |
Damjan Marion | 3f54b18 | 2016-08-16 11:27:02 +0200 | [diff] [blame] | 225 | if (spd_id == ~0) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 226 | { |
| 227 | error = clib_error_return (0, "please specify SPD ID"); |
| 228 | goto done; |
| 229 | } |
Damjan Marion | 3f54b18 | 2016-08-16 11:27:02 +0200 | [diff] [blame] | 230 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 231 | ipsec_add_del_spd (vm, spd_id, is_add); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 232 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 233 | done: |
| 234 | unformat_free (line_input); |
| 235 | |
| 236 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 239 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 240 | VLIB_CLI_COMMAND (ipsec_spd_add_del_command, static) = { |
| 241 | .path = "ipsec spd", |
| 242 | .short_help = |
| 243 | "ipsec spd [add|del] <id>", |
| 244 | .function = ipsec_spd_add_del_command_fn, |
| 245 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 246 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 247 | |
| 248 | |
| 249 | static clib_error_t * |
| 250 | ipsec_policy_add_del_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 251 | unformat_input_t * input, |
| 252 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 253 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 254 | unformat_input_t _line_input, *line_input = &_line_input; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 255 | ipsec_policy_t p; |
| 256 | int is_add = 0; |
| 257 | int is_ip_any = 1; |
| 258 | u32 tmp, tmp2; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 259 | clib_error_t *error = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 260 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 261 | memset (&p, 0, sizeof (p)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 262 | p.lport.stop = p.rport.stop = ~0; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 263 | p.laddr.stop.ip4.as_u32 = p.raddr.stop.ip4.as_u32 = (u32) ~ 0; |
| 264 | p.laddr.stop.ip6.as_u64[0] = p.laddr.stop.ip6.as_u64[1] = (u64) ~ 0; |
| 265 | 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] | 266 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 267 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 268 | return 0; |
| 269 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 270 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 271 | { |
| 272 | if (unformat (line_input, "add")) |
| 273 | is_add = 1; |
| 274 | else if (unformat (line_input, "del")) |
| 275 | is_add = 0; |
| 276 | else if (unformat (line_input, "spd %u", &p.id)) |
| 277 | ; |
| 278 | else if (unformat (line_input, "inbound")) |
| 279 | p.is_outbound = 0; |
| 280 | else if (unformat (line_input, "outbound")) |
| 281 | p.is_outbound = 1; |
| 282 | else if (unformat (line_input, "priority %d", &p.priority)) |
| 283 | ; |
| 284 | else if (unformat (line_input, "protocol %u", &tmp)) |
| 285 | p.protocol = (u8) tmp; |
| 286 | else |
| 287 | if (unformat |
| 288 | (line_input, "action %U", unformat_ipsec_policy_action, |
| 289 | &p.policy)) |
| 290 | { |
| 291 | if (p.policy == IPSEC_POLICY_ACTION_RESOLVE) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 292 | { |
| 293 | error = clib_error_return (0, "unsupported action: 'resolve'"); |
| 294 | goto done; |
| 295 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 296 | } |
| 297 | else if (unformat (line_input, "sa %u", &p.sa_id)) |
| 298 | ; |
| 299 | else if (unformat (line_input, "local-ip-range %U - %U", |
| 300 | unformat_ip4_address, &p.laddr.start.ip4, |
| 301 | unformat_ip4_address, &p.laddr.stop.ip4)) |
| 302 | is_ip_any = 0; |
| 303 | else if (unformat (line_input, "remote-ip-range %U - %U", |
| 304 | unformat_ip4_address, &p.raddr.start.ip4, |
| 305 | unformat_ip4_address, &p.raddr.stop.ip4)) |
| 306 | is_ip_any = 0; |
| 307 | else if (unformat (line_input, "local-ip-range %U - %U", |
| 308 | unformat_ip6_address, &p.laddr.start.ip6, |
| 309 | unformat_ip6_address, &p.laddr.stop.ip6)) |
| 310 | { |
| 311 | p.is_ipv6 = 1; |
| 312 | is_ip_any = 0; |
| 313 | } |
| 314 | else if (unformat (line_input, "remote-ip-range %U - %U", |
| 315 | unformat_ip6_address, &p.raddr.start.ip6, |
| 316 | unformat_ip6_address, &p.raddr.stop.ip6)) |
| 317 | { |
| 318 | p.is_ipv6 = 1; |
| 319 | is_ip_any = 0; |
| 320 | } |
| 321 | else if (unformat (line_input, "local-port-range %u - %u", &tmp, &tmp2)) |
| 322 | { |
| 323 | p.lport.start = tmp; |
| 324 | p.lport.stop = tmp2; |
| 325 | } |
| 326 | else |
| 327 | if (unformat (line_input, "remote-port-range %u - %u", &tmp, &tmp2)) |
| 328 | { |
| 329 | p.rport.start = tmp; |
| 330 | p.rport.stop = tmp2; |
| 331 | } |
| 332 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 333 | { |
| 334 | error = clib_error_return (0, "parse error: '%U'", |
| 335 | format_unformat_error, line_input); |
| 336 | goto done; |
| 337 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 338 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 339 | |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 340 | /* Check if SA is for IPv6/AH which is not supported. Return error if TRUE. */ |
| 341 | if (p.sa_id) |
| 342 | { |
| 343 | uword *p1; |
| 344 | ipsec_main_t *im = &ipsec_main; |
| 345 | ipsec_sa_t *sa = 0; |
| 346 | p1 = hash_get (im->sa_index_by_sa_id, p.sa_id); |
| 347 | sa = pool_elt_at_index (im->sad, p1[0]); |
| 348 | if (sa && sa->protocol == IPSEC_PROTOCOL_AH && is_add && p.is_ipv6) |
| 349 | { |
| 350 | error = clib_error_return (0, "AH not supported for IPV6: '%U'", |
| 351 | format_unformat_error, line_input); |
| 352 | goto done; |
| 353 | } |
| 354 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 355 | ipsec_add_del_policy (vm, &p, is_add); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 356 | if (is_ip_any) |
| 357 | { |
| 358 | p.is_ipv6 = 1; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 359 | ipsec_add_del_policy (vm, &p, is_add); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 360 | } |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 361 | |
| 362 | done: |
| 363 | unformat_free (line_input); |
| 364 | |
| 365 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 368 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 369 | VLIB_CLI_COMMAND (ipsec_policy_add_del_command, static) = { |
| 370 | .path = "ipsec policy", |
| 371 | .short_help = |
| 372 | "ipsec policy [add|del] spd <id> priority <n> ", |
| 373 | .function = ipsec_policy_add_del_command_fn, |
| 374 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 375 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 376 | |
| 377 | static clib_error_t * |
| 378 | set_ipsec_sa_key_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 379 | unformat_input_t * input, |
| 380 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 381 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 382 | unformat_input_t _line_input, *line_input = &_line_input; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 383 | ipsec_sa_t sa; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 384 | u8 *ck = 0, *ik = 0; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 385 | clib_error_t *error = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 386 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 387 | memset (&sa, 0, sizeof (sa)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 388 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 389 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 390 | return 0; |
| 391 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 392 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 393 | { |
| 394 | if (unformat (line_input, "%u", &sa.id)) |
| 395 | ; |
| 396 | else |
| 397 | if (unformat (line_input, "crypto-key %U", unformat_hex_string, &ck)) |
| 398 | sa.crypto_key_len = vec_len (ck); |
| 399 | else |
| 400 | if (unformat (line_input, "integ-key %U", unformat_hex_string, &ik)) |
| 401 | sa.integ_key_len = vec_len (ik); |
| 402 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 403 | { |
| 404 | error = clib_error_return (0, "parse error: '%U'", |
| 405 | format_unformat_error, line_input); |
| 406 | goto done; |
| 407 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 408 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 409 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 410 | if (sa.crypto_key_len > sizeof (sa.crypto_key)) |
| 411 | sa.crypto_key_len = sizeof (sa.crypto_key); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 412 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 413 | if (sa.integ_key_len > sizeof (sa.integ_key)) |
| 414 | sa.integ_key_len = sizeof (sa.integ_key); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 415 | |
| 416 | if (ck) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 417 | strncpy ((char *) sa.crypto_key, (char *) ck, sa.crypto_key_len); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 418 | |
| 419 | if (ik) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 420 | strncpy ((char *) sa.integ_key, (char *) ik, sa.integ_key_len); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 421 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 422 | ipsec_set_sa_key (vm, &sa); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 423 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 424 | done: |
| 425 | unformat_free (line_input); |
| 426 | |
| 427 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 430 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 431 | VLIB_CLI_COMMAND (set_ipsec_sa_key_command, static) = { |
| 432 | .path = "set ipsec sa", |
| 433 | .short_help = |
| 434 | "set ipsec sa <id> crypto-key <key> integ-key <key>", |
| 435 | .function = set_ipsec_sa_key_command_fn, |
| 436 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 437 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 438 | |
| 439 | static clib_error_t * |
| 440 | show_ipsec_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 441 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 442 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 443 | ipsec_spd_t *spd; |
| 444 | ipsec_sa_t *sa; |
| 445 | ipsec_policy_t *p; |
| 446 | ipsec_main_t *im = &ipsec_main; |
| 447 | u32 *i; |
| 448 | ipsec_tunnel_if_t *t; |
| 449 | vnet_hw_interface_t *hi; |
Klement Sekera | ea84130 | 2018-05-11 12:59:05 +0200 | [diff] [blame] | 450 | u8 *protocol = NULL; |
| 451 | u8 *policy = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 452 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 453 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 454 | pool_foreach (sa, im->sad, ({ |
| 455 | if (sa->id) { |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 456 | vlib_cli_output(vm, "sa %u spi %u mode %s protocol %s%s", sa->id, sa->spi, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 457 | sa->is_tunnel ? "tunnel" : "transport", |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 458 | sa->protocol ? "esp" : "ah", |
| 459 | sa->udp_encap ? " udp-encap-enabled" : ""); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 460 | if (sa->protocol == IPSEC_PROTOCOL_ESP) { |
| 461 | vlib_cli_output(vm, " crypto alg %U%s%U integrity alg %U%s%U", |
| 462 | format_ipsec_crypto_alg, sa->crypto_alg, |
| 463 | sa->crypto_alg ? " key " : "", |
| 464 | format_hex_bytes, sa->crypto_key, sa->crypto_key_len, |
| 465 | format_ipsec_integ_alg, sa->integ_alg, |
| 466 | sa->integ_alg ? " key " : "", |
| 467 | format_hex_bytes, sa->integ_key, sa->integ_key_len); |
| 468 | } |
| 469 | if (sa->is_tunnel && sa->is_tunnel_ip6) { |
| 470 | vlib_cli_output(vm, " tunnel src %U dst %U", |
| 471 | format_ip6_address, &sa->tunnel_src_addr.ip6, |
| 472 | format_ip6_address, &sa->tunnel_dst_addr.ip6); |
| 473 | } else if (sa->is_tunnel) { |
| 474 | vlib_cli_output(vm, " tunnel src %U dst %U", |
| 475 | format_ip4_address, &sa->tunnel_src_addr.ip4, |
| 476 | format_ip4_address, &sa->tunnel_dst_addr.ip4); |
| 477 | } |
| 478 | } |
| 479 | })); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 480 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 481 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 482 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 483 | pool_foreach (spd, im->spds, ({ |
| 484 | vlib_cli_output(vm, "spd %u", spd->id); |
| 485 | |
| 486 | vlib_cli_output(vm, " outbound policies"); |
| 487 | vec_foreach(i, spd->ipv4_outbound_policies) |
| 488 | { |
| 489 | p = pool_elt_at_index(spd->policies, *i); |
Klement Sekera | ea84130 | 2018-05-11 12:59:05 +0200 | [diff] [blame] | 490 | vec_reset_length(protocol); |
| 491 | vec_reset_length(policy); |
| 492 | if (p->protocol) { |
| 493 | protocol = format(protocol, "%U", format_ip_protocol, p->protocol); |
| 494 | } else { |
| 495 | protocol = format(protocol, "any"); |
| 496 | } |
| 497 | if (p->policy == IPSEC_POLICY_ACTION_PROTECT) { |
| 498 | policy = format(policy, " sa %u", p->sa_id); |
| 499 | } |
| 500 | |
| 501 | vlib_cli_output(vm, " priority %d action %U protocol %v%v", |
| 502 | p->priority, format_ipsec_policy_action, p->policy, |
| 503 | protocol, policy); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 504 | vlib_cli_output(vm, " local addr range %U - %U port range %u - %u", |
| 505 | format_ip4_address, &p->laddr.start.ip4, |
| 506 | format_ip4_address, &p->laddr.stop.ip4, |
| 507 | p->lport.start, p->lport.stop); |
| 508 | vlib_cli_output(vm, " remte addr range %U - %U port range %u - %u", |
| 509 | format_ip4_address, &p->raddr.start.ip4, |
| 510 | format_ip4_address, &p->raddr.stop.ip4, |
| 511 | p->rport.start, p->rport.stop); |
| 512 | vlib_cli_output(vm, " packets %u bytes %u", p->counter.packets, |
| 513 | p->counter.bytes); |
| 514 | }; |
| 515 | vec_foreach(i, spd->ipv6_outbound_policies) |
| 516 | { |
| 517 | p = pool_elt_at_index(spd->policies, *i); |
Klement Sekera | ea84130 | 2018-05-11 12:59:05 +0200 | [diff] [blame] | 518 | vec_reset_length(protocol); |
| 519 | vec_reset_length(policy); |
| 520 | if (p->protocol) { |
| 521 | protocol = format(protocol, "%U", format_ip_protocol, p->protocol); |
| 522 | } else { |
| 523 | protocol = format(protocol, "any"); |
| 524 | } |
| 525 | if (p->policy == IPSEC_POLICY_ACTION_PROTECT) { |
| 526 | policy = format(policy, " sa %u", p->sa_id); |
| 527 | } |
| 528 | vlib_cli_output(vm, " priority %d action %U protocol %v%v", |
| 529 | p->priority, format_ipsec_policy_action, p->policy, |
| 530 | protocol, policy); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 531 | vlib_cli_output(vm, " local addr range %U - %U port range %u - %u", |
| 532 | format_ip6_address, &p->laddr.start.ip6, |
| 533 | format_ip6_address, &p->laddr.stop.ip6, |
| 534 | p->lport.start, p->lport.stop); |
| 535 | vlib_cli_output(vm, " remote addr range %U - %U port range %u - %u", |
| 536 | format_ip6_address, &p->raddr.start.ip6, |
| 537 | format_ip6_address, &p->raddr.stop.ip6, |
| 538 | p->rport.start, p->rport.stop); |
| 539 | vlib_cli_output(vm, " packets %u bytes %u", p->counter.packets, |
| 540 | p->counter.bytes); |
| 541 | }; |
| 542 | vlib_cli_output(vm, " inbound policies"); |
| 543 | vec_foreach(i, spd->ipv4_inbound_protect_policy_indices) |
| 544 | { |
| 545 | p = pool_elt_at_index(spd->policies, *i); |
Klement Sekera | ea84130 | 2018-05-11 12:59:05 +0200 | [diff] [blame] | 546 | vec_reset_length(protocol); |
| 547 | vec_reset_length(policy); |
| 548 | if (p->protocol) { |
| 549 | protocol = format(protocol, "%U", format_ip_protocol, p->protocol); |
| 550 | } else { |
| 551 | protocol = format(protocol, "any"); |
| 552 | } |
| 553 | if (p->policy == IPSEC_POLICY_ACTION_PROTECT) { |
| 554 | policy = format(policy, " sa %u", p->sa_id); |
| 555 | } |
| 556 | vlib_cli_output(vm, " priority %d action %U protocol %v%v", |
| 557 | p->priority, format_ipsec_policy_action, p->policy, |
| 558 | protocol, policy); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 559 | vlib_cli_output(vm, " local addr range %U - %U port range %u - %u", |
| 560 | format_ip4_address, &p->laddr.start.ip4, |
| 561 | format_ip4_address, &p->laddr.stop.ip4, |
| 562 | p->lport.start, p->lport.stop); |
| 563 | vlib_cli_output(vm, " remte addr range %U - %U port range %u - %u", |
| 564 | format_ip4_address, &p->raddr.start.ip4, |
| 565 | format_ip4_address, &p->raddr.stop.ip4, |
| 566 | p->rport.start, p->rport.stop); |
| 567 | vlib_cli_output(vm, " packets %u bytes %u", p->counter.packets, |
| 568 | p->counter.bytes); |
| 569 | }; |
| 570 | vec_foreach(i, spd->ipv4_inbound_policy_discard_and_bypass_indices) |
| 571 | { |
| 572 | p = pool_elt_at_index(spd->policies, *i); |
Klement Sekera | ea84130 | 2018-05-11 12:59:05 +0200 | [diff] [blame] | 573 | vec_reset_length(protocol); |
| 574 | vec_reset_length(policy); |
| 575 | if (p->protocol) { |
| 576 | protocol = format(protocol, "%U", format_ip_protocol, p->protocol); |
| 577 | } else { |
| 578 | protocol = format(protocol, "any"); |
| 579 | } |
| 580 | if (p->policy == IPSEC_POLICY_ACTION_PROTECT) { |
| 581 | policy = format(policy, " sa %u", p->sa_id); |
| 582 | } |
| 583 | vlib_cli_output(vm, " priority %d action %U protocol %v%v", |
| 584 | p->priority, format_ipsec_policy_action, p->policy, |
| 585 | protocol, policy); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 586 | vlib_cli_output(vm, " local addr range %U - %U port range %u - %u", |
| 587 | format_ip4_address, &p->laddr.start.ip4, |
| 588 | format_ip4_address, &p->laddr.stop.ip4, |
| 589 | p->lport.start, p->lport.stop); |
| 590 | vlib_cli_output(vm, " remte addr range %U - %U port range %u - %u", |
| 591 | format_ip4_address, &p->raddr.start.ip4, |
| 592 | format_ip4_address, &p->raddr.stop.ip4, |
| 593 | p->rport.start, p->rport.stop); |
| 594 | vlib_cli_output(vm, " packets %u bytes %u", p->counter.packets, |
| 595 | p->counter.bytes); |
| 596 | }; |
| 597 | vec_foreach(i, spd->ipv6_inbound_protect_policy_indices) |
| 598 | { |
| 599 | p = pool_elt_at_index(spd->policies, *i); |
Klement Sekera | ea84130 | 2018-05-11 12:59:05 +0200 | [diff] [blame] | 600 | vec_reset_length(protocol); |
| 601 | vec_reset_length(policy); |
| 602 | if (p->protocol) { |
| 603 | protocol = format(protocol, "%U", format_ip_protocol, p->protocol); |
| 604 | } else { |
| 605 | protocol = format(protocol, "any"); |
| 606 | } |
| 607 | if (p->policy == IPSEC_POLICY_ACTION_PROTECT) { |
| 608 | policy = format(policy, " sa %u", p->sa_id); |
| 609 | } |
| 610 | vlib_cli_output(vm, " priority %d action %U protocol %v%v", |
| 611 | p->priority, format_ipsec_policy_action, p->policy, |
| 612 | protocol, policy); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 613 | vlib_cli_output(vm, " local addr range %U - %U port range %u - %u", |
| 614 | format_ip6_address, &p->laddr.start.ip6, |
| 615 | format_ip6_address, &p->laddr.stop.ip6, |
| 616 | p->lport.start, p->lport.stop); |
| 617 | vlib_cli_output(vm, " remote addr range %U - %U port range %u - %u", |
| 618 | format_ip6_address, &p->raddr.start.ip6, |
| 619 | format_ip6_address, &p->raddr.stop.ip6, |
| 620 | p->rport.start, p->rport.stop); |
| 621 | vlib_cli_output(vm, " packets %u bytes %u", p->counter.packets, |
| 622 | p->counter.bytes); |
| 623 | }; |
| 624 | vec_foreach(i, spd->ipv6_inbound_policy_discard_and_bypass_indices) |
| 625 | { |
| 626 | p = pool_elt_at_index(spd->policies, *i); |
Klement Sekera | ea84130 | 2018-05-11 12:59:05 +0200 | [diff] [blame] | 627 | vec_reset_length(protocol); |
| 628 | vec_reset_length(policy); |
| 629 | if (p->protocol) { |
| 630 | protocol = format(protocol, "%U", format_ip_protocol, p->protocol); |
| 631 | } else { |
| 632 | protocol = format(protocol, "any"); |
| 633 | } |
| 634 | if (p->policy == IPSEC_POLICY_ACTION_PROTECT) { |
| 635 | policy = format(policy, " sa %u", p->sa_id); |
| 636 | } |
| 637 | vlib_cli_output(vm, " priority %d action %U protocol %v%v", |
| 638 | p->priority, format_ipsec_policy_action, p->policy, |
| 639 | protocol, policy); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 640 | vlib_cli_output(vm, " local addr range %U - %U port range %u - %u", |
| 641 | format_ip6_address, &p->laddr.start.ip6, |
| 642 | format_ip6_address, &p->laddr.stop.ip6, |
| 643 | p->lport.start, p->lport.stop); |
| 644 | vlib_cli_output(vm, " remote addr range %U - %U port range %u - %u", |
| 645 | format_ip6_address, &p->raddr.start.ip6, |
| 646 | format_ip6_address, &p->raddr.stop.ip6, |
| 647 | p->rport.start, p->rport.stop); |
| 648 | vlib_cli_output(vm, " packets %u bytes %u", p->counter.packets, |
| 649 | p->counter.bytes); |
| 650 | }; |
| 651 | })); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 652 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 653 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 654 | vlib_cli_output (vm, "tunnel interfaces"); |
| 655 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 656 | pool_foreach (t, im->tunnel_interfaces, ({ |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 657 | if (t->hw_if_index == ~0) |
| 658 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 659 | hi = vnet_get_hw_interface (im->vnet_main, t->hw_if_index); |
| 660 | vlib_cli_output(vm, " %s seq", hi->name); |
| 661 | sa = pool_elt_at_index(im->sad, t->output_sa_index); |
| 662 | vlib_cli_output(vm, " seq %u seq-hi %u esn %u anti-replay %u", |
| 663 | sa->seq, sa->seq_hi, sa->use_esn, sa->use_anti_replay); |
| 664 | vlib_cli_output(vm, " local-spi %u local-ip %U", sa->spi, |
| 665 | format_ip4_address, &sa->tunnel_src_addr.ip4); |
| 666 | vlib_cli_output(vm, " local-crypto %U %U", |
| 667 | format_ipsec_crypto_alg, sa->crypto_alg, |
| 668 | format_hex_bytes, sa->crypto_key, sa->crypto_key_len); |
| 669 | vlib_cli_output(vm, " local-integrity %U %U", |
| 670 | format_ipsec_integ_alg, sa->integ_alg, |
| 671 | format_hex_bytes, sa->integ_key, sa->integ_key_len); |
| 672 | sa = pool_elt_at_index(im->sad, t->input_sa_index); |
| 673 | vlib_cli_output(vm, " last-seq %u last-seq-hi %u esn %u anti-replay %u window %U", |
| 674 | sa->last_seq, sa->last_seq_hi, sa->use_esn, |
| 675 | sa->use_anti_replay, |
| 676 | format_ipsec_replay_window, sa->replay_window); |
| 677 | vlib_cli_output(vm, " remote-spi %u remote-ip %U", sa->spi, |
| 678 | format_ip4_address, &sa->tunnel_src_addr.ip4); |
| 679 | vlib_cli_output(vm, " remote-crypto %U %U", |
| 680 | format_ipsec_crypto_alg, sa->crypto_alg, |
| 681 | format_hex_bytes, sa->crypto_key, sa->crypto_key_len); |
| 682 | vlib_cli_output(vm, " remote-integrity %U %U", |
| 683 | format_ipsec_integ_alg, sa->integ_alg, |
| 684 | format_hex_bytes, sa->integ_key, sa->integ_key_len); |
| 685 | })); |
Klement Sekera | ea84130 | 2018-05-11 12:59:05 +0200 | [diff] [blame] | 686 | vec_free(policy); |
| 687 | vec_free(protocol); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 688 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 689 | return 0; |
| 690 | } |
| 691 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 692 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 693 | VLIB_CLI_COMMAND (show_ipsec_command, static) = { |
| 694 | .path = "show ipsec", |
| 695 | .short_help = "show ipsec", |
| 696 | .function = show_ipsec_command_fn, |
| 697 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 698 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 699 | |
| 700 | static clib_error_t * |
| 701 | clear_ipsec_counters_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 702 | unformat_input_t * input, |
| 703 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 704 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 705 | ipsec_main_t *im = &ipsec_main; |
| 706 | ipsec_spd_t *spd; |
| 707 | ipsec_policy_t *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | pool_foreach (spd, im->spds, ({ |
| 711 | pool_foreach(p, spd->policies, ({ |
| 712 | p->counter.packets = p->counter.bytes = 0; |
| 713 | })); |
| 714 | })); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 715 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 716 | |
| 717 | return 0; |
| 718 | } |
| 719 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 720 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 721 | VLIB_CLI_COMMAND (clear_ipsec_counters_command, static) = { |
| 722 | .path = "clear ipsec counters", |
| 723 | .short_help = "clear ipsec counters", |
| 724 | .function = clear_ipsec_counters_command_fn, |
| 725 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 726 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 727 | |
| 728 | static clib_error_t * |
| 729 | create_ipsec_tunnel_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 730 | unformat_input_t * input, |
| 731 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 732 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 733 | unformat_input_t _line_input, *line_input = &_line_input; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 734 | ipsec_add_del_tunnel_args_t a; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 735 | int rv; |
| 736 | u32 num_m_args = 0; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 737 | clib_error_t *error = NULL; |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 738 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 739 | memset (&a, 0, sizeof (a)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 740 | a.is_add = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 741 | |
| 742 | /* Get a line of input. */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 743 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 744 | return 0; |
| 745 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 746 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 747 | { |
| 748 | if (unformat |
| 749 | (line_input, "local-ip %U", unformat_ip4_address, &a.local_ip)) |
| 750 | num_m_args++; |
| 751 | else |
| 752 | if (unformat |
| 753 | (line_input, "remote-ip %U", unformat_ip4_address, &a.remote_ip)) |
| 754 | num_m_args++; |
| 755 | else if (unformat (line_input, "local-spi %u", &a.local_spi)) |
| 756 | num_m_args++; |
| 757 | else if (unformat (line_input, "remote-spi %u", &a.remote_spi)) |
| 758 | num_m_args++; |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 759 | else if (unformat (line_input, "instance %u", &a.show_instance)) |
| 760 | a.renumber = 1; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 761 | else if (unformat (line_input, "del")) |
| 762 | a.is_add = 0; |
| 763 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 764 | { |
| 765 | error = clib_error_return (0, "unknown input `%U'", |
| 766 | format_unformat_error, line_input); |
| 767 | goto done; |
| 768 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 769 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 770 | |
| 771 | if (num_m_args < 4) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 772 | { |
| 773 | error = clib_error_return (0, "mandatory argument(s) missing"); |
| 774 | goto done; |
| 775 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 776 | |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 777 | rv = ipsec_add_del_tunnel_if (&a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 778 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 779 | switch (rv) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 780 | { |
| 781 | case 0: |
| 782 | break; |
| 783 | case VNET_API_ERROR_INVALID_VALUE: |
| 784 | if (a.is_add) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 785 | error = clib_error_return (0, |
| 786 | "IPSec tunnel interface already exists..."); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 787 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 788 | error = clib_error_return (0, "IPSec tunnel interface not exists..."); |
| 789 | goto done; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 790 | default: |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 791 | error = clib_error_return (0, "ipsec_register_interface returned %d", |
| 792 | rv); |
| 793 | goto done; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 794 | } |
| 795 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 796 | done: |
| 797 | unformat_free (line_input); |
| 798 | |
| 799 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 800 | } |
| 801 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 802 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 803 | VLIB_CLI_COMMAND (create_ipsec_tunnel_command, static) = { |
| 804 | .path = "create ipsec tunnel", |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 805 | .short_help = "create ipsec tunnel local-ip <addr> local-spi <spi> remote-ip <addr> remote-spi <spi> [instance <inst_num>]", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 806 | .function = create_ipsec_tunnel_command_fn, |
| 807 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 808 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 809 | |
| 810 | static clib_error_t * |
| 811 | set_interface_key_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 812 | unformat_input_t * input, |
| 813 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 814 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 815 | unformat_input_t _line_input, *line_input = &_line_input; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 816 | ipsec_main_t *im = &ipsec_main; |
| 817 | 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] | 818 | u32 hw_if_index = (u32) ~ 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 819 | u32 alg; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 820 | u8 *key = 0; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 821 | clib_error_t *error = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 822 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 823 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 824 | return 0; |
| 825 | |
| 826 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 827 | { |
| 828 | if (unformat (line_input, "%U", |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 829 | unformat_vnet_hw_interface, im->vnet_main, &hw_if_index)) |
| 830 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 831 | else |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 832 | if (unformat |
| 833 | (line_input, "local crypto %U", unformat_ipsec_crypto_alg, &alg)) |
| 834 | type = IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO; |
| 835 | else |
| 836 | if (unformat |
| 837 | (line_input, "remote crypto %U", unformat_ipsec_crypto_alg, &alg)) |
| 838 | type = IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO; |
| 839 | else |
| 840 | if (unformat |
| 841 | (line_input, "local integ %U", unformat_ipsec_integ_alg, &alg)) |
| 842 | type = IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG; |
| 843 | else |
| 844 | if (unformat |
| 845 | (line_input, "remote integ %U", unformat_ipsec_integ_alg, &alg)) |
| 846 | type = IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG; |
| 847 | else if (unformat (line_input, "%U", unformat_hex_string, &key)) |
| 848 | ; |
| 849 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 850 | { |
| 851 | error = clib_error_return (0, "parse error: '%U'", |
| 852 | format_unformat_error, line_input); |
| 853 | goto done; |
| 854 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 855 | } |
| 856 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 857 | if (type == IPSEC_IF_SET_KEY_TYPE_NONE) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 858 | { |
| 859 | error = clib_error_return (0, "unknown key type"); |
| 860 | goto done; |
| 861 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 862 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 863 | if (alg > 0 && vec_len (key) == 0) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 864 | { |
| 865 | error = clib_error_return (0, "key is not specified"); |
| 866 | goto done; |
| 867 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 868 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 869 | if (hw_if_index == (u32) ~ 0) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 870 | { |
| 871 | error = clib_error_return (0, "interface not specified"); |
| 872 | goto done; |
| 873 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 874 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 875 | 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] | 876 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 877 | done: |
| 878 | vec_free (key); |
| 879 | unformat_free (line_input); |
| 880 | |
| 881 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 882 | } |
| 883 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 884 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 885 | VLIB_CLI_COMMAND (set_interface_key_command, static) = { |
| 886 | .path = "set interface ipsec key", |
| 887 | .short_help = |
| 888 | "set interface ipsec key <int> <local|remote> <crypto|integ> <key type> <key>", |
| 889 | .function = set_interface_key_command_fn, |
| 890 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 891 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 892 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 893 | clib_error_t * |
| 894 | ipsec_cli_init (vlib_main_t * vm) |
| 895 | { |
| 896 | return 0; |
| 897 | } |
| 898 | |
| 899 | VLIB_INIT_FUNCTION (ipsec_cli_init); |
| 900 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 901 | |
| 902 | /* |
| 903 | * fd.io coding-style-patch-verification: ON |
| 904 | * |
| 905 | * Local Variables: |
| 906 | * eval: (c-set-style "gnu") |
| 907 | * End: |
| 908 | */ |