2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
15 #include <vlib/vlib.h>
16 #include <vnet/vnet.h>
17 #include <vnet/pg/pg.h>
18 #include <vppinfra/error.h>
19 #include <vnet/ip/udp.h>
20 #include <vnet/ipsec/ikev2.h>
21 #include <vnet/ipsec/ikev2_priv.h>
24 format_ikev2_id_type_and_data (u8 * s, va_list * args)
26 ikev2_id_t *id = va_arg (*args, ikev2_id_t *);
28 if (id->type == 0 || vec_len (id->data) == 0)
29 return format (s, "none");
31 s = format (s, "%U", format_ikev2_id_type, id->type);
33 if (id->type == IKEV2_ID_TYPE_ID_FQDN ||
34 id->type == IKEV2_ID_TYPE_ID_RFC822_ADDR)
36 s = format (s, " %v", id->data);
41 format (s, " %U", format_hex_bytes, &id->data,
42 (uword) (vec_len (id->data)));
50 show_ikev2_sa_command_fn (vlib_main_t * vm,
51 unformat_input_t * input, vlib_cli_command_t * cmd)
53 ikev2_main_t *km = &ikev2_main;
54 ikev2_main_per_thread_data_t *tkm;
57 ikev2_child_sa_t *child;
58 ikev2_sa_transform_t *tr;
60 vec_foreach (tkm, km->per_thread_data)
63 pool_foreach (sa, tkm->sas, ({
65 vlib_cli_output(vm, " iip %U ispi %lx rip %U rspi %lx",
66 format_ip4_address, &sa->iaddr, sa->ispi,
67 format_ip4_address, &sa->raddr, sa->rspi);
69 tr = ikev2_sa_get_td_for_type(sa->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
70 s = format(s, "%U ", format_ikev2_sa_transform, tr);
72 tr = ikev2_sa_get_td_for_type(sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
73 s = format(s, "%U ", format_ikev2_sa_transform, tr);
75 tr = ikev2_sa_get_td_for_type(sa->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
76 s = format(s, "%U ", format_ikev2_sa_transform, tr);
78 tr = ikev2_sa_get_td_for_type(sa->r_proposals, IKEV2_TRANSFORM_TYPE_DH);
79 s = format(s, "%U ", format_ikev2_sa_transform, tr);
81 vlib_cli_output(vm, " %v", s);
84 vlib_cli_output(vm, " nonce i:%U\n r:%U",
85 format_hex_bytes, sa->i_nonce, vec_len(sa->i_nonce),
86 format_hex_bytes, sa->r_nonce, vec_len(sa->r_nonce));
88 vlib_cli_output(vm, " SK_d %U",
89 format_hex_bytes, sa->sk_d, vec_len(sa->sk_d));
90 vlib_cli_output(vm, " SK_a i:%U\n r:%U",
91 format_hex_bytes, sa->sk_ai, vec_len(sa->sk_ai),
92 format_hex_bytes, sa->sk_ar, vec_len(sa->sk_ar));
93 vlib_cli_output(vm, " SK_e i:%U\n r:%U",
94 format_hex_bytes, sa->sk_ei, vec_len(sa->sk_ei),
95 format_hex_bytes, sa->sk_er, vec_len(sa->sk_er));
96 vlib_cli_output(vm, " SK_p i:%U\n r:%U",
97 format_hex_bytes, sa->sk_pi, vec_len(sa->sk_pi),
98 format_hex_bytes, sa->sk_pr, vec_len(sa->sk_pr));
100 vlib_cli_output(vm, " identifier (i) %U",
101 format_ikev2_id_type_and_data, &sa->i_id);
102 vlib_cli_output(vm, " identifier (r) %U",
103 format_ikev2_id_type_and_data, &sa->r_id);
105 vec_foreach(child, sa->childs)
107 vlib_cli_output(vm, " child sa %u:", child - sa->childs);
109 tr = ikev2_sa_get_td_for_type(child->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
110 s = format(s, "%U ", format_ikev2_sa_transform, tr);
112 tr = ikev2_sa_get_td_for_type(child->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
113 s = format(s, "%U ", format_ikev2_sa_transform, tr);
115 tr = ikev2_sa_get_td_for_type(child->r_proposals, IKEV2_TRANSFORM_TYPE_ESN);
116 s = format(s, "%U ", format_ikev2_sa_transform, tr);
118 vlib_cli_output(vm, " %v", s);
121 vlib_cli_output(vm, " spi(i) %lx spi(r) %lx",
122 child->i_proposals ? child->i_proposals[0].spi : 0,
123 child->r_proposals ? child->r_proposals[0].spi : 0);
125 vlib_cli_output(vm, " SK_e i:%U\n r:%U",
126 format_hex_bytes, child->sk_ei, vec_len(child->sk_ei),
127 format_hex_bytes, child->sk_er, vec_len(child->sk_er));
128 vlib_cli_output(vm, " SK_a i:%U\n r:%U",
129 format_hex_bytes, child->sk_ai, vec_len(child->sk_ai),
130 format_hex_bytes, child->sk_ar, vec_len(child->sk_ar));
131 vlib_cli_output(vm, " traffic selectors (i):");
132 vec_foreach(ts, child->tsi)
134 vlib_cli_output(vm, " %u type %u protocol_id %u addr "
135 "%U - %U port %u - %u",
137 ts->ts_type, ts->protocol_id,
138 format_ip4_address, &ts->start_addr,
139 format_ip4_address, &ts->end_addr,
140 clib_net_to_host_u16( ts->start_port),
141 clib_net_to_host_u16( ts->end_port));
143 vlib_cli_output(vm, " traffic selectors (r):");
144 vec_foreach(ts, child->tsr)
146 vlib_cli_output(vm, " %u type %u protocol_id %u addr "
147 "%U - %U port %u - %u",
149 ts->ts_type, ts->protocol_id,
150 format_ip4_address, &ts->start_addr,
151 format_ip4_address, &ts->end_addr,
152 clib_net_to_host_u16( ts->start_port),
153 clib_net_to_host_u16( ts->end_port));
156 vlib_cli_output(vm, "");
164 VLIB_CLI_COMMAND (show_ikev2_sa_command, static) = {
165 .path = "show ikev2 sa",
166 .short_help = "show ikev2 sa",
167 .function = show_ikev2_sa_command_fn,
171 static clib_error_t *
172 ikev2_profile_add_del_command_fn (vlib_main_t * vm,
173 unformat_input_t * input,
174 vlib_cli_command_t * cmd)
176 unformat_input_t _line_input, *line_input = &_line_input;
181 u32 tmp1, tmp2, tmp3;
183 ip4_address_t end_addr;
185 const char *valid_chars = "a-zA-Z0-9_";
187 if (!unformat_user (input, unformat_line_input, line_input))
190 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
192 if (unformat (line_input, "add %U", unformat_token, valid_chars, &name))
194 r = ikev2_add_del_profile (vm, name, 1);
199 (line_input, "del %U", unformat_token, valid_chars, &name))
201 r = ikev2_add_del_profile (vm, name, 0);
204 else if (unformat (line_input, "set %U auth shared-key-mic string %v",
205 unformat_token, valid_chars, &name, &data))
208 ikev2_set_profile_auth (vm, name,
209 IKEV2_AUTH_METHOD_SHARED_KEY_MIC, data,
213 else if (unformat (line_input, "set %U auth shared-key-mic hex %U",
214 unformat_token, valid_chars, &name,
215 unformat_hex_string, &data))
218 ikev2_set_profile_auth (vm, name,
219 IKEV2_AUTH_METHOD_SHARED_KEY_MIC, data,
223 else if (unformat (line_input, "set %U auth rsa-sig cert-file %v",
224 unformat_token, valid_chars, &name, &data))
227 ikev2_set_profile_auth (vm, name, IKEV2_AUTH_METHOD_RSA_SIG, data,
231 else if (unformat (line_input, "set %U id local %U %U",
232 unformat_token, valid_chars, &name,
233 unformat_ikev2_id_type, &id_type,
234 unformat_ip4_address, &ip4))
236 data = vec_new (u8, 4);
237 clib_memcpy (data, ip4.as_u8, 4);
239 ikev2_set_profile_id (vm, name, (u8) id_type, data, /*local */ 1);
242 else if (unformat (line_input, "set %U id local %U 0x%U",
243 unformat_token, valid_chars, &name,
244 unformat_ikev2_id_type, &id_type,
245 unformat_hex_string, &data))
248 ikev2_set_profile_id (vm, name, (u8) id_type, data, /*local */ 1);
251 else if (unformat (line_input, "set %U id local %U %v",
252 unformat_token, valid_chars, &name,
253 unformat_ikev2_id_type, &id_type, &data))
256 ikev2_set_profile_id (vm, name, (u8) id_type, data, /*local */ 1);
259 else if (unformat (line_input, "set %U id remote %U %U",
260 unformat_token, valid_chars, &name,
261 unformat_ikev2_id_type, &id_type,
262 unformat_ip4_address, &ip4))
264 data = vec_new (u8, 4);
265 clib_memcpy (data, ip4.as_u8, 4);
267 ikev2_set_profile_id (vm, name, (u8) id_type, data, /*remote */
271 else if (unformat (line_input, "set %U id remote %U 0x%U",
272 unformat_token, valid_chars, &name,
273 unformat_ikev2_id_type, &id_type,
274 unformat_hex_string, &data))
277 ikev2_set_profile_id (vm, name, (u8) id_type, data, /*remote */
281 else if (unformat (line_input, "set %U id remote %U %v",
282 unformat_token, valid_chars, &name,
283 unformat_ikev2_id_type, &id_type, &data))
286 ikev2_set_profile_id (vm, name, (u8) id_type, data, /*remote */
290 else if (unformat (line_input, "set %U traffic-selector local "
291 "ip-range %U - %U port-range %u - %u protocol %u",
292 unformat_token, valid_chars, &name,
293 unformat_ip4_address, &ip4,
294 unformat_ip4_address, &end_addr,
295 &tmp1, &tmp2, &tmp3))
298 ikev2_set_profile_ts (vm, name, (u8) tmp3, (u16) tmp1, (u16) tmp2,
299 ip4, end_addr, /*local */ 1);
302 else if (unformat (line_input, "set %U traffic-selector remote "
303 "ip-range %U - %U port-range %u - %u protocol %u",
304 unformat_token, valid_chars, &name,
305 unformat_ip4_address, &ip4,
306 unformat_ip4_address, &end_addr,
307 &tmp1, &tmp2, &tmp3))
310 ikev2_set_profile_ts (vm, name, (u8) tmp3, (u16) tmp1, (u16) tmp2,
311 ip4, end_addr, /*remote */ 0);
318 r = clib_error_return (0, "parse error: '%U'",
319 format_unformat_error, line_input);
324 unformat_free (line_input);
329 VLIB_CLI_COMMAND (ikev2_profile_add_del_command, static) = {
330 .path = "ikev2 profile",
332 "ikev2 profile [add|del] <id>\n"
333 "ikev2 profile set <id> auth [rsa-sig|shared-key-mic] [cert-file|string|hex]"
335 "ikev2 profile set <id> id <local|remote> <type> <data>\n"
336 "ikev2 profile set <id> traffic-selector <local|remote> ip-range "
337 "<start-addr> - <end-addr> port-range <start-port> - <end-port> "
338 "protocol <protocol-number>",
339 .function = ikev2_profile_add_del_command_fn,
343 static clib_error_t *
344 show_ikev2_profile_command_fn (vlib_main_t * vm,
345 unformat_input_t * input,
346 vlib_cli_command_t * cmd)
348 ikev2_main_t *km = &ikev2_main;
352 pool_foreach (p, km->profiles, ({
353 vlib_cli_output(vm, "profile %v", p->name);
358 vlib_cli_output(vm, " auth-method %U auth data 0x%U",
359 format_ikev2_auth_method, p->auth.method,
360 format_hex_bytes, p->auth.data, vec_len(p->auth.data));
362 vlib_cli_output(vm, " auth-method %U auth data %v",
363 format_ikev2_auth_method, p->auth.method, p->auth.data);
368 if (p->loc_id.type == IKEV2_ID_TYPE_ID_IPV4_ADDR)
369 vlib_cli_output(vm, " local id-type %U data %U",
370 format_ikev2_id_type, p->loc_id.type,
371 format_ip4_address, p->loc_id.data);
372 else if (p->loc_id.type == IKEV2_ID_TYPE_ID_KEY_ID)
373 vlib_cli_output(vm, " local id-type %U data 0x%U",
374 format_ikev2_id_type, p->loc_id.type,
375 format_hex_bytes, p->loc_id.data,
376 vec_len(p->loc_id.data));
378 vlib_cli_output(vm, " local id-type %U data %v",
379 format_ikev2_id_type, p->loc_id.type, p->loc_id.data);
384 if (p->rem_id.type == IKEV2_ID_TYPE_ID_IPV4_ADDR)
385 vlib_cli_output(vm, " remote id-type %U data %U",
386 format_ikev2_id_type, p->rem_id.type,
387 format_ip4_address, p->rem_id.data);
388 else if (p->rem_id.type == IKEV2_ID_TYPE_ID_KEY_ID)
389 vlib_cli_output(vm, " remote id-type %U data 0x%U",
390 format_ikev2_id_type, p->rem_id.type,
391 format_hex_bytes, p->rem_id.data,
392 vec_len(p->rem_id.data));
394 vlib_cli_output(vm, " remote id-type %U data %v",
395 format_ikev2_id_type, p->rem_id.type, p->rem_id.data);
398 if (p->loc_ts.end_addr.as_u32)
399 vlib_cli_output(vm, " local traffic-selector addr %U - %U port %u - %u"
401 format_ip4_address, &p->loc_ts.start_addr,
402 format_ip4_address, &p->loc_ts.end_addr,
403 p->loc_ts.start_port, p->loc_ts.end_port,
404 p->loc_ts.protocol_id);
406 if (p->rem_ts.end_addr.as_u32)
407 vlib_cli_output(vm, " remote traffic-selector addr %U - %U port %u - %u"
409 format_ip4_address, &p->rem_ts.start_addr,
410 format_ip4_address, &p->rem_ts.end_addr,
411 p->rem_ts.start_port, p->rem_ts.end_port,
412 p->rem_ts.protocol_id);
420 VLIB_CLI_COMMAND (show_ikev2_profile_command, static) = {
421 .path = "show ikev2 profile",
422 .short_help = "show ikev2 profile",
423 .function = show_ikev2_profile_command_fn,
427 static clib_error_t *
428 set_ikev2_local_key_command_fn (vlib_main_t * vm,
429 unformat_input_t * input,
430 vlib_cli_command_t * cmd)
432 unformat_input_t _line_input, *line_input = &_line_input;
436 if (!unformat_user (input, unformat_line_input, line_input))
439 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
441 if (unformat (line_input, "%v", &data))
443 r = ikev2_set_local_key (vm, data);
450 r = clib_error_return (0, "parse error: '%U'",
451 format_unformat_error, line_input);
455 unformat_free (line_input);
460 VLIB_CLI_COMMAND (set_ikev2_local_key_command, static) = {
461 .path = "set ikev2 local key",
463 "set ikev2 local key <file>",
464 .function = set_ikev2_local_key_command_fn,
469 ikev2_cli_init (vlib_main_t * vm)
474 VLIB_INIT_FUNCTION (ikev2_cli_init);
477 * fd.io coding-style-patch-verification: ON
480 * eval: (c-set-style "gnu")