blob: 4d452d53d22acb35ef9002302862a1edac147d87 [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;
Ed Warnickecb9cada2015-12-08 15:45:58 -070039
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070040 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -070041 return 0;
42
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070043 if (unformat
44 (line_input, "%U %u", unformat_vnet_sw_interface, im->vnet_main,
45 &sw_if_index, &spd_id))
Ed Warnickecb9cada2015-12-08 15:45:58 -070046 ;
47 else if (unformat (line_input, "del"))
48 is_add = 0;
49 else
Billy McFalla9a20e72017-02-15 11:39:12 -050050 {
51 error = clib_error_return (0, "parse error: '%U'",
52 format_unformat_error, line_input);
53 goto done;
54 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070055
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070056 ipsec_set_interface_spd (vm, sw_if_index, spd_id, is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -070057
Billy McFalla9a20e72017-02-15 11:39:12 -050058done:
59 unformat_free (line_input);
60
61 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -070062}
63
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070064/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070065VLIB_CLI_COMMAND (set_interface_spd_command, static) = {
66 .path = "set interface ipsec spd",
67 .short_help =
68 "set interface ipsec spd <int> <id>",
69 .function = set_interface_spd_command_fn,
70};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070071/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070072
73static clib_error_t *
74ipsec_sa_add_del_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070075 unformat_input_t * input,
76 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -070077{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070078 unformat_input_t _line_input, *line_input = &_line_input;
Neale Ranns8d7c5022019-02-06 01:41:05 -080079 ip46_address_t tun_src = { }, tun_dst =
80 {
81 };
82 ipsec_crypto_alg_t crypto_alg;
83 ipsec_integ_alg_t integ_alg;
84 ipsec_protocol_t proto;
85 ipsec_sa_flags_t flags;
86 clib_error_t *error;
Kingwel Xied3d12052019-03-07 06:34:30 -050087 ipsec_key_t ck = { 0 };
88 ipsec_key_t ik = { 0 };
Neale Rannsabc56602020-04-01 09:45:23 +000089 u32 id, spi, salt, sai;
90 u16 udp_src, udp_dst;
Neale Ranns8d7c5022019-02-06 01:41:05 -080091 int is_add, rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -070092
Neale Ranns8dc75c02019-12-10 01:08:19 +000093 salt = 0;
Neale Ranns8d7c5022019-02-06 01:41:05 -080094 error = NULL;
95 is_add = 0;
96 flags = IPSEC_SA_FLAG_NONE;
97 proto = IPSEC_PROTOCOL_ESP;
Neale Rannse6be7022019-06-04 15:37:34 +000098 integ_alg = IPSEC_INTEG_ALG_NONE;
99 crypto_alg = IPSEC_CRYPTO_ALG_NONE;
Neale Rannsabc56602020-04-01 09:45:23 +0000100 udp_src = udp_dst = IPSEC_UDP_PORT_NONE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700101
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700102 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103 return 0;
104
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700105 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
106 {
Neale Ranns8d7c5022019-02-06 01:41:05 -0800107 if (unformat (line_input, "add %u", &id))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700108 is_add = 1;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800109 else if (unformat (line_input, "del %u", &id))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700110 is_add = 0;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800111 else if (unformat (line_input, "spi %u", &spi))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700112 ;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800113 else if (unformat (line_input, "salt 0x%x", &salt))
Neale Ranns80f6fd52019-04-16 02:41:34 +0000114 ;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700115 else if (unformat (line_input, "esp"))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800116 proto = IPSEC_PROTOCOL_ESP;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700117 else if (unformat (line_input, "ah"))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800118 proto = IPSEC_PROTOCOL_AH;
119 else if (unformat (line_input, "crypto-key %U",
120 unformat_ipsec_key, &ck))
121 ;
122 else if (unformat (line_input, "crypto-alg %U",
123 unformat_ipsec_crypto_alg, &crypto_alg))
124 ;
125 else if (unformat (line_input, "integ-key %U", unformat_ipsec_key, &ik))
126 ;
127 else if (unformat (line_input, "integ-alg %U",
128 unformat_ipsec_integ_alg, &integ_alg))
129 ;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700130 else if (unformat (line_input, "tunnel-src %U",
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500131 unformat_ip46_address, &tun_src, IP46_TYPE_ANY))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700132 {
Neale Ranns8d7c5022019-02-06 01:41:05 -0800133 flags |= IPSEC_SA_FLAG_IS_TUNNEL;
134 if (!ip46_address_is_ip4 (&tun_src))
135 flags |= IPSEC_SA_FLAG_IS_TUNNEL_V6;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700136 }
137 else if (unformat (line_input, "tunnel-dst %U",
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500138 unformat_ip46_address, &tun_dst, IP46_TYPE_ANY))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800139 ;
Radu Nicolau717de092018-08-03 10:37:24 +0100140 else if (unformat (line_input, "udp-encap"))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800141 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700142 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500143 {
144 error = clib_error_return (0, "parse error: '%U'",
145 format_unformat_error, line_input);
146 goto done;
147 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700148 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000150 if (is_add)
Neale Ranns495d7ff2019-07-12 09:15:26 +0000151 rv = ipsec_sa_add_and_lock (id, spi, proto, crypto_alg,
152 &ck, integ_alg, &ik, flags,
153 0, clib_host_to_net_u32 (salt),
Neale Rannsabc56602020-04-01 09:45:23 +0000154 &tun_src, &tun_dst, &sai, udp_src, udp_dst);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800155 else
Neale Ranns495d7ff2019-07-12 09:15:26 +0000156 rv = ipsec_sa_unlock_id (id);
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000157
Neale Ranns8d7c5022019-02-06 01:41:05 -0800158 if (rv)
Neale Rannse6be7022019-06-04 15:37:34 +0000159 error = clib_error_return (0, "failed");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160
Billy McFalla9a20e72017-02-15 11:39:12 -0500161done:
162 unformat_free (line_input);
163
164 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165}
166
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700167/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168VLIB_CLI_COMMAND (ipsec_sa_add_del_command, static) = {
169 .path = "ipsec sa",
170 .short_help =
171 "ipsec sa [add|del]",
172 .function = ipsec_sa_add_del_command_fn,
173};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700174/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175
176static clib_error_t *
177ipsec_spd_add_del_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700178 unformat_input_t * input,
179 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700180{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700181 unformat_input_t _line_input, *line_input = &_line_input;
Damjan Marion3f54b182016-08-16 11:27:02 +0200182 u32 spd_id = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183 int is_add = ~0;
Billy McFalla9a20e72017-02-15 11:39:12 -0500184 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700186 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187 return 0;
188
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700189 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
190 {
191 if (unformat (line_input, "add"))
192 is_add = 1;
193 else if (unformat (line_input, "del"))
194 is_add = 0;
195 else if (unformat (line_input, "%u", &spd_id))
196 ;
197 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500198 {
199 error = clib_error_return (0, "parse error: '%U'",
200 format_unformat_error, line_input);
201 goto done;
202 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700203 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204
Damjan Marion3f54b182016-08-16 11:27:02 +0200205 if (spd_id == ~0)
Billy McFalla9a20e72017-02-15 11:39:12 -0500206 {
207 error = clib_error_return (0, "please specify SPD ID");
208 goto done;
209 }
Damjan Marion3f54b182016-08-16 11:27:02 +0200210
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700211 ipsec_add_del_spd (vm, spd_id, is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212
Billy McFalla9a20e72017-02-15 11:39:12 -0500213done:
214 unformat_free (line_input);
215
216 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700217}
218
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700219/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700220VLIB_CLI_COMMAND (ipsec_spd_add_del_command, static) = {
221 .path = "ipsec spd",
222 .short_help =
223 "ipsec spd [add|del] <id>",
224 .function = ipsec_spd_add_del_command_fn,
225};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700226/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700227
228
229static clib_error_t *
230ipsec_policy_add_del_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700231 unformat_input_t * input,
232 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700233{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700234 unformat_input_t _line_input, *line_input = &_line_input;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700235 ipsec_policy_t p;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800236 int rv, is_add = 0;
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000237 u32 tmp, tmp2, stat_index, local_range_set, remote_range_set;
Billy McFalla9a20e72017-02-15 11:39:12 -0500238 clib_error_t *error = NULL;
Neale Ranns9f231d42019-03-19 10:06:00 +0000239 u32 is_outbound;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700240
Dave Barachb7b92992018-10-17 10:38:51 -0400241 clib_memset (&p, 0, sizeof (p));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242 p.lport.stop = p.rport.stop = ~0;
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000243 remote_range_set = local_range_set = is_outbound = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700244
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700245 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700246 return 0;
247
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700248 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
249 {
250 if (unformat (line_input, "add"))
251 is_add = 1;
252 else if (unformat (line_input, "del"))
253 is_add = 0;
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000254 else if (unformat (line_input, "ip6"))
255 p.is_ipv6 = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700256 else if (unformat (line_input, "spd %u", &p.id))
257 ;
258 else if (unformat (line_input, "inbound"))
Neale Ranns9f231d42019-03-19 10:06:00 +0000259 is_outbound = 0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700260 else if (unformat (line_input, "outbound"))
Neale Ranns9f231d42019-03-19 10:06:00 +0000261 is_outbound = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700262 else if (unformat (line_input, "priority %d", &p.priority))
263 ;
264 else if (unformat (line_input, "protocol %u", &tmp))
265 p.protocol = (u8) tmp;
266 else
267 if (unformat
268 (line_input, "action %U", unformat_ipsec_policy_action,
269 &p.policy))
270 {
271 if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
Billy McFalla9a20e72017-02-15 11:39:12 -0500272 {
273 error = clib_error_return (0, "unsupported action: 'resolve'");
274 goto done;
275 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700276 }
277 else if (unformat (line_input, "sa %u", &p.sa_id))
278 ;
279 else if (unformat (line_input, "local-ip-range %U - %U",
280 unformat_ip4_address, &p.laddr.start.ip4,
281 unformat_ip4_address, &p.laddr.stop.ip4))
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000282 local_range_set = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700283 else if (unformat (line_input, "remote-ip-range %U - %U",
284 unformat_ip4_address, &p.raddr.start.ip4,
285 unformat_ip4_address, &p.raddr.stop.ip4))
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000286 remote_range_set = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700287 else if (unformat (line_input, "local-ip-range %U - %U",
288 unformat_ip6_address, &p.laddr.start.ip6,
289 unformat_ip6_address, &p.laddr.stop.ip6))
290 {
291 p.is_ipv6 = 1;
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000292 local_range_set = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700293 }
294 else if (unformat (line_input, "remote-ip-range %U - %U",
295 unformat_ip6_address, &p.raddr.start.ip6,
296 unformat_ip6_address, &p.raddr.stop.ip6))
297 {
298 p.is_ipv6 = 1;
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000299 remote_range_set = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700300 }
301 else if (unformat (line_input, "local-port-range %u - %u", &tmp, &tmp2))
302 {
303 p.lport.start = tmp;
304 p.lport.stop = tmp2;
305 }
306 else
307 if (unformat (line_input, "remote-port-range %u - %u", &tmp, &tmp2))
308 {
309 p.rport.start = tmp;
310 p.rport.stop = tmp2;
311 }
312 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500313 {
314 error = clib_error_return (0, "parse error: '%U'",
315 format_unformat_error, line_input);
316 goto done;
317 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700318 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700319
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000320 if (!remote_range_set)
321 {
322 if (p.is_ipv6)
323 clib_memset (&p.raddr.stop.ip6, 0xff, 16);
324 else
325 clib_memset (&p.raddr.stop.ip4, 0xff, 4);
326 }
327 if (!local_range_set)
328 {
329 if (p.is_ipv6)
330 clib_memset (&p.laddr.stop.ip6, 0xff, 16);
331 else
332 clib_memset (&p.laddr.stop.ip4, 0xff, 4);
333 }
334
Neale Ranns9f231d42019-03-19 10:06:00 +0000335 rv = ipsec_policy_mk_type (is_outbound, p.is_ipv6, p.policy, &p.type);
336
337 if (rv)
338 {
339 error = clib_error_return (0, "unsupported policy type for:",
340 " outboud:%s %s action:%U",
341 (is_outbound ? "yes" : "no"),
342 (p.is_ipv6 ? "IPv4" : "IPv6"),
343 format_ipsec_policy_action, p.policy);
344 goto done;
345 }
346
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800347 rv = ipsec_add_del_policy (vm, &p, is_add, &stat_index);
348
349 if (!rv)
350 vlib_cli_output (vm, "policy-index:%d", stat_index);
351 else
352 vlib_cli_output (vm, "error:%d", rv);
Billy McFalla9a20e72017-02-15 11:39:12 -0500353
354done:
355 unformat_free (line_input);
356
357 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358}
359
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700360/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700361VLIB_CLI_COMMAND (ipsec_policy_add_del_command, static) = {
362 .path = "ipsec policy",
363 .short_help =
364 "ipsec policy [add|del] spd <id> priority <n> ",
365 .function = ipsec_policy_add_del_command_fn,
366};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700367/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700368
Neale Rannsb294f102019-04-03 13:17:50 +0000369static void
Neale Ranns670027a2019-08-27 12:47:17 +0000370ipsec_sa_show_all (vlib_main_t * vm, ipsec_main_t * im, u8 detail)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700371{
Neale Rannsb294f102019-04-03 13:17:50 +0000372 u32 sai;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700373
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700374 /* *INDENT-OFF* */
Neale Ranns8d7c5022019-02-06 01:41:05 -0800375 pool_foreach_index (sai, im->sad, ({
Neale Ranns670027a2019-08-27 12:47:17 +0000376 vlib_cli_output(vm, "%U", format_ipsec_sa, sai,
377 (detail ? IPSEC_FORMAT_DETAIL : IPSEC_FORMAT_BRIEF));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700378 }));
Neale Rannsb294f102019-04-03 13:17:50 +0000379 /* *INDENT-ON* */
380}
381
382static void
383ipsec_spd_show_all (vlib_main_t * vm, ipsec_main_t * im)
384{
385 u32 spdi;
386
387 /* *INDENT-OFF* */
388 pool_foreach_index (spdi, im->spds, ({
389 vlib_cli_output(vm, "%U", format_ipsec_spd, spdi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700390 }));
Neale Rannsb294f102019-04-03 13:17:50 +0000391 /* *INDENT-ON* */
392}
393
394static void
395ipsec_spd_bindings_show_all (vlib_main_t * vm, ipsec_main_t * im)
396{
397 u32 spd_id, sw_if_index;
Neale Rannsd83c4a82019-06-14 06:48:27 -0700398 ipsec_spd_t *spd;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700399
Neale Ranns311124e2019-01-24 04:52:25 -0800400 vlib_cli_output (vm, "SPD Bindings:");
Neale Ranns8d7c5022019-02-06 01:41:05 -0800401
Neale Rannsb294f102019-04-03 13:17:50 +0000402 /* *INDENT-OFF* */
Neale Ranns311124e2019-01-24 04:52:25 -0800403 hash_foreach(sw_if_index, spd_id, im->spd_index_by_sw_if_index, ({
Neale Rannsd83c4a82019-06-14 06:48:27 -0700404 spd = pool_elt_at_index (im->spds, spd_id);
405 vlib_cli_output (vm, " %d -> %U", spd->id,
Neale Ranns8d7c5022019-02-06 01:41:05 -0800406 format_vnet_sw_if_index_name, im->vnet_main,
407 sw_if_index);
Neale Ranns311124e2019-01-24 04:52:25 -0800408 }));
409 /* *INDENT-ON* */
Neale Rannsb294f102019-04-03 13:17:50 +0000410}
Neale Ranns311124e2019-01-24 04:52:25 -0800411
Neale Ranns12989b52019-09-26 16:20:19 +0000412static walk_rc_t
413ipsec_tun_protect_show_one (index_t itpi, void *ctx)
Neale Rannsb294f102019-04-03 13:17:50 +0000414{
Neale Ranns28287212019-12-16 00:53:11 +0000415 vlib_cli_output (ctx, "%U", format_ipsec_tun_protect_index, itpi);
Neale Rannsb294f102019-04-03 13:17:50 +0000416
Neale Ranns12989b52019-09-26 16:20:19 +0000417 return (WALK_CONTINUE);
418}
419
420static void
421ipsec_tunnel_show_all (vlib_main_t * vm)
422{
423 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
Neale Rannsb294f102019-04-03 13:17:50 +0000424}
425
426static clib_error_t *
427show_ipsec_command_fn (vlib_main_t * vm,
428 unformat_input_t * input, vlib_cli_command_t * cmd)
429{
430 ipsec_main_t *im = &ipsec_main;
431
Neale Ranns670027a2019-08-27 12:47:17 +0000432 ipsec_sa_show_all (vm, im, 0);
Neale Rannsb294f102019-04-03 13:17:50 +0000433 ipsec_spd_show_all (vm, im);
434 ipsec_spd_bindings_show_all (vm, im);
Neale Ranns12989b52019-09-26 16:20:19 +0000435 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
Neale Rannsb294f102019-04-03 13:17:50 +0000436
Fan Zhangf5395782020-04-29 14:00:03 +0100437 vlib_cli_output (vm, "IPSec async mode: %s",
438 (im->async_mode ? "on" : "off"));
439
Ed Warnickecb9cada2015-12-08 15:45:58 -0700440 return 0;
441}
442
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700443/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700444VLIB_CLI_COMMAND (show_ipsec_command, static) = {
Neale Rannsb294f102019-04-03 13:17:50 +0000445 .path = "show ipsec all",
446 .short_help = "show ipsec all",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700447 .function = show_ipsec_command_fn,
448};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700449/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700450
451static clib_error_t *
Neale Rannsb294f102019-04-03 13:17:50 +0000452show_ipsec_sa_command_fn (vlib_main_t * vm,
453 unformat_input_t * input, vlib_cli_command_t * cmd)
454{
455 ipsec_main_t *im = &ipsec_main;
456 u32 sai = ~0;
Neale Ranns670027a2019-08-27 12:47:17 +0000457 u8 detail = 0;
Neale Rannsb294f102019-04-03 13:17:50 +0000458
459 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
460 {
461 if (unformat (input, "%u", &sai))
462 ;
Neale Ranns670027a2019-08-27 12:47:17 +0000463 if (unformat (input, "detail"))
464 detail = 1;
Neale Rannsb294f102019-04-03 13:17:50 +0000465 else
466 break;
467 }
468
469 if (~0 == sai)
Neale Ranns670027a2019-08-27 12:47:17 +0000470 ipsec_sa_show_all (vm, im, detail);
Neale Rannsb294f102019-04-03 13:17:50 +0000471 else
Christian E. Hopps01d61e72019-09-27 14:43:22 -0400472 vlib_cli_output (vm, "%U", format_ipsec_sa, sai,
473 IPSEC_FORMAT_DETAIL | IPSEC_FORMAT_INSECURE);
Neale Rannsb294f102019-04-03 13:17:50 +0000474
475 return 0;
476}
477
Neale Rannsc87b66c2019-02-07 07:26:12 -0800478static clib_error_t *
479clear_ipsec_sa_command_fn (vlib_main_t * vm,
480 unformat_input_t * input, vlib_cli_command_t * cmd)
481{
482 ipsec_main_t *im = &ipsec_main;
483 u32 sai = ~0;
484
485 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
486 {
487 if (unformat (input, "%u", &sai))
488 ;
489 else
490 break;
491 }
492
493 if (~0 == sai)
494 {
495 /* *INDENT-OFF* */
496 pool_foreach_index (sai, im->sad, ({
497 ipsec_sa_clear(sai);
498 }));
499 /* *INDENT-ON* */
500 }
501 else
502 {
503 if (pool_is_free_index (im->sad, sai))
504 return clib_error_return (0, "unknown SA index: %d", sai);
505 else
506 ipsec_sa_clear (sai);
507 }
508
509 return 0;
510}
511
Neale Rannsb294f102019-04-03 13:17:50 +0000512/* *INDENT-OFF* */
513VLIB_CLI_COMMAND (show_ipsec_sa_command, static) = {
514 .path = "show ipsec sa",
515 .short_help = "show ipsec sa [index]",
516 .function = show_ipsec_sa_command_fn,
517};
Neale Rannsc87b66c2019-02-07 07:26:12 -0800518
519VLIB_CLI_COMMAND (clear_ipsec_sa_command, static) = {
520 .path = "clear ipsec sa",
521 .short_help = "clear ipsec sa [index]",
522 .function = clear_ipsec_sa_command_fn,
523};
Neale Rannsb294f102019-04-03 13:17:50 +0000524/* *INDENT-ON* */
525
526static clib_error_t *
527show_ipsec_spd_command_fn (vlib_main_t * vm,
528 unformat_input_t * input, vlib_cli_command_t * cmd)
529{
530 ipsec_main_t *im = &ipsec_main;
531 u8 show_bindings = 0;
532 u32 spdi = ~0;
533
534 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
535 {
536 if (unformat (input, "%u", &spdi))
537 ;
538 else if (unformat (input, "bindings"))
539 show_bindings = 1;
540 else
541 break;
542 }
543
544 if (show_bindings)
545 ipsec_spd_bindings_show_all (vm, im);
546 else if (~0 != spdi)
547 vlib_cli_output (vm, "%U", format_ipsec_spd, spdi);
548 else
549 ipsec_spd_show_all (vm, im);
550
551 return 0;
552}
553
554/* *INDENT-OFF* */
555VLIB_CLI_COMMAND (show_ipsec_spd_command, static) = {
556 .path = "show ipsec spd",
557 .short_help = "show ipsec spd [index]",
558 .function = show_ipsec_spd_command_fn,
559};
560/* *INDENT-ON* */
561
562static clib_error_t *
563show_ipsec_tunnel_command_fn (vlib_main_t * vm,
564 unformat_input_t * input,
565 vlib_cli_command_t * cmd)
566{
Neale Ranns12989b52019-09-26 16:20:19 +0000567 ipsec_tunnel_show_all (vm);
Neale Rannsb294f102019-04-03 13:17:50 +0000568
569 return 0;
570}
571
572/* *INDENT-OFF* */
573VLIB_CLI_COMMAND (show_ipsec_tunnel_command, static) = {
574 .path = "show ipsec tunnel",
Neale Ranns12989b52019-09-26 16:20:19 +0000575 .short_help = "show ipsec tunnel",
Neale Rannsb294f102019-04-03 13:17:50 +0000576 .function = show_ipsec_tunnel_command_fn,
577};
578/* *INDENT-ON* */
579
580static clib_error_t *
Klement Sekerab4d30532018-11-08 13:00:02 +0100581ipsec_show_backends_command_fn (vlib_main_t * vm,
582 unformat_input_t * input,
583 vlib_cli_command_t * cmd)
584{
585 ipsec_main_t *im = &ipsec_main;
586 u32 verbose = 0;
587
588 (void) unformat (input, "verbose %u", &verbose);
589
590 vlib_cli_output (vm, "IPsec AH backends available:");
591 u8 *s = format (NULL, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
592 ipsec_ah_backend_t *ab;
593 /* *INDENT-OFF* */
594 pool_foreach (ab, im->ah_backends, {
595 s = format (s, "%=25s %=25u %=10s\n", ab->name, ab - im->ah_backends,
596 ab - im->ah_backends == im->ah_current_backend ? "yes" : "no");
597 if (verbose) {
598 vlib_node_t *n;
599 n = vlib_get_node (vm, ab->ah4_encrypt_node_index);
600 s = format (s, " enc4 %s (next %d)\n", n->name, ab->ah4_encrypt_next_index);
601 n = vlib_get_node (vm, ab->ah4_decrypt_node_index);
602 s = format (s, " dec4 %s (next %d)\n", n->name, ab->ah4_decrypt_next_index);
603 n = vlib_get_node (vm, ab->ah6_encrypt_node_index);
604 s = format (s, " enc6 %s (next %d)\n", n->name, ab->ah6_encrypt_next_index);
605 n = vlib_get_node (vm, ab->ah6_decrypt_node_index);
606 s = format (s, " dec6 %s (next %d)\n", n->name, ab->ah6_decrypt_next_index);
607 }
608 });
609 /* *INDENT-ON* */
610 vlib_cli_output (vm, "%v", s);
611 _vec_len (s) = 0;
612 vlib_cli_output (vm, "IPsec ESP backends available:");
613 s = format (s, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
614 ipsec_esp_backend_t *eb;
615 /* *INDENT-OFF* */
616 pool_foreach (eb, im->esp_backends, {
617 s = format (s, "%=25s %=25u %=10s\n", eb->name, eb - im->esp_backends,
618 eb - im->esp_backends == im->esp_current_backend ? "yes"
619 : "no");
620 if (verbose) {
621 vlib_node_t *n;
622 n = vlib_get_node (vm, eb->esp4_encrypt_node_index);
623 s = format (s, " enc4 %s (next %d)\n", n->name, eb->esp4_encrypt_next_index);
624 n = vlib_get_node (vm, eb->esp4_decrypt_node_index);
625 s = format (s, " dec4 %s (next %d)\n", n->name, eb->esp4_decrypt_next_index);
626 n = vlib_get_node (vm, eb->esp6_encrypt_node_index);
627 s = format (s, " enc6 %s (next %d)\n", n->name, eb->esp6_encrypt_next_index);
628 n = vlib_get_node (vm, eb->esp6_decrypt_node_index);
629 s = format (s, " dec6 %s (next %d)\n", n->name, eb->esp6_decrypt_next_index);
630 }
631 });
632 /* *INDENT-ON* */
633 vlib_cli_output (vm, "%v", s);
634
635 vec_free (s);
636 return 0;
637}
638
639/* *INDENT-OFF* */
640VLIB_CLI_COMMAND (ipsec_show_backends_command, static) = {
641 .path = "show ipsec backends",
642 .short_help = "show ipsec backends",
643 .function = ipsec_show_backends_command_fn,
644};
645/* *INDENT-ON* */
646
647static clib_error_t *
648ipsec_select_backend_command_fn (vlib_main_t * vm,
649 unformat_input_t * input,
650 vlib_cli_command_t * cmd)
651{
Klement Sekerab4d30532018-11-08 13:00:02 +0100652 unformat_input_t _line_input, *line_input = &_line_input;
Neale Rannse8915fc2019-04-23 20:57:55 -0400653 ipsec_main_t *im = &ipsec_main;
654 clib_error_t *error;
655 u32 backend_index;
656
657 error = ipsec_rsc_in_use (im);
658
659 if (error)
660 return error;
661
Klement Sekerab4d30532018-11-08 13:00:02 +0100662 /* Get a line of input. */
663 if (!unformat_user (input, unformat_line_input, line_input))
664 return 0;
665
666 if (unformat (line_input, "ah"))
667 {
668 if (unformat (line_input, "%u", &backend_index))
669 {
670 if (ipsec_select_ah_backend (im, backend_index) < 0)
671 {
672 return clib_error_return (0, "Invalid AH backend index `%u'",
673 backend_index);
674 }
675 }
676 else
677 {
678 return clib_error_return (0, "Invalid backend index `%U'",
679 format_unformat_error, line_input);
680 }
681 }
682 else if (unformat (line_input, "esp"))
683 {
684 if (unformat (line_input, "%u", &backend_index))
685 {
686 if (ipsec_select_esp_backend (im, backend_index) < 0)
687 {
688 return clib_error_return (0, "Invalid ESP backend index `%u'",
689 backend_index);
690 }
691 }
692 else
693 {
694 return clib_error_return (0, "Invalid backend index `%U'",
695 format_unformat_error, line_input);
696 }
697 }
698 else
699 {
700 return clib_error_return (0, "Unknown input `%U'",
701 format_unformat_error, line_input);
702 }
703
704 return 0;
705}
706
707/* *INDENT-OFF* */
708VLIB_CLI_COMMAND (ipsec_select_backend_command, static) = {
709 .path = "ipsec select backend",
710 .short_help = "ipsec select backend <ah|esp> <backend index>",
711 .function = ipsec_select_backend_command_fn,
712};
713
714/* *INDENT-ON* */
715
716static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700717clear_ipsec_counters_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700718 unformat_input_t * input,
719 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700720{
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800721 vlib_clear_combined_counters (&ipsec_spd_policy_counters);
Neale Rannseba31ec2019-02-17 18:04:27 +0000722 vlib_clear_combined_counters (&ipsec_sa_counters);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700723
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800724 return (NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700725}
726
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700727/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700728VLIB_CLI_COMMAND (clear_ipsec_counters_command, static) = {
729 .path = "clear ipsec counters",
730 .short_help = "clear ipsec counters",
731 .function = clear_ipsec_counters_command_fn,
732};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700733/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700734
Neale Ranns12989b52019-09-26 16:20:19 +0000735static u32
736ipsec_tun_mk_local_sa_id (u32 ti)
737{
738 return (0x80000000 | ti);
739}
740
741static u32
742ipsec_tun_mk_remote_sa_id (u32 ti)
743{
744 return (0xc0000000 | ti);
745}
746
Ed Warnickecb9cada2015-12-08 15:45:58 -0700747static clib_error_t *
748create_ipsec_tunnel_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700749 unformat_input_t * input,
750 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700751{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700752 unformat_input_t _line_input, *line_input = &_line_input;
Neale Ranns12989b52019-09-26 16:20:19 +0000753 ip46_address_t local_ip = ip46_address_initializer;
754 ip46_address_t remote_ip = ip46_address_initializer;
Neale Ranns28287212019-12-16 00:53:11 +0000755 ip_address_t nh = IP_ADDRESS_V4_ALL_0S;
Damjan Marion68449852020-03-16 17:53:38 +0100756 ipsec_crypto_alg_t crypto_alg = IPSEC_CRYPTO_ALG_NONE;
757 ipsec_integ_alg_t integ_alg = IPSEC_INTEG_ALG_NONE;
Neale Ranns12989b52019-09-26 16:20:19 +0000758 ipsec_sa_flags_t flags;
759 u32 local_spi, remote_spi, salt, table_id, fib_index;
760 u32 instance = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700761 int rv;
762 u32 num_m_args = 0;
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500763 u8 ipv4_set = 0;
764 u8 ipv6_set = 0;
Neale Ranns12989b52019-09-26 16:20:19 +0000765 u8 is_add = 1;
Billy McFalla9a20e72017-02-15 11:39:12 -0500766 clib_error_t *error = NULL;
Kingwel Xied3d12052019-03-07 06:34:30 -0500767 ipsec_key_t rck = { 0 };
768 ipsec_key_t lck = { 0 };
769 ipsec_key_t lik = { 0 };
770 ipsec_key_t rik = { 0 };
Matthew Smith2838a232016-06-21 16:05:09 -0500771
Neale Ranns12989b52019-09-26 16:20:19 +0000772 table_id = 0;
773 flags = IPSEC_SA_FLAG_NONE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700774
775 /* Get a line of input. */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700776 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700777 return 0;
778
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700779 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
780 {
781 if (unformat
Neale Ranns12989b52019-09-26 16:20:19 +0000782 (line_input, "local-ip %U", unformat_ip46_address, &local_ip,
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500783 IP46_TYPE_ANY))
784 {
Neale Ranns12989b52019-09-26 16:20:19 +0000785 ip46_address_is_ip4 (&local_ip) ? (ipv4_set = 1) : (ipv6_set = 1);
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500786 num_m_args++;
787 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700788 else
789 if (unformat
Neale Ranns12989b52019-09-26 16:20:19 +0000790 (line_input, "remote-ip %U", unformat_ip46_address, &remote_ip,
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500791 IP46_TYPE_ANY))
792 {
Neale Ranns12989b52019-09-26 16:20:19 +0000793 ip46_address_is_ip4 (&remote_ip) ? (ipv4_set = 1) : (ipv6_set = 1);
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500794 num_m_args++;
795 }
Neale Ranns12989b52019-09-26 16:20:19 +0000796 else if (unformat (line_input, "local-spi %u", &local_spi))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700797 num_m_args++;
Neale Ranns12989b52019-09-26 16:20:19 +0000798 else if (unformat (line_input, "remote-spi %u", &remote_spi))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700799 num_m_args++;
Neale Ranns12989b52019-09-26 16:20:19 +0000800 else if (unformat (line_input, "salt 0x%x", &salt))
Neale Ranns47feb112019-04-11 15:14:07 +0000801 ;
Radu Nicolau717de092018-08-03 10:37:24 +0100802 else if (unformat (line_input, "udp-encap"))
Neale Ranns12989b52019-09-26 16:20:19 +0000803 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
Kingwel Xie2baf9422019-02-04 02:07:06 -0800804 else if (unformat (line_input, "use-esn"))
Neale Ranns12989b52019-09-26 16:20:19 +0000805 flags |= IPSEC_SA_FLAG_USE_ESN;
Kingwel Xie2baf9422019-02-04 02:07:06 -0800806 else if (unformat (line_input, "use-anti-replay"))
Neale Ranns12989b52019-09-26 16:20:19 +0000807 flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
808 else if (unformat (line_input, "instance %u", &instance))
809 ;
810 else if (unformat (line_input, "tx-table %u", &table_id))
Pierre Pfister4c422f92018-12-10 11:19:08 +0100811 ;
Neale Rannsfd060842019-03-04 13:44:42 +0000812 else
813 if (unformat
814 (line_input, "local-crypto-key %U", unformat_ipsec_key, &lck))
815 ;
816 else
817 if (unformat
818 (line_input, "remote-crypto-key %U", unformat_ipsec_key, &rck))
819 ;
820 else if (unformat (line_input, "crypto-alg %U",
Neale Ranns12989b52019-09-26 16:20:19 +0000821 unformat_ipsec_crypto_alg, &crypto_alg))
Neale Rannsfd060842019-03-04 13:44:42 +0000822 ;
823 else
824 if (unformat
825 (line_input, "local-integ-key %U", unformat_ipsec_key, &lik))
826 ;
827 else
828 if (unformat
Simon Zhange8e950a2019-04-23 23:04:07 +0800829 (line_input, "remote-integ-key %U", unformat_ipsec_key, &rik))
Neale Rannsfd060842019-03-04 13:44:42 +0000830 ;
831 else if (unformat (line_input, "integ-alg %U",
Neale Ranns12989b52019-09-26 16:20:19 +0000832 unformat_ipsec_integ_alg, &integ_alg))
Neale Rannsfd060842019-03-04 13:44:42 +0000833 ;
Kingwel Xie2baf9422019-02-04 02:07:06 -0800834 else if (unformat (line_input, "del"))
Neale Ranns12989b52019-09-26 16:20:19 +0000835 is_add = 0;
Neale Ranns28287212019-12-16 00:53:11 +0000836 else if (unformat (line_input, "nh &U", unformat_ip_address, &nh))
837 ;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700838 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500839 {
840 error = clib_error_return (0, "unknown input `%U'",
841 format_unformat_error, line_input);
842 goto done;
843 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700844 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700845
846 if (num_m_args < 4)
Billy McFalla9a20e72017-02-15 11:39:12 -0500847 {
848 error = clib_error_return (0, "mandatory argument(s) missing");
849 goto done;
850 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700851
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500852 if (ipv4_set && ipv6_set)
853 return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
854
Neale Ranns12989b52019-09-26 16:20:19 +0000855 fib_index = fib_table_find (fib_ip_proto (ipv6_set), table_id);
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400856
Neale Ranns12989b52019-09-26 16:20:19 +0000857 if (~0 == fib_index)
858 {
859 rv = VNET_API_ERROR_NO_SUCH_FIB;
860 goto done;
861 }
Neale Rannsfd060842019-03-04 13:44:42 +0000862
Neale Ranns12989b52019-09-26 16:20:19 +0000863 if (is_add)
864 {
865 // remote = input, local = output
866 u32 sw_if_index;
Neale Rannsfd060842019-03-04 13:44:42 +0000867
Neale Ranns12989b52019-09-26 16:20:19 +0000868 /* create an ip-ip tunnel, then the two SA, then bind them */
869 rv =
870 ipip_add_tunnel (ipv6_set ? IPIP_TRANSPORT_IP6 : IPIP_TRANSPORT_IP4,
Neale Ranns95346962019-11-25 13:04:44 +0000871 instance, &local_ip, &remote_ip, fib_index,
Neale Ranns59ff9182019-12-29 23:55:18 +0000872 TUNNEL_ENCAP_DECAP_FLAG_NONE, IP_DSCP_CS0,
Neale Ranns14053c92019-12-29 23:55:18 +0000873 TUNNEL_MODE_P2P, &sw_if_index);
Neale Ranns12989b52019-09-26 16:20:19 +0000874 rv |=
875 ipsec_sa_add_and_lock (ipsec_tun_mk_local_sa_id (sw_if_index),
876 local_spi, IPSEC_PROTOCOL_ESP, crypto_alg,
877 &lck, integ_alg, &lik, flags, table_id,
878 clib_host_to_net_u32 (salt), &local_ip,
Neale Rannsabc56602020-04-01 09:45:23 +0000879 &remote_ip, NULL, IPSEC_UDP_PORT_NONE,
880 IPSEC_UDP_PORT_NONE);
Neale Ranns12989b52019-09-26 16:20:19 +0000881 rv |=
882 ipsec_sa_add_and_lock (ipsec_tun_mk_remote_sa_id (sw_if_index),
883 remote_spi, IPSEC_PROTOCOL_ESP, crypto_alg,
884 &rck, integ_alg, &rik,
885 (flags | IPSEC_SA_FLAG_IS_INBOUND), table_id,
886 clib_host_to_net_u32 (salt), &remote_ip,
Neale Rannsabc56602020-04-01 09:45:23 +0000887 &local_ip, NULL, IPSEC_UDP_PORT_NONE,
888 IPSEC_UDP_PORT_NONE);
Neale Ranns12989b52019-09-26 16:20:19 +0000889 rv |=
Neale Ranns28287212019-12-16 00:53:11 +0000890 ipsec_tun_protect_update_one (sw_if_index, &nh,
Neale Ranns12989b52019-09-26 16:20:19 +0000891 ipsec_tun_mk_local_sa_id (sw_if_index),
892 ipsec_tun_mk_remote_sa_id
893 (sw_if_index));
894 }
895 else
896 rv = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700897
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700898 switch (rv)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700899 {
900 case 0:
901 break;
902 case VNET_API_ERROR_INVALID_VALUE:
Neale Rannsd14fccd2019-11-15 15:03:27 +0000903 error = clib_error_return (0,
904 "IPSec tunnel interface already exists...");
Billy McFalla9a20e72017-02-15 11:39:12 -0500905 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700906 default:
Billy McFalla9a20e72017-02-15 11:39:12 -0500907 error = clib_error_return (0, "ipsec_register_interface returned %d",
908 rv);
909 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700910 }
911
Billy McFalla9a20e72017-02-15 11:39:12 -0500912done:
913 unformat_free (line_input);
914
915 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700916}
917
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700918/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700919VLIB_CLI_COMMAND (create_ipsec_tunnel_command, static) = {
920 .path = "create ipsec tunnel",
Pierre Pfister4c422f92018-12-10 11:19:08 +0100921 .short_help = "create ipsec tunnel local-ip <addr> local-spi <spi> "
Kingwel Xie2baf9422019-02-04 02:07:06 -0800922 "remote-ip <addr> remote-spi <spi> [instance <inst_num>] [udp-encap] [use-esn] [use-anti-replay] "
Pierre Pfister4c422f92018-12-10 11:19:08 +0100923 "[tx-table <table-id>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700924 .function = create_ipsec_tunnel_command_fn,
925};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700926/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700927
Neale Rannsc87b66c2019-02-07 07:26:12 -0800928static clib_error_t *
929ipsec_tun_protect_cmd (vlib_main_t * vm,
930 unformat_input_t * input, vlib_cli_command_t * cmd)
931{
932 unformat_input_t _line_input, *line_input = &_line_input;
933 u32 sw_if_index, is_del, sa_in, sa_out, *sa_ins = NULL;
Neale Ranns28287212019-12-16 00:53:11 +0000934 ip_address_t peer = { };
Neale Rannsc87b66c2019-02-07 07:26:12 -0800935 vnet_main_t *vnm;
936
937 is_del = 0;
938 sw_if_index = ~0;
939 vnm = vnet_get_main ();
940
941 if (!unformat_user (input, unformat_line_input, line_input))
942 return 0;
943
944 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
945 {
946 if (unformat (line_input, "del"))
947 is_del = 1;
948 else if (unformat (line_input, "add"))
949 is_del = 0;
950 else if (unformat (line_input, "sa-in %d", &sa_in))
951 vec_add1 (sa_ins, sa_in);
952 else if (unformat (line_input, "sa-out %d", &sa_out))
953 ;
954 else if (unformat (line_input, "%U",
955 unformat_vnet_sw_interface, vnm, &sw_if_index))
956 ;
Neale Ranns28287212019-12-16 00:53:11 +0000957 else if (unformat (line_input, "%U", unformat_ip_address, &peer))
958 ;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800959 else
960 return (clib_error_return (0, "unknown input '%U'",
961 format_unformat_error, line_input));
962 }
963
964 if (!is_del)
Neale Ranns28287212019-12-16 00:53:11 +0000965 ipsec_tun_protect_update (sw_if_index, &peer, sa_out, sa_ins);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800966
967 unformat_free (line_input);
968 return NULL;
969}
970
971/**
972 * Protect tunnel with IPSEC
973 */
974/* *INDENT-OFF* */
975VLIB_CLI_COMMAND (ipsec_tun_protect_cmd_node, static) =
976{
977 .path = "ipsec tunnel protect",
978 .function = ipsec_tun_protect_cmd,
979 .short_help = "ipsec tunnel protect <interface> input-sa <SA> output-sa <SA>",
980 // this is not MP safe
981};
982/* *INDENT-ON* */
983
Neale Rannsc87b66c2019-02-07 07:26:12 -0800984
985static clib_error_t *
986ipsec_tun_protect_show (vlib_main_t * vm,
987 unformat_input_t * input, vlib_cli_command_t * cmd)
988{
989 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
990
991 return NULL;
992}
993
994/**
995 * show IPSEC tunnel protection
996 */
997/* *INDENT-OFF* */
998VLIB_CLI_COMMAND (ipsec_tun_protect_show_node, static) =
999{
1000 .path = "show ipsec protect",
1001 .function = ipsec_tun_protect_show,
1002 .short_help = "show ipsec protect",
1003};
1004/* *INDENT-ON* */
1005
Neale Ranns41afb332019-07-16 06:19:35 -07001006static clib_error_t *
1007ipsec_tun_protect_hash_show (vlib_main_t * vm,
1008 unformat_input_t * input,
1009 vlib_cli_command_t * cmd)
1010{
1011 ipsec_main_t *im = &ipsec_main;
1012
1013 {
1014 ipsec_tun_lkup_result_t value;
1015 ipsec4_tunnel_key_t key;
1016
1017 vlib_cli_output (vm, "IPv4:");
1018
1019 /* *INDENT-OFF* */
1020 hash_foreach(key.as_u64, value.as_u64, im->tun4_protect_by_key,
1021 ({
1022 vlib_cli_output (vm, " %U", format_ipsec4_tunnel_key, &key);
1023 vlib_cli_output (vm, " tun:%d sa:%d", value.tun_index, value.sa_index);
1024 }));
1025 /* *INDENT-ON* */
1026 }
1027
1028 {
1029 ipsec_tun_lkup_result_t value;
1030 ipsec6_tunnel_key_t *key;
1031
1032 vlib_cli_output (vm, "IPv6:");
1033
1034 /* *INDENT-OFF* */
1035 hash_foreach_mem(key, value.as_u64, im->tun6_protect_by_key,
1036 ({
1037 vlib_cli_output (vm, " %U", format_ipsec6_tunnel_key, key);
1038 vlib_cli_output (vm, " tun:%d sa:%d", value.tun_index, value.sa_index);
1039 }));
1040 /* *INDENT-ON* */
1041 }
1042
1043 return NULL;
1044}
1045
1046/**
1047 * show IPSEC tunnel protection hash tables
1048 */
1049/* *INDENT-OFF* */
1050VLIB_CLI_COMMAND (ipsec_tun_protect_hash_show_node, static) =
1051{
1052 .path = "show ipsec protect-hash",
1053 .function = ipsec_tun_protect_hash_show,
1054 .short_help = "show ipsec protect-hash",
1055};
1056/* *INDENT-ON* */
1057
Ed Warnickecb9cada2015-12-08 15:45:58 -07001058clib_error_t *
1059ipsec_cli_init (vlib_main_t * vm)
1060{
1061 return 0;
1062}
1063
1064VLIB_INIT_FUNCTION (ipsec_cli_init);
1065
Fan Zhangf5395782020-04-29 14:00:03 +01001066static clib_error_t *
1067set_async_mode_command_fn (vlib_main_t * vm, unformat_input_t * input,
1068 vlib_cli_command_t * cmd)
1069{
1070 unformat_input_t _line_input, *line_input = &_line_input;
1071 int async_enable = 0;
1072
1073 if (!unformat_user (input, unformat_line_input, line_input))
1074 return 0;
1075
1076 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1077 {
1078 if (unformat (line_input, "on"))
1079 async_enable = 1;
1080 else if (unformat (line_input, "off"))
1081 async_enable = 0;
1082 else
1083 return (clib_error_return (0, "unknown input '%U'",
1084 format_unformat_error, line_input));
1085 }
1086
1087 vnet_crypto_request_async_mode (async_enable);
1088 ipsec_set_async_mode (async_enable);
1089
1090 unformat_free (line_input);
1091 return (NULL);
1092}
1093
1094/* *INDENT-OFF* */
1095VLIB_CLI_COMMAND (set_async_mode_command, static) = {
1096 .path = "set ipsec async mode",
1097 .short_help = "set ipsec async mode on|off",
1098 .function = set_async_mode_command_fn,
1099};
1100/* *INDENT-ON* */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001101
1102/*
1103 * fd.io coding-style-patch-verification: ON
1104 *
1105 * Local Variables:
1106 * eval: (c-set-style "gnu")
1107 * End:
1108 */