blob: 5aef630a33f14d61946d0514ccae7928abf844c4 [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;
Maxime Peim0e2f1882022-12-22 11:26:57 +000091 u32 anti_replay_window_size;
Neale Ranns8d7c5022019-02-06 01:41:05 -080092 ipsec_protocol_t proto;
93 ipsec_sa_flags_t flags;
94 clib_error_t *error;
Kingwel Xied3d12052019-03-07 06:34:30 -050095 ipsec_key_t ck = { 0 };
96 ipsec_key_t ik = { 0 };
Neale Rannsabc56602020-04-01 09:45:23 +000097 u32 id, spi, salt, sai;
Mohammed Hawari048189e2021-02-05 19:04:42 +010098 int i = 0;
Neale Rannsabc56602020-04-01 09:45:23 +000099 u16 udp_src, udp_dst;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800100 int is_add, rv;
Benoît Gannebdd8b572020-07-28 15:56:15 +0200101 u32 m_args = 0;
Neale Rannsf16e9a52021-02-25 19:09:24 +0000102 tunnel_t tun = {};
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103
Neale Ranns8dc75c02019-12-10 01:08:19 +0000104 salt = 0;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800105 error = NULL;
106 is_add = 0;
107 flags = IPSEC_SA_FLAG_NONE;
108 proto = IPSEC_PROTOCOL_ESP;
Maxime Peim0e2f1882022-12-22 11:26:57 +0000109 anti_replay_window_size = 0;
Neale Rannse6be7022019-06-04 15:37:34 +0000110 integ_alg = IPSEC_INTEG_ALG_NONE;
111 crypto_alg = IPSEC_CRYPTO_ALG_NONE;
Neale Rannsabc56602020-04-01 09:45:23 +0000112 udp_src = udp_dst = IPSEC_UDP_PORT_NONE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700114 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115 return 0;
116
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700117 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
118 {
Neale Ranns8d7c5022019-02-06 01:41:05 -0800119 if (unformat (line_input, "add %u", &id))
Benoît Gannebdd8b572020-07-28 15:56:15 +0200120 {
121 is_add = 1;
122 m_args |= 1 << 0;
123 }
Neale Ranns8d7c5022019-02-06 01:41:05 -0800124 else if (unformat (line_input, "del %u", &id))
Benoît Gannebdd8b572020-07-28 15:56:15 +0200125 {
126 is_add = 0;
127 m_args |= 1 << 0;
128 }
Neale Ranns8d7c5022019-02-06 01:41:05 -0800129 else if (unformat (line_input, "spi %u", &spi))
Benoît Gannebdd8b572020-07-28 15:56:15 +0200130 m_args |= 1 << 1;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800131 else if (unformat (line_input, "salt 0x%x", &salt))
Neale Ranns80f6fd52019-04-16 02:41:34 +0000132 ;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700133 else if (unformat (line_input, "esp"))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800134 proto = IPSEC_PROTOCOL_ESP;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700135 else if (unformat (line_input, "ah"))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800136 proto = IPSEC_PROTOCOL_AH;
137 else if (unformat (line_input, "crypto-key %U",
138 unformat_ipsec_key, &ck))
139 ;
140 else if (unformat (line_input, "crypto-alg %U",
141 unformat_ipsec_crypto_alg, &crypto_alg))
142 ;
143 else if (unformat (line_input, "integ-key %U", unformat_ipsec_key, &ik))
144 ;
145 else if (unformat (line_input, "integ-alg %U",
146 unformat_ipsec_integ_alg, &integ_alg))
147 ;
Damjan Marionc50bcbd2022-05-08 20:48:37 +0200148 else if (unformat (line_input, "%U", unformat_tunnel, &tun))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700149 {
Neale Ranns8d7c5022019-02-06 01:41:05 -0800150 flags |= IPSEC_SA_FLAG_IS_TUNNEL;
Neale Ranns9ec846c2021-02-09 14:04:02 +0000151 if (AF_IP6 == tunnel_get_af (&tun))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800152 flags |= IPSEC_SA_FLAG_IS_TUNNEL_V6;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700153 }
Mohammed Hawari048189e2021-02-05 19:04:42 +0100154 else if (unformat (line_input, "udp-src-port %d", &i))
155 udp_src = i;
156 else if (unformat (line_input, "udp-dst-port %d", &i))
157 udp_dst = i;
Maxime Peim0e2f1882022-12-22 11:26:57 +0000158 else if (unformat (line_input, "anti-replay-size %d",
159 &anti_replay_window_size))
160 flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
Christian Hopps99975382020-07-17 09:53:18 -0400161 else if (unformat (line_input, "inbound"))
162 flags |= IPSEC_SA_FLAG_IS_INBOUND;
163 else if (unformat (line_input, "use-anti-replay"))
164 flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
165 else if (unformat (line_input, "use-esn"))
166 flags |= IPSEC_SA_FLAG_USE_ESN;
Radu Nicolau717de092018-08-03 10:37:24 +0100167 else if (unformat (line_input, "udp-encap"))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800168 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
Neale Rannsf16e9a52021-02-25 19:09:24 +0000169 else if (unformat (line_input, "async"))
170 flags |= IPSEC_SA_FLAG_IS_ASYNC;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700171 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500172 {
173 error = clib_error_return (0, "parse error: '%U'",
174 format_unformat_error, line_input);
175 goto done;
176 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700177 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178
Benoît Gannebdd8b572020-07-28 15:56:15 +0200179 if (!(m_args & 1))
180 {
181 error = clib_error_return (0, "missing id");
182 goto done;
183 }
184
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000185 if (is_add)
Benoît Gannebdd8b572020-07-28 15:56:15 +0200186 {
187 if (!(m_args & 2))
188 {
189 error = clib_error_return (0, "missing spi");
190 goto done;
191 }
Maxime Peim0e2f1882022-12-22 11:26:57 +0000192 rv =
193 ipsec_sa_add_and_lock (id, spi, proto, crypto_alg, &ck, integ_alg, &ik,
194 flags, clib_host_to_net_u32 (salt), udp_src,
195 udp_dst, anti_replay_window_size, &tun, &sai);
Benoît Gannebdd8b572020-07-28 15:56:15 +0200196 }
Neale Ranns8d7c5022019-02-06 01:41:05 -0800197 else
Benoît Gannebdd8b572020-07-28 15:56:15 +0200198 {
199 rv = ipsec_sa_unlock_id (id);
200 }
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000201
Neale Ranns8d7c5022019-02-06 01:41:05 -0800202 if (rv)
Neale Rannsf16e9a52021-02-25 19:09:24 +0000203 error = clib_error_return (0, "failed: %d", rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204
Billy McFalla9a20e72017-02-15 11:39:12 -0500205done:
206 unformat_free (line_input);
207
208 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700209}
210
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700211/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212VLIB_CLI_COMMAND (ipsec_sa_add_del_command, static) = {
213 .path = "ipsec sa",
214 .short_help =
215 "ipsec sa [add|del]",
216 .function = ipsec_sa_add_del_command_fn,
217};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700218/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700219
220static clib_error_t *
Maxime Peim1271e3a2023-03-20 14:13:56 +0000221ipsec_sa_bind_cli (vlib_main_t *vm, unformat_input_t *input,
222 vlib_cli_command_t *cmd)
223{
224 unformat_input_t _line_input, *line_input = &_line_input;
225 u32 id = ~0;
226 u32 worker = ~0;
227 bool bind = 1;
228 int rv;
229 clib_error_t *error = NULL;
230
231 if (!unformat_user (input, unformat_line_input, line_input))
232 return 0;
233
234 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
235 {
236 if (unformat (line_input, "unbind"))
237 bind = 0;
Maxime Peimf902ba52023-07-03 09:42:58 +0200238 else if (id == ~0 && unformat (line_input, "%u", &id))
Maxime Peim1271e3a2023-03-20 14:13:56 +0000239 ;
240 else if (unformat (line_input, "%u", &worker))
241 ;
242 else
243 {
244 error = clib_error_return (0, "parse error: '%U'",
245 format_unformat_error, line_input);
246 goto done;
247 }
248 }
249
250 if (id == ~0)
251 {
252 error = clib_error_return (0, "please specify SA ID");
253 goto done;
254 }
255
256 if (bind && ~0 == worker)
257 {
258 error = clib_error_return (0, "please specify worker to bind to");
259 goto done;
260 }
261
262 rv = ipsec_sa_bind (id, worker, bind);
263 switch (rv)
264 {
265 case VNET_API_ERROR_INVALID_VALUE:
266 error = clib_error_return (0, "please specify a valid SA ID");
267 break;
268 case VNET_API_ERROR_INVALID_WORKER:
269 error = clib_error_return (0, "please specify a valid worker index");
270 break;
271 }
272
273done:
274 unformat_free (line_input);
275
276 return error;
277}
278
279VLIB_CLI_COMMAND (ipsec_sa_bind_cmd, static) = {
280 .path = "ipsec sa bind",
281 .short_help = "ipsec sa [unbind] <sa-id> <worker>",
282 .function = ipsec_sa_bind_cli,
283};
284
285static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700286ipsec_spd_add_del_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700287 unformat_input_t * input,
288 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700289{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700290 unformat_input_t _line_input, *line_input = &_line_input;
Damjan Marion3f54b182016-08-16 11:27:02 +0200291 u32 spd_id = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700292 int is_add = ~0;
Billy McFalla9a20e72017-02-15 11:39:12 -0500293 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700294
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700295 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700296 return 0;
297
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700298 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
299 {
300 if (unformat (line_input, "add"))
301 is_add = 1;
302 else if (unformat (line_input, "del"))
303 is_add = 0;
304 else if (unformat (line_input, "%u", &spd_id))
305 ;
306 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500307 {
308 error = clib_error_return (0, "parse error: '%U'",
309 format_unformat_error, line_input);
310 goto done;
311 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700312 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700313
Damjan Marion3f54b182016-08-16 11:27:02 +0200314 if (spd_id == ~0)
Billy McFalla9a20e72017-02-15 11:39:12 -0500315 {
316 error = clib_error_return (0, "please specify SPD ID");
317 goto done;
318 }
Damjan Marion3f54b182016-08-16 11:27:02 +0200319
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700320 ipsec_add_del_spd (vm, spd_id, is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700321
Billy McFalla9a20e72017-02-15 11:39:12 -0500322done:
323 unformat_free (line_input);
324
325 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700326}
327
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700328/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700329VLIB_CLI_COMMAND (ipsec_spd_add_del_command, static) = {
330 .path = "ipsec spd",
331 .short_help =
332 "ipsec spd [add|del] <id>",
333 .function = ipsec_spd_add_del_command_fn,
334};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700335/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700336
337
338static clib_error_t *
339ipsec_policy_add_del_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700340 unformat_input_t * input,
341 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700342{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700343 unformat_input_t _line_input, *line_input = &_line_input;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700344 ipsec_policy_t p;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800345 int rv, is_add = 0;
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000346 u32 tmp, tmp2, stat_index, local_range_set, remote_range_set;
Billy McFalla9a20e72017-02-15 11:39:12 -0500347 clib_error_t *error = NULL;
Neale Ranns9f231d42019-03-19 10:06:00 +0000348 u32 is_outbound;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700349
Dave Barachb7b92992018-10-17 10:38:51 -0400350 clib_memset (&p, 0, sizeof (p));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700351 p.lport.stop = p.rport.stop = ~0;
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000352 remote_range_set = local_range_set = is_outbound = 0;
Piotr Bronowski815c6a42022-06-09 09:09:28 +0000353 p.protocol = IPSEC_POLICY_PROTOCOL_ANY;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700354
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700355 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700356 return 0;
357
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700358 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
359 {
360 if (unformat (line_input, "add"))
361 is_add = 1;
362 else if (unformat (line_input, "del"))
363 is_add = 0;
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000364 else if (unformat (line_input, "ip6"))
365 p.is_ipv6 = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700366 else if (unformat (line_input, "spd %u", &p.id))
367 ;
368 else if (unformat (line_input, "inbound"))
Neale Ranns9f231d42019-03-19 10:06:00 +0000369 is_outbound = 0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700370 else if (unformat (line_input, "outbound"))
Neale Ranns9f231d42019-03-19 10:06:00 +0000371 is_outbound = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700372 else if (unformat (line_input, "priority %d", &p.priority))
373 ;
374 else if (unformat (line_input, "protocol %u", &tmp))
375 p.protocol = (u8) tmp;
376 else
377 if (unformat
378 (line_input, "action %U", unformat_ipsec_policy_action,
379 &p.policy))
380 {
381 if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
Billy McFalla9a20e72017-02-15 11:39:12 -0500382 {
383 error = clib_error_return (0, "unsupported action: 'resolve'");
384 goto done;
385 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700386 }
387 else if (unformat (line_input, "sa %u", &p.sa_id))
388 ;
389 else if (unformat (line_input, "local-ip-range %U - %U",
390 unformat_ip4_address, &p.laddr.start.ip4,
391 unformat_ip4_address, &p.laddr.stop.ip4))
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000392 local_range_set = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700393 else if (unformat (line_input, "remote-ip-range %U - %U",
394 unformat_ip4_address, &p.raddr.start.ip4,
395 unformat_ip4_address, &p.raddr.stop.ip4))
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000396 remote_range_set = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700397 else if (unformat (line_input, "local-ip-range %U - %U",
398 unformat_ip6_address, &p.laddr.start.ip6,
399 unformat_ip6_address, &p.laddr.stop.ip6))
400 {
401 p.is_ipv6 = 1;
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000402 local_range_set = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700403 }
404 else if (unformat (line_input, "remote-ip-range %U - %U",
405 unformat_ip6_address, &p.raddr.start.ip6,
406 unformat_ip6_address, &p.raddr.stop.ip6))
407 {
408 p.is_ipv6 = 1;
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000409 remote_range_set = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700410 }
411 else if (unformat (line_input, "local-port-range %u - %u", &tmp, &tmp2))
412 {
413 p.lport.start = tmp;
414 p.lport.stop = tmp2;
415 }
416 else
417 if (unformat (line_input, "remote-port-range %u - %u", &tmp, &tmp2))
418 {
419 p.rport.start = tmp;
420 p.rport.stop = tmp2;
421 }
422 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500423 {
424 error = clib_error_return (0, "parse error: '%U'",
425 format_unformat_error, line_input);
426 goto done;
427 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700428 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700429
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000430 if (!remote_range_set)
431 {
432 if (p.is_ipv6)
433 clib_memset (&p.raddr.stop.ip6, 0xff, 16);
434 else
435 clib_memset (&p.raddr.stop.ip4, 0xff, 4);
436 }
437 if (!local_range_set)
438 {
439 if (p.is_ipv6)
440 clib_memset (&p.laddr.stop.ip6, 0xff, 16);
441 else
442 clib_memset (&p.laddr.stop.ip4, 0xff, 4);
443 }
444
Neale Ranns9f231d42019-03-19 10:06:00 +0000445 rv = ipsec_policy_mk_type (is_outbound, p.is_ipv6, p.policy, &p.type);
446
447 if (rv)
448 {
449 error = clib_error_return (0, "unsupported policy type for:",
450 " outboud:%s %s action:%U",
451 (is_outbound ? "yes" : "no"),
452 (p.is_ipv6 ? "IPv4" : "IPv6"),
453 format_ipsec_policy_action, p.policy);
454 goto done;
455 }
456
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800457 rv = ipsec_add_del_policy (vm, &p, is_add, &stat_index);
458
459 if (!rv)
460 vlib_cli_output (vm, "policy-index:%d", stat_index);
461 else
462 vlib_cli_output (vm, "error:%d", rv);
Billy McFalla9a20e72017-02-15 11:39:12 -0500463
464done:
465 unformat_free (line_input);
466
467 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700468}
469
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700470/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700471VLIB_CLI_COMMAND (ipsec_policy_add_del_command, static) = {
472 .path = "ipsec policy",
473 .short_help =
474 "ipsec policy [add|del] spd <id> priority <n> ",
475 .function = ipsec_policy_add_del_command_fn,
476};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700477/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700478
Neale Rannsb294f102019-04-03 13:17:50 +0000479static void
Neale Ranns670027a2019-08-27 12:47:17 +0000480ipsec_sa_show_all (vlib_main_t * vm, ipsec_main_t * im, u8 detail)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700481{
Neale Rannsb294f102019-04-03 13:17:50 +0000482 u32 sai;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700483
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700484 /* *INDENT-OFF* */
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000485 pool_foreach_index (sai, ipsec_sa_pool)
486 {
487 vlib_cli_output (vm, "%U", format_ipsec_sa, sai,
488 (detail ? IPSEC_FORMAT_DETAIL : IPSEC_FORMAT_BRIEF));
489 }
Neale Rannsb294f102019-04-03 13:17:50 +0000490 /* *INDENT-ON* */
491}
492
493static void
494ipsec_spd_show_all (vlib_main_t * vm, ipsec_main_t * im)
495{
496 u32 spdi;
497
498 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100499 pool_foreach_index (spdi, im->spds) {
Neale Rannsb294f102019-04-03 13:17:50 +0000500 vlib_cli_output(vm, "%U", format_ipsec_spd, spdi);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100501 }
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +0000502
Zachary Leaf7cd35f52021-06-25 08:11:15 -0500503 if (im->output_flow_cache_flag)
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +0000504 {
Zachary Leaf7cd35f52021-06-25 08:11:15 -0500505 vlib_cli_output (vm, "%U", format_ipsec_out_spd_flow_cache);
506 }
507 if (im->input_flow_cache_flag)
508 {
509 vlib_cli_output (vm, "%U", format_ipsec_in_spd_flow_cache);
Govindarajan Mohandoss6d7dfcb2021-03-19 19:20:49 +0000510 }
Neale Rannsb294f102019-04-03 13:17:50 +0000511 /* *INDENT-ON* */
512}
513
514static void
515ipsec_spd_bindings_show_all (vlib_main_t * vm, ipsec_main_t * im)
516{
517 u32 spd_id, sw_if_index;
Neale Rannsd83c4a82019-06-14 06:48:27 -0700518 ipsec_spd_t *spd;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700519
Neale Ranns311124e2019-01-24 04:52:25 -0800520 vlib_cli_output (vm, "SPD Bindings:");
Neale Ranns8d7c5022019-02-06 01:41:05 -0800521
Neale Rannsb294f102019-04-03 13:17:50 +0000522 /* *INDENT-OFF* */
Neale Ranns311124e2019-01-24 04:52:25 -0800523 hash_foreach(sw_if_index, spd_id, im->spd_index_by_sw_if_index, ({
Neale Rannsd83c4a82019-06-14 06:48:27 -0700524 spd = pool_elt_at_index (im->spds, spd_id);
525 vlib_cli_output (vm, " %d -> %U", spd->id,
Neale Ranns8d7c5022019-02-06 01:41:05 -0800526 format_vnet_sw_if_index_name, im->vnet_main,
527 sw_if_index);
Neale Ranns311124e2019-01-24 04:52:25 -0800528 }));
529 /* *INDENT-ON* */
Neale Rannsb294f102019-04-03 13:17:50 +0000530}
Neale Ranns311124e2019-01-24 04:52:25 -0800531
Neale Ranns12989b52019-09-26 16:20:19 +0000532static walk_rc_t
533ipsec_tun_protect_show_one (index_t itpi, void *ctx)
Neale Rannsb294f102019-04-03 13:17:50 +0000534{
Neale Ranns28287212019-12-16 00:53:11 +0000535 vlib_cli_output (ctx, "%U", format_ipsec_tun_protect_index, itpi);
Neale Rannsb294f102019-04-03 13:17:50 +0000536
Neale Ranns12989b52019-09-26 16:20:19 +0000537 return (WALK_CONTINUE);
538}
539
540static void
541ipsec_tunnel_show_all (vlib_main_t * vm)
542{
543 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
Neale Rannsb294f102019-04-03 13:17:50 +0000544}
545
546static clib_error_t *
547show_ipsec_command_fn (vlib_main_t * vm,
548 unformat_input_t * input, vlib_cli_command_t * cmd)
549{
550 ipsec_main_t *im = &ipsec_main;
551
Neale Ranns670027a2019-08-27 12:47:17 +0000552 ipsec_sa_show_all (vm, im, 0);
Neale Rannsb294f102019-04-03 13:17:50 +0000553 ipsec_spd_show_all (vm, im);
554 ipsec_spd_bindings_show_all (vm, im);
Neale Ranns12989b52019-09-26 16:20:19 +0000555 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
Neale Rannsb294f102019-04-03 13:17:50 +0000556
Fan Zhangf5395782020-04-29 14:00:03 +0100557 vlib_cli_output (vm, "IPSec async mode: %s",
558 (im->async_mode ? "on" : "off"));
559
Ed Warnickecb9cada2015-12-08 15:45:58 -0700560 return 0;
561}
562
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700563/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700564VLIB_CLI_COMMAND (show_ipsec_command, static) = {
Neale Rannsb294f102019-04-03 13:17:50 +0000565 .path = "show ipsec all",
566 .short_help = "show ipsec all",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700567 .function = show_ipsec_command_fn,
568};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700569/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700570
571static clib_error_t *
Neale Rannsb294f102019-04-03 13:17:50 +0000572show_ipsec_sa_command_fn (vlib_main_t * vm,
573 unformat_input_t * input, vlib_cli_command_t * cmd)
574{
575 ipsec_main_t *im = &ipsec_main;
576 u32 sai = ~0;
Neale Ranns670027a2019-08-27 12:47:17 +0000577 u8 detail = 0;
Neale Rannsb294f102019-04-03 13:17:50 +0000578
579 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
580 {
581 if (unformat (input, "%u", &sai))
582 ;
Neale Ranns670027a2019-08-27 12:47:17 +0000583 if (unformat (input, "detail"))
584 detail = 1;
Neale Rannsb294f102019-04-03 13:17:50 +0000585 else
586 break;
587 }
588
589 if (~0 == sai)
Neale Ranns670027a2019-08-27 12:47:17 +0000590 ipsec_sa_show_all (vm, im, detail);
Neale Rannsb294f102019-04-03 13:17:50 +0000591 else
Christian E. Hopps01d61e72019-09-27 14:43:22 -0400592 vlib_cli_output (vm, "%U", format_ipsec_sa, sai,
593 IPSEC_FORMAT_DETAIL | IPSEC_FORMAT_INSECURE);
Neale Rannsb294f102019-04-03 13:17:50 +0000594
595 return 0;
596}
597
Neale Rannsc87b66c2019-02-07 07:26:12 -0800598static clib_error_t *
599clear_ipsec_sa_command_fn (vlib_main_t * vm,
600 unformat_input_t * input, vlib_cli_command_t * cmd)
601{
Neale Rannsc87b66c2019-02-07 07:26:12 -0800602 u32 sai = ~0;
603
604 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
605 {
606 if (unformat (input, "%u", &sai))
607 ;
608 else
609 break;
610 }
611
612 if (~0 == sai)
613 {
614 /* *INDENT-OFF* */
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000615 pool_foreach_index (sai, ipsec_sa_pool)
616 {
617 ipsec_sa_clear (sai);
618 }
Neale Rannsc87b66c2019-02-07 07:26:12 -0800619 /* *INDENT-ON* */
620 }
621 else
622 {
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000623 if (pool_is_free_index (ipsec_sa_pool, sai))
Neale Rannsc87b66c2019-02-07 07:26:12 -0800624 return clib_error_return (0, "unknown SA index: %d", sai);
625 else
626 ipsec_sa_clear (sai);
627 }
628
629 return 0;
630}
631
Neale Rannsb294f102019-04-03 13:17:50 +0000632/* *INDENT-OFF* */
633VLIB_CLI_COMMAND (show_ipsec_sa_command, static) = {
634 .path = "show ipsec sa",
635 .short_help = "show ipsec sa [index]",
636 .function = show_ipsec_sa_command_fn,
637};
Neale Rannsc87b66c2019-02-07 07:26:12 -0800638
639VLIB_CLI_COMMAND (clear_ipsec_sa_command, static) = {
640 .path = "clear ipsec sa",
641 .short_help = "clear ipsec sa [index]",
642 .function = clear_ipsec_sa_command_fn,
643};
Neale Rannsb294f102019-04-03 13:17:50 +0000644/* *INDENT-ON* */
645
646static clib_error_t *
647show_ipsec_spd_command_fn (vlib_main_t * vm,
648 unformat_input_t * input, vlib_cli_command_t * cmd)
649{
650 ipsec_main_t *im = &ipsec_main;
651 u8 show_bindings = 0;
652 u32 spdi = ~0;
653
654 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
655 {
656 if (unformat (input, "%u", &spdi))
657 ;
658 else if (unformat (input, "bindings"))
659 show_bindings = 1;
660 else
661 break;
662 }
663
664 if (show_bindings)
665 ipsec_spd_bindings_show_all (vm, im);
666 else if (~0 != spdi)
667 vlib_cli_output (vm, "%U", format_ipsec_spd, spdi);
668 else
669 ipsec_spd_show_all (vm, im);
670
671 return 0;
672}
673
674/* *INDENT-OFF* */
675VLIB_CLI_COMMAND (show_ipsec_spd_command, static) = {
676 .path = "show ipsec spd",
677 .short_help = "show ipsec spd [index]",
678 .function = show_ipsec_spd_command_fn,
679};
680/* *INDENT-ON* */
681
682static clib_error_t *
683show_ipsec_tunnel_command_fn (vlib_main_t * vm,
684 unformat_input_t * input,
685 vlib_cli_command_t * cmd)
686{
Neale Ranns12989b52019-09-26 16:20:19 +0000687 ipsec_tunnel_show_all (vm);
Neale Rannsb294f102019-04-03 13:17:50 +0000688
689 return 0;
690}
691
692/* *INDENT-OFF* */
693VLIB_CLI_COMMAND (show_ipsec_tunnel_command, static) = {
694 .path = "show ipsec tunnel",
Neale Ranns12989b52019-09-26 16:20:19 +0000695 .short_help = "show ipsec tunnel",
Neale Rannsb294f102019-04-03 13:17:50 +0000696 .function = show_ipsec_tunnel_command_fn,
697};
698/* *INDENT-ON* */
699
700static clib_error_t *
Klement Sekerab4d30532018-11-08 13:00:02 +0100701ipsec_show_backends_command_fn (vlib_main_t * vm,
702 unformat_input_t * input,
703 vlib_cli_command_t * cmd)
704{
705 ipsec_main_t *im = &ipsec_main;
706 u32 verbose = 0;
707
708 (void) unformat (input, "verbose %u", &verbose);
709
710 vlib_cli_output (vm, "IPsec AH backends available:");
711 u8 *s = format (NULL, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
712 ipsec_ah_backend_t *ab;
713 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100714 pool_foreach (ab, im->ah_backends) {
Klement Sekerab4d30532018-11-08 13:00:02 +0100715 s = format (s, "%=25s %=25u %=10s\n", ab->name, ab - im->ah_backends,
716 ab - im->ah_backends == im->ah_current_backend ? "yes" : "no");
717 if (verbose) {
718 vlib_node_t *n;
719 n = vlib_get_node (vm, ab->ah4_encrypt_node_index);
720 s = format (s, " enc4 %s (next %d)\n", n->name, ab->ah4_encrypt_next_index);
721 n = vlib_get_node (vm, ab->ah4_decrypt_node_index);
722 s = format (s, " dec4 %s (next %d)\n", n->name, ab->ah4_decrypt_next_index);
723 n = vlib_get_node (vm, ab->ah6_encrypt_node_index);
724 s = format (s, " enc6 %s (next %d)\n", n->name, ab->ah6_encrypt_next_index);
725 n = vlib_get_node (vm, ab->ah6_decrypt_node_index);
726 s = format (s, " dec6 %s (next %d)\n", n->name, ab->ah6_decrypt_next_index);
727 }
Damjan Marionb2c31b62020-12-13 21:47:40 +0100728 }
Klement Sekerab4d30532018-11-08 13:00:02 +0100729 /* *INDENT-ON* */
730 vlib_cli_output (vm, "%v", s);
Damjan Marion8bea5892022-04-04 22:40:45 +0200731 vec_set_len (s, 0);
Klement Sekerab4d30532018-11-08 13:00:02 +0100732 vlib_cli_output (vm, "IPsec ESP backends available:");
733 s = format (s, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
734 ipsec_esp_backend_t *eb;
735 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100736 pool_foreach (eb, im->esp_backends) {
Klement Sekerab4d30532018-11-08 13:00:02 +0100737 s = format (s, "%=25s %=25u %=10s\n", eb->name, eb - im->esp_backends,
738 eb - im->esp_backends == im->esp_current_backend ? "yes"
739 : "no");
740 if (verbose) {
741 vlib_node_t *n;
742 n = vlib_get_node (vm, eb->esp4_encrypt_node_index);
743 s = format (s, " enc4 %s (next %d)\n", n->name, eb->esp4_encrypt_next_index);
744 n = vlib_get_node (vm, eb->esp4_decrypt_node_index);
745 s = format (s, " dec4 %s (next %d)\n", n->name, eb->esp4_decrypt_next_index);
746 n = vlib_get_node (vm, eb->esp6_encrypt_node_index);
747 s = format (s, " enc6 %s (next %d)\n", n->name, eb->esp6_encrypt_next_index);
748 n = vlib_get_node (vm, eb->esp6_decrypt_node_index);
749 s = format (s, " dec6 %s (next %d)\n", n->name, eb->esp6_decrypt_next_index);
750 }
Damjan Marionb2c31b62020-12-13 21:47:40 +0100751 }
Klement Sekerab4d30532018-11-08 13:00:02 +0100752 /* *INDENT-ON* */
753 vlib_cli_output (vm, "%v", s);
754
755 vec_free (s);
756 return 0;
757}
758
759/* *INDENT-OFF* */
760VLIB_CLI_COMMAND (ipsec_show_backends_command, static) = {
761 .path = "show ipsec backends",
762 .short_help = "show ipsec backends",
763 .function = ipsec_show_backends_command_fn,
764};
765/* *INDENT-ON* */
766
767static clib_error_t *
768ipsec_select_backend_command_fn (vlib_main_t * vm,
769 unformat_input_t * input,
770 vlib_cli_command_t * cmd)
771{
Klement Sekerab4d30532018-11-08 13:00:02 +0100772 unformat_input_t _line_input, *line_input = &_line_input;
Neale Rannse8915fc2019-04-23 20:57:55 -0400773 ipsec_main_t *im = &ipsec_main;
774 clib_error_t *error;
775 u32 backend_index;
776
777 error = ipsec_rsc_in_use (im);
778
779 if (error)
780 return error;
781
Klement Sekerab4d30532018-11-08 13:00:02 +0100782 /* Get a line of input. */
783 if (!unformat_user (input, unformat_line_input, line_input))
784 return 0;
785
786 if (unformat (line_input, "ah"))
787 {
788 if (unformat (line_input, "%u", &backend_index))
789 {
790 if (ipsec_select_ah_backend (im, backend_index) < 0)
791 {
792 return clib_error_return (0, "Invalid AH backend index `%u'",
793 backend_index);
794 }
795 }
796 else
797 {
798 return clib_error_return (0, "Invalid backend index `%U'",
799 format_unformat_error, line_input);
800 }
801 }
802 else if (unformat (line_input, "esp"))
803 {
804 if (unformat (line_input, "%u", &backend_index))
805 {
806 if (ipsec_select_esp_backend (im, backend_index) < 0)
807 {
808 return clib_error_return (0, "Invalid ESP backend index `%u'",
809 backend_index);
810 }
811 }
812 else
813 {
814 return clib_error_return (0, "Invalid backend index `%U'",
815 format_unformat_error, line_input);
816 }
817 }
818 else
819 {
820 return clib_error_return (0, "Unknown input `%U'",
821 format_unformat_error, line_input);
822 }
823
824 return 0;
825}
826
827/* *INDENT-OFF* */
828VLIB_CLI_COMMAND (ipsec_select_backend_command, static) = {
829 .path = "ipsec select backend",
830 .short_help = "ipsec select backend <ah|esp> <backend index>",
831 .function = ipsec_select_backend_command_fn,
832};
833
834/* *INDENT-ON* */
835
836static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700837clear_ipsec_counters_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700838 unformat_input_t * input,
839 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700840{
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800841 vlib_clear_combined_counters (&ipsec_spd_policy_counters);
Neale Rannseba31ec2019-02-17 18:04:27 +0000842 vlib_clear_combined_counters (&ipsec_sa_counters);
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100843 for (int i = 0; i < IPSEC_SA_N_ERRORS; i++)
844 vlib_clear_simple_counters (&ipsec_sa_err_counters[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700845
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800846 return (NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700847}
848
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700849/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700850VLIB_CLI_COMMAND (clear_ipsec_counters_command, static) = {
851 .path = "clear ipsec counters",
852 .short_help = "clear ipsec counters",
853 .function = clear_ipsec_counters_command_fn,
854};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700855/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700856
Neale Rannsc87b66c2019-02-07 07:26:12 -0800857static clib_error_t *
858ipsec_tun_protect_cmd (vlib_main_t * vm,
859 unformat_input_t * input, vlib_cli_command_t * cmd)
860{
861 unformat_input_t _line_input, *line_input = &_line_input;
862 u32 sw_if_index, is_del, sa_in, sa_out, *sa_ins = NULL;
Neale Ranns28287212019-12-16 00:53:11 +0000863 ip_address_t peer = { };
Neale Rannsc87b66c2019-02-07 07:26:12 -0800864 vnet_main_t *vnm;
865
866 is_del = 0;
867 sw_if_index = ~0;
868 vnm = vnet_get_main ();
869
870 if (!unformat_user (input, unformat_line_input, line_input))
871 return 0;
872
873 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
874 {
875 if (unformat (line_input, "del"))
876 is_del = 1;
877 else if (unformat (line_input, "add"))
878 is_del = 0;
879 else if (unformat (line_input, "sa-in %d", &sa_in))
880 vec_add1 (sa_ins, sa_in);
881 else if (unformat (line_input, "sa-out %d", &sa_out))
882 ;
883 else if (unformat (line_input, "%U",
884 unformat_vnet_sw_interface, vnm, &sw_if_index))
885 ;
Neale Ranns28287212019-12-16 00:53:11 +0000886 else if (unformat (line_input, "%U", unformat_ip_address, &peer))
887 ;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800888 else
889 return (clib_error_return (0, "unknown input '%U'",
890 format_unformat_error, line_input));
891 }
892
893 if (!is_del)
Neale Ranns28287212019-12-16 00:53:11 +0000894 ipsec_tun_protect_update (sw_if_index, &peer, sa_out, sa_ins);
Eric Kinzie609d5792020-10-13 20:02:11 -0400895 else
896 ipsec_tun_protect_del (sw_if_index, &peer);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800897
898 unformat_free (line_input);
899 return NULL;
900}
901
902/**
903 * Protect tunnel with IPSEC
904 */
905/* *INDENT-OFF* */
906VLIB_CLI_COMMAND (ipsec_tun_protect_cmd_node, static) =
907{
908 .path = "ipsec tunnel protect",
909 .function = ipsec_tun_protect_cmd,
Eric Kinzie609d5792020-10-13 20:02:11 -0400910 .short_help = "ipsec tunnel protect <interface> input-sa <SA> output-sa <SA> [add|del]",
Neale Rannsc87b66c2019-02-07 07:26:12 -0800911 // this is not MP safe
912};
913/* *INDENT-ON* */
914
Neale Rannsc87b66c2019-02-07 07:26:12 -0800915
916static clib_error_t *
917ipsec_tun_protect_show (vlib_main_t * vm,
918 unformat_input_t * input, vlib_cli_command_t * cmd)
919{
920 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
921
922 return NULL;
923}
924
925/**
926 * show IPSEC tunnel protection
927 */
928/* *INDENT-OFF* */
929VLIB_CLI_COMMAND (ipsec_tun_protect_show_node, static) =
930{
931 .path = "show ipsec protect",
932 .function = ipsec_tun_protect_show,
933 .short_help = "show ipsec protect",
934};
935/* *INDENT-ON* */
936
Neale Ranns7b4e52f2020-05-24 16:17:50 +0000937static int
Neale Ranns302b25a2020-10-19 13:23:33 +0000938ipsec_tun_protect4_hash_show_one (clib_bihash_kv_8_16_t * kv, void *arg)
Neale Ranns7b4e52f2020-05-24 16:17:50 +0000939{
940 ipsec4_tunnel_kv_t *ikv = (ipsec4_tunnel_kv_t *) kv;
941 vlib_main_t *vm = arg;
942
943 vlib_cli_output (vm, " %U", format_ipsec4_tunnel_kv, ikv);
944
945 return (BIHASH_WALK_CONTINUE);
946}
947
948static int
Neale Ranns302b25a2020-10-19 13:23:33 +0000949ipsec_tun_protect6_hash_show_one (clib_bihash_kv_24_16_t * kv, void *arg)
Neale Ranns7b4e52f2020-05-24 16:17:50 +0000950{
951 ipsec6_tunnel_kv_t *ikv = (ipsec6_tunnel_kv_t *) kv;
952 vlib_main_t *vm = arg;
953
954 vlib_cli_output (vm, " %U", format_ipsec6_tunnel_kv, ikv);
955
956 return (BIHASH_WALK_CONTINUE);
957}
958
Neale Ranns41afb332019-07-16 06:19:35 -0700959static clib_error_t *
960ipsec_tun_protect_hash_show (vlib_main_t * vm,
961 unformat_input_t * input,
962 vlib_cli_command_t * cmd)
963{
964 ipsec_main_t *im = &ipsec_main;
965
966 {
Neale Ranns41afb332019-07-16 06:19:35 -0700967 vlib_cli_output (vm, "IPv4:");
968
Neale Ranns302b25a2020-10-19 13:23:33 +0000969 clib_bihash_foreach_key_value_pair_8_16
Neale Ranns7b4e52f2020-05-24 16:17:50 +0000970 (&im->tun4_protect_by_key, ipsec_tun_protect4_hash_show_one, vm);
Neale Ranns41afb332019-07-16 06:19:35 -0700971
972 vlib_cli_output (vm, "IPv6:");
973
Neale Ranns302b25a2020-10-19 13:23:33 +0000974 clib_bihash_foreach_key_value_pair_24_16
Neale Ranns7b4e52f2020-05-24 16:17:50 +0000975 (&im->tun6_protect_by_key, ipsec_tun_protect6_hash_show_one, vm);
Neale Ranns41afb332019-07-16 06:19:35 -0700976 }
977
978 return NULL;
979}
980
981/**
982 * show IPSEC tunnel protection hash tables
983 */
984/* *INDENT-OFF* */
985VLIB_CLI_COMMAND (ipsec_tun_protect_hash_show_node, static) =
986{
987 .path = "show ipsec protect-hash",
988 .function = ipsec_tun_protect_hash_show,
989 .short_help = "show ipsec protect-hash",
990};
991/* *INDENT-ON* */
992
Ed Warnickecb9cada2015-12-08 15:45:58 -0700993clib_error_t *
994ipsec_cli_init (vlib_main_t * vm)
995{
996 return 0;
997}
998
999VLIB_INIT_FUNCTION (ipsec_cli_init);
1000
Fan Zhangf5395782020-04-29 14:00:03 +01001001static clib_error_t *
1002set_async_mode_command_fn (vlib_main_t * vm, unformat_input_t * input,
1003 vlib_cli_command_t * cmd)
1004{
1005 unformat_input_t _line_input, *line_input = &_line_input;
1006 int async_enable = 0;
1007
1008 if (!unformat_user (input, unformat_line_input, line_input))
1009 return 0;
1010
1011 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1012 {
1013 if (unformat (line_input, "on"))
1014 async_enable = 1;
1015 else if (unformat (line_input, "off"))
1016 async_enable = 0;
1017 else
1018 return (clib_error_return (0, "unknown input '%U'",
1019 format_unformat_error, line_input));
1020 }
1021
Fan Zhangf5395782020-04-29 14:00:03 +01001022 ipsec_set_async_mode (async_enable);
1023
1024 unformat_free (line_input);
1025 return (NULL);
1026}
1027
1028/* *INDENT-OFF* */
1029VLIB_CLI_COMMAND (set_async_mode_command, static) = {
1030 .path = "set ipsec async mode",
1031 .short_help = "set ipsec async mode on|off",
1032 .function = set_async_mode_command_fn,
1033};
1034/* *INDENT-ON* */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001035
1036/*
1037 * fd.io coding-style-patch-verification: ON
1038 *
1039 * Local Variables:
1040 * eval: (c-set-style "gnu")
1041 * End:
1042 */