blob: 1ab8bd99db3cda889ef8f2a4d0e0d79268e3048d [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 Ranns80f6fd52019-04-16 02:41:34 +000089 u32 id, spi, salt;
Neale Ranns8d7c5022019-02-06 01:41:05 -080090 int is_add, rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -070091
Neale Ranns8dc75c02019-12-10 01:08:19 +000092 salt = 0;
Neale Ranns8d7c5022019-02-06 01:41:05 -080093 error = NULL;
94 is_add = 0;
95 flags = IPSEC_SA_FLAG_NONE;
96 proto = IPSEC_PROTOCOL_ESP;
Neale Rannse6be7022019-06-04 15:37:34 +000097 integ_alg = IPSEC_INTEG_ALG_NONE;
98 crypto_alg = IPSEC_CRYPTO_ALG_NONE;
Ed Warnickecb9cada2015-12-08 15:45:58 -070099
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700100 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700101 return 0;
102
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700103 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
104 {
Neale Ranns8d7c5022019-02-06 01:41:05 -0800105 if (unformat (line_input, "add %u", &id))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700106 is_add = 1;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800107 else if (unformat (line_input, "del %u", &id))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700108 is_add = 0;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800109 else if (unformat (line_input, "spi %u", &spi))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700110 ;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800111 else if (unformat (line_input, "salt 0x%x", &salt))
Neale Ranns80f6fd52019-04-16 02:41:34 +0000112 ;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700113 else if (unformat (line_input, "esp"))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800114 proto = IPSEC_PROTOCOL_ESP;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700115 else if (unformat (line_input, "ah"))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800116 proto = IPSEC_PROTOCOL_AH;
117 else if (unformat (line_input, "crypto-key %U",
118 unformat_ipsec_key, &ck))
119 ;
120 else if (unformat (line_input, "crypto-alg %U",
121 unformat_ipsec_crypto_alg, &crypto_alg))
122 ;
123 else if (unformat (line_input, "integ-key %U", unformat_ipsec_key, &ik))
124 ;
125 else if (unformat (line_input, "integ-alg %U",
126 unformat_ipsec_integ_alg, &integ_alg))
127 ;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700128 else if (unformat (line_input, "tunnel-src %U",
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500129 unformat_ip46_address, &tun_src, IP46_TYPE_ANY))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700130 {
Neale Ranns8d7c5022019-02-06 01:41:05 -0800131 flags |= IPSEC_SA_FLAG_IS_TUNNEL;
132 if (!ip46_address_is_ip4 (&tun_src))
133 flags |= IPSEC_SA_FLAG_IS_TUNNEL_V6;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700134 }
135 else if (unformat (line_input, "tunnel-dst %U",
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500136 unformat_ip46_address, &tun_dst, IP46_TYPE_ANY))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800137 ;
Radu Nicolau717de092018-08-03 10:37:24 +0100138 else if (unformat (line_input, "udp-encap"))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800139 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700140 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500141 {
142 error = clib_error_return (0, "parse error: '%U'",
143 format_unformat_error, line_input);
144 goto done;
145 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700146 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000148 if (is_add)
Neale Ranns495d7ff2019-07-12 09:15:26 +0000149 rv = ipsec_sa_add_and_lock (id, spi, proto, crypto_alg,
150 &ck, integ_alg, &ik, flags,
151 0, clib_host_to_net_u32 (salt),
152 &tun_src, &tun_dst, NULL);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800153 else
Neale Ranns495d7ff2019-07-12 09:15:26 +0000154 rv = ipsec_sa_unlock_id (id);
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000155
Neale Ranns8d7c5022019-02-06 01:41:05 -0800156 if (rv)
Neale Rannse6be7022019-06-04 15:37:34 +0000157 error = clib_error_return (0, "failed");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158
Billy McFalla9a20e72017-02-15 11:39:12 -0500159done:
160 unformat_free (line_input);
161
162 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163}
164
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700165/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166VLIB_CLI_COMMAND (ipsec_sa_add_del_command, static) = {
167 .path = "ipsec sa",
168 .short_help =
169 "ipsec sa [add|del]",
170 .function = ipsec_sa_add_del_command_fn,
171};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700172/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173
174static clib_error_t *
175ipsec_spd_add_del_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700176 unformat_input_t * input,
177 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700179 unformat_input_t _line_input, *line_input = &_line_input;
Damjan Marion3f54b182016-08-16 11:27:02 +0200180 u32 spd_id = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181 int is_add = ~0;
Billy McFalla9a20e72017-02-15 11:39:12 -0500182 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700184 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185 return 0;
186
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700187 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
188 {
189 if (unformat (line_input, "add"))
190 is_add = 1;
191 else if (unformat (line_input, "del"))
192 is_add = 0;
193 else if (unformat (line_input, "%u", &spd_id))
194 ;
195 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500196 {
197 error = clib_error_return (0, "parse error: '%U'",
198 format_unformat_error, line_input);
199 goto done;
200 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700201 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700202
Damjan Marion3f54b182016-08-16 11:27:02 +0200203 if (spd_id == ~0)
Billy McFalla9a20e72017-02-15 11:39:12 -0500204 {
205 error = clib_error_return (0, "please specify SPD ID");
206 goto done;
207 }
Damjan Marion3f54b182016-08-16 11:27:02 +0200208
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700209 ipsec_add_del_spd (vm, spd_id, is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700210
Billy McFalla9a20e72017-02-15 11:39:12 -0500211done:
212 unformat_free (line_input);
213
214 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700215}
216
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700217/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700218VLIB_CLI_COMMAND (ipsec_spd_add_del_command, static) = {
219 .path = "ipsec spd",
220 .short_help =
221 "ipsec spd [add|del] <id>",
222 .function = ipsec_spd_add_del_command_fn,
223};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700224/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700225
226
227static clib_error_t *
228ipsec_policy_add_del_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700229 unformat_input_t * input,
230 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700231{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700232 unformat_input_t _line_input, *line_input = &_line_input;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700233 ipsec_policy_t p;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800234 int rv, is_add = 0;
235 u32 tmp, tmp2, stat_index;
Billy McFalla9a20e72017-02-15 11:39:12 -0500236 clib_error_t *error = NULL;
Neale Ranns9f231d42019-03-19 10:06:00 +0000237 u32 is_outbound;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700238
Dave Barachb7b92992018-10-17 10:38:51 -0400239 clib_memset (&p, 0, sizeof (p));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700240 p.lport.stop = p.rport.stop = ~0;
Neale Ranns9f231d42019-03-19 10:06:00 +0000241 is_outbound = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700243 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700244 return 0;
245
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700246 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
247 {
248 if (unformat (line_input, "add"))
249 is_add = 1;
250 else if (unformat (line_input, "del"))
251 is_add = 0;
252 else if (unformat (line_input, "spd %u", &p.id))
253 ;
254 else if (unformat (line_input, "inbound"))
Neale Ranns9f231d42019-03-19 10:06:00 +0000255 is_outbound = 0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700256 else if (unformat (line_input, "outbound"))
Neale Ranns9f231d42019-03-19 10:06:00 +0000257 is_outbound = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700258 else if (unformat (line_input, "priority %d", &p.priority))
259 ;
260 else if (unformat (line_input, "protocol %u", &tmp))
261 p.protocol = (u8) tmp;
262 else
263 if (unformat
264 (line_input, "action %U", unformat_ipsec_policy_action,
265 &p.policy))
266 {
267 if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
Billy McFalla9a20e72017-02-15 11:39:12 -0500268 {
269 error = clib_error_return (0, "unsupported action: 'resolve'");
270 goto done;
271 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700272 }
273 else if (unformat (line_input, "sa %u", &p.sa_id))
274 ;
275 else if (unformat (line_input, "local-ip-range %U - %U",
276 unformat_ip4_address, &p.laddr.start.ip4,
277 unformat_ip4_address, &p.laddr.stop.ip4))
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800278 ;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700279 else if (unformat (line_input, "remote-ip-range %U - %U",
280 unformat_ip4_address, &p.raddr.start.ip4,
281 unformat_ip4_address, &p.raddr.stop.ip4))
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800282 ;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700283 else if (unformat (line_input, "local-ip-range %U - %U",
284 unformat_ip6_address, &p.laddr.start.ip6,
285 unformat_ip6_address, &p.laddr.stop.ip6))
286 {
287 p.is_ipv6 = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700288 }
289 else if (unformat (line_input, "remote-ip-range %U - %U",
290 unformat_ip6_address, &p.raddr.start.ip6,
291 unformat_ip6_address, &p.raddr.stop.ip6))
292 {
293 p.is_ipv6 = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700294 }
295 else if (unformat (line_input, "local-port-range %u - %u", &tmp, &tmp2))
296 {
297 p.lport.start = tmp;
298 p.lport.stop = tmp2;
299 }
300 else
301 if (unformat (line_input, "remote-port-range %u - %u", &tmp, &tmp2))
302 {
303 p.rport.start = tmp;
304 p.rport.stop = tmp2;
305 }
306 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500307 {
308 error = clib_error_return (0, "parse error: '%U'",
309 format_unformat_error, line_input);
310 goto done;
311 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700312 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700313
Neale Ranns9f231d42019-03-19 10:06:00 +0000314 rv = ipsec_policy_mk_type (is_outbound, p.is_ipv6, p.policy, &p.type);
315
316 if (rv)
317 {
318 error = clib_error_return (0, "unsupported policy type for:",
319 " outboud:%s %s action:%U",
320 (is_outbound ? "yes" : "no"),
321 (p.is_ipv6 ? "IPv4" : "IPv6"),
322 format_ipsec_policy_action, p.policy);
323 goto done;
324 }
325
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800326 rv = ipsec_add_del_policy (vm, &p, is_add, &stat_index);
327
328 if (!rv)
329 vlib_cli_output (vm, "policy-index:%d", stat_index);
330 else
331 vlib_cli_output (vm, "error:%d", rv);
Billy McFalla9a20e72017-02-15 11:39:12 -0500332
333done:
334 unformat_free (line_input);
335
336 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700337}
338
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700339/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700340VLIB_CLI_COMMAND (ipsec_policy_add_del_command, static) = {
341 .path = "ipsec policy",
342 .short_help =
343 "ipsec policy [add|del] spd <id> priority <n> ",
344 .function = ipsec_policy_add_del_command_fn,
345};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700346/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700347
Neale Rannsb294f102019-04-03 13:17:50 +0000348static void
Neale Ranns670027a2019-08-27 12:47:17 +0000349ipsec_sa_show_all (vlib_main_t * vm, ipsec_main_t * im, u8 detail)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700350{
Neale Rannsb294f102019-04-03 13:17:50 +0000351 u32 sai;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700352
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700353 /* *INDENT-OFF* */
Neale Ranns8d7c5022019-02-06 01:41:05 -0800354 pool_foreach_index (sai, im->sad, ({
Neale Ranns670027a2019-08-27 12:47:17 +0000355 vlib_cli_output(vm, "%U", format_ipsec_sa, sai,
356 (detail ? IPSEC_FORMAT_DETAIL : IPSEC_FORMAT_BRIEF));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700357 }));
Neale Rannsb294f102019-04-03 13:17:50 +0000358 /* *INDENT-ON* */
359}
360
361static void
362ipsec_spd_show_all (vlib_main_t * vm, ipsec_main_t * im)
363{
364 u32 spdi;
365
366 /* *INDENT-OFF* */
367 pool_foreach_index (spdi, im->spds, ({
368 vlib_cli_output(vm, "%U", format_ipsec_spd, spdi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700369 }));
Neale Rannsb294f102019-04-03 13:17:50 +0000370 /* *INDENT-ON* */
371}
372
373static void
374ipsec_spd_bindings_show_all (vlib_main_t * vm, ipsec_main_t * im)
375{
376 u32 spd_id, sw_if_index;
Neale Rannsd83c4a82019-06-14 06:48:27 -0700377 ipsec_spd_t *spd;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700378
Neale Ranns311124e2019-01-24 04:52:25 -0800379 vlib_cli_output (vm, "SPD Bindings:");
Neale Ranns8d7c5022019-02-06 01:41:05 -0800380
Neale Rannsb294f102019-04-03 13:17:50 +0000381 /* *INDENT-OFF* */
Neale Ranns311124e2019-01-24 04:52:25 -0800382 hash_foreach(sw_if_index, spd_id, im->spd_index_by_sw_if_index, ({
Neale Rannsd83c4a82019-06-14 06:48:27 -0700383 spd = pool_elt_at_index (im->spds, spd_id);
384 vlib_cli_output (vm, " %d -> %U", spd->id,
Neale Ranns8d7c5022019-02-06 01:41:05 -0800385 format_vnet_sw_if_index_name, im->vnet_main,
386 sw_if_index);
Neale Ranns311124e2019-01-24 04:52:25 -0800387 }));
388 /* *INDENT-ON* */
Neale Rannsb294f102019-04-03 13:17:50 +0000389}
Neale Ranns311124e2019-01-24 04:52:25 -0800390
Neale Ranns12989b52019-09-26 16:20:19 +0000391static walk_rc_t
392ipsec_tun_protect_show_one (index_t itpi, void *ctx)
Neale Rannsb294f102019-04-03 13:17:50 +0000393{
Neale Ranns28287212019-12-16 00:53:11 +0000394 vlib_cli_output (ctx, "%U", format_ipsec_tun_protect_index, itpi);
Neale Rannsb294f102019-04-03 13:17:50 +0000395
Neale Ranns12989b52019-09-26 16:20:19 +0000396 return (WALK_CONTINUE);
397}
398
399static void
400ipsec_tunnel_show_all (vlib_main_t * vm)
401{
402 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
Neale Rannsb294f102019-04-03 13:17:50 +0000403}
404
405static clib_error_t *
406show_ipsec_command_fn (vlib_main_t * vm,
407 unformat_input_t * input, vlib_cli_command_t * cmd)
408{
409 ipsec_main_t *im = &ipsec_main;
410
Neale Ranns670027a2019-08-27 12:47:17 +0000411 ipsec_sa_show_all (vm, im, 0);
Neale Rannsb294f102019-04-03 13:17:50 +0000412 ipsec_spd_show_all (vm, im);
413 ipsec_spd_bindings_show_all (vm, im);
Neale Ranns12989b52019-09-26 16:20:19 +0000414 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
Neale Rannsb294f102019-04-03 13:17:50 +0000415
Ed Warnickecb9cada2015-12-08 15:45:58 -0700416 return 0;
417}
418
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700419/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700420VLIB_CLI_COMMAND (show_ipsec_command, static) = {
Neale Rannsb294f102019-04-03 13:17:50 +0000421 .path = "show ipsec all",
422 .short_help = "show ipsec all",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423 .function = show_ipsec_command_fn,
424};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700425/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700426
427static clib_error_t *
Neale Rannsb294f102019-04-03 13:17:50 +0000428show_ipsec_sa_command_fn (vlib_main_t * vm,
429 unformat_input_t * input, vlib_cli_command_t * cmd)
430{
431 ipsec_main_t *im = &ipsec_main;
432 u32 sai = ~0;
Neale Ranns670027a2019-08-27 12:47:17 +0000433 u8 detail = 0;
Neale Rannsb294f102019-04-03 13:17:50 +0000434
435 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
436 {
437 if (unformat (input, "%u", &sai))
438 ;
Neale Ranns670027a2019-08-27 12:47:17 +0000439 if (unformat (input, "detail"))
440 detail = 1;
Neale Rannsb294f102019-04-03 13:17:50 +0000441 else
442 break;
443 }
444
445 if (~0 == sai)
Neale Ranns670027a2019-08-27 12:47:17 +0000446 ipsec_sa_show_all (vm, im, detail);
Neale Rannsb294f102019-04-03 13:17:50 +0000447 else
Christian E. Hopps01d61e72019-09-27 14:43:22 -0400448 vlib_cli_output (vm, "%U", format_ipsec_sa, sai,
449 IPSEC_FORMAT_DETAIL | IPSEC_FORMAT_INSECURE);
Neale Rannsb294f102019-04-03 13:17:50 +0000450
451 return 0;
452}
453
Neale Rannsc87b66c2019-02-07 07:26:12 -0800454static clib_error_t *
455clear_ipsec_sa_command_fn (vlib_main_t * vm,
456 unformat_input_t * input, vlib_cli_command_t * cmd)
457{
458 ipsec_main_t *im = &ipsec_main;
459 u32 sai = ~0;
460
461 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
462 {
463 if (unformat (input, "%u", &sai))
464 ;
465 else
466 break;
467 }
468
469 if (~0 == sai)
470 {
471 /* *INDENT-OFF* */
472 pool_foreach_index (sai, im->sad, ({
473 ipsec_sa_clear(sai);
474 }));
475 /* *INDENT-ON* */
476 }
477 else
478 {
479 if (pool_is_free_index (im->sad, sai))
480 return clib_error_return (0, "unknown SA index: %d", sai);
481 else
482 ipsec_sa_clear (sai);
483 }
484
485 return 0;
486}
487
Neale Rannsb294f102019-04-03 13:17:50 +0000488/* *INDENT-OFF* */
489VLIB_CLI_COMMAND (show_ipsec_sa_command, static) = {
490 .path = "show ipsec sa",
491 .short_help = "show ipsec sa [index]",
492 .function = show_ipsec_sa_command_fn,
493};
Neale Rannsc87b66c2019-02-07 07:26:12 -0800494
495VLIB_CLI_COMMAND (clear_ipsec_sa_command, static) = {
496 .path = "clear ipsec sa",
497 .short_help = "clear ipsec sa [index]",
498 .function = clear_ipsec_sa_command_fn,
499};
Neale Rannsb294f102019-04-03 13:17:50 +0000500/* *INDENT-ON* */
501
502static clib_error_t *
503show_ipsec_spd_command_fn (vlib_main_t * vm,
504 unformat_input_t * input, vlib_cli_command_t * cmd)
505{
506 ipsec_main_t *im = &ipsec_main;
507 u8 show_bindings = 0;
508 u32 spdi = ~0;
509
510 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
511 {
512 if (unformat (input, "%u", &spdi))
513 ;
514 else if (unformat (input, "bindings"))
515 show_bindings = 1;
516 else
517 break;
518 }
519
520 if (show_bindings)
521 ipsec_spd_bindings_show_all (vm, im);
522 else if (~0 != spdi)
523 vlib_cli_output (vm, "%U", format_ipsec_spd, spdi);
524 else
525 ipsec_spd_show_all (vm, im);
526
527 return 0;
528}
529
530/* *INDENT-OFF* */
531VLIB_CLI_COMMAND (show_ipsec_spd_command, static) = {
532 .path = "show ipsec spd",
533 .short_help = "show ipsec spd [index]",
534 .function = show_ipsec_spd_command_fn,
535};
536/* *INDENT-ON* */
537
538static clib_error_t *
539show_ipsec_tunnel_command_fn (vlib_main_t * vm,
540 unformat_input_t * input,
541 vlib_cli_command_t * cmd)
542{
Neale Ranns12989b52019-09-26 16:20:19 +0000543 ipsec_tunnel_show_all (vm);
Neale Rannsb294f102019-04-03 13:17:50 +0000544
545 return 0;
546}
547
548/* *INDENT-OFF* */
549VLIB_CLI_COMMAND (show_ipsec_tunnel_command, static) = {
550 .path = "show ipsec tunnel",
Neale Ranns12989b52019-09-26 16:20:19 +0000551 .short_help = "show ipsec tunnel",
Neale Rannsb294f102019-04-03 13:17:50 +0000552 .function = show_ipsec_tunnel_command_fn,
553};
554/* *INDENT-ON* */
555
556static clib_error_t *
Klement Sekerab4d30532018-11-08 13:00:02 +0100557ipsec_show_backends_command_fn (vlib_main_t * vm,
558 unformat_input_t * input,
559 vlib_cli_command_t * cmd)
560{
561 ipsec_main_t *im = &ipsec_main;
562 u32 verbose = 0;
563
564 (void) unformat (input, "verbose %u", &verbose);
565
566 vlib_cli_output (vm, "IPsec AH backends available:");
567 u8 *s = format (NULL, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
568 ipsec_ah_backend_t *ab;
569 /* *INDENT-OFF* */
570 pool_foreach (ab, im->ah_backends, {
571 s = format (s, "%=25s %=25u %=10s\n", ab->name, ab - im->ah_backends,
572 ab - im->ah_backends == im->ah_current_backend ? "yes" : "no");
573 if (verbose) {
574 vlib_node_t *n;
575 n = vlib_get_node (vm, ab->ah4_encrypt_node_index);
576 s = format (s, " enc4 %s (next %d)\n", n->name, ab->ah4_encrypt_next_index);
577 n = vlib_get_node (vm, ab->ah4_decrypt_node_index);
578 s = format (s, " dec4 %s (next %d)\n", n->name, ab->ah4_decrypt_next_index);
579 n = vlib_get_node (vm, ab->ah6_encrypt_node_index);
580 s = format (s, " enc6 %s (next %d)\n", n->name, ab->ah6_encrypt_next_index);
581 n = vlib_get_node (vm, ab->ah6_decrypt_node_index);
582 s = format (s, " dec6 %s (next %d)\n", n->name, ab->ah6_decrypt_next_index);
583 }
584 });
585 /* *INDENT-ON* */
586 vlib_cli_output (vm, "%v", s);
587 _vec_len (s) = 0;
588 vlib_cli_output (vm, "IPsec ESP backends available:");
589 s = format (s, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
590 ipsec_esp_backend_t *eb;
591 /* *INDENT-OFF* */
592 pool_foreach (eb, im->esp_backends, {
593 s = format (s, "%=25s %=25u %=10s\n", eb->name, eb - im->esp_backends,
594 eb - im->esp_backends == im->esp_current_backend ? "yes"
595 : "no");
596 if (verbose) {
597 vlib_node_t *n;
598 n = vlib_get_node (vm, eb->esp4_encrypt_node_index);
599 s = format (s, " enc4 %s (next %d)\n", n->name, eb->esp4_encrypt_next_index);
600 n = vlib_get_node (vm, eb->esp4_decrypt_node_index);
601 s = format (s, " dec4 %s (next %d)\n", n->name, eb->esp4_decrypt_next_index);
602 n = vlib_get_node (vm, eb->esp6_encrypt_node_index);
603 s = format (s, " enc6 %s (next %d)\n", n->name, eb->esp6_encrypt_next_index);
604 n = vlib_get_node (vm, eb->esp6_decrypt_node_index);
605 s = format (s, " dec6 %s (next %d)\n", n->name, eb->esp6_decrypt_next_index);
606 }
607 });
608 /* *INDENT-ON* */
609 vlib_cli_output (vm, "%v", s);
610
611 vec_free (s);
612 return 0;
613}
614
615/* *INDENT-OFF* */
616VLIB_CLI_COMMAND (ipsec_show_backends_command, static) = {
617 .path = "show ipsec backends",
618 .short_help = "show ipsec backends",
619 .function = ipsec_show_backends_command_fn,
620};
621/* *INDENT-ON* */
622
623static clib_error_t *
624ipsec_select_backend_command_fn (vlib_main_t * vm,
625 unformat_input_t * input,
626 vlib_cli_command_t * cmd)
627{
Klement Sekerab4d30532018-11-08 13:00:02 +0100628 unformat_input_t _line_input, *line_input = &_line_input;
Neale Rannse8915fc2019-04-23 20:57:55 -0400629 ipsec_main_t *im = &ipsec_main;
630 clib_error_t *error;
631 u32 backend_index;
632
633 error = ipsec_rsc_in_use (im);
634
635 if (error)
636 return error;
637
Klement Sekerab4d30532018-11-08 13:00:02 +0100638 /* Get a line of input. */
639 if (!unformat_user (input, unformat_line_input, line_input))
640 return 0;
641
642 if (unformat (line_input, "ah"))
643 {
644 if (unformat (line_input, "%u", &backend_index))
645 {
646 if (ipsec_select_ah_backend (im, backend_index) < 0)
647 {
648 return clib_error_return (0, "Invalid AH backend index `%u'",
649 backend_index);
650 }
651 }
652 else
653 {
654 return clib_error_return (0, "Invalid backend index `%U'",
655 format_unformat_error, line_input);
656 }
657 }
658 else if (unformat (line_input, "esp"))
659 {
660 if (unformat (line_input, "%u", &backend_index))
661 {
662 if (ipsec_select_esp_backend (im, backend_index) < 0)
663 {
664 return clib_error_return (0, "Invalid ESP backend index `%u'",
665 backend_index);
666 }
667 }
668 else
669 {
670 return clib_error_return (0, "Invalid backend index `%U'",
671 format_unformat_error, line_input);
672 }
673 }
674 else
675 {
676 return clib_error_return (0, "Unknown input `%U'",
677 format_unformat_error, line_input);
678 }
679
680 return 0;
681}
682
683/* *INDENT-OFF* */
684VLIB_CLI_COMMAND (ipsec_select_backend_command, static) = {
685 .path = "ipsec select backend",
686 .short_help = "ipsec select backend <ah|esp> <backend index>",
687 .function = ipsec_select_backend_command_fn,
688};
689
690/* *INDENT-ON* */
691
692static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700693clear_ipsec_counters_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700694 unformat_input_t * input,
695 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700696{
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800697 vlib_clear_combined_counters (&ipsec_spd_policy_counters);
Neale Rannseba31ec2019-02-17 18:04:27 +0000698 vlib_clear_combined_counters (&ipsec_sa_counters);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700699
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800700 return (NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700701}
702
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700703/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700704VLIB_CLI_COMMAND (clear_ipsec_counters_command, static) = {
705 .path = "clear ipsec counters",
706 .short_help = "clear ipsec counters",
707 .function = clear_ipsec_counters_command_fn,
708};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700709/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700710
Neale Ranns12989b52019-09-26 16:20:19 +0000711static u32
712ipsec_tun_mk_local_sa_id (u32 ti)
713{
714 return (0x80000000 | ti);
715}
716
717static u32
718ipsec_tun_mk_remote_sa_id (u32 ti)
719{
720 return (0xc0000000 | ti);
721}
722
Ed Warnickecb9cada2015-12-08 15:45:58 -0700723static clib_error_t *
724create_ipsec_tunnel_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700725 unformat_input_t * input,
726 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700727{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700728 unformat_input_t _line_input, *line_input = &_line_input;
Neale Ranns12989b52019-09-26 16:20:19 +0000729 ip46_address_t local_ip = ip46_address_initializer;
730 ip46_address_t remote_ip = ip46_address_initializer;
Neale Ranns28287212019-12-16 00:53:11 +0000731 ip_address_t nh = IP_ADDRESS_V4_ALL_0S;
Neale Ranns12989b52019-09-26 16:20:19 +0000732 ipsec_crypto_alg_t crypto_alg;
733 ipsec_integ_alg_t integ_alg;
734 ipsec_sa_flags_t flags;
735 u32 local_spi, remote_spi, salt, table_id, fib_index;
736 u32 instance = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700737 int rv;
738 u32 num_m_args = 0;
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500739 u8 ipv4_set = 0;
740 u8 ipv6_set = 0;
Neale Ranns12989b52019-09-26 16:20:19 +0000741 u8 is_add = 1;
Billy McFalla9a20e72017-02-15 11:39:12 -0500742 clib_error_t *error = NULL;
Kingwel Xied3d12052019-03-07 06:34:30 -0500743 ipsec_key_t rck = { 0 };
744 ipsec_key_t lck = { 0 };
745 ipsec_key_t lik = { 0 };
746 ipsec_key_t rik = { 0 };
Matthew Smith2838a232016-06-21 16:05:09 -0500747
Neale Ranns12989b52019-09-26 16:20:19 +0000748 table_id = 0;
749 flags = IPSEC_SA_FLAG_NONE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700750
751 /* Get a line of input. */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700752 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700753 return 0;
754
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700755 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
756 {
757 if (unformat
Neale Ranns12989b52019-09-26 16:20:19 +0000758 (line_input, "local-ip %U", unformat_ip46_address, &local_ip,
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500759 IP46_TYPE_ANY))
760 {
Neale Ranns12989b52019-09-26 16:20:19 +0000761 ip46_address_is_ip4 (&local_ip) ? (ipv4_set = 1) : (ipv6_set = 1);
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500762 num_m_args++;
763 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700764 else
765 if (unformat
Neale Ranns12989b52019-09-26 16:20:19 +0000766 (line_input, "remote-ip %U", unformat_ip46_address, &remote_ip,
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500767 IP46_TYPE_ANY))
768 {
Neale Ranns12989b52019-09-26 16:20:19 +0000769 ip46_address_is_ip4 (&remote_ip) ? (ipv4_set = 1) : (ipv6_set = 1);
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500770 num_m_args++;
771 }
Neale Ranns12989b52019-09-26 16:20:19 +0000772 else if (unformat (line_input, "local-spi %u", &local_spi))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700773 num_m_args++;
Neale Ranns12989b52019-09-26 16:20:19 +0000774 else if (unformat (line_input, "remote-spi %u", &remote_spi))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700775 num_m_args++;
Neale Ranns12989b52019-09-26 16:20:19 +0000776 else if (unformat (line_input, "salt 0x%x", &salt))
Neale Ranns47feb112019-04-11 15:14:07 +0000777 ;
Radu Nicolau717de092018-08-03 10:37:24 +0100778 else if (unformat (line_input, "udp-encap"))
Neale Ranns12989b52019-09-26 16:20:19 +0000779 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
Kingwel Xie2baf9422019-02-04 02:07:06 -0800780 else if (unformat (line_input, "use-esn"))
Neale Ranns12989b52019-09-26 16:20:19 +0000781 flags |= IPSEC_SA_FLAG_USE_ESN;
Kingwel Xie2baf9422019-02-04 02:07:06 -0800782 else if (unformat (line_input, "use-anti-replay"))
Neale Ranns12989b52019-09-26 16:20:19 +0000783 flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
784 else if (unformat (line_input, "instance %u", &instance))
785 ;
786 else if (unformat (line_input, "tx-table %u", &table_id))
Pierre Pfister4c422f92018-12-10 11:19:08 +0100787 ;
Neale Rannsfd060842019-03-04 13:44:42 +0000788 else
789 if (unformat
790 (line_input, "local-crypto-key %U", unformat_ipsec_key, &lck))
791 ;
792 else
793 if (unformat
794 (line_input, "remote-crypto-key %U", unformat_ipsec_key, &rck))
795 ;
796 else if (unformat (line_input, "crypto-alg %U",
Neale Ranns12989b52019-09-26 16:20:19 +0000797 unformat_ipsec_crypto_alg, &crypto_alg))
Neale Rannsfd060842019-03-04 13:44:42 +0000798 ;
799 else
800 if (unformat
801 (line_input, "local-integ-key %U", unformat_ipsec_key, &lik))
802 ;
803 else
804 if (unformat
Simon Zhange8e950a2019-04-23 23:04:07 +0800805 (line_input, "remote-integ-key %U", unformat_ipsec_key, &rik))
Neale Rannsfd060842019-03-04 13:44:42 +0000806 ;
807 else if (unformat (line_input, "integ-alg %U",
Neale Ranns12989b52019-09-26 16:20:19 +0000808 unformat_ipsec_integ_alg, &integ_alg))
Neale Rannsfd060842019-03-04 13:44:42 +0000809 ;
Kingwel Xie2baf9422019-02-04 02:07:06 -0800810 else if (unformat (line_input, "del"))
Neale Ranns12989b52019-09-26 16:20:19 +0000811 is_add = 0;
Neale Ranns28287212019-12-16 00:53:11 +0000812 else if (unformat (line_input, "nh &U", unformat_ip_address, &nh))
813 ;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700814 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500815 {
816 error = clib_error_return (0, "unknown input `%U'",
817 format_unformat_error, line_input);
818 goto done;
819 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700820 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700821
822 if (num_m_args < 4)
Billy McFalla9a20e72017-02-15 11:39:12 -0500823 {
824 error = clib_error_return (0, "mandatory argument(s) missing");
825 goto done;
826 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700827
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500828 if (ipv4_set && ipv6_set)
829 return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
830
Neale Ranns12989b52019-09-26 16:20:19 +0000831 fib_index = fib_table_find (fib_ip_proto (ipv6_set), table_id);
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400832
Neale Ranns12989b52019-09-26 16:20:19 +0000833 if (~0 == fib_index)
834 {
835 rv = VNET_API_ERROR_NO_SUCH_FIB;
836 goto done;
837 }
Neale Rannsfd060842019-03-04 13:44:42 +0000838
Neale Ranns12989b52019-09-26 16:20:19 +0000839 if (is_add)
840 {
841 // remote = input, local = output
842 u32 sw_if_index;
Neale Rannsfd060842019-03-04 13:44:42 +0000843
Neale Ranns12989b52019-09-26 16:20:19 +0000844 /* create an ip-ip tunnel, then the two SA, then bind them */
845 rv =
846 ipip_add_tunnel (ipv6_set ? IPIP_TRANSPORT_IP6 : IPIP_TRANSPORT_IP4,
Neale Ranns95346962019-11-25 13:04:44 +0000847 instance, &local_ip, &remote_ip, fib_index,
Neale Ranns59ff9182019-12-29 23:55:18 +0000848 TUNNEL_ENCAP_DECAP_FLAG_NONE, IP_DSCP_CS0,
Neale Ranns14053c92019-12-29 23:55:18 +0000849 TUNNEL_MODE_P2P, &sw_if_index);
Neale Ranns12989b52019-09-26 16:20:19 +0000850 rv |=
851 ipsec_sa_add_and_lock (ipsec_tun_mk_local_sa_id (sw_if_index),
852 local_spi, IPSEC_PROTOCOL_ESP, crypto_alg,
853 &lck, integ_alg, &lik, flags, table_id,
854 clib_host_to_net_u32 (salt), &local_ip,
855 &remote_ip, NULL);
856 rv |=
857 ipsec_sa_add_and_lock (ipsec_tun_mk_remote_sa_id (sw_if_index),
858 remote_spi, IPSEC_PROTOCOL_ESP, crypto_alg,
859 &rck, integ_alg, &rik,
860 (flags | IPSEC_SA_FLAG_IS_INBOUND), table_id,
861 clib_host_to_net_u32 (salt), &remote_ip,
862 &local_ip, NULL);
863 rv |=
Neale Ranns28287212019-12-16 00:53:11 +0000864 ipsec_tun_protect_update_one (sw_if_index, &nh,
Neale Ranns12989b52019-09-26 16:20:19 +0000865 ipsec_tun_mk_local_sa_id (sw_if_index),
866 ipsec_tun_mk_remote_sa_id
867 (sw_if_index));
868 }
869 else
870 rv = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700871
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700872 switch (rv)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700873 {
874 case 0:
875 break;
876 case VNET_API_ERROR_INVALID_VALUE:
Neale Rannsd14fccd2019-11-15 15:03:27 +0000877 error = clib_error_return (0,
878 "IPSec tunnel interface already exists...");
Billy McFalla9a20e72017-02-15 11:39:12 -0500879 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700880 default:
Billy McFalla9a20e72017-02-15 11:39:12 -0500881 error = clib_error_return (0, "ipsec_register_interface returned %d",
882 rv);
883 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700884 }
885
Billy McFalla9a20e72017-02-15 11:39:12 -0500886done:
887 unformat_free (line_input);
888
889 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700890}
891
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700892/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700893VLIB_CLI_COMMAND (create_ipsec_tunnel_command, static) = {
894 .path = "create ipsec tunnel",
Pierre Pfister4c422f92018-12-10 11:19:08 +0100895 .short_help = "create ipsec tunnel local-ip <addr> local-spi <spi> "
Kingwel Xie2baf9422019-02-04 02:07:06 -0800896 "remote-ip <addr> remote-spi <spi> [instance <inst_num>] [udp-encap] [use-esn] [use-anti-replay] "
Pierre Pfister4c422f92018-12-10 11:19:08 +0100897 "[tx-table <table-id>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700898 .function = create_ipsec_tunnel_command_fn,
899};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700900/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700901
Neale Rannsc87b66c2019-02-07 07:26:12 -0800902static clib_error_t *
903ipsec_tun_protect_cmd (vlib_main_t * vm,
904 unformat_input_t * input, vlib_cli_command_t * cmd)
905{
906 unformat_input_t _line_input, *line_input = &_line_input;
907 u32 sw_if_index, is_del, sa_in, sa_out, *sa_ins = NULL;
Neale Ranns28287212019-12-16 00:53:11 +0000908 ip_address_t peer = { };
Neale Rannsc87b66c2019-02-07 07:26:12 -0800909 vnet_main_t *vnm;
910
911 is_del = 0;
912 sw_if_index = ~0;
913 vnm = vnet_get_main ();
914
915 if (!unformat_user (input, unformat_line_input, line_input))
916 return 0;
917
918 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
919 {
920 if (unformat (line_input, "del"))
921 is_del = 1;
922 else if (unformat (line_input, "add"))
923 is_del = 0;
924 else if (unformat (line_input, "sa-in %d", &sa_in))
925 vec_add1 (sa_ins, sa_in);
926 else if (unformat (line_input, "sa-out %d", &sa_out))
927 ;
928 else if (unformat (line_input, "%U",
929 unformat_vnet_sw_interface, vnm, &sw_if_index))
930 ;
Neale Ranns28287212019-12-16 00:53:11 +0000931 else if (unformat (line_input, "%U", unformat_ip_address, &peer))
932 ;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800933 else
934 return (clib_error_return (0, "unknown input '%U'",
935 format_unformat_error, line_input));
936 }
937
938 if (!is_del)
Neale Ranns28287212019-12-16 00:53:11 +0000939 ipsec_tun_protect_update (sw_if_index, &peer, sa_out, sa_ins);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800940
941 unformat_free (line_input);
942 return NULL;
943}
944
945/**
946 * Protect tunnel with IPSEC
947 */
948/* *INDENT-OFF* */
949VLIB_CLI_COMMAND (ipsec_tun_protect_cmd_node, static) =
950{
951 .path = "ipsec tunnel protect",
952 .function = ipsec_tun_protect_cmd,
953 .short_help = "ipsec tunnel protect <interface> input-sa <SA> output-sa <SA>",
954 // this is not MP safe
955};
956/* *INDENT-ON* */
957
Neale Rannsc87b66c2019-02-07 07:26:12 -0800958
959static clib_error_t *
960ipsec_tun_protect_show (vlib_main_t * vm,
961 unformat_input_t * input, vlib_cli_command_t * cmd)
962{
963 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
964
965 return NULL;
966}
967
968/**
969 * show IPSEC tunnel protection
970 */
971/* *INDENT-OFF* */
972VLIB_CLI_COMMAND (ipsec_tun_protect_show_node, static) =
973{
974 .path = "show ipsec protect",
975 .function = ipsec_tun_protect_show,
976 .short_help = "show ipsec protect",
977};
978/* *INDENT-ON* */
979
Neale Ranns41afb332019-07-16 06:19:35 -0700980static clib_error_t *
981ipsec_tun_protect_hash_show (vlib_main_t * vm,
982 unformat_input_t * input,
983 vlib_cli_command_t * cmd)
984{
985 ipsec_main_t *im = &ipsec_main;
986
987 {
988 ipsec_tun_lkup_result_t value;
989 ipsec4_tunnel_key_t key;
990
991 vlib_cli_output (vm, "IPv4:");
992
993 /* *INDENT-OFF* */
994 hash_foreach(key.as_u64, value.as_u64, im->tun4_protect_by_key,
995 ({
996 vlib_cli_output (vm, " %U", format_ipsec4_tunnel_key, &key);
997 vlib_cli_output (vm, " tun:%d sa:%d", value.tun_index, value.sa_index);
998 }));
999 /* *INDENT-ON* */
1000 }
1001
1002 {
1003 ipsec_tun_lkup_result_t value;
1004 ipsec6_tunnel_key_t *key;
1005
1006 vlib_cli_output (vm, "IPv6:");
1007
1008 /* *INDENT-OFF* */
1009 hash_foreach_mem(key, value.as_u64, im->tun6_protect_by_key,
1010 ({
1011 vlib_cli_output (vm, " %U", format_ipsec6_tunnel_key, key);
1012 vlib_cli_output (vm, " tun:%d sa:%d", value.tun_index, value.sa_index);
1013 }));
1014 /* *INDENT-ON* */
1015 }
1016
1017 return NULL;
1018}
1019
1020/**
1021 * show IPSEC tunnel protection hash tables
1022 */
1023/* *INDENT-OFF* */
1024VLIB_CLI_COMMAND (ipsec_tun_protect_hash_show_node, static) =
1025{
1026 .path = "show ipsec protect-hash",
1027 .function = ipsec_tun_protect_hash_show,
1028 .short_help = "show ipsec protect-hash",
1029};
1030/* *INDENT-ON* */
1031
Ed Warnickecb9cada2015-12-08 15:45:58 -07001032clib_error_t *
1033ipsec_cli_init (vlib_main_t * vm)
1034{
1035 return 0;
1036}
1037
1038VLIB_INIT_FUNCTION (ipsec_cli_init);
1039
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001040
1041/*
1042 * fd.io coding-style-patch-verification: ON
1043 *
1044 * Local Variables:
1045 * eval: (c-set-style "gnu")
1046 * End:
1047 */