blob: 6cb9aba4d8ac2fc1c016391ea49b4bbcc6a689ff [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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 Pfister4c422f92018-12-10 11:19:08 +010022#include <vnet/fib/fib.h>
Neale Ranns12989b52019-09-26 16:20:19 +000023#include <vnet/ipip/ipip.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070024
25#include <vnet/ipsec/ipsec.h>
Neale Rannsc87b66c2019-02-07 07:26:12 -080026#include <vnet/ipsec/ipsec_tun.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070027
28static clib_error_t *
29set_interface_spd_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070030 unformat_input_t * input,
31 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -070032{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070033 unformat_input_t _line_input, *line_input = &_line_input;
Ed Warnickecb9cada2015-12-08 15:45:58 -070034 ipsec_main_t *im = &ipsec_main;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070035 u32 sw_if_index = (u32) ~ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070036 u32 spd_id;
37 int is_add = 1;
Billy McFalla9a20e72017-02-15 11:39:12 -050038 clib_error_t *error = NULL;
Benoît Ganne40aa27e2020-11-06 14:14:23 +010039 int err;
Ed Warnickecb9cada2015-12-08 15:45:58 -070040
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070041 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -070042 return 0;
43
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070044 if (unformat
45 (line_input, "%U %u", unformat_vnet_sw_interface, im->vnet_main,
46 &sw_if_index, &spd_id))
Ed Warnickecb9cada2015-12-08 15:45:58 -070047 ;
48 else if (unformat (line_input, "del"))
49 is_add = 0;
50 else
Billy McFalla9a20e72017-02-15 11:39:12 -050051 {
52 error = clib_error_return (0, "parse error: '%U'",
53 format_unformat_error, line_input);
54 goto done;
55 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070056
Benoît Ganne40aa27e2020-11-06 14:14:23 +010057 err = ipsec_set_interface_spd (vm, sw_if_index, spd_id, is_add);
58 switch (err)
59 {
60 case VNET_API_ERROR_SYSCALL_ERROR_1:
61 error = clib_error_return (0, "no such spd-id");
62 break;
63 case VNET_API_ERROR_SYSCALL_ERROR_2:
64 error = clib_error_return (0, "spd already assigned");
65 break;
66 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070067
Billy McFalla9a20e72017-02-15 11:39:12 -050068done:
69 unformat_free (line_input);
70
71 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -070072}
73
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070074/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070075VLIB_CLI_COMMAND (set_interface_spd_command, static) = {
76 .path = "set interface ipsec spd",
77 .short_help =
78 "set interface ipsec spd <int> <id>",
79 .function = set_interface_spd_command_fn,
80};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070081/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070082
83static clib_error_t *
84ipsec_sa_add_del_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070085 unformat_input_t * input,
86 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -070087{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070088 unformat_input_t _line_input, *line_input = &_line_input;
Neale Ranns8d7c5022019-02-06 01:41:05 -080089 ipsec_crypto_alg_t crypto_alg;
90 ipsec_integ_alg_t integ_alg;
91 ipsec_protocol_t proto;
92 ipsec_sa_flags_t flags;
93 clib_error_t *error;
Kingwel Xied3d12052019-03-07 06:34:30 -050094 ipsec_key_t ck = { 0 };
95 ipsec_key_t ik = { 0 };
Neale Rannsabc56602020-04-01 09:45:23 +000096 u32 id, spi, salt, sai;
Mohammed Hawari048189e2021-02-05 19:04:42 +010097 int i = 0;
Neale Rannsabc56602020-04-01 09:45:23 +000098 u16 udp_src, udp_dst;
Neale Ranns8d7c5022019-02-06 01:41:05 -080099 int is_add, rv;
Benoît Gannebdd8b572020-07-28 15:56:15 +0200100 u32 m_args = 0;
Neale Rannsf16e9a52021-02-25 19:09:24 +0000101 tunnel_t tun = {};
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102
Neale Ranns8dc75c02019-12-10 01:08:19 +0000103 salt = 0;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800104 error = NULL;
105 is_add = 0;
106 flags = IPSEC_SA_FLAG_NONE;
107 proto = IPSEC_PROTOCOL_ESP;
Neale Rannse6be7022019-06-04 15:37:34 +0000108 integ_alg = IPSEC_INTEG_ALG_NONE;
109 crypto_alg = IPSEC_CRYPTO_ALG_NONE;
Neale Rannsabc56602020-04-01 09:45:23 +0000110 udp_src = udp_dst = IPSEC_UDP_PORT_NONE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700111
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700112 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113 return 0;
114
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700115 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
116 {
Neale Ranns8d7c5022019-02-06 01:41:05 -0800117 if (unformat (line_input, "add %u", &id))
Benoît Gannebdd8b572020-07-28 15:56:15 +0200118 {
119 is_add = 1;
120 m_args |= 1 << 0;
121 }
Neale Ranns8d7c5022019-02-06 01:41:05 -0800122 else if (unformat (line_input, "del %u", &id))
Benoît Gannebdd8b572020-07-28 15:56:15 +0200123 {
124 is_add = 0;
125 m_args |= 1 << 0;
126 }
Neale Ranns8d7c5022019-02-06 01:41:05 -0800127 else if (unformat (line_input, "spi %u", &spi))
Benoît Gannebdd8b572020-07-28 15:56:15 +0200128 m_args |= 1 << 1;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800129 else if (unformat (line_input, "salt 0x%x", &salt))
Neale Ranns80f6fd52019-04-16 02:41:34 +0000130 ;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700131 else if (unformat (line_input, "esp"))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800132 proto = IPSEC_PROTOCOL_ESP;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700133 else if (unformat (line_input, "ah"))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800134 proto = IPSEC_PROTOCOL_AH;
135 else if (unformat (line_input, "crypto-key %U",
136 unformat_ipsec_key, &ck))
137 ;
138 else if (unformat (line_input, "crypto-alg %U",
139 unformat_ipsec_crypto_alg, &crypto_alg))
140 ;
141 else if (unformat (line_input, "integ-key %U", unformat_ipsec_key, &ik))
142 ;
143 else if (unformat (line_input, "integ-alg %U",
144 unformat_ipsec_integ_alg, &integ_alg))
145 ;
Damjan Marionc50bcbd2022-05-08 20:48:37 +0200146 else if (unformat (line_input, "%U", unformat_tunnel, &tun))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700147 {
Neale Ranns8d7c5022019-02-06 01:41:05 -0800148 flags |= IPSEC_SA_FLAG_IS_TUNNEL;
Neale Ranns9ec846c2021-02-09 14:04:02 +0000149 if (AF_IP6 == tunnel_get_af (&tun))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800150 flags |= IPSEC_SA_FLAG_IS_TUNNEL_V6;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700151 }
Mohammed Hawari048189e2021-02-05 19:04:42 +0100152 else if (unformat (line_input, "udp-src-port %d", &i))
153 udp_src = i;
154 else if (unformat (line_input, "udp-dst-port %d", &i))
155 udp_dst = i;
Christian Hopps99975382020-07-17 09:53:18 -0400156 else if (unformat (line_input, "inbound"))
157 flags |= IPSEC_SA_FLAG_IS_INBOUND;
158 else if (unformat (line_input, "use-anti-replay"))
159 flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
160 else if (unformat (line_input, "use-esn"))
161 flags |= IPSEC_SA_FLAG_USE_ESN;
Radu Nicolau717de092018-08-03 10:37:24 +0100162 else if (unformat (line_input, "udp-encap"))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800163 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
Neale Rannsf16e9a52021-02-25 19:09:24 +0000164 else if (unformat (line_input, "async"))
165 flags |= IPSEC_SA_FLAG_IS_ASYNC;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700166 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500167 {
168 error = clib_error_return (0, "parse error: '%U'",
169 format_unformat_error, line_input);
170 goto done;
171 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700172 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173
Benoît Gannebdd8b572020-07-28 15:56:15 +0200174 if (!(m_args & 1))
175 {
176 error = clib_error_return (0, "missing id");
177 goto done;
178 }
179
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000180 if (is_add)
Benoît Gannebdd8b572020-07-28 15:56:15 +0200181 {
182 if (!(m_args & 2))
183 {
184 error = clib_error_return (0, "missing spi");
185 goto done;
186 }
Neale Ranns9ec846c2021-02-09 14:04:02 +0000187 rv = ipsec_sa_add_and_lock (id, spi, proto, crypto_alg, &ck, integ_alg,
188 &ik, flags, clib_host_to_net_u32 (salt),
189 udp_src, udp_dst, &tun, &sai);
Benoît Gannebdd8b572020-07-28 15:56:15 +0200190 }
Neale Ranns8d7c5022019-02-06 01:41:05 -0800191 else
Benoît Gannebdd8b572020-07-28 15:56:15 +0200192 {
193 rv = ipsec_sa_unlock_id (id);
194 }
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000195
Neale Ranns8d7c5022019-02-06 01:41:05 -0800196 if (rv)
Neale Rannsf16e9a52021-02-25 19:09:24 +0000197 error = clib_error_return (0, "failed: %d", rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700198
Billy McFalla9a20e72017-02-15 11:39:12 -0500199done:
200 unformat_free (line_input);
201
202 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700203}
204
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700205/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700206VLIB_CLI_COMMAND (ipsec_sa_add_del_command, static) = {
207 .path = "ipsec sa",
208 .short_help =
209 "ipsec sa [add|del]",
210 .function = ipsec_sa_add_del_command_fn,
211};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700212/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700213
214static clib_error_t *
Maxime Peim1271e3a2023-03-20 14:13:56 +0000215ipsec_sa_bind_cli (vlib_main_t *vm, unformat_input_t *input,
216 vlib_cli_command_t *cmd)
217{
218 unformat_input_t _line_input, *line_input = &_line_input;
219 u32 id = ~0;
220 u32 worker = ~0;
221 bool bind = 1;
222 int rv;
223 clib_error_t *error = NULL;
224
225 if (!unformat_user (input, unformat_line_input, line_input))
226 return 0;
227
228 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
229 {
230 if (unformat (line_input, "unbind"))
231 bind = 0;
Maxime Peimf902ba52023-07-03 09:42:58 +0200232 else if (id == ~0 && unformat (line_input, "%u", &id))
Maxime Peim1271e3a2023-03-20 14:13:56 +0000233 ;
234 else if (unformat (line_input, "%u", &worker))
235 ;
236 else
237 {
238 error = clib_error_return (0, "parse error: '%U'",
239 format_unformat_error, line_input);
240 goto done;
241 }
242 }
243
244 if (id == ~0)
245 {
246 error = clib_error_return (0, "please specify SA ID");
247 goto done;
248 }
249
250 if (bind && ~0 == worker)
251 {
252 error = clib_error_return (0, "please specify worker to bind to");
253 goto done;
254 }
255
256 rv = ipsec_sa_bind (id, worker, bind);
257 switch (rv)
258 {
259 case VNET_API_ERROR_INVALID_VALUE:
260 error = clib_error_return (0, "please specify a valid SA ID");
261 break;
262 case VNET_API_ERROR_INVALID_WORKER:
263 error = clib_error_return (0, "please specify a valid worker index");
264 break;
265 }
266
267done:
268 unformat_free (line_input);
269
270 return error;
271}
272
273VLIB_CLI_COMMAND (ipsec_sa_bind_cmd, static) = {
274 .path = "ipsec sa bind",
275 .short_help = "ipsec sa [unbind] <sa-id> <worker>",
276 .function = ipsec_sa_bind_cli,
277};
278
279static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700280ipsec_spd_add_del_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700281 unformat_input_t * input,
282 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700283{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700284 unformat_input_t _line_input, *line_input = &_line_input;
Damjan Marion3f54b182016-08-16 11:27:02 +0200285 u32 spd_id = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700286 int is_add = ~0;
Billy McFalla9a20e72017-02-15 11:39:12 -0500287 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700288
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700289 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700290 return 0;
291
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700292 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
293 {
294 if (unformat (line_input, "add"))
295 is_add = 1;
296 else if (unformat (line_input, "del"))
297 is_add = 0;
298 else if (unformat (line_input, "%u", &spd_id))
299 ;
300 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500301 {
302 error = clib_error_return (0, "parse error: '%U'",
303 format_unformat_error, line_input);
304 goto done;
305 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700306 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700307
Damjan Marion3f54b182016-08-16 11:27:02 +0200308 if (spd_id == ~0)
Billy McFalla9a20e72017-02-15 11:39:12 -0500309 {
310 error = clib_error_return (0, "please specify SPD ID");
311 goto done;
312 }
Damjan Marion3f54b182016-08-16 11:27:02 +0200313
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700314 ipsec_add_del_spd (vm, spd_id, is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700315
Billy McFalla9a20e72017-02-15 11:39:12 -0500316done:
317 unformat_free (line_input);
318
319 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700320}
321
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700322/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700323VLIB_CLI_COMMAND (ipsec_spd_add_del_command, static) = {
324 .path = "ipsec spd",
325 .short_help =
326 "ipsec spd [add|del] <id>",
327 .function = ipsec_spd_add_del_command_fn,
328};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700329/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700330
331
332static clib_error_t *
333ipsec_policy_add_del_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700334 unformat_input_t * input,
335 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700336{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700337 unformat_input_t _line_input, *line_input = &_line_input;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700338 ipsec_policy_t p;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800339 int rv, is_add = 0;
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000340 u32 tmp, tmp2, stat_index, local_range_set, remote_range_set;
Billy McFalla9a20e72017-02-15 11:39:12 -0500341 clib_error_t *error = NULL;
Neale Ranns9f231d42019-03-19 10:06:00 +0000342 u32 is_outbound;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700343
Dave Barachb7b92992018-10-17 10:38:51 -0400344 clib_memset (&p, 0, sizeof (p));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700345 p.lport.stop = p.rport.stop = ~0;
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000346 remote_range_set = local_range_set = is_outbound = 0;
Piotr Bronowski815c6a42022-06-09 09:09:28 +0000347 p.protocol = IPSEC_POLICY_PROTOCOL_ANY;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700348
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700349 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700350 return 0;
351
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700352 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
353 {
354 if (unformat (line_input, "add"))
355 is_add = 1;
356 else if (unformat (line_input, "del"))
357 is_add = 0;
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000358 else if (unformat (line_input, "ip6"))
359 p.is_ipv6 = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700360 else if (unformat (line_input, "spd %u", &p.id))
361 ;
362 else if (unformat (line_input, "inbound"))
Neale Ranns9f231d42019-03-19 10:06:00 +0000363 is_outbound = 0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700364 else if (unformat (line_input, "outbound"))
Neale Ranns9f231d42019-03-19 10:06:00 +0000365 is_outbound = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700366 else if (unformat (line_input, "priority %d", &p.priority))
367 ;
368 else if (unformat (line_input, "protocol %u", &tmp))
369 p.protocol = (u8) tmp;
370 else
371 if (unformat
372 (line_input, "action %U", unformat_ipsec_policy_action,
373 &p.policy))
374 {
375 if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
Billy McFalla9a20e72017-02-15 11:39:12 -0500376 {
377 error = clib_error_return (0, "unsupported action: 'resolve'");
378 goto done;
379 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700380 }
381 else if (unformat (line_input, "sa %u", &p.sa_id))
382 ;
383 else if (unformat (line_input, "local-ip-range %U - %U",
384 unformat_ip4_address, &p.laddr.start.ip4,
385 unformat_ip4_address, &p.laddr.stop.ip4))
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000386 local_range_set = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700387 else if (unformat (line_input, "remote-ip-range %U - %U",
388 unformat_ip4_address, &p.raddr.start.ip4,
389 unformat_ip4_address, &p.raddr.stop.ip4))
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000390 remote_range_set = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700391 else if (unformat (line_input, "local-ip-range %U - %U",
392 unformat_ip6_address, &p.laddr.start.ip6,
393 unformat_ip6_address, &p.laddr.stop.ip6))
394 {
395 p.is_ipv6 = 1;
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000396 local_range_set = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700397 }
398 else if (unformat (line_input, "remote-ip-range %U - %U",
399 unformat_ip6_address, &p.raddr.start.ip6,
400 unformat_ip6_address, &p.raddr.stop.ip6))
401 {
402 p.is_ipv6 = 1;
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000403 remote_range_set = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700404 }
405 else if (unformat (line_input, "local-port-range %u - %u", &tmp, &tmp2))
406 {
407 p.lport.start = tmp;
408 p.lport.stop = tmp2;
409 }
410 else
411 if (unformat (line_input, "remote-port-range %u - %u", &tmp, &tmp2))
412 {
413 p.rport.start = tmp;
414 p.rport.stop = tmp2;
415 }
416 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500417 {
418 error = clib_error_return (0, "parse error: '%U'",
419 format_unformat_error, line_input);
420 goto done;
421 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700422 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000424 if (!remote_range_set)
425 {
426 if (p.is_ipv6)
427 clib_memset (&p.raddr.stop.ip6, 0xff, 16);
428 else
429 clib_memset (&p.raddr.stop.ip4, 0xff, 4);
430 }
431 if (!local_range_set)
432 {
433 if (p.is_ipv6)
434 clib_memset (&p.laddr.stop.ip6, 0xff, 16);
435 else
436 clib_memset (&p.laddr.stop.ip4, 0xff, 4);
437 }
438
Neale Ranns9f231d42019-03-19 10:06:00 +0000439 rv = ipsec_policy_mk_type (is_outbound, p.is_ipv6, p.policy, &p.type);
440
441 if (rv)
442 {
443 error = clib_error_return (0, "unsupported policy type for:",
444 " outboud:%s %s action:%U",
445 (is_outbound ? "yes" : "no"),
446 (p.is_ipv6 ? "IPv4" : "IPv6"),
447 format_ipsec_policy_action, p.policy);
448 goto done;
449 }
450
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800451 rv = ipsec_add_del_policy (vm, &p, is_add, &stat_index);
452
453 if (!rv)
454 vlib_cli_output (vm, "policy-index:%d", stat_index);
455 else
456 vlib_cli_output (vm, "error:%d", rv);
Billy McFalla9a20e72017-02-15 11:39:12 -0500457
458done:
459 unformat_free (line_input);
460
461 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700462}
463
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700464/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700465VLIB_CLI_COMMAND (ipsec_policy_add_del_command, static) = {
466 .path = "ipsec policy",
467 .short_help =
468 "ipsec policy [add|del] spd <id> priority <n> ",
469 .function = ipsec_policy_add_del_command_fn,
470};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700471/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700472
Neale Rannsb294f102019-04-03 13:17:50 +0000473static void
Neale Ranns670027a2019-08-27 12:47:17 +0000474ipsec_sa_show_all (vlib_main_t * vm, ipsec_main_t * im, u8 detail)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700475{
Neale Rannsb294f102019-04-03 13:17:50 +0000476 u32 sai;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700477
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700478 /* *INDENT-OFF* */
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000479 pool_foreach_index (sai, ipsec_sa_pool)
480 {
481 vlib_cli_output (vm, "%U", format_ipsec_sa, sai,
482 (detail ? IPSEC_FORMAT_DETAIL : IPSEC_FORMAT_BRIEF));
483 }
Neale Rannsb294f102019-04-03 13:17:50 +0000484 /* *INDENT-ON* */
485}
486
487static void
488ipsec_spd_show_all (vlib_main_t * vm, ipsec_main_t * im)
489{
490 u32 spdi;
491
492 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100493 pool_foreach_index (spdi, im->spds) {
Neale Rannsb294f102019-04-03 13:17:50 +0000494 vlib_cli_output(vm, "%U", format_ipsec_spd, spdi);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100495 }
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +0000496
Zachary Leaf7cd35f52021-06-25 08:11:15 -0500497 if (im->output_flow_cache_flag)
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +0000498 {
Zachary Leaf7cd35f52021-06-25 08:11:15 -0500499 vlib_cli_output (vm, "%U", format_ipsec_out_spd_flow_cache);
500 }
501 if (im->input_flow_cache_flag)
502 {
503 vlib_cli_output (vm, "%U", format_ipsec_in_spd_flow_cache);
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +0000504 }
Neale Rannsb294f102019-04-03 13:17:50 +0000505 /* *INDENT-ON* */
506}
507
508static void
509ipsec_spd_bindings_show_all (vlib_main_t * vm, ipsec_main_t * im)
510{
511 u32 spd_id, sw_if_index;
Neale Rannsd83c4a82019-06-14 06:48:27 -0700512 ipsec_spd_t *spd;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700513
Neale Ranns311124e2019-01-24 04:52:25 -0800514 vlib_cli_output (vm, "SPD Bindings:");
Neale Ranns8d7c5022019-02-06 01:41:05 -0800515
Neale Rannsb294f102019-04-03 13:17:50 +0000516 /* *INDENT-OFF* */
Neale Ranns311124e2019-01-24 04:52:25 -0800517 hash_foreach(sw_if_index, spd_id, im->spd_index_by_sw_if_index, ({
Neale Rannsd83c4a82019-06-14 06:48:27 -0700518 spd = pool_elt_at_index (im->spds, spd_id);
519 vlib_cli_output (vm, " %d -> %U", spd->id,
Neale Ranns8d7c5022019-02-06 01:41:05 -0800520 format_vnet_sw_if_index_name, im->vnet_main,
521 sw_if_index);
Neale Ranns311124e2019-01-24 04:52:25 -0800522 }));
523 /* *INDENT-ON* */
Neale Rannsb294f102019-04-03 13:17:50 +0000524}
Neale Ranns311124e2019-01-24 04:52:25 -0800525
Neale Ranns12989b52019-09-26 16:20:19 +0000526static walk_rc_t
527ipsec_tun_protect_show_one (index_t itpi, void *ctx)
Neale Rannsb294f102019-04-03 13:17:50 +0000528{
Neale Ranns28287212019-12-16 00:53:11 +0000529 vlib_cli_output (ctx, "%U", format_ipsec_tun_protect_index, itpi);
Neale Rannsb294f102019-04-03 13:17:50 +0000530
Neale Ranns12989b52019-09-26 16:20:19 +0000531 return (WALK_CONTINUE);
532}
533
534static void
535ipsec_tunnel_show_all (vlib_main_t * vm)
536{
537 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
Neale Rannsb294f102019-04-03 13:17:50 +0000538}
539
540static clib_error_t *
541show_ipsec_command_fn (vlib_main_t * vm,
542 unformat_input_t * input, vlib_cli_command_t * cmd)
543{
544 ipsec_main_t *im = &ipsec_main;
545
Neale Ranns670027a2019-08-27 12:47:17 +0000546 ipsec_sa_show_all (vm, im, 0);
Neale Rannsb294f102019-04-03 13:17:50 +0000547 ipsec_spd_show_all (vm, im);
548 ipsec_spd_bindings_show_all (vm, im);
Neale Ranns12989b52019-09-26 16:20:19 +0000549 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
Neale Rannsb294f102019-04-03 13:17:50 +0000550
Fan Zhangf5395782020-04-29 14:00:03 +0100551 vlib_cli_output (vm, "IPSec async mode: %s",
552 (im->async_mode ? "on" : "off"));
553
Ed Warnickecb9cada2015-12-08 15:45:58 -0700554 return 0;
555}
556
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700557/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700558VLIB_CLI_COMMAND (show_ipsec_command, static) = {
Neale Rannsb294f102019-04-03 13:17:50 +0000559 .path = "show ipsec all",
560 .short_help = "show ipsec all",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700561 .function = show_ipsec_command_fn,
562};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700563/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700564
565static clib_error_t *
Neale Rannsb294f102019-04-03 13:17:50 +0000566show_ipsec_sa_command_fn (vlib_main_t * vm,
567 unformat_input_t * input, vlib_cli_command_t * cmd)
568{
569 ipsec_main_t *im = &ipsec_main;
570 u32 sai = ~0;
Neale Ranns670027a2019-08-27 12:47:17 +0000571 u8 detail = 0;
Neale Rannsb294f102019-04-03 13:17:50 +0000572
573 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
574 {
575 if (unformat (input, "%u", &sai))
576 ;
Neale Ranns670027a2019-08-27 12:47:17 +0000577 if (unformat (input, "detail"))
578 detail = 1;
Neale Rannsb294f102019-04-03 13:17:50 +0000579 else
580 break;
581 }
582
583 if (~0 == sai)
Neale Ranns670027a2019-08-27 12:47:17 +0000584 ipsec_sa_show_all (vm, im, detail);
Neale Rannsb294f102019-04-03 13:17:50 +0000585 else
Christian E. Hopps01d61e72019-09-27 14:43:22 -0400586 vlib_cli_output (vm, "%U", format_ipsec_sa, sai,
587 IPSEC_FORMAT_DETAIL | IPSEC_FORMAT_INSECURE);
Neale Rannsb294f102019-04-03 13:17:50 +0000588
589 return 0;
590}
591
Neale Rannsc87b66c2019-02-07 07:26:12 -0800592static clib_error_t *
593clear_ipsec_sa_command_fn (vlib_main_t * vm,
594 unformat_input_t * input, vlib_cli_command_t * cmd)
595{
Neale Rannsc87b66c2019-02-07 07:26:12 -0800596 u32 sai = ~0;
597
598 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
599 {
600 if (unformat (input, "%u", &sai))
601 ;
602 else
603 break;
604 }
605
606 if (~0 == sai)
607 {
608 /* *INDENT-OFF* */
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000609 pool_foreach_index (sai, ipsec_sa_pool)
610 {
611 ipsec_sa_clear (sai);
612 }
Neale Rannsc87b66c2019-02-07 07:26:12 -0800613 /* *INDENT-ON* */
614 }
615 else
616 {
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000617 if (pool_is_free_index (ipsec_sa_pool, sai))
Neale Rannsc87b66c2019-02-07 07:26:12 -0800618 return clib_error_return (0, "unknown SA index: %d", sai);
619 else
620 ipsec_sa_clear (sai);
621 }
622
623 return 0;
624}
625
Neale Rannsb294f102019-04-03 13:17:50 +0000626/* *INDENT-OFF* */
627VLIB_CLI_COMMAND (show_ipsec_sa_command, static) = {
628 .path = "show ipsec sa",
629 .short_help = "show ipsec sa [index]",
630 .function = show_ipsec_sa_command_fn,
631};
Neale Rannsc87b66c2019-02-07 07:26:12 -0800632
633VLIB_CLI_COMMAND (clear_ipsec_sa_command, static) = {
634 .path = "clear ipsec sa",
635 .short_help = "clear ipsec sa [index]",
636 .function = clear_ipsec_sa_command_fn,
637};
Neale Rannsb294f102019-04-03 13:17:50 +0000638/* *INDENT-ON* */
639
640static clib_error_t *
641show_ipsec_spd_command_fn (vlib_main_t * vm,
642 unformat_input_t * input, vlib_cli_command_t * cmd)
643{
644 ipsec_main_t *im = &ipsec_main;
645 u8 show_bindings = 0;
646 u32 spdi = ~0;
647
648 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
649 {
650 if (unformat (input, "%u", &spdi))
651 ;
652 else if (unformat (input, "bindings"))
653 show_bindings = 1;
654 else
655 break;
656 }
657
658 if (show_bindings)
659 ipsec_spd_bindings_show_all (vm, im);
660 else if (~0 != spdi)
661 vlib_cli_output (vm, "%U", format_ipsec_spd, spdi);
662 else
663 ipsec_spd_show_all (vm, im);
664
665 return 0;
666}
667
668/* *INDENT-OFF* */
669VLIB_CLI_COMMAND (show_ipsec_spd_command, static) = {
670 .path = "show ipsec spd",
671 .short_help = "show ipsec spd [index]",
672 .function = show_ipsec_spd_command_fn,
673};
674/* *INDENT-ON* */
675
676static clib_error_t *
677show_ipsec_tunnel_command_fn (vlib_main_t * vm,
678 unformat_input_t * input,
679 vlib_cli_command_t * cmd)
680{
Neale Ranns12989b52019-09-26 16:20:19 +0000681 ipsec_tunnel_show_all (vm);
Neale Rannsb294f102019-04-03 13:17:50 +0000682
683 return 0;
684}
685
686/* *INDENT-OFF* */
687VLIB_CLI_COMMAND (show_ipsec_tunnel_command, static) = {
688 .path = "show ipsec tunnel",
Neale Ranns12989b52019-09-26 16:20:19 +0000689 .short_help = "show ipsec tunnel",
Neale Rannsb294f102019-04-03 13:17:50 +0000690 .function = show_ipsec_tunnel_command_fn,
691};
692/* *INDENT-ON* */
693
694static clib_error_t *
Klement Sekerab4d30532018-11-08 13:00:02 +0100695ipsec_show_backends_command_fn (vlib_main_t * vm,
696 unformat_input_t * input,
697 vlib_cli_command_t * cmd)
698{
699 ipsec_main_t *im = &ipsec_main;
700 u32 verbose = 0;
701
702 (void) unformat (input, "verbose %u", &verbose);
703
704 vlib_cli_output (vm, "IPsec AH backends available:");
705 u8 *s = format (NULL, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
706 ipsec_ah_backend_t *ab;
707 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100708 pool_foreach (ab, im->ah_backends) {
Klement Sekerab4d30532018-11-08 13:00:02 +0100709 s = format (s, "%=25s %=25u %=10s\n", ab->name, ab - im->ah_backends,
710 ab - im->ah_backends == im->ah_current_backend ? "yes" : "no");
711 if (verbose) {
712 vlib_node_t *n;
713 n = vlib_get_node (vm, ab->ah4_encrypt_node_index);
714 s = format (s, " enc4 %s (next %d)\n", n->name, ab->ah4_encrypt_next_index);
715 n = vlib_get_node (vm, ab->ah4_decrypt_node_index);
716 s = format (s, " dec4 %s (next %d)\n", n->name, ab->ah4_decrypt_next_index);
717 n = vlib_get_node (vm, ab->ah6_encrypt_node_index);
718 s = format (s, " enc6 %s (next %d)\n", n->name, ab->ah6_encrypt_next_index);
719 n = vlib_get_node (vm, ab->ah6_decrypt_node_index);
720 s = format (s, " dec6 %s (next %d)\n", n->name, ab->ah6_decrypt_next_index);
721 }
Damjan Marionb2c31b62020-12-13 21:47:40 +0100722 }
Klement Sekerab4d30532018-11-08 13:00:02 +0100723 /* *INDENT-ON* */
724 vlib_cli_output (vm, "%v", s);
Damjan Marion8bea5892022-04-04 22:40:45 +0200725 vec_set_len (s, 0);
Klement Sekerab4d30532018-11-08 13:00:02 +0100726 vlib_cli_output (vm, "IPsec ESP backends available:");
727 s = format (s, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
728 ipsec_esp_backend_t *eb;
729 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100730 pool_foreach (eb, im->esp_backends) {
Klement Sekerab4d30532018-11-08 13:00:02 +0100731 s = format (s, "%=25s %=25u %=10s\n", eb->name, eb - im->esp_backends,
732 eb - im->esp_backends == im->esp_current_backend ? "yes"
733 : "no");
734 if (verbose) {
735 vlib_node_t *n;
736 n = vlib_get_node (vm, eb->esp4_encrypt_node_index);
737 s = format (s, " enc4 %s (next %d)\n", n->name, eb->esp4_encrypt_next_index);
738 n = vlib_get_node (vm, eb->esp4_decrypt_node_index);
739 s = format (s, " dec4 %s (next %d)\n", n->name, eb->esp4_decrypt_next_index);
740 n = vlib_get_node (vm, eb->esp6_encrypt_node_index);
741 s = format (s, " enc6 %s (next %d)\n", n->name, eb->esp6_encrypt_next_index);
742 n = vlib_get_node (vm, eb->esp6_decrypt_node_index);
743 s = format (s, " dec6 %s (next %d)\n", n->name, eb->esp6_decrypt_next_index);
744 }
Damjan Marionb2c31b62020-12-13 21:47:40 +0100745 }
Klement Sekerab4d30532018-11-08 13:00:02 +0100746 /* *INDENT-ON* */
747 vlib_cli_output (vm, "%v", s);
748
749 vec_free (s);
750 return 0;
751}
752
753/* *INDENT-OFF* */
754VLIB_CLI_COMMAND (ipsec_show_backends_command, static) = {
755 .path = "show ipsec backends",
756 .short_help = "show ipsec backends",
757 .function = ipsec_show_backends_command_fn,
758};
759/* *INDENT-ON* */
760
761static clib_error_t *
762ipsec_select_backend_command_fn (vlib_main_t * vm,
763 unformat_input_t * input,
764 vlib_cli_command_t * cmd)
765{
Klement Sekerab4d30532018-11-08 13:00:02 +0100766 unformat_input_t _line_input, *line_input = &_line_input;
Neale Rannse8915fc2019-04-23 20:57:55 -0400767 ipsec_main_t *im = &ipsec_main;
768 clib_error_t *error;
769 u32 backend_index;
770
771 error = ipsec_rsc_in_use (im);
772
773 if (error)
774 return error;
775
Klement Sekerab4d30532018-11-08 13:00:02 +0100776 /* Get a line of input. */
777 if (!unformat_user (input, unformat_line_input, line_input))
778 return 0;
779
780 if (unformat (line_input, "ah"))
781 {
782 if (unformat (line_input, "%u", &backend_index))
783 {
784 if (ipsec_select_ah_backend (im, backend_index) < 0)
785 {
786 return clib_error_return (0, "Invalid AH backend index `%u'",
787 backend_index);
788 }
789 }
790 else
791 {
792 return clib_error_return (0, "Invalid backend index `%U'",
793 format_unformat_error, line_input);
794 }
795 }
796 else if (unformat (line_input, "esp"))
797 {
798 if (unformat (line_input, "%u", &backend_index))
799 {
800 if (ipsec_select_esp_backend (im, backend_index) < 0)
801 {
802 return clib_error_return (0, "Invalid ESP backend index `%u'",
803 backend_index);
804 }
805 }
806 else
807 {
808 return clib_error_return (0, "Invalid backend index `%U'",
809 format_unformat_error, line_input);
810 }
811 }
812 else
813 {
814 return clib_error_return (0, "Unknown input `%U'",
815 format_unformat_error, line_input);
816 }
817
818 return 0;
819}
820
821/* *INDENT-OFF* */
822VLIB_CLI_COMMAND (ipsec_select_backend_command, static) = {
823 .path = "ipsec select backend",
824 .short_help = "ipsec select backend <ah|esp> <backend index>",
825 .function = ipsec_select_backend_command_fn,
826};
827
828/* *INDENT-ON* */
829
830static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700831clear_ipsec_counters_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700832 unformat_input_t * input,
833 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700834{
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800835 vlib_clear_combined_counters (&ipsec_spd_policy_counters);
Neale Rannseba31ec2019-02-17 18:04:27 +0000836 vlib_clear_combined_counters (&ipsec_sa_counters);
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100837 for (int i = 0; i < IPSEC_SA_N_ERRORS; i++)
838 vlib_clear_simple_counters (&ipsec_sa_err_counters[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700839
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800840 return (NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700841}
842
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700843/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700844VLIB_CLI_COMMAND (clear_ipsec_counters_command, static) = {
845 .path = "clear ipsec counters",
846 .short_help = "clear ipsec counters",
847 .function = clear_ipsec_counters_command_fn,
848};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700849/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700850
Neale Rannsc87b66c2019-02-07 07:26:12 -0800851static clib_error_t *
852ipsec_tun_protect_cmd (vlib_main_t * vm,
853 unformat_input_t * input, vlib_cli_command_t * cmd)
854{
855 unformat_input_t _line_input, *line_input = &_line_input;
856 u32 sw_if_index, is_del, sa_in, sa_out, *sa_ins = NULL;
Neale Ranns28287212019-12-16 00:53:11 +0000857 ip_address_t peer = { };
Neale Rannsc87b66c2019-02-07 07:26:12 -0800858 vnet_main_t *vnm;
859
860 is_del = 0;
861 sw_if_index = ~0;
862 vnm = vnet_get_main ();
863
864 if (!unformat_user (input, unformat_line_input, line_input))
865 return 0;
866
867 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
868 {
869 if (unformat (line_input, "del"))
870 is_del = 1;
871 else if (unformat (line_input, "add"))
872 is_del = 0;
873 else if (unformat (line_input, "sa-in %d", &sa_in))
874 vec_add1 (sa_ins, sa_in);
875 else if (unformat (line_input, "sa-out %d", &sa_out))
876 ;
877 else if (unformat (line_input, "%U",
878 unformat_vnet_sw_interface, vnm, &sw_if_index))
879 ;
Neale Ranns28287212019-12-16 00:53:11 +0000880 else if (unformat (line_input, "%U", unformat_ip_address, &peer))
881 ;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800882 else
883 return (clib_error_return (0, "unknown input '%U'",
884 format_unformat_error, line_input));
885 }
886
887 if (!is_del)
Neale Ranns28287212019-12-16 00:53:11 +0000888 ipsec_tun_protect_update (sw_if_index, &peer, sa_out, sa_ins);
Eric Kinzie609d5792020-10-13 20:02:11 -0400889 else
890 ipsec_tun_protect_del (sw_if_index, &peer);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800891
892 unformat_free (line_input);
893 return NULL;
894}
895
896/**
897 * Protect tunnel with IPSEC
898 */
899/* *INDENT-OFF* */
900VLIB_CLI_COMMAND (ipsec_tun_protect_cmd_node, static) =
901{
902 .path = "ipsec tunnel protect",
903 .function = ipsec_tun_protect_cmd,
Eric Kinzie609d5792020-10-13 20:02:11 -0400904 .short_help = "ipsec tunnel protect <interface> input-sa <SA> output-sa <SA> [add|del]",
Neale Rannsc87b66c2019-02-07 07:26:12 -0800905 // this is not MP safe
906};
907/* *INDENT-ON* */
908
Neale Rannsc87b66c2019-02-07 07:26:12 -0800909
910static clib_error_t *
911ipsec_tun_protect_show (vlib_main_t * vm,
912 unformat_input_t * input, vlib_cli_command_t * cmd)
913{
914 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
915
916 return NULL;
917}
918
919/**
920 * show IPSEC tunnel protection
921 */
922/* *INDENT-OFF* */
923VLIB_CLI_COMMAND (ipsec_tun_protect_show_node, static) =
924{
925 .path = "show ipsec protect",
926 .function = ipsec_tun_protect_show,
927 .short_help = "show ipsec protect",
928};
929/* *INDENT-ON* */
930
Neale Ranns7b4e52f2020-05-24 16:17:50 +0000931static int
Neale Ranns302b25a2020-10-19 13:23:33 +0000932ipsec_tun_protect4_hash_show_one (clib_bihash_kv_8_16_t * kv, void *arg)
Neale Ranns7b4e52f2020-05-24 16:17:50 +0000933{
934 ipsec4_tunnel_kv_t *ikv = (ipsec4_tunnel_kv_t *) kv;
935 vlib_main_t *vm = arg;
936
937 vlib_cli_output (vm, " %U", format_ipsec4_tunnel_kv, ikv);
938
939 return (BIHASH_WALK_CONTINUE);
940}
941
942static int
Neale Ranns302b25a2020-10-19 13:23:33 +0000943ipsec_tun_protect6_hash_show_one (clib_bihash_kv_24_16_t * kv, void *arg)
Neale Ranns7b4e52f2020-05-24 16:17:50 +0000944{
945 ipsec6_tunnel_kv_t *ikv = (ipsec6_tunnel_kv_t *) kv;
946 vlib_main_t *vm = arg;
947
948 vlib_cli_output (vm, " %U", format_ipsec6_tunnel_kv, ikv);
949
950 return (BIHASH_WALK_CONTINUE);
951}
952
Neale Ranns41afb332019-07-16 06:19:35 -0700953static clib_error_t *
954ipsec_tun_protect_hash_show (vlib_main_t * vm,
955 unformat_input_t * input,
956 vlib_cli_command_t * cmd)
957{
958 ipsec_main_t *im = &ipsec_main;
959
960 {
Neale Ranns41afb332019-07-16 06:19:35 -0700961 vlib_cli_output (vm, "IPv4:");
962
Neale Ranns302b25a2020-10-19 13:23:33 +0000963 clib_bihash_foreach_key_value_pair_8_16
Neale Ranns7b4e52f2020-05-24 16:17:50 +0000964 (&im->tun4_protect_by_key, ipsec_tun_protect4_hash_show_one, vm);
Neale Ranns41afb332019-07-16 06:19:35 -0700965
966 vlib_cli_output (vm, "IPv6:");
967
Neale Ranns302b25a2020-10-19 13:23:33 +0000968 clib_bihash_foreach_key_value_pair_24_16
Neale Ranns7b4e52f2020-05-24 16:17:50 +0000969 (&im->tun6_protect_by_key, ipsec_tun_protect6_hash_show_one, vm);
Neale Ranns41afb332019-07-16 06:19:35 -0700970 }
971
972 return NULL;
973}
974
975/**
976 * show IPSEC tunnel protection hash tables
977 */
978/* *INDENT-OFF* */
979VLIB_CLI_COMMAND (ipsec_tun_protect_hash_show_node, static) =
980{
981 .path = "show ipsec protect-hash",
982 .function = ipsec_tun_protect_hash_show,
983 .short_help = "show ipsec protect-hash",
984};
985/* *INDENT-ON* */
986
Ed Warnickecb9cada2015-12-08 15:45:58 -0700987clib_error_t *
988ipsec_cli_init (vlib_main_t * vm)
989{
990 return 0;
991}
992
993VLIB_INIT_FUNCTION (ipsec_cli_init);
994
Fan Zhangf5395782020-04-29 14:00:03 +0100995static clib_error_t *
996set_async_mode_command_fn (vlib_main_t * vm, unformat_input_t * input,
997 vlib_cli_command_t * cmd)
998{
999 unformat_input_t _line_input, *line_input = &_line_input;
1000 int async_enable = 0;
1001
1002 if (!unformat_user (input, unformat_line_input, line_input))
1003 return 0;
1004
1005 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1006 {
1007 if (unformat (line_input, "on"))
1008 async_enable = 1;
1009 else if (unformat (line_input, "off"))
1010 async_enable = 0;
1011 else
1012 return (clib_error_return (0, "unknown input '%U'",
1013 format_unformat_error, line_input));
1014 }
1015
Fan Zhangf5395782020-04-29 14:00:03 +01001016 ipsec_set_async_mode (async_enable);
1017
1018 unformat_free (line_input);
1019 return (NULL);
1020}
1021
1022/* *INDENT-OFF* */
1023VLIB_CLI_COMMAND (set_async_mode_command, static) = {
1024 .path = "set ipsec async mode",
1025 .short_help = "set ipsec async mode on|off",
1026 .function = set_async_mode_command_fn,
1027};
1028/* *INDENT-ON* */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001029
1030/*
1031 * fd.io coding-style-patch-verification: ON
1032 *
1033 * Local Variables:
1034 * eval: (c-set-style "gnu")
1035 * End:
1036 */