c8c8ea140b51dda624725b948b53b840cf3a52a7
[vpp.git] / vnet / vnet / ipsec / ikev2_cli.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
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>
22
23 u8 *
24 format_ikev2_id_type_and_data (u8 * s, va_list * args)
25 {
26   ikev2_id_t * id = va_arg (*args, ikev2_id_t *);
27
28   if (id->type == 0 || vec_len(id->data) == 0)
29     return format(s, "none");
30
31   s = format(s, "%U", format_ikev2_id_type, id->type);
32
33   if (id->type == IKEV2_ID_TYPE_ID_FQDN ||
34       id->type == IKEV2_ID_TYPE_ID_RFC822_ADDR)
35     {
36       s = format(s, " %v", id->data);
37     }
38   else
39     {
40       s = format(s, " %U", format_hex_bytes, &id->data, (uword) (vec_len(id->data)));
41     }
42
43   return s;
44 }
45
46
47 static clib_error_t *
48 show_ikev2_sa_command_fn (vlib_main_t * vm,
49                           unformat_input_t * input,
50                           vlib_cli_command_t * cmd)
51 {
52   ikev2_main_t * km = &ikev2_main;
53   ikev2_main_per_thread_data_t * tkm;
54   ikev2_sa_t * sa;
55   ikev2_ts_t * ts;
56   ikev2_child_sa_t * child;
57   ikev2_sa_transform_t * tr;
58
59   vec_foreach(tkm, km->per_thread_data) {
60     pool_foreach (sa, tkm->sas, ({
61       u8 * s = 0;
62       vlib_cli_output(vm, " iip %U ispi %lx rip %U rspi %lx",
63                       format_ip4_address, &sa->iaddr, sa->ispi,
64                       format_ip4_address, &sa->raddr, sa->rspi);
65   
66        tr = ikev2_sa_get_td_for_type(sa->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
67        s = format(s, "%U ", format_ikev2_sa_transform, tr);
68   
69        tr = ikev2_sa_get_td_for_type(sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
70        s = format(s, "%U ", format_ikev2_sa_transform, tr);
71   
72        tr = ikev2_sa_get_td_for_type(sa->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
73        s = format(s, "%U ", format_ikev2_sa_transform, tr);
74   
75        tr = ikev2_sa_get_td_for_type(sa->r_proposals, IKEV2_TRANSFORM_TYPE_DH);
76        s = format(s, "%U ", format_ikev2_sa_transform, tr);
77   
78       vlib_cli_output(vm, " %v", s);
79       vec_free(s);
80   
81       vlib_cli_output(vm, "  nonce i:%U\n        r:%U",
82                       format_hex_bytes, sa->i_nonce,  vec_len(sa->i_nonce),
83                       format_hex_bytes, sa->r_nonce,  vec_len(sa->r_nonce));
84   
85       vlib_cli_output(vm, "  SK_d    %U",
86                       format_hex_bytes, sa->sk_d,  vec_len(sa->sk_d));
87       vlib_cli_output(vm, "  SK_a  i:%U\n        r:%U",
88                       format_hex_bytes, sa->sk_ai, vec_len(sa->sk_ai),
89                       format_hex_bytes, sa->sk_ar, vec_len(sa->sk_ar));
90       vlib_cli_output(vm, "  SK_e  i:%U\n        r:%U",
91                       format_hex_bytes, sa->sk_ei, vec_len(sa->sk_ei),
92                       format_hex_bytes, sa->sk_er, vec_len(sa->sk_er));
93       vlib_cli_output(vm, "  SK_p  i:%U\n        r:%U",
94                       format_hex_bytes, sa->sk_pi, vec_len(sa->sk_pi),
95                       format_hex_bytes, sa->sk_pr, vec_len(sa->sk_pr));
96   
97       vlib_cli_output(vm, "  identifier (i) %U",
98                       format_ikev2_id_type_and_data, &sa->i_id);
99       vlib_cli_output(vm, "  identifier (r) %U",
100                       format_ikev2_id_type_and_data, &sa->r_id);
101   
102       vec_foreach(child, sa->childs)
103         {
104           vlib_cli_output(vm, "  child sa %u:", child - sa->childs);
105   
106           tr = ikev2_sa_get_td_for_type(child->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
107           s = format(s, "%U ", format_ikev2_sa_transform, tr);
108   
109           tr = ikev2_sa_get_td_for_type(child->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
110           s = format(s, "%U ", format_ikev2_sa_transform, tr);
111   
112           tr = ikev2_sa_get_td_for_type(child->r_proposals, IKEV2_TRANSFORM_TYPE_ESN);
113           s = format(s, "%U ", format_ikev2_sa_transform, tr);
114   
115           vlib_cli_output(vm, "    %v", s);
116           vec_free(s);
117   
118           vlib_cli_output(vm, "    spi(i) %lx spi(r) %lx",
119                           child->i_proposals ? child->i_proposals[0].spi : 0,
120                           child->r_proposals ? child->r_proposals[0].spi : 0);
121   
122           vlib_cli_output(vm, "    SK_e  i:%U\n          r:%U",
123                           format_hex_bytes, child->sk_ei, vec_len(child->sk_ei),
124                           format_hex_bytes, child->sk_er, vec_len(child->sk_er));
125           vlib_cli_output(vm, "    SK_a  i:%U\n          r:%U",
126                           format_hex_bytes, child->sk_ai, vec_len(child->sk_ai),
127                           format_hex_bytes, child->sk_ar, vec_len(child->sk_ar));
128           vlib_cli_output(vm, "    traffic selectors (i):");
129           vec_foreach(ts, child->tsi)
130             {
131               vlib_cli_output(vm, "      %u type %u protocol_id %u addr "
132                               "%U - %U port %u - %u",
133                               ts - child->tsi,
134                               ts->ts_type, ts->protocol_id,
135                               format_ip4_address, &ts->start_addr,
136                               format_ip4_address, &ts->end_addr,
137                               clib_net_to_host_u16( ts->start_port),
138                               clib_net_to_host_u16( ts->end_port));
139             }
140           vlib_cli_output(vm, "    traffic selectors (r):");
141           vec_foreach(ts, child->tsr)
142             {
143               vlib_cli_output(vm, "      %u type %u protocol_id %u addr "
144                               "%U - %U port %u - %u",
145                               ts - child->tsr,
146                               ts->ts_type, ts->protocol_id,
147                               format_ip4_address, &ts->start_addr,
148                               format_ip4_address, &ts->end_addr,
149                               clib_net_to_host_u16( ts->start_port),
150                               clib_net_to_host_u16( ts->end_port));
151             }
152         }
153       vlib_cli_output(vm, "");
154     }));
155   }
156   return 0;
157 }
158
159 VLIB_CLI_COMMAND (show_ikev2_sa_command, static) = {
160     .path = "show ikev2 sa",
161     .short_help = "show ikev2 sa",
162     .function = show_ikev2_sa_command_fn,
163 };
164
165 static clib_error_t *
166 ikev2_profile_add_del_command_fn (vlib_main_t * vm,
167                                  unformat_input_t * input,
168                                  vlib_cli_command_t * cmd)
169 {
170   unformat_input_t _line_input, * line_input = &_line_input;
171   u8 * name = 0;
172   clib_error_t * r = 0;
173   u32 id_type;
174   u8 * data = 0;
175   u32 tmp1, tmp2, tmp3;
176   ip4_address_t ip4;
177   ip4_address_t end_addr;
178
179   const char * valid_chars = "a-zA-Z0-9_";
180
181   if (! unformat_user (input, unformat_line_input, line_input))
182     return 0;
183
184   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
185     if (unformat (line_input, "add %U", unformat_token, valid_chars, &name))
186         {
187           r = ikev2_add_del_profile(vm, name, 1);
188           goto done;
189         }
190     else if (unformat (line_input, "del %U", unformat_token, valid_chars, &name))
191         {
192           r = ikev2_add_del_profile(vm, name, 0);
193           goto done;
194         }
195     else if (unformat (line_input, "set %U auth shared-key-mic string %v",
196                        unformat_token, valid_chars, &name, &data))
197         {
198           r = ikev2_set_profile_auth(vm, name, IKEV2_AUTH_METHOD_SHARED_KEY_MIC,
199                                      data, 0);
200           goto done;
201         }
202     else if (unformat (line_input, "set %U auth shared-key-mic hex %U",
203                        unformat_token, valid_chars, &name,
204                        unformat_hex_string, &data))
205         {
206           r = ikev2_set_profile_auth(vm, name, IKEV2_AUTH_METHOD_SHARED_KEY_MIC,
207                                      data, 1);
208           goto done;
209         }
210     else if (unformat (line_input, "set %U auth rsa-sig cert-file %v",
211                        unformat_token, valid_chars, &name,
212                        &data))
213         {
214           r = ikev2_set_profile_auth(vm, name, IKEV2_AUTH_METHOD_RSA_SIG, data, 0);
215           goto done;
216         }
217     else if (unformat (line_input, "set %U id local %U %U",
218                        unformat_token, valid_chars, &name,
219                        unformat_ikev2_id_type, &id_type,
220                        unformat_ip4_address, &ip4))
221         {
222           data = vec_new(u8, 4);
223           clib_memcpy(data, ip4.as_u8, 4);
224           r = ikev2_set_profile_id(vm, name, (u8) id_type, data, /*local*/ 1);
225           goto done;
226         }
227     else if (unformat (line_input, "set %U id local %U 0x%U",
228                        unformat_token, valid_chars, &name,
229                        unformat_ikev2_id_type, &id_type,
230                        unformat_hex_string, &data))
231         {
232           r = ikev2_set_profile_id(vm, name, (u8) id_type, data, /*local*/ 1);
233           goto done;
234         }
235     else if (unformat (line_input, "set %U id local %U %v",
236                        unformat_token, valid_chars, &name,
237                        unformat_ikev2_id_type, &id_type, &data))
238         {
239           r = ikev2_set_profile_id(vm, name, (u8) id_type, data, /*local*/ 1);
240           goto done;
241         }
242     else if (unformat (line_input, "set %U id remote %U %U",
243                        unformat_token, valid_chars, &name,
244                        unformat_ikev2_id_type, &id_type,
245                        unformat_ip4_address, &ip4))
246         {
247           data = vec_new(u8, 4);
248           clib_memcpy(data, ip4.as_u8, 4);
249           r = ikev2_set_profile_id(vm, name, (u8) id_type, data, /*remote*/ 0);
250           goto done;
251         }
252     else if (unformat (line_input, "set %U id remote %U 0x%U",
253                        unformat_token, valid_chars, &name,
254                        unformat_ikev2_id_type, &id_type,
255                        unformat_hex_string, &data))
256         {
257           r = ikev2_set_profile_id(vm, name, (u8) id_type, data, /*remote*/ 0);
258           goto done;
259         }
260     else if (unformat (line_input, "set %U id remote %U %v",
261                        unformat_token, valid_chars, &name,
262                        unformat_ikev2_id_type, &id_type, &data))
263         {
264           r = ikev2_set_profile_id(vm, name, (u8) id_type, data, /*remote*/ 0);
265           goto done;
266         }
267     else if (unformat (line_input, "set %U traffic-selector local "
268                        "ip-range %U - %U port-range %u - %u protocol %u",
269                        unformat_token, valid_chars, &name,
270                        unformat_ip4_address, &ip4,
271                        unformat_ip4_address, &end_addr,
272                        &tmp1, &tmp2, &tmp3))
273         {
274           r = ikev2_set_profile_ts(vm, name, (u8)tmp3, (u16)tmp1, (u16)tmp2,
275                                    ip4, end_addr, /*local*/ 1);
276           goto done;
277         }
278     else if (unformat (line_input, "set %U traffic-selector remote "
279                        "ip-range %U - %U port-range %u - %u protocol %u",
280                        unformat_token, valid_chars, &name,
281                        unformat_ip4_address, &ip4,
282                        unformat_ip4_address, &end_addr,
283                        &tmp1, &tmp2, &tmp3))
284         {
285           r = ikev2_set_profile_ts(vm, name, (u8)tmp3, (u16)tmp1, (u16)tmp2,
286                                    ip4, end_addr, /*remote*/ 0);
287           goto done;
288         }
289     else
290       break;
291   }
292
293   r = clib_error_return (0, "parse error: '%U'",
294                          format_unformat_error, line_input);
295
296 done:
297   vec_free(name);
298   vec_free(data);
299   unformat_free (line_input);
300   return r;
301 }
302
303 VLIB_CLI_COMMAND (ikev2_profile_add_del_command, static) = {
304     .path = "ikev2 profile",
305     .short_help =
306     "ikev2 profile [add|del] <id>\n"
307     "ikev2 profile set <id> auth [rsa-sig|shared-key-mic] [cert-file|string|hex]"
308     " <data>\n"
309     "ikev2 profile set <id> id <local|remote> <type> <data>\n"
310     "ikev2 profile set <id> traffic-selector <local|remote> ip-range "
311     "<start-addr> - <end-addr> port-range <start-port> - <end-port> "
312     "protocol <protocol-number>",
313     .function = ikev2_profile_add_del_command_fn,
314 };
315
316 static clib_error_t *
317 show_ikev2_profile_command_fn (vlib_main_t * vm,
318                                unformat_input_t * input,
319                                vlib_cli_command_t * cmd)
320 {
321   ikev2_main_t * km = &ikev2_main;
322   ikev2_profile_t * p;
323
324   pool_foreach (p, km->profiles, ({
325     vlib_cli_output(vm, "profile %v", p->name);
326
327     if (p->auth.data)
328       {
329         if (p->auth.hex)
330           vlib_cli_output(vm, "  auth-method %U auth data 0x%U",
331                           format_ikev2_auth_method, p->auth.method,
332                           format_hex_bytes, p->auth.data, vec_len(p->auth.data));
333         else
334           vlib_cli_output(vm, "  auth-method %U auth data %v",
335                    format_ikev2_auth_method, p->auth.method, p->auth.data);
336       }
337
338     if (p->loc_id.data)
339       {
340         if (p->loc_id.type == IKEV2_ID_TYPE_ID_IPV4_ADDR)
341           vlib_cli_output(vm, "  local id-type %U data %U",
342                           format_ikev2_id_type, p->loc_id.type,
343                           format_ip4_address, p->loc_id.data);
344         else if (p->loc_id.type == IKEV2_ID_TYPE_ID_KEY_ID)
345           vlib_cli_output(vm, "  local id-type %U data 0x%U",
346                           format_ikev2_id_type, p->loc_id.type,
347                           format_hex_bytes, p->loc_id.data,
348                           vec_len(p->loc_id.data));
349         else
350           vlib_cli_output(vm, "  local id-type %U data %v",
351                           format_ikev2_id_type, p->loc_id.type, p->loc_id.data);
352       }
353
354     if (p->rem_id.data)
355       {
356         if (p->rem_id.type == IKEV2_ID_TYPE_ID_IPV4_ADDR)
357           vlib_cli_output(vm, "  remote id-type %U data %U",
358                           format_ikev2_id_type, p->rem_id.type,
359                           format_ip4_address, p->rem_id.data);
360         else if (p->rem_id.type == IKEV2_ID_TYPE_ID_KEY_ID)
361           vlib_cli_output(vm, "  remote id-type %U data 0x%U",
362                           format_ikev2_id_type, p->rem_id.type,
363                           format_hex_bytes, p->rem_id.data,
364                           vec_len(p->rem_id.data));
365         else
366           vlib_cli_output(vm, "  remote id-type %U data %v",
367                           format_ikev2_id_type, p->rem_id.type, p->rem_id.data);
368       }
369
370     if (p->loc_ts.end_addr.as_u32)
371       vlib_cli_output(vm, "  local traffic-selector addr %U - %U port %u - %u"
372                       " protocol %u",
373                       format_ip4_address, &p->loc_ts.start_addr,
374                       format_ip4_address, &p->loc_ts.end_addr,
375                       p->loc_ts.start_port, p->loc_ts.end_port,
376                       p->loc_ts.protocol_id);
377
378     if (p->rem_ts.end_addr.as_u32)
379       vlib_cli_output(vm, "  remote traffic-selector addr %U - %U port %u - %u"
380                       " protocol %u",
381                       format_ip4_address, &p->rem_ts.start_addr,
382                       format_ip4_address, &p->rem_ts.end_addr,
383                       p->rem_ts.start_port, p->rem_ts.end_port,
384                       p->rem_ts.protocol_id);
385   }));
386
387   return 0;
388 }
389
390 VLIB_CLI_COMMAND (show_ikev2_profile_command, static) = {
391     .path = "show ikev2 profile",
392     .short_help = "show ikev2 profile",
393     .function = show_ikev2_profile_command_fn,
394 };
395
396 static clib_error_t *
397 set_ikev2_local_key_command_fn (vlib_main_t * vm,
398                                 unformat_input_t * input,
399                                 vlib_cli_command_t * cmd)
400 {
401   unformat_input_t _line_input, * line_input = &_line_input;
402   clib_error_t * r = 0;
403   u8 * data = 0;
404
405   if (! unformat_user (input, unformat_line_input, line_input))
406     return 0;
407
408   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
409     if (unformat (line_input, "%v", &data))
410       {
411         r = ikev2_set_local_key(vm, data);
412         goto done;
413       }
414     else
415       break;
416   }
417
418   r = clib_error_return (0, "parse error: '%U'",
419                          format_unformat_error, line_input);
420
421 done:
422   vec_free(data);
423   unformat_free (line_input);
424   return r;
425 }
426
427 VLIB_CLI_COMMAND (set_ikev2_local_key_command, static) = {
428     .path = "set ikev2 local key",
429     .short_help =
430     "set ikev2 local key <file>",
431     .function = set_ikev2_local_key_command_fn,
432 };
433
434 clib_error_t *
435 ikev2_cli_init (vlib_main_t * vm)
436 {
437   return 0;
438 }
439
440 VLIB_INIT_FUNCTION (ikev2_cli_init);