blob: f66bf6d5aa1d4bcc3677ff7a2640ea0917fb03ca [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>
Ed Warnickecb9cada2015-12-08 15:45:58 -070023
24#include <vnet/ipsec/ipsec.h>
25
26static clib_error_t *
27set_interface_spd_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070028 unformat_input_t * input,
29 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -070030{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070031 unformat_input_t _line_input, *line_input = &_line_input;
Ed Warnickecb9cada2015-12-08 15:45:58 -070032 ipsec_main_t *im = &ipsec_main;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070033 u32 sw_if_index = (u32) ~ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070034 u32 spd_id;
35 int is_add = 1;
Billy McFalla9a20e72017-02-15 11:39:12 -050036 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -070037
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070038 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -070039 return 0;
40
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070041 if (unformat
42 (line_input, "%U %u", unformat_vnet_sw_interface, im->vnet_main,
43 &sw_if_index, &spd_id))
Ed Warnickecb9cada2015-12-08 15:45:58 -070044 ;
45 else if (unformat (line_input, "del"))
46 is_add = 0;
47 else
Billy McFalla9a20e72017-02-15 11:39:12 -050048 {
49 error = clib_error_return (0, "parse error: '%U'",
50 format_unformat_error, line_input);
51 goto done;
52 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070053
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070054 ipsec_set_interface_spd (vm, sw_if_index, spd_id, is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -070055
Billy McFalla9a20e72017-02-15 11:39:12 -050056done:
57 unformat_free (line_input);
58
59 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -070060}
61
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070062/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070063VLIB_CLI_COMMAND (set_interface_spd_command, static) = {
64 .path = "set interface ipsec spd",
65 .short_help =
66 "set interface ipsec spd <int> <id>",
67 .function = set_interface_spd_command_fn,
68};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070069/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070070
71static clib_error_t *
72ipsec_sa_add_del_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070073 unformat_input_t * input,
74 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -070075{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070076 unformat_input_t _line_input, *line_input = &_line_input;
Neale Ranns8d7c5022019-02-06 01:41:05 -080077 ip46_address_t tun_src = { }, tun_dst =
78 {
79 };
80 ipsec_crypto_alg_t crypto_alg;
81 ipsec_integ_alg_t integ_alg;
82 ipsec_protocol_t proto;
83 ipsec_sa_flags_t flags;
84 clib_error_t *error;
85 ipsec_key_t ck, ik;
86 int is_add, rv;
87 u32 id, spi;
Ed Warnickecb9cada2015-12-08 15:45:58 -070088
Neale Ranns8d7c5022019-02-06 01:41:05 -080089 error = NULL;
90 is_add = 0;
91 flags = IPSEC_SA_FLAG_NONE;
92 proto = IPSEC_PROTOCOL_ESP;
Ed Warnickecb9cada2015-12-08 15:45:58 -070093
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070094 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -070095 return 0;
96
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070097 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
98 {
Neale Ranns8d7c5022019-02-06 01:41:05 -080099 if (unformat (line_input, "add %u", &id))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700100 is_add = 1;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800101 else if (unformat (line_input, "del %u", &id))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700102 is_add = 0;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800103 else if (unformat (line_input, "spi %u", &spi))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700104 ;
105 else if (unformat (line_input, "esp"))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800106 proto = IPSEC_PROTOCOL_ESP;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700107 else if (unformat (line_input, "ah"))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800108 proto = IPSEC_PROTOCOL_AH;
109 else if (unformat (line_input, "crypto-key %U",
110 unformat_ipsec_key, &ck))
111 ;
112 else if (unformat (line_input, "crypto-alg %U",
113 unformat_ipsec_crypto_alg, &crypto_alg))
114 ;
115 else if (unformat (line_input, "integ-key %U", unformat_ipsec_key, &ik))
116 ;
117 else if (unformat (line_input, "integ-alg %U",
118 unformat_ipsec_integ_alg, &integ_alg))
119 ;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700120 else if (unformat (line_input, "tunnel-src %U",
Neale Ranns8d7c5022019-02-06 01:41:05 -0800121 unformat_ip46_address, &tun_src))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700122 {
Neale Ranns8d7c5022019-02-06 01:41:05 -0800123 flags |= IPSEC_SA_FLAG_IS_TUNNEL;
124 if (!ip46_address_is_ip4 (&tun_src))
125 flags |= IPSEC_SA_FLAG_IS_TUNNEL_V6;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700126 }
127 else if (unformat (line_input, "tunnel-dst %U",
Neale Ranns8d7c5022019-02-06 01:41:05 -0800128 unformat_ip46_address, &tun_dst))
129 ;
Radu Nicolau717de092018-08-03 10:37:24 +0100130 else if (unformat (line_input, "udp-encap"))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800131 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700132 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500133 {
134 error = clib_error_return (0, "parse error: '%U'",
135 format_unformat_error, line_input);
136 goto done;
137 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700138 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700139
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000140 if (is_add)
Neale Ranns8d7c5022019-02-06 01:41:05 -0800141 rv = ipsec_sa_add (id, spi, proto, crypto_alg,
142 &ck, integ_alg, &ik, flags,
143 0, &tun_src, &tun_dst, NULL);
144 else
145 rv = ipsec_sa_del (id);
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000146
Neale Ranns8d7c5022019-02-06 01:41:05 -0800147 if (rv)
148 clib_error_return (0, "failed");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149
Billy McFalla9a20e72017-02-15 11:39:12 -0500150done:
151 unformat_free (line_input);
152
153 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154}
155
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700156/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157VLIB_CLI_COMMAND (ipsec_sa_add_del_command, static) = {
158 .path = "ipsec sa",
159 .short_help =
160 "ipsec sa [add|del]",
161 .function = ipsec_sa_add_del_command_fn,
162};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700163/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164
165static clib_error_t *
166ipsec_spd_add_del_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700167 unformat_input_t * input,
168 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700170 unformat_input_t _line_input, *line_input = &_line_input;
Damjan Marion3f54b182016-08-16 11:27:02 +0200171 u32 spd_id = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172 int is_add = ~0;
Billy McFalla9a20e72017-02-15 11:39:12 -0500173 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700174
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700175 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700176 return 0;
177
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700178 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
179 {
180 if (unformat (line_input, "add"))
181 is_add = 1;
182 else if (unformat (line_input, "del"))
183 is_add = 0;
184 else if (unformat (line_input, "%u", &spd_id))
185 ;
186 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500187 {
188 error = clib_error_return (0, "parse error: '%U'",
189 format_unformat_error, line_input);
190 goto done;
191 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700192 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193
Damjan Marion3f54b182016-08-16 11:27:02 +0200194 if (spd_id == ~0)
Billy McFalla9a20e72017-02-15 11:39:12 -0500195 {
196 error = clib_error_return (0, "please specify SPD ID");
197 goto done;
198 }
Damjan Marion3f54b182016-08-16 11:27:02 +0200199
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700200 ipsec_add_del_spd (vm, spd_id, is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700201
Billy McFalla9a20e72017-02-15 11:39:12 -0500202done:
203 unformat_free (line_input);
204
205 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700206}
207
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700208/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700209VLIB_CLI_COMMAND (ipsec_spd_add_del_command, static) = {
210 .path = "ipsec spd",
211 .short_help =
212 "ipsec spd [add|del] <id>",
213 .function = ipsec_spd_add_del_command_fn,
214};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700215/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216
217
218static clib_error_t *
219ipsec_policy_add_del_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700220 unformat_input_t * input,
221 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700222{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700223 unformat_input_t _line_input, *line_input = &_line_input;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700224 ipsec_policy_t p;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800225 int rv, is_add = 0;
226 u32 tmp, tmp2, stat_index;
Billy McFalla9a20e72017-02-15 11:39:12 -0500227 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700228
Dave Barachb7b92992018-10-17 10:38:51 -0400229 clib_memset (&p, 0, sizeof (p));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700230 p.lport.stop = p.rport.stop = ~0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700231 p.laddr.stop.ip4.as_u32 = p.raddr.stop.ip4.as_u32 = (u32) ~ 0;
232 p.laddr.stop.ip6.as_u64[0] = p.laddr.stop.ip6.as_u64[1] = (u64) ~ 0;
233 p.raddr.stop.ip6.as_u64[0] = p.raddr.stop.ip6.as_u64[1] = (u64) ~ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700234
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700235 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700236 return 0;
237
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700238 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
239 {
240 if (unformat (line_input, "add"))
241 is_add = 1;
242 else if (unformat (line_input, "del"))
243 is_add = 0;
244 else if (unformat (line_input, "spd %u", &p.id))
245 ;
246 else if (unformat (line_input, "inbound"))
247 p.is_outbound = 0;
248 else if (unformat (line_input, "outbound"))
249 p.is_outbound = 1;
250 else if (unformat (line_input, "priority %d", &p.priority))
251 ;
252 else if (unformat (line_input, "protocol %u", &tmp))
253 p.protocol = (u8) tmp;
254 else
255 if (unformat
256 (line_input, "action %U", unformat_ipsec_policy_action,
257 &p.policy))
258 {
259 if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
Billy McFalla9a20e72017-02-15 11:39:12 -0500260 {
261 error = clib_error_return (0, "unsupported action: 'resolve'");
262 goto done;
263 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700264 }
265 else if (unformat (line_input, "sa %u", &p.sa_id))
266 ;
267 else if (unformat (line_input, "local-ip-range %U - %U",
268 unformat_ip4_address, &p.laddr.start.ip4,
269 unformat_ip4_address, &p.laddr.stop.ip4))
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800270 ;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700271 else if (unformat (line_input, "remote-ip-range %U - %U",
272 unformat_ip4_address, &p.raddr.start.ip4,
273 unformat_ip4_address, &p.raddr.stop.ip4))
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800274 ;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700275 else if (unformat (line_input, "local-ip-range %U - %U",
276 unformat_ip6_address, &p.laddr.start.ip6,
277 unformat_ip6_address, &p.laddr.stop.ip6))
278 {
279 p.is_ipv6 = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700280 }
281 else if (unformat (line_input, "remote-ip-range %U - %U",
282 unformat_ip6_address, &p.raddr.start.ip6,
283 unformat_ip6_address, &p.raddr.stop.ip6))
284 {
285 p.is_ipv6 = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700286 }
287 else if (unformat (line_input, "local-port-range %u - %u", &tmp, &tmp2))
288 {
289 p.lport.start = tmp;
290 p.lport.stop = tmp2;
291 }
292 else
293 if (unformat (line_input, "remote-port-range %u - %u", &tmp, &tmp2))
294 {
295 p.rport.start = tmp;
296 p.rport.stop = tmp2;
297 }
298 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500299 {
300 error = clib_error_return (0, "parse error: '%U'",
301 format_unformat_error, line_input);
302 goto done;
303 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700304 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700305
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800306 /* Check if SA is for IPv6/AH which is not supported. Return error if TRUE. */
307 if (p.sa_id)
308 {
309 uword *p1;
310 ipsec_main_t *im = &ipsec_main;
311 ipsec_sa_t *sa = 0;
312 p1 = hash_get (im->sa_index_by_sa_id, p.sa_id);
Klement Sekerac2fc57e2018-06-28 14:20:12 +0200313 if (!p1)
314 {
315 error =
316 clib_error_return (0, "SA with index %u not found", p.sa_id);
317 goto done;
318 }
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800319 sa = pool_elt_at_index (im->sad, p1[0]);
320 if (sa && sa->protocol == IPSEC_PROTOCOL_AH && is_add && p.is_ipv6)
321 {
322 error = clib_error_return (0, "AH not supported for IPV6: '%U'",
323 format_unformat_error, line_input);
324 goto done;
325 }
326 }
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800327 rv = ipsec_add_del_policy (vm, &p, is_add, &stat_index);
328
329 if (!rv)
330 vlib_cli_output (vm, "policy-index:%d", stat_index);
331 else
332 vlib_cli_output (vm, "error:%d", rv);
Billy McFalla9a20e72017-02-15 11:39:12 -0500333
334done:
335 unformat_free (line_input);
336
337 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700338}
339
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700340/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700341VLIB_CLI_COMMAND (ipsec_policy_add_del_command, static) = {
342 .path = "ipsec policy",
343 .short_help =
344 "ipsec policy [add|del] spd <id> priority <n> ",
345 .function = ipsec_policy_add_del_command_fn,
346};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700347/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700348
349static clib_error_t *
350set_ipsec_sa_key_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700351 unformat_input_t * input,
352 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700353{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700354 unformat_input_t _line_input, *line_input = &_line_input;
Billy McFalla9a20e72017-02-15 11:39:12 -0500355 clib_error_t *error = NULL;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800356 ipsec_key_t ck, ik;
357 u32 id;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700359 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700360 return 0;
361
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700362 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
363 {
Neale Ranns8d7c5022019-02-06 01:41:05 -0800364 if (unformat (line_input, "%u", &id))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700365 ;
366 else
Neale Ranns8d7c5022019-02-06 01:41:05 -0800367 if (unformat (line_input, "crypto-key %U", unformat_ipsec_key, &ck))
368 ;
369 else if (unformat (line_input, "integ-key %U", unformat_ipsec_key, &ik))
370 ;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700371 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500372 {
373 error = clib_error_return (0, "parse error: '%U'",
374 format_unformat_error, line_input);
375 goto done;
376 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700377 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700378
Neale Ranns8d7c5022019-02-06 01:41:05 -0800379 ipsec_set_sa_key (id, &ck, &ik);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700380
Billy McFalla9a20e72017-02-15 11:39:12 -0500381done:
382 unformat_free (line_input);
383
384 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700385}
386
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700387/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700388VLIB_CLI_COMMAND (set_ipsec_sa_key_command, static) = {
389 .path = "set ipsec sa",
390 .short_help =
391 "set ipsec sa <id> crypto-key <key> integ-key <key>",
392 .function = set_ipsec_sa_key_command_fn,
393};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700394/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700395
396static clib_error_t *
397show_ipsec_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700398 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700399{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700400 ipsec_main_t *im = &ipsec_main;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800401 u32 spd_id, sw_if_index, sai;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700402 vnet_hw_interface_t *hi;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800403 ipsec_tunnel_if_t *t;
Klement Sekeraea841302018-05-11 12:59:05 +0200404 u8 *protocol = NULL;
405 u8 *policy = NULL;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800406 u32 i;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700407
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700408 /* *INDENT-OFF* */
Neale Ranns8d7c5022019-02-06 01:41:05 -0800409 pool_foreach_index (sai, im->sad, ({
410 vlib_cli_output(vm, "%U", format_ipsec_sa, sai);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700411 }));
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800412 pool_foreach_index (i, im->spds, ({
413 vlib_cli_output(vm, "%U", format_ipsec_spd, i);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700414 }));
415
Neale Ranns311124e2019-01-24 04:52:25 -0800416 vlib_cli_output (vm, "SPD Bindings:");
Neale Ranns8d7c5022019-02-06 01:41:05 -0800417
Neale Ranns311124e2019-01-24 04:52:25 -0800418 hash_foreach(sw_if_index, spd_id, im->spd_index_by_sw_if_index, ({
Neale Ranns8d7c5022019-02-06 01:41:05 -0800419 vlib_cli_output (vm, " %d -> %U", spd_id,
420 format_vnet_sw_if_index_name, im->vnet_main,
421 sw_if_index);
Neale Ranns311124e2019-01-24 04:52:25 -0800422 }));
423 /* *INDENT-ON* */
424
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700425 vlib_cli_output (vm, "tunnel interfaces");
426 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700427 pool_foreach (t, im->tunnel_interfaces, ({
Matus Fabian694265d2016-08-10 01:55:36 -0700428 if (t->hw_if_index == ~0)
429 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700430 hi = vnet_get_hw_interface (im->vnet_main, t->hw_if_index);
Pierre Pfister4c422f92018-12-10 11:19:08 +0100431
Neale Ranns8d7c5022019-02-06 01:41:05 -0800432 vlib_cli_output(vm, " %s", hi->name);
Pierre Pfister4c422f92018-12-10 11:19:08 +0100433
Neale Ranns8d7c5022019-02-06 01:41:05 -0800434 vlib_cli_output(vm, " out-bound sa");
435 vlib_cli_output(vm, " %U", format_ipsec_sa, t->output_sa_index);
436
437 vlib_cli_output(vm, " in-bound sa");
438 vlib_cli_output(vm, " %U", format_ipsec_sa, t->input_sa_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700439 }));
Klement Sekeraea841302018-05-11 12:59:05 +0200440 vec_free(policy);
441 vec_free(protocol);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700442 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700443 return 0;
444}
445
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700446/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700447VLIB_CLI_COMMAND (show_ipsec_command, static) = {
448 .path = "show ipsec",
Klement Sekerab4d30532018-11-08 13:00:02 +0100449 .short_help = "show ipsec [backends]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700450 .function = show_ipsec_command_fn,
451};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700452/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700453
454static clib_error_t *
Klement Sekerab4d30532018-11-08 13:00:02 +0100455ipsec_show_backends_command_fn (vlib_main_t * vm,
456 unformat_input_t * input,
457 vlib_cli_command_t * cmd)
458{
459 ipsec_main_t *im = &ipsec_main;
460 u32 verbose = 0;
461
462 (void) unformat (input, "verbose %u", &verbose);
463
464 vlib_cli_output (vm, "IPsec AH backends available:");
465 u8 *s = format (NULL, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
466 ipsec_ah_backend_t *ab;
467 /* *INDENT-OFF* */
468 pool_foreach (ab, im->ah_backends, {
469 s = format (s, "%=25s %=25u %=10s\n", ab->name, ab - im->ah_backends,
470 ab - im->ah_backends == im->ah_current_backend ? "yes" : "no");
471 if (verbose) {
472 vlib_node_t *n;
473 n = vlib_get_node (vm, ab->ah4_encrypt_node_index);
474 s = format (s, " enc4 %s (next %d)\n", n->name, ab->ah4_encrypt_next_index);
475 n = vlib_get_node (vm, ab->ah4_decrypt_node_index);
476 s = format (s, " dec4 %s (next %d)\n", n->name, ab->ah4_decrypt_next_index);
477 n = vlib_get_node (vm, ab->ah6_encrypt_node_index);
478 s = format (s, " enc6 %s (next %d)\n", n->name, ab->ah6_encrypt_next_index);
479 n = vlib_get_node (vm, ab->ah6_decrypt_node_index);
480 s = format (s, " dec6 %s (next %d)\n", n->name, ab->ah6_decrypt_next_index);
481 }
482 });
483 /* *INDENT-ON* */
484 vlib_cli_output (vm, "%v", s);
485 _vec_len (s) = 0;
486 vlib_cli_output (vm, "IPsec ESP backends available:");
487 s = format (s, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
488 ipsec_esp_backend_t *eb;
489 /* *INDENT-OFF* */
490 pool_foreach (eb, im->esp_backends, {
491 s = format (s, "%=25s %=25u %=10s\n", eb->name, eb - im->esp_backends,
492 eb - im->esp_backends == im->esp_current_backend ? "yes"
493 : "no");
494 if (verbose) {
495 vlib_node_t *n;
496 n = vlib_get_node (vm, eb->esp4_encrypt_node_index);
497 s = format (s, " enc4 %s (next %d)\n", n->name, eb->esp4_encrypt_next_index);
498 n = vlib_get_node (vm, eb->esp4_decrypt_node_index);
499 s = format (s, " dec4 %s (next %d)\n", n->name, eb->esp4_decrypt_next_index);
500 n = vlib_get_node (vm, eb->esp6_encrypt_node_index);
501 s = format (s, " enc6 %s (next %d)\n", n->name, eb->esp6_encrypt_next_index);
502 n = vlib_get_node (vm, eb->esp6_decrypt_node_index);
503 s = format (s, " dec6 %s (next %d)\n", n->name, eb->esp6_decrypt_next_index);
504 }
505 });
506 /* *INDENT-ON* */
507 vlib_cli_output (vm, "%v", s);
508
509 vec_free (s);
510 return 0;
511}
512
513/* *INDENT-OFF* */
514VLIB_CLI_COMMAND (ipsec_show_backends_command, static) = {
515 .path = "show ipsec backends",
516 .short_help = "show ipsec backends",
517 .function = ipsec_show_backends_command_fn,
518};
519/* *INDENT-ON* */
520
521static clib_error_t *
522ipsec_select_backend_command_fn (vlib_main_t * vm,
523 unformat_input_t * input,
524 vlib_cli_command_t * cmd)
525{
526 u32 backend_index;
527 ipsec_main_t *im = &ipsec_main;
528
529 if (pool_elts (im->sad) > 0)
530 {
531 return clib_error_return (0,
532 "Cannot change IPsec backend, while %u SA entries are configured",
533 pool_elts (im->sad));
534 }
535
536 unformat_input_t _line_input, *line_input = &_line_input;
537 /* Get a line of input. */
538 if (!unformat_user (input, unformat_line_input, line_input))
539 return 0;
540
541 if (unformat (line_input, "ah"))
542 {
543 if (unformat (line_input, "%u", &backend_index))
544 {
545 if (ipsec_select_ah_backend (im, backend_index) < 0)
546 {
547 return clib_error_return (0, "Invalid AH backend index `%u'",
548 backend_index);
549 }
550 }
551 else
552 {
553 return clib_error_return (0, "Invalid backend index `%U'",
554 format_unformat_error, line_input);
555 }
556 }
557 else if (unformat (line_input, "esp"))
558 {
559 if (unformat (line_input, "%u", &backend_index))
560 {
561 if (ipsec_select_esp_backend (im, backend_index) < 0)
562 {
563 return clib_error_return (0, "Invalid ESP backend index `%u'",
564 backend_index);
565 }
566 }
567 else
568 {
569 return clib_error_return (0, "Invalid backend index `%U'",
570 format_unformat_error, line_input);
571 }
572 }
573 else
574 {
575 return clib_error_return (0, "Unknown input `%U'",
576 format_unformat_error, line_input);
577 }
578
579 return 0;
580}
581
582/* *INDENT-OFF* */
583VLIB_CLI_COMMAND (ipsec_select_backend_command, static) = {
584 .path = "ipsec select backend",
585 .short_help = "ipsec select backend <ah|esp> <backend index>",
586 .function = ipsec_select_backend_command_fn,
587};
588
589/* *INDENT-ON* */
590
591static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700592clear_ipsec_counters_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700593 unformat_input_t * input,
594 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700595{
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800596 vlib_clear_combined_counters (&ipsec_spd_policy_counters);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700597
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800598 return (NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700599}
600
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700601/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700602VLIB_CLI_COMMAND (clear_ipsec_counters_command, static) = {
603 .path = "clear ipsec counters",
604 .short_help = "clear ipsec counters",
605 .function = clear_ipsec_counters_command_fn,
606};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700607/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700608
609static clib_error_t *
610create_ipsec_tunnel_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700611 unformat_input_t * input,
612 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700613{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700614 unformat_input_t _line_input, *line_input = &_line_input;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700615 ipsec_add_del_tunnel_args_t a;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700616 int rv;
617 u32 num_m_args = 0;
Billy McFalla9a20e72017-02-15 11:39:12 -0500618 clib_error_t *error = NULL;
Matthew Smith2838a232016-06-21 16:05:09 -0500619
Dave Barachb7b92992018-10-17 10:38:51 -0400620 clib_memset (&a, 0, sizeof (a));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700621 a.is_add = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700622
623 /* Get a line of input. */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700624 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700625 return 0;
626
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700627 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
628 {
629 if (unformat
630 (line_input, "local-ip %U", unformat_ip4_address, &a.local_ip))
631 num_m_args++;
632 else
633 if (unformat
634 (line_input, "remote-ip %U", unformat_ip4_address, &a.remote_ip))
635 num_m_args++;
636 else if (unformat (line_input, "local-spi %u", &a.local_spi))
637 num_m_args++;
638 else if (unformat (line_input, "remote-spi %u", &a.remote_spi))
639 num_m_args++;
Matthew Smith8e1039a2018-04-12 07:32:56 -0500640 else if (unformat (line_input, "instance %u", &a.show_instance))
641 a.renumber = 1;
Radu Nicolau717de092018-08-03 10:37:24 +0100642 else if (unformat (line_input, "udp-encap"))
643 a.udp_encap = 1;
Kingwel Xie2baf9422019-02-04 02:07:06 -0800644 else if (unformat (line_input, "use-esn"))
645 a.esn = 1;
646 else if (unformat (line_input, "use-anti-replay"))
647 a.anti_replay = 1;
Pierre Pfister4c422f92018-12-10 11:19:08 +0100648 else if (unformat (line_input, "tx-table %u", &a.tx_table_id))
649 ;
Kingwel Xie2baf9422019-02-04 02:07:06 -0800650 else if (unformat (line_input, "del"))
651 a.is_add = 0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700652 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500653 {
654 error = clib_error_return (0, "unknown input `%U'",
655 format_unformat_error, line_input);
656 goto done;
657 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700658 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700659
660 if (num_m_args < 4)
Billy McFalla9a20e72017-02-15 11:39:12 -0500661 {
662 error = clib_error_return (0, "mandatory argument(s) missing");
663 goto done;
664 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700665
Matthew Smith2838a232016-06-21 16:05:09 -0500666 rv = ipsec_add_del_tunnel_if (&a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700667
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700668 switch (rv)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700669 {
670 case 0:
671 break;
672 case VNET_API_ERROR_INVALID_VALUE:
673 if (a.is_add)
Billy McFalla9a20e72017-02-15 11:39:12 -0500674 error = clib_error_return (0,
675 "IPSec tunnel interface already exists...");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700676 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500677 error = clib_error_return (0, "IPSec tunnel interface not exists...");
678 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700679 default:
Billy McFalla9a20e72017-02-15 11:39:12 -0500680 error = clib_error_return (0, "ipsec_register_interface returned %d",
681 rv);
682 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700683 }
684
Billy McFalla9a20e72017-02-15 11:39:12 -0500685done:
686 unformat_free (line_input);
687
688 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700689}
690
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700691/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700692VLIB_CLI_COMMAND (create_ipsec_tunnel_command, static) = {
693 .path = "create ipsec tunnel",
Pierre Pfister4c422f92018-12-10 11:19:08 +0100694 .short_help = "create ipsec tunnel local-ip <addr> local-spi <spi> "
Kingwel Xie2baf9422019-02-04 02:07:06 -0800695 "remote-ip <addr> remote-spi <spi> [instance <inst_num>] [udp-encap] [use-esn] [use-anti-replay] "
Pierre Pfister4c422f92018-12-10 11:19:08 +0100696 "[tx-table <table-id>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700697 .function = create_ipsec_tunnel_command_fn,
698};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700699/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700700
701static clib_error_t *
702set_interface_key_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700703 unformat_input_t * input,
704 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700705{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700706 unformat_input_t _line_input, *line_input = &_line_input;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700707 ipsec_main_t *im = &ipsec_main;
708 ipsec_if_set_key_type_t type = IPSEC_IF_SET_KEY_TYPE_NONE;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700709 u32 hw_if_index = (u32) ~ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700710 u32 alg;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700711 u8 *key = 0;
Billy McFalla9a20e72017-02-15 11:39:12 -0500712 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700713
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700714 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700715 return 0;
716
717 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
718 {
719 if (unformat (line_input, "%U",
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700720 unformat_vnet_hw_interface, im->vnet_main, &hw_if_index))
721 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700722 else
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700723 if (unformat
724 (line_input, "local crypto %U", unformat_ipsec_crypto_alg, &alg))
725 type = IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO;
726 else
727 if (unformat
728 (line_input, "remote crypto %U", unformat_ipsec_crypto_alg, &alg))
729 type = IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO;
730 else
731 if (unformat
732 (line_input, "local integ %U", unformat_ipsec_integ_alg, &alg))
733 type = IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG;
734 else
735 if (unformat
736 (line_input, "remote integ %U", unformat_ipsec_integ_alg, &alg))
737 type = IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG;
738 else if (unformat (line_input, "%U", unformat_hex_string, &key))
739 ;
740 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500741 {
742 error = clib_error_return (0, "parse error: '%U'",
743 format_unformat_error, line_input);
744 goto done;
745 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700746 }
747
Ed Warnickecb9cada2015-12-08 15:45:58 -0700748 if (type == IPSEC_IF_SET_KEY_TYPE_NONE)
Billy McFalla9a20e72017-02-15 11:39:12 -0500749 {
750 error = clib_error_return (0, "unknown key type");
751 goto done;
752 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700753
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700754 if (alg > 0 && vec_len (key) == 0)
Billy McFalla9a20e72017-02-15 11:39:12 -0500755 {
756 error = clib_error_return (0, "key is not specified");
757 goto done;
758 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700759
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700760 if (hw_if_index == (u32) ~ 0)
Billy McFalla9a20e72017-02-15 11:39:12 -0500761 {
762 error = clib_error_return (0, "interface not specified");
763 goto done;
764 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700765
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700766 ipsec_set_interface_key (im->vnet_main, hw_if_index, type, alg, key);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700767
Billy McFalla9a20e72017-02-15 11:39:12 -0500768done:
769 vec_free (key);
770 unformat_free (line_input);
771
772 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700773}
774
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700775/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700776VLIB_CLI_COMMAND (set_interface_key_command, static) = {
777 .path = "set interface ipsec key",
778 .short_help =
779 "set interface ipsec key <int> <local|remote> <crypto|integ> <key type> <key>",
780 .function = set_interface_key_command_fn,
781};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700782/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700783
Ed Warnickecb9cada2015-12-08 15:45:58 -0700784clib_error_t *
785ipsec_cli_init (vlib_main_t * vm)
786{
787 return 0;
788}
789
790VLIB_INIT_FUNCTION (ipsec_cli_init);
791
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700792
793/*
794 * fd.io coding-style-patch-verification: ON
795 *
796 * Local Variables:
797 * eval: (c-set-style "gnu")
798 * End:
799 */