blob: fc79c4ca34788ef1c42e2ea9290ec14c42bb489b [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;
Benoît Gannebdd8b572020-07-28 15:56:15 +020092 u32 m_args = 0;
Neale Ranns041add72020-01-02 04:06:10 +000093 ip_dscp_t dscp;
Ed Warnickecb9cada2015-12-08 15:45:58 -070094
Neale Ranns8dc75c02019-12-10 01:08:19 +000095 salt = 0;
Neale Ranns8d7c5022019-02-06 01:41:05 -080096 error = NULL;
97 is_add = 0;
98 flags = IPSEC_SA_FLAG_NONE;
99 proto = IPSEC_PROTOCOL_ESP;
Neale Rannse6be7022019-06-04 15:37:34 +0000100 integ_alg = IPSEC_INTEG_ALG_NONE;
101 crypto_alg = IPSEC_CRYPTO_ALG_NONE;
Neale Rannsabc56602020-04-01 09:45:23 +0000102 udp_src = udp_dst = IPSEC_UDP_PORT_NONE;
Neale Ranns041add72020-01-02 04:06:10 +0000103 dscp = IP_DSCP_CS0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700104
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700105 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106 return 0;
107
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700108 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
109 {
Neale Ranns8d7c5022019-02-06 01:41:05 -0800110 if (unformat (line_input, "add %u", &id))
Benoît Gannebdd8b572020-07-28 15:56:15 +0200111 {
112 is_add = 1;
113 m_args |= 1 << 0;
114 }
Neale Ranns8d7c5022019-02-06 01:41:05 -0800115 else if (unformat (line_input, "del %u", &id))
Benoît Gannebdd8b572020-07-28 15:56:15 +0200116 {
117 is_add = 0;
118 m_args |= 1 << 0;
119 }
Neale Ranns8d7c5022019-02-06 01:41:05 -0800120 else if (unformat (line_input, "spi %u", &spi))
Benoît Gannebdd8b572020-07-28 15:56:15 +0200121 m_args |= 1 << 1;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800122 else if (unformat (line_input, "salt 0x%x", &salt))
Neale Ranns80f6fd52019-04-16 02:41:34 +0000123 ;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700124 else if (unformat (line_input, "esp"))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800125 proto = IPSEC_PROTOCOL_ESP;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700126 else if (unformat (line_input, "ah"))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800127 proto = IPSEC_PROTOCOL_AH;
128 else if (unformat (line_input, "crypto-key %U",
129 unformat_ipsec_key, &ck))
130 ;
131 else if (unformat (line_input, "crypto-alg %U",
132 unformat_ipsec_crypto_alg, &crypto_alg))
133 ;
134 else if (unformat (line_input, "integ-key %U", unformat_ipsec_key, &ik))
135 ;
136 else if (unformat (line_input, "integ-alg %U",
137 unformat_ipsec_integ_alg, &integ_alg))
138 ;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700139 else if (unformat (line_input, "tunnel-src %U",
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500140 unformat_ip46_address, &tun_src, IP46_TYPE_ANY))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700141 {
Neale Ranns8d7c5022019-02-06 01:41:05 -0800142 flags |= IPSEC_SA_FLAG_IS_TUNNEL;
143 if (!ip46_address_is_ip4 (&tun_src))
144 flags |= IPSEC_SA_FLAG_IS_TUNNEL_V6;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700145 }
146 else if (unformat (line_input, "tunnel-dst %U",
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500147 unformat_ip46_address, &tun_dst, IP46_TYPE_ANY))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800148 ;
Christian Hopps99975382020-07-17 09:53:18 -0400149 else if (unformat (line_input, "inbound"))
150 flags |= IPSEC_SA_FLAG_IS_INBOUND;
151 else if (unformat (line_input, "use-anti-replay"))
152 flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
153 else if (unformat (line_input, "use-esn"))
154 flags |= IPSEC_SA_FLAG_USE_ESN;
Radu Nicolau717de092018-08-03 10:37:24 +0100155 else if (unformat (line_input, "udp-encap"))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800156 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700157 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500158 {
159 error = clib_error_return (0, "parse error: '%U'",
160 format_unformat_error, line_input);
161 goto done;
162 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700163 }
Christian Hopps99975382020-07-17 09:53:18 -0400164 if ((flags & IPSEC_SA_FLAG_IS_INBOUND)
165 && !(flags & IPSEC_SA_FLAG_IS_TUNNEL))
166 {
167 error = clib_error_return (0, "inbound specified on non-tunnel SA");
168 goto done;
169 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700170
Benoît Gannebdd8b572020-07-28 15:56:15 +0200171 if (!(m_args & 1))
172 {
173 error = clib_error_return (0, "missing id");
174 goto done;
175 }
176
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000177 if (is_add)
Benoît Gannebdd8b572020-07-28 15:56:15 +0200178 {
179 if (!(m_args & 2))
180 {
181 error = clib_error_return (0, "missing spi");
182 goto done;
183 }
184 rv = ipsec_sa_add_and_lock (id, spi, proto, crypto_alg,
185 &ck, integ_alg, &ik, flags,
186 0, clib_host_to_net_u32 (salt),
Neale Ranns041add72020-01-02 04:06:10 +0000187 &tun_src, &tun_dst,
188 TUNNEL_ENCAP_DECAP_FLAG_NONE, dscp,
189 &sai, udp_src, udp_dst);
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 Rannse6be7022019-06-04 15:37:34 +0000197 error = clib_error_return (0, "failed");
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 *
215ipsec_spd_add_del_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700216 unformat_input_t * input,
217 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700218{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700219 unformat_input_t _line_input, *line_input = &_line_input;
Damjan Marion3f54b182016-08-16 11:27:02 +0200220 u32 spd_id = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700221 int is_add = ~0;
Billy McFalla9a20e72017-02-15 11:39:12 -0500222 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700223
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700224 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700225 return 0;
226
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700227 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
228 {
229 if (unformat (line_input, "add"))
230 is_add = 1;
231 else if (unformat (line_input, "del"))
232 is_add = 0;
233 else if (unformat (line_input, "%u", &spd_id))
234 ;
235 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500236 {
237 error = clib_error_return (0, "parse error: '%U'",
238 format_unformat_error, line_input);
239 goto done;
240 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700241 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242
Damjan Marion3f54b182016-08-16 11:27:02 +0200243 if (spd_id == ~0)
Billy McFalla9a20e72017-02-15 11:39:12 -0500244 {
245 error = clib_error_return (0, "please specify SPD ID");
246 goto done;
247 }
Damjan Marion3f54b182016-08-16 11:27:02 +0200248
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700249 ipsec_add_del_spd (vm, spd_id, is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700250
Billy McFalla9a20e72017-02-15 11:39:12 -0500251done:
252 unformat_free (line_input);
253
254 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700255}
256
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700257/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700258VLIB_CLI_COMMAND (ipsec_spd_add_del_command, static) = {
259 .path = "ipsec spd",
260 .short_help =
261 "ipsec spd [add|del] <id>",
262 .function = ipsec_spd_add_del_command_fn,
263};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700264/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700265
266
267static clib_error_t *
268ipsec_policy_add_del_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700269 unformat_input_t * input,
270 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700271{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700272 unformat_input_t _line_input, *line_input = &_line_input;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700273 ipsec_policy_t p;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800274 int rv, is_add = 0;
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000275 u32 tmp, tmp2, stat_index, local_range_set, remote_range_set;
Billy McFalla9a20e72017-02-15 11:39:12 -0500276 clib_error_t *error = NULL;
Neale Ranns9f231d42019-03-19 10:06:00 +0000277 u32 is_outbound;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700278
Dave Barachb7b92992018-10-17 10:38:51 -0400279 clib_memset (&p, 0, sizeof (p));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700280 p.lport.stop = p.rport.stop = ~0;
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000281 remote_range_set = local_range_set = is_outbound = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700282
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700283 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700284 return 0;
285
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700286 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
287 {
288 if (unformat (line_input, "add"))
289 is_add = 1;
290 else if (unformat (line_input, "del"))
291 is_add = 0;
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000292 else if (unformat (line_input, "ip6"))
293 p.is_ipv6 = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700294 else if (unformat (line_input, "spd %u", &p.id))
295 ;
296 else if (unformat (line_input, "inbound"))
Neale Ranns9f231d42019-03-19 10:06:00 +0000297 is_outbound = 0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700298 else if (unformat (line_input, "outbound"))
Neale Ranns9f231d42019-03-19 10:06:00 +0000299 is_outbound = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700300 else if (unformat (line_input, "priority %d", &p.priority))
301 ;
302 else if (unformat (line_input, "protocol %u", &tmp))
303 p.protocol = (u8) tmp;
304 else
305 if (unformat
306 (line_input, "action %U", unformat_ipsec_policy_action,
307 &p.policy))
308 {
309 if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
Billy McFalla9a20e72017-02-15 11:39:12 -0500310 {
311 error = clib_error_return (0, "unsupported action: 'resolve'");
312 goto done;
313 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700314 }
315 else if (unformat (line_input, "sa %u", &p.sa_id))
316 ;
317 else if (unformat (line_input, "local-ip-range %U - %U",
318 unformat_ip4_address, &p.laddr.start.ip4,
319 unformat_ip4_address, &p.laddr.stop.ip4))
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000320 local_range_set = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700321 else if (unformat (line_input, "remote-ip-range %U - %U",
322 unformat_ip4_address, &p.raddr.start.ip4,
323 unformat_ip4_address, &p.raddr.stop.ip4))
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000324 remote_range_set = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700325 else if (unformat (line_input, "local-ip-range %U - %U",
326 unformat_ip6_address, &p.laddr.start.ip6,
327 unformat_ip6_address, &p.laddr.stop.ip6))
328 {
329 p.is_ipv6 = 1;
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000330 local_range_set = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700331 }
332 else if (unformat (line_input, "remote-ip-range %U - %U",
333 unformat_ip6_address, &p.raddr.start.ip6,
334 unformat_ip6_address, &p.raddr.stop.ip6))
335 {
336 p.is_ipv6 = 1;
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000337 remote_range_set = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700338 }
339 else if (unformat (line_input, "local-port-range %u - %u", &tmp, &tmp2))
340 {
341 p.lport.start = tmp;
342 p.lport.stop = tmp2;
343 }
344 else
345 if (unformat (line_input, "remote-port-range %u - %u", &tmp, &tmp2))
346 {
347 p.rport.start = tmp;
348 p.rport.stop = tmp2;
349 }
350 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500351 {
352 error = clib_error_return (0, "parse error: '%U'",
353 format_unformat_error, line_input);
354 goto done;
355 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700356 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700357
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000358 if (!remote_range_set)
359 {
360 if (p.is_ipv6)
361 clib_memset (&p.raddr.stop.ip6, 0xff, 16);
362 else
363 clib_memset (&p.raddr.stop.ip4, 0xff, 4);
364 }
365 if (!local_range_set)
366 {
367 if (p.is_ipv6)
368 clib_memset (&p.laddr.stop.ip6, 0xff, 16);
369 else
370 clib_memset (&p.laddr.stop.ip4, 0xff, 4);
371 }
372
Neale Ranns9f231d42019-03-19 10:06:00 +0000373 rv = ipsec_policy_mk_type (is_outbound, p.is_ipv6, p.policy, &p.type);
374
375 if (rv)
376 {
377 error = clib_error_return (0, "unsupported policy type for:",
378 " outboud:%s %s action:%U",
379 (is_outbound ? "yes" : "no"),
380 (p.is_ipv6 ? "IPv4" : "IPv6"),
381 format_ipsec_policy_action, p.policy);
382 goto done;
383 }
384
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800385 rv = ipsec_add_del_policy (vm, &p, is_add, &stat_index);
386
387 if (!rv)
388 vlib_cli_output (vm, "policy-index:%d", stat_index);
389 else
390 vlib_cli_output (vm, "error:%d", rv);
Billy McFalla9a20e72017-02-15 11:39:12 -0500391
392done:
393 unformat_free (line_input);
394
395 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700396}
397
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700398/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700399VLIB_CLI_COMMAND (ipsec_policy_add_del_command, static) = {
400 .path = "ipsec policy",
401 .short_help =
402 "ipsec policy [add|del] spd <id> priority <n> ",
403 .function = ipsec_policy_add_del_command_fn,
404};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700405/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700406
Neale Rannsb294f102019-04-03 13:17:50 +0000407static void
Neale Ranns670027a2019-08-27 12:47:17 +0000408ipsec_sa_show_all (vlib_main_t * vm, ipsec_main_t * im, u8 detail)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700409{
Neale Rannsb294f102019-04-03 13:17:50 +0000410 u32 sai;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700411
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700412 /* *INDENT-OFF* */
Neale Ranns8d7c5022019-02-06 01:41:05 -0800413 pool_foreach_index (sai, im->sad, ({
Neale Ranns670027a2019-08-27 12:47:17 +0000414 vlib_cli_output(vm, "%U", format_ipsec_sa, sai,
415 (detail ? IPSEC_FORMAT_DETAIL : IPSEC_FORMAT_BRIEF));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700416 }));
Neale Rannsb294f102019-04-03 13:17:50 +0000417 /* *INDENT-ON* */
418}
419
420static void
421ipsec_spd_show_all (vlib_main_t * vm, ipsec_main_t * im)
422{
423 u32 spdi;
424
425 /* *INDENT-OFF* */
426 pool_foreach_index (spdi, im->spds, ({
427 vlib_cli_output(vm, "%U", format_ipsec_spd, spdi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700428 }));
Neale Rannsb294f102019-04-03 13:17:50 +0000429 /* *INDENT-ON* */
430}
431
432static void
433ipsec_spd_bindings_show_all (vlib_main_t * vm, ipsec_main_t * im)
434{
435 u32 spd_id, sw_if_index;
Neale Rannsd83c4a82019-06-14 06:48:27 -0700436 ipsec_spd_t *spd;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700437
Neale Ranns311124e2019-01-24 04:52:25 -0800438 vlib_cli_output (vm, "SPD Bindings:");
Neale Ranns8d7c5022019-02-06 01:41:05 -0800439
Neale Rannsb294f102019-04-03 13:17:50 +0000440 /* *INDENT-OFF* */
Neale Ranns311124e2019-01-24 04:52:25 -0800441 hash_foreach(sw_if_index, spd_id, im->spd_index_by_sw_if_index, ({
Neale Rannsd83c4a82019-06-14 06:48:27 -0700442 spd = pool_elt_at_index (im->spds, spd_id);
443 vlib_cli_output (vm, " %d -> %U", spd->id,
Neale Ranns8d7c5022019-02-06 01:41:05 -0800444 format_vnet_sw_if_index_name, im->vnet_main,
445 sw_if_index);
Neale Ranns311124e2019-01-24 04:52:25 -0800446 }));
447 /* *INDENT-ON* */
Neale Rannsb294f102019-04-03 13:17:50 +0000448}
Neale Ranns311124e2019-01-24 04:52:25 -0800449
Neale Ranns12989b52019-09-26 16:20:19 +0000450static walk_rc_t
451ipsec_tun_protect_show_one (index_t itpi, void *ctx)
Neale Rannsb294f102019-04-03 13:17:50 +0000452{
Neale Ranns28287212019-12-16 00:53:11 +0000453 vlib_cli_output (ctx, "%U", format_ipsec_tun_protect_index, itpi);
Neale Rannsb294f102019-04-03 13:17:50 +0000454
Neale Ranns12989b52019-09-26 16:20:19 +0000455 return (WALK_CONTINUE);
456}
457
458static void
459ipsec_tunnel_show_all (vlib_main_t * vm)
460{
461 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
Neale Rannsb294f102019-04-03 13:17:50 +0000462}
463
464static clib_error_t *
465show_ipsec_command_fn (vlib_main_t * vm,
466 unformat_input_t * input, vlib_cli_command_t * cmd)
467{
468 ipsec_main_t *im = &ipsec_main;
469
Neale Ranns670027a2019-08-27 12:47:17 +0000470 ipsec_sa_show_all (vm, im, 0);
Neale Rannsb294f102019-04-03 13:17:50 +0000471 ipsec_spd_show_all (vm, im);
472 ipsec_spd_bindings_show_all (vm, im);
Neale Ranns12989b52019-09-26 16:20:19 +0000473 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
Neale Rannsb294f102019-04-03 13:17:50 +0000474
Fan Zhangf5395782020-04-29 14:00:03 +0100475 vlib_cli_output (vm, "IPSec async mode: %s",
476 (im->async_mode ? "on" : "off"));
477
Ed Warnickecb9cada2015-12-08 15:45:58 -0700478 return 0;
479}
480
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700481/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700482VLIB_CLI_COMMAND (show_ipsec_command, static) = {
Neale Rannsb294f102019-04-03 13:17:50 +0000483 .path = "show ipsec all",
484 .short_help = "show ipsec all",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700485 .function = show_ipsec_command_fn,
486};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700487/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700488
489static clib_error_t *
Neale Rannsb294f102019-04-03 13:17:50 +0000490show_ipsec_sa_command_fn (vlib_main_t * vm,
491 unformat_input_t * input, vlib_cli_command_t * cmd)
492{
493 ipsec_main_t *im = &ipsec_main;
494 u32 sai = ~0;
Neale Ranns670027a2019-08-27 12:47:17 +0000495 u8 detail = 0;
Neale Rannsb294f102019-04-03 13:17:50 +0000496
497 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
498 {
499 if (unformat (input, "%u", &sai))
500 ;
Neale Ranns670027a2019-08-27 12:47:17 +0000501 if (unformat (input, "detail"))
502 detail = 1;
Neale Rannsb294f102019-04-03 13:17:50 +0000503 else
504 break;
505 }
506
507 if (~0 == sai)
Neale Ranns670027a2019-08-27 12:47:17 +0000508 ipsec_sa_show_all (vm, im, detail);
Neale Rannsb294f102019-04-03 13:17:50 +0000509 else
Christian E. Hopps01d61e72019-09-27 14:43:22 -0400510 vlib_cli_output (vm, "%U", format_ipsec_sa, sai,
511 IPSEC_FORMAT_DETAIL | IPSEC_FORMAT_INSECURE);
Neale Rannsb294f102019-04-03 13:17:50 +0000512
513 return 0;
514}
515
Neale Rannsc87b66c2019-02-07 07:26:12 -0800516static clib_error_t *
517clear_ipsec_sa_command_fn (vlib_main_t * vm,
518 unformat_input_t * input, vlib_cli_command_t * cmd)
519{
520 ipsec_main_t *im = &ipsec_main;
521 u32 sai = ~0;
522
523 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
524 {
525 if (unformat (input, "%u", &sai))
526 ;
527 else
528 break;
529 }
530
531 if (~0 == sai)
532 {
533 /* *INDENT-OFF* */
534 pool_foreach_index (sai, im->sad, ({
535 ipsec_sa_clear(sai);
536 }));
537 /* *INDENT-ON* */
538 }
539 else
540 {
541 if (pool_is_free_index (im->sad, sai))
542 return clib_error_return (0, "unknown SA index: %d", sai);
543 else
544 ipsec_sa_clear (sai);
545 }
546
547 return 0;
548}
549
Neale Rannsb294f102019-04-03 13:17:50 +0000550/* *INDENT-OFF* */
551VLIB_CLI_COMMAND (show_ipsec_sa_command, static) = {
552 .path = "show ipsec sa",
553 .short_help = "show ipsec sa [index]",
554 .function = show_ipsec_sa_command_fn,
555};
Neale Rannsc87b66c2019-02-07 07:26:12 -0800556
557VLIB_CLI_COMMAND (clear_ipsec_sa_command, static) = {
558 .path = "clear ipsec sa",
559 .short_help = "clear ipsec sa [index]",
560 .function = clear_ipsec_sa_command_fn,
561};
Neale Rannsb294f102019-04-03 13:17:50 +0000562/* *INDENT-ON* */
563
564static clib_error_t *
565show_ipsec_spd_command_fn (vlib_main_t * vm,
566 unformat_input_t * input, vlib_cli_command_t * cmd)
567{
568 ipsec_main_t *im = &ipsec_main;
569 u8 show_bindings = 0;
570 u32 spdi = ~0;
571
572 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
573 {
574 if (unformat (input, "%u", &spdi))
575 ;
576 else if (unformat (input, "bindings"))
577 show_bindings = 1;
578 else
579 break;
580 }
581
582 if (show_bindings)
583 ipsec_spd_bindings_show_all (vm, im);
584 else if (~0 != spdi)
585 vlib_cli_output (vm, "%U", format_ipsec_spd, spdi);
586 else
587 ipsec_spd_show_all (vm, im);
588
589 return 0;
590}
591
592/* *INDENT-OFF* */
593VLIB_CLI_COMMAND (show_ipsec_spd_command, static) = {
594 .path = "show ipsec spd",
595 .short_help = "show ipsec spd [index]",
596 .function = show_ipsec_spd_command_fn,
597};
598/* *INDENT-ON* */
599
600static clib_error_t *
601show_ipsec_tunnel_command_fn (vlib_main_t * vm,
602 unformat_input_t * input,
603 vlib_cli_command_t * cmd)
604{
Neale Ranns12989b52019-09-26 16:20:19 +0000605 ipsec_tunnel_show_all (vm);
Neale Rannsb294f102019-04-03 13:17:50 +0000606
607 return 0;
608}
609
610/* *INDENT-OFF* */
611VLIB_CLI_COMMAND (show_ipsec_tunnel_command, static) = {
612 .path = "show ipsec tunnel",
Neale Ranns12989b52019-09-26 16:20:19 +0000613 .short_help = "show ipsec tunnel",
Neale Rannsb294f102019-04-03 13:17:50 +0000614 .function = show_ipsec_tunnel_command_fn,
615};
616/* *INDENT-ON* */
617
618static clib_error_t *
Klement Sekerab4d30532018-11-08 13:00:02 +0100619ipsec_show_backends_command_fn (vlib_main_t * vm,
620 unformat_input_t * input,
621 vlib_cli_command_t * cmd)
622{
623 ipsec_main_t *im = &ipsec_main;
624 u32 verbose = 0;
625
626 (void) unformat (input, "verbose %u", &verbose);
627
628 vlib_cli_output (vm, "IPsec AH backends available:");
629 u8 *s = format (NULL, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
630 ipsec_ah_backend_t *ab;
631 /* *INDENT-OFF* */
632 pool_foreach (ab, im->ah_backends, {
633 s = format (s, "%=25s %=25u %=10s\n", ab->name, ab - im->ah_backends,
634 ab - im->ah_backends == im->ah_current_backend ? "yes" : "no");
635 if (verbose) {
636 vlib_node_t *n;
637 n = vlib_get_node (vm, ab->ah4_encrypt_node_index);
638 s = format (s, " enc4 %s (next %d)\n", n->name, ab->ah4_encrypt_next_index);
639 n = vlib_get_node (vm, ab->ah4_decrypt_node_index);
640 s = format (s, " dec4 %s (next %d)\n", n->name, ab->ah4_decrypt_next_index);
641 n = vlib_get_node (vm, ab->ah6_encrypt_node_index);
642 s = format (s, " enc6 %s (next %d)\n", n->name, ab->ah6_encrypt_next_index);
643 n = vlib_get_node (vm, ab->ah6_decrypt_node_index);
644 s = format (s, " dec6 %s (next %d)\n", n->name, ab->ah6_decrypt_next_index);
645 }
646 });
647 /* *INDENT-ON* */
648 vlib_cli_output (vm, "%v", s);
649 _vec_len (s) = 0;
650 vlib_cli_output (vm, "IPsec ESP backends available:");
651 s = format (s, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
652 ipsec_esp_backend_t *eb;
653 /* *INDENT-OFF* */
654 pool_foreach (eb, im->esp_backends, {
655 s = format (s, "%=25s %=25u %=10s\n", eb->name, eb - im->esp_backends,
656 eb - im->esp_backends == im->esp_current_backend ? "yes"
657 : "no");
658 if (verbose) {
659 vlib_node_t *n;
660 n = vlib_get_node (vm, eb->esp4_encrypt_node_index);
661 s = format (s, " enc4 %s (next %d)\n", n->name, eb->esp4_encrypt_next_index);
662 n = vlib_get_node (vm, eb->esp4_decrypt_node_index);
663 s = format (s, " dec4 %s (next %d)\n", n->name, eb->esp4_decrypt_next_index);
664 n = vlib_get_node (vm, eb->esp6_encrypt_node_index);
665 s = format (s, " enc6 %s (next %d)\n", n->name, eb->esp6_encrypt_next_index);
666 n = vlib_get_node (vm, eb->esp6_decrypt_node_index);
667 s = format (s, " dec6 %s (next %d)\n", n->name, eb->esp6_decrypt_next_index);
668 }
669 });
670 /* *INDENT-ON* */
671 vlib_cli_output (vm, "%v", s);
672
673 vec_free (s);
674 return 0;
675}
676
677/* *INDENT-OFF* */
678VLIB_CLI_COMMAND (ipsec_show_backends_command, static) = {
679 .path = "show ipsec backends",
680 .short_help = "show ipsec backends",
681 .function = ipsec_show_backends_command_fn,
682};
683/* *INDENT-ON* */
684
685static clib_error_t *
686ipsec_select_backend_command_fn (vlib_main_t * vm,
687 unformat_input_t * input,
688 vlib_cli_command_t * cmd)
689{
Klement Sekerab4d30532018-11-08 13:00:02 +0100690 unformat_input_t _line_input, *line_input = &_line_input;
Neale Rannse8915fc2019-04-23 20:57:55 -0400691 ipsec_main_t *im = &ipsec_main;
692 clib_error_t *error;
693 u32 backend_index;
694
695 error = ipsec_rsc_in_use (im);
696
697 if (error)
698 return error;
699
Klement Sekerab4d30532018-11-08 13:00:02 +0100700 /* Get a line of input. */
701 if (!unformat_user (input, unformat_line_input, line_input))
702 return 0;
703
704 if (unformat (line_input, "ah"))
705 {
706 if (unformat (line_input, "%u", &backend_index))
707 {
708 if (ipsec_select_ah_backend (im, backend_index) < 0)
709 {
710 return clib_error_return (0, "Invalid AH backend index `%u'",
711 backend_index);
712 }
713 }
714 else
715 {
716 return clib_error_return (0, "Invalid backend index `%U'",
717 format_unformat_error, line_input);
718 }
719 }
720 else if (unformat (line_input, "esp"))
721 {
722 if (unformat (line_input, "%u", &backend_index))
723 {
724 if (ipsec_select_esp_backend (im, backend_index) < 0)
725 {
726 return clib_error_return (0, "Invalid ESP backend index `%u'",
727 backend_index);
728 }
729 }
730 else
731 {
732 return clib_error_return (0, "Invalid backend index `%U'",
733 format_unformat_error, line_input);
734 }
735 }
736 else
737 {
738 return clib_error_return (0, "Unknown input `%U'",
739 format_unformat_error, line_input);
740 }
741
742 return 0;
743}
744
745/* *INDENT-OFF* */
746VLIB_CLI_COMMAND (ipsec_select_backend_command, static) = {
747 .path = "ipsec select backend",
748 .short_help = "ipsec select backend <ah|esp> <backend index>",
749 .function = ipsec_select_backend_command_fn,
750};
751
752/* *INDENT-ON* */
753
754static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700755clear_ipsec_counters_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700756 unformat_input_t * input,
757 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700758{
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800759 vlib_clear_combined_counters (&ipsec_spd_policy_counters);
Neale Rannseba31ec2019-02-17 18:04:27 +0000760 vlib_clear_combined_counters (&ipsec_sa_counters);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700761
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800762 return (NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700763}
764
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700765/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700766VLIB_CLI_COMMAND (clear_ipsec_counters_command, static) = {
767 .path = "clear ipsec counters",
768 .short_help = "clear ipsec counters",
769 .function = clear_ipsec_counters_command_fn,
770};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700771/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700772
Neale Ranns12989b52019-09-26 16:20:19 +0000773static u32
774ipsec_tun_mk_local_sa_id (u32 ti)
775{
776 return (0x80000000 | ti);
777}
778
779static u32
780ipsec_tun_mk_remote_sa_id (u32 ti)
781{
782 return (0xc0000000 | ti);
783}
784
Ed Warnickecb9cada2015-12-08 15:45:58 -0700785static clib_error_t *
786create_ipsec_tunnel_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700787 unformat_input_t * input,
788 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700789{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700790 unformat_input_t _line_input, *line_input = &_line_input;
Neale Ranns12989b52019-09-26 16:20:19 +0000791 ip46_address_t local_ip = ip46_address_initializer;
792 ip46_address_t remote_ip = ip46_address_initializer;
Neale Ranns28287212019-12-16 00:53:11 +0000793 ip_address_t nh = IP_ADDRESS_V4_ALL_0S;
Damjan Marion68449852020-03-16 17:53:38 +0100794 ipsec_crypto_alg_t crypto_alg = IPSEC_CRYPTO_ALG_NONE;
795 ipsec_integ_alg_t integ_alg = IPSEC_INTEG_ALG_NONE;
Neale Ranns12989b52019-09-26 16:20:19 +0000796 ipsec_sa_flags_t flags;
Benoît Gannebdd8b572020-07-28 15:56:15 +0200797 u32 local_spi, remote_spi, salt = 0, table_id, fib_index;
Neale Ranns12989b52019-09-26 16:20:19 +0000798 u32 instance = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700799 int rv;
Benoît Gannebdd8b572020-07-28 15:56:15 +0200800 u32 m_args = 0;
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500801 u8 ipv4_set = 0;
802 u8 ipv6_set = 0;
Neale Ranns12989b52019-09-26 16:20:19 +0000803 u8 is_add = 1;
Billy McFalla9a20e72017-02-15 11:39:12 -0500804 clib_error_t *error = NULL;
Kingwel Xied3d12052019-03-07 06:34:30 -0500805 ipsec_key_t rck = { 0 };
806 ipsec_key_t lck = { 0 };
807 ipsec_key_t lik = { 0 };
808 ipsec_key_t rik = { 0 };
Matthew Smith2838a232016-06-21 16:05:09 -0500809
Neale Ranns12989b52019-09-26 16:20:19 +0000810 table_id = 0;
811 flags = IPSEC_SA_FLAG_NONE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700812
813 /* Get a line of input. */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700814 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700815 return 0;
816
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700817 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
818 {
819 if (unformat
Neale Ranns12989b52019-09-26 16:20:19 +0000820 (line_input, "local-ip %U", unformat_ip46_address, &local_ip,
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500821 IP46_TYPE_ANY))
822 {
Neale Ranns12989b52019-09-26 16:20:19 +0000823 ip46_address_is_ip4 (&local_ip) ? (ipv4_set = 1) : (ipv6_set = 1);
Benoît Gannebdd8b572020-07-28 15:56:15 +0200824 m_args |= 1 << 0;
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500825 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700826 else
827 if (unformat
Neale Ranns12989b52019-09-26 16:20:19 +0000828 (line_input, "remote-ip %U", unformat_ip46_address, &remote_ip,
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500829 IP46_TYPE_ANY))
830 {
Neale Ranns12989b52019-09-26 16:20:19 +0000831 ip46_address_is_ip4 (&remote_ip) ? (ipv4_set = 1) : (ipv6_set = 1);
Benoît Gannebdd8b572020-07-28 15:56:15 +0200832 m_args |= 1 << 1;
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500833 }
Neale Ranns12989b52019-09-26 16:20:19 +0000834 else if (unformat (line_input, "local-spi %u", &local_spi))
Benoît Gannebdd8b572020-07-28 15:56:15 +0200835 m_args |= 1 << 2;
Neale Ranns12989b52019-09-26 16:20:19 +0000836 else if (unformat (line_input, "remote-spi %u", &remote_spi))
Benoît Gannebdd8b572020-07-28 15:56:15 +0200837 m_args |= 1 << 3;
Neale Ranns12989b52019-09-26 16:20:19 +0000838 else if (unformat (line_input, "salt 0x%x", &salt))
Neale Ranns47feb112019-04-11 15:14:07 +0000839 ;
Radu Nicolau717de092018-08-03 10:37:24 +0100840 else if (unformat (line_input, "udp-encap"))
Neale Ranns12989b52019-09-26 16:20:19 +0000841 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
Kingwel Xie2baf9422019-02-04 02:07:06 -0800842 else if (unformat (line_input, "use-esn"))
Neale Ranns12989b52019-09-26 16:20:19 +0000843 flags |= IPSEC_SA_FLAG_USE_ESN;
Kingwel Xie2baf9422019-02-04 02:07:06 -0800844 else if (unformat (line_input, "use-anti-replay"))
Neale Ranns12989b52019-09-26 16:20:19 +0000845 flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
846 else if (unformat (line_input, "instance %u", &instance))
847 ;
848 else if (unformat (line_input, "tx-table %u", &table_id))
Pierre Pfister4c422f92018-12-10 11:19:08 +0100849 ;
Neale Rannsfd060842019-03-04 13:44:42 +0000850 else
851 if (unformat
852 (line_input, "local-crypto-key %U", unformat_ipsec_key, &lck))
853 ;
854 else
855 if (unformat
856 (line_input, "remote-crypto-key %U", unformat_ipsec_key, &rck))
857 ;
858 else if (unformat (line_input, "crypto-alg %U",
Neale Ranns12989b52019-09-26 16:20:19 +0000859 unformat_ipsec_crypto_alg, &crypto_alg))
Neale Rannsfd060842019-03-04 13:44:42 +0000860 ;
861 else
862 if (unformat
863 (line_input, "local-integ-key %U", unformat_ipsec_key, &lik))
864 ;
865 else
866 if (unformat
Simon Zhange8e950a2019-04-23 23:04:07 +0800867 (line_input, "remote-integ-key %U", unformat_ipsec_key, &rik))
Neale Rannsfd060842019-03-04 13:44:42 +0000868 ;
869 else if (unformat (line_input, "integ-alg %U",
Neale Ranns12989b52019-09-26 16:20:19 +0000870 unformat_ipsec_integ_alg, &integ_alg))
Neale Rannsfd060842019-03-04 13:44:42 +0000871 ;
Kingwel Xie2baf9422019-02-04 02:07:06 -0800872 else if (unformat (line_input, "del"))
Neale Ranns12989b52019-09-26 16:20:19 +0000873 is_add = 0;
Neale Ranns28287212019-12-16 00:53:11 +0000874 else if (unformat (line_input, "nh &U", unformat_ip_address, &nh))
875 ;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700876 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500877 {
878 error = clib_error_return (0, "unknown input `%U'",
879 format_unformat_error, line_input);
880 goto done;
881 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700882 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700883
Benoît Gannebdd8b572020-07-28 15:56:15 +0200884 if (0xf != m_args)
Billy McFalla9a20e72017-02-15 11:39:12 -0500885 {
886 error = clib_error_return (0, "mandatory argument(s) missing");
887 goto done;
888 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700889
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500890 if (ipv4_set && ipv6_set)
891 return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
892
Neale Ranns12989b52019-09-26 16:20:19 +0000893 fib_index = fib_table_find (fib_ip_proto (ipv6_set), table_id);
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400894
Neale Ranns12989b52019-09-26 16:20:19 +0000895 if (~0 == fib_index)
896 {
897 rv = VNET_API_ERROR_NO_SUCH_FIB;
898 goto done;
899 }
Neale Rannsfd060842019-03-04 13:44:42 +0000900
Neale Ranns12989b52019-09-26 16:20:19 +0000901 if (is_add)
902 {
903 // remote = input, local = output
904 u32 sw_if_index;
Neale Rannsfd060842019-03-04 13:44:42 +0000905
Neale Ranns12989b52019-09-26 16:20:19 +0000906 /* create an ip-ip tunnel, then the two SA, then bind them */
907 rv =
908 ipip_add_tunnel (ipv6_set ? IPIP_TRANSPORT_IP6 : IPIP_TRANSPORT_IP4,
Neale Ranns95346962019-11-25 13:04:44 +0000909 instance, &local_ip, &remote_ip, fib_index,
Neale Ranns59ff9182019-12-29 23:55:18 +0000910 TUNNEL_ENCAP_DECAP_FLAG_NONE, IP_DSCP_CS0,
Neale Ranns14053c92019-12-29 23:55:18 +0000911 TUNNEL_MODE_P2P, &sw_if_index);
Neale Ranns12989b52019-09-26 16:20:19 +0000912 rv |=
913 ipsec_sa_add_and_lock (ipsec_tun_mk_local_sa_id (sw_if_index),
914 local_spi, IPSEC_PROTOCOL_ESP, crypto_alg,
915 &lck, integ_alg, &lik, flags, table_id,
916 clib_host_to_net_u32 (salt), &local_ip,
Neale Ranns041add72020-01-02 04:06:10 +0000917 &remote_ip, TUNNEL_ENCAP_DECAP_FLAG_NONE,
918 IP_DSCP_CS0, NULL,
919 IPSEC_UDP_PORT_NONE, IPSEC_UDP_PORT_NONE);
Neale Ranns12989b52019-09-26 16:20:19 +0000920 rv |=
921 ipsec_sa_add_and_lock (ipsec_tun_mk_remote_sa_id (sw_if_index),
922 remote_spi, IPSEC_PROTOCOL_ESP, crypto_alg,
923 &rck, integ_alg, &rik,
924 (flags | IPSEC_SA_FLAG_IS_INBOUND), table_id,
925 clib_host_to_net_u32 (salt), &remote_ip,
Neale Ranns041add72020-01-02 04:06:10 +0000926 &local_ip, TUNNEL_ENCAP_DECAP_FLAG_NONE,
927 IP_DSCP_CS0, NULL,
928 IPSEC_UDP_PORT_NONE, IPSEC_UDP_PORT_NONE);
Neale Ranns12989b52019-09-26 16:20:19 +0000929 rv |=
Neale Ranns28287212019-12-16 00:53:11 +0000930 ipsec_tun_protect_update_one (sw_if_index, &nh,
Neale Ranns12989b52019-09-26 16:20:19 +0000931 ipsec_tun_mk_local_sa_id (sw_if_index),
932 ipsec_tun_mk_remote_sa_id
933 (sw_if_index));
934 }
935 else
936 rv = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700937
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700938 switch (rv)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700939 {
940 case 0:
941 break;
942 case VNET_API_ERROR_INVALID_VALUE:
Neale Rannsd14fccd2019-11-15 15:03:27 +0000943 error = clib_error_return (0,
944 "IPSec tunnel interface already exists...");
Billy McFalla9a20e72017-02-15 11:39:12 -0500945 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700946 default:
Billy McFalla9a20e72017-02-15 11:39:12 -0500947 error = clib_error_return (0, "ipsec_register_interface returned %d",
948 rv);
949 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700950 }
951
Billy McFalla9a20e72017-02-15 11:39:12 -0500952done:
953 unformat_free (line_input);
954
955 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700956}
957
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700958/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700959VLIB_CLI_COMMAND (create_ipsec_tunnel_command, static) = {
960 .path = "create ipsec tunnel",
Pierre Pfister4c422f92018-12-10 11:19:08 +0100961 .short_help = "create ipsec tunnel local-ip <addr> local-spi <spi> "
Kingwel Xie2baf9422019-02-04 02:07:06 -0800962 "remote-ip <addr> remote-spi <spi> [instance <inst_num>] [udp-encap] [use-esn] [use-anti-replay] "
Pierre Pfister4c422f92018-12-10 11:19:08 +0100963 "[tx-table <table-id>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700964 .function = create_ipsec_tunnel_command_fn,
965};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700966/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700967
Neale Rannsc87b66c2019-02-07 07:26:12 -0800968static clib_error_t *
969ipsec_tun_protect_cmd (vlib_main_t * vm,
970 unformat_input_t * input, vlib_cli_command_t * cmd)
971{
972 unformat_input_t _line_input, *line_input = &_line_input;
973 u32 sw_if_index, is_del, sa_in, sa_out, *sa_ins = NULL;
Neale Ranns28287212019-12-16 00:53:11 +0000974 ip_address_t peer = { };
Neale Rannsc87b66c2019-02-07 07:26:12 -0800975 vnet_main_t *vnm;
976
977 is_del = 0;
978 sw_if_index = ~0;
979 vnm = vnet_get_main ();
980
981 if (!unformat_user (input, unformat_line_input, line_input))
982 return 0;
983
984 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
985 {
986 if (unformat (line_input, "del"))
987 is_del = 1;
988 else if (unformat (line_input, "add"))
989 is_del = 0;
990 else if (unformat (line_input, "sa-in %d", &sa_in))
991 vec_add1 (sa_ins, sa_in);
992 else if (unformat (line_input, "sa-out %d", &sa_out))
993 ;
994 else if (unformat (line_input, "%U",
995 unformat_vnet_sw_interface, vnm, &sw_if_index))
996 ;
Neale Ranns28287212019-12-16 00:53:11 +0000997 else if (unformat (line_input, "%U", unformat_ip_address, &peer))
998 ;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800999 else
1000 return (clib_error_return (0, "unknown input '%U'",
1001 format_unformat_error, line_input));
1002 }
1003
1004 if (!is_del)
Neale Ranns28287212019-12-16 00:53:11 +00001005 ipsec_tun_protect_update (sw_if_index, &peer, sa_out, sa_ins);
Eric Kinzie609d5792020-10-13 20:02:11 -04001006 else
1007 ipsec_tun_protect_del (sw_if_index, &peer);
Neale Rannsc87b66c2019-02-07 07:26:12 -08001008
1009 unformat_free (line_input);
1010 return NULL;
1011}
1012
1013/**
1014 * Protect tunnel with IPSEC
1015 */
1016/* *INDENT-OFF* */
1017VLIB_CLI_COMMAND (ipsec_tun_protect_cmd_node, static) =
1018{
1019 .path = "ipsec tunnel protect",
1020 .function = ipsec_tun_protect_cmd,
Eric Kinzie609d5792020-10-13 20:02:11 -04001021 .short_help = "ipsec tunnel protect <interface> input-sa <SA> output-sa <SA> [add|del]",
Neale Rannsc87b66c2019-02-07 07:26:12 -08001022 // this is not MP safe
1023};
1024/* *INDENT-ON* */
1025
Neale Rannsc87b66c2019-02-07 07:26:12 -08001026
1027static clib_error_t *
1028ipsec_tun_protect_show (vlib_main_t * vm,
1029 unformat_input_t * input, vlib_cli_command_t * cmd)
1030{
1031 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
1032
1033 return NULL;
1034}
1035
1036/**
1037 * show IPSEC tunnel protection
1038 */
1039/* *INDENT-OFF* */
1040VLIB_CLI_COMMAND (ipsec_tun_protect_show_node, static) =
1041{
1042 .path = "show ipsec protect",
1043 .function = ipsec_tun_protect_show,
1044 .short_help = "show ipsec protect",
1045};
1046/* *INDENT-ON* */
1047
Neale Ranns7b4e52f2020-05-24 16:17:50 +00001048static int
Neale Ranns302b25a2020-10-19 13:23:33 +00001049ipsec_tun_protect4_hash_show_one (clib_bihash_kv_8_16_t * kv, void *arg)
Neale Ranns7b4e52f2020-05-24 16:17:50 +00001050{
1051 ipsec4_tunnel_kv_t *ikv = (ipsec4_tunnel_kv_t *) kv;
1052 vlib_main_t *vm = arg;
1053
1054 vlib_cli_output (vm, " %U", format_ipsec4_tunnel_kv, ikv);
1055
1056 return (BIHASH_WALK_CONTINUE);
1057}
1058
1059static int
Neale Ranns302b25a2020-10-19 13:23:33 +00001060ipsec_tun_protect6_hash_show_one (clib_bihash_kv_24_16_t * kv, void *arg)
Neale Ranns7b4e52f2020-05-24 16:17:50 +00001061{
1062 ipsec6_tunnel_kv_t *ikv = (ipsec6_tunnel_kv_t *) kv;
1063 vlib_main_t *vm = arg;
1064
1065 vlib_cli_output (vm, " %U", format_ipsec6_tunnel_kv, ikv);
1066
1067 return (BIHASH_WALK_CONTINUE);
1068}
1069
Neale Ranns41afb332019-07-16 06:19:35 -07001070static clib_error_t *
1071ipsec_tun_protect_hash_show (vlib_main_t * vm,
1072 unformat_input_t * input,
1073 vlib_cli_command_t * cmd)
1074{
1075 ipsec_main_t *im = &ipsec_main;
1076
1077 {
Neale Ranns41afb332019-07-16 06:19:35 -07001078 vlib_cli_output (vm, "IPv4:");
1079
Neale Ranns302b25a2020-10-19 13:23:33 +00001080 clib_bihash_foreach_key_value_pair_8_16
Neale Ranns7b4e52f2020-05-24 16:17:50 +00001081 (&im->tun4_protect_by_key, ipsec_tun_protect4_hash_show_one, vm);
Neale Ranns41afb332019-07-16 06:19:35 -07001082
1083 vlib_cli_output (vm, "IPv6:");
1084
Neale Ranns302b25a2020-10-19 13:23:33 +00001085 clib_bihash_foreach_key_value_pair_24_16
Neale Ranns7b4e52f2020-05-24 16:17:50 +00001086 (&im->tun6_protect_by_key, ipsec_tun_protect6_hash_show_one, vm);
Neale Ranns41afb332019-07-16 06:19:35 -07001087 }
1088
1089 return NULL;
1090}
1091
1092/**
1093 * show IPSEC tunnel protection hash tables
1094 */
1095/* *INDENT-OFF* */
1096VLIB_CLI_COMMAND (ipsec_tun_protect_hash_show_node, static) =
1097{
1098 .path = "show ipsec protect-hash",
1099 .function = ipsec_tun_protect_hash_show,
1100 .short_help = "show ipsec protect-hash",
1101};
1102/* *INDENT-ON* */
1103
Ed Warnickecb9cada2015-12-08 15:45:58 -07001104clib_error_t *
1105ipsec_cli_init (vlib_main_t * vm)
1106{
1107 return 0;
1108}
1109
1110VLIB_INIT_FUNCTION (ipsec_cli_init);
1111
Fan Zhangf5395782020-04-29 14:00:03 +01001112static clib_error_t *
1113set_async_mode_command_fn (vlib_main_t * vm, unformat_input_t * input,
1114 vlib_cli_command_t * cmd)
1115{
1116 unformat_input_t _line_input, *line_input = &_line_input;
1117 int async_enable = 0;
1118
1119 if (!unformat_user (input, unformat_line_input, line_input))
1120 return 0;
1121
1122 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1123 {
1124 if (unformat (line_input, "on"))
1125 async_enable = 1;
1126 else if (unformat (line_input, "off"))
1127 async_enable = 0;
1128 else
1129 return (clib_error_return (0, "unknown input '%U'",
1130 format_unformat_error, line_input));
1131 }
1132
1133 vnet_crypto_request_async_mode (async_enable);
1134 ipsec_set_async_mode (async_enable);
1135
1136 unformat_free (line_input);
1137 return (NULL);
1138}
1139
1140/* *INDENT-OFF* */
1141VLIB_CLI_COMMAND (set_async_mode_command, static) = {
1142 .path = "set ipsec async mode",
1143 .short_help = "set ipsec async mode on|off",
1144 .function = set_async_mode_command_fn,
1145};
1146/* *INDENT-ON* */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001147
1148/*
1149 * fd.io coding-style-patch-verification: ON
1150 *
1151 * Local Variables:
1152 * eval: (c-set-style "gnu")
1153 * End:
1154 */