ikev2: make liveness params configurable
[vpp.git] / src / plugins / ikev2 / ikev2_api.c
1 /*
2  *------------------------------------------------------------------
3  * ipsec_api.c - ipsec api
4  *
5  * Copyright (c) 2016 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *------------------------------------------------------------------
18  */
19
20 #include <vnet/vnet.h>
21 #include <vlibmemory/api.h>
22 #include <vnet/api_errno.h>
23 #include <vpp/app/version.h>
24
25 #include <ikev2/ikev2.h>
26 #include <ikev2/ikev2_priv.h>
27
28 /* define message IDs */
29 #include <vnet/format_fns.h>
30 #include <plugins/ikev2/ikev2.api_enum.h>
31 #include <plugins/ikev2/ikev2.api_types.h>
32
33 extern ikev2_main_t ikev2_main;
34
35 #define IKEV2_PLUGIN_VERSION_MAJOR 1
36 #define IKEV2_PLUGIN_VERSION_MINOR 0
37 #define REPLY_MSG_ID_BASE ikev2_main.msg_id_base
38 #include <vlibapi/api_helper_macros.h>
39
40 static void
41 vl_api_ikev2_plugin_get_version_t_handler (vl_api_ikev2_plugin_get_version_t *
42                                            mp)
43 {
44   ikev2_main_t *im = &ikev2_main;
45   vl_api_ikev2_plugin_get_version_reply_t *rmp;
46   int msg_size = sizeof (*rmp);
47   vl_api_registration_t *reg;
48
49   reg = vl_api_client_index_to_registration (mp->client_index);
50   if (!reg)
51     return;
52
53   rmp = vl_msg_api_alloc (msg_size);
54   clib_memset (rmp, 0, msg_size);
55   rmp->_vl_msg_id =
56     ntohs (VL_API_IKEV2_PLUGIN_GET_VERSION_REPLY + im->msg_id_base);
57   rmp->context = mp->context;
58   rmp->major = htonl (IKEV2_PLUGIN_VERSION_MAJOR);
59   rmp->minor = htonl (IKEV2_PLUGIN_VERSION_MINOR);
60
61   vl_api_send_msg (reg, (u8 *) rmp);
62 }
63
64 static void
65   vl_api_ikev2_profile_set_liveness_t_handler
66   (vl_api_ikev2_profile_set_liveness_t * mp)
67 {
68   vl_api_ikev2_profile_set_liveness_reply_t *rmp;
69   int rv = 0;
70
71 #if WITH_LIBSSL > 0
72   clib_error_t *error;
73   error = ikev2_set_liveness_params (clib_net_to_host_u32 (mp->period),
74                                      clib_net_to_host_u32 (mp->max_retries));
75   if (error)
76     rv = VNET_API_ERROR_UNSPECIFIED;
77 #else
78   rv = VNET_API_ERROR_UNIMPLEMENTED;
79 #endif
80
81   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_LIVENESS_REPLY);
82 }
83
84 static void
85 vl_api_ikev2_profile_add_del_t_handler (vl_api_ikev2_profile_add_del_t * mp)
86 {
87   vl_api_ikev2_profile_add_del_reply_t *rmp;
88   int rv = 0;
89
90 #if WITH_LIBSSL > 0
91   vlib_main_t *vm = vlib_get_main ();
92   clib_error_t *error;
93   u8 *tmp = format (0, "%s", mp->name);
94   error = ikev2_add_del_profile (vm, tmp, mp->is_add);
95   vec_free (tmp);
96   if (error)
97     rv = VNET_API_ERROR_UNSPECIFIED;
98 #else
99   rv = VNET_API_ERROR_UNIMPLEMENTED;
100 #endif
101
102   REPLY_MACRO (VL_API_IKEV2_PROFILE_ADD_DEL_REPLY);
103 }
104
105 static void
106   vl_api_ikev2_profile_set_auth_t_handler
107   (vl_api_ikev2_profile_set_auth_t * mp)
108 {
109   vl_api_ikev2_profile_set_auth_reply_t *rmp;
110   int rv = 0;
111
112 #if WITH_LIBSSL > 0
113   vlib_main_t *vm = vlib_get_main ();
114   clib_error_t *error;
115   int data_len = ntohl (mp->data_len);
116   u8 *tmp = format (0, "%s", mp->name);
117   u8 *data = vec_new (u8, data_len);
118   clib_memcpy (data, mp->data, data_len);
119   error = ikev2_set_profile_auth (vm, tmp, mp->auth_method, data, mp->is_hex);
120   vec_free (tmp);
121   vec_free (data);
122   if (error)
123     rv = VNET_API_ERROR_UNSPECIFIED;
124 #else
125   rv = VNET_API_ERROR_UNIMPLEMENTED;
126 #endif
127
128   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_AUTH_REPLY);
129 }
130
131 static void
132 vl_api_ikev2_profile_set_id_t_handler (vl_api_ikev2_profile_set_id_t * mp)
133 {
134   vl_api_ikev2_profile_set_id_reply_t *rmp;
135   int rv = 0;
136
137 #if WITH_LIBSSL > 0
138   vlib_main_t *vm = vlib_get_main ();
139   clib_error_t *error;
140   u8 *tmp = format (0, "%s", mp->name);
141   int data_len = ntohl (mp->data_len);
142   u8 *data = vec_new (u8, data_len);
143   clib_memcpy (data, mp->data, data_len);
144   error = ikev2_set_profile_id (vm, tmp, mp->id_type, data, mp->is_local);
145   vec_free (tmp);
146   vec_free (data);
147   if (error)
148     rv = VNET_API_ERROR_UNSPECIFIED;
149 #else
150   rv = VNET_API_ERROR_UNIMPLEMENTED;
151 #endif
152
153   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_ID_REPLY);
154 }
155
156 static void
157   vl_api_ikev2_profile_set_udp_encap_t_handler
158   (vl_api_ikev2_profile_set_udp_encap_t * mp)
159 {
160   vl_api_ikev2_profile_set_udp_encap_reply_t *rmp;
161   int rv = 0;
162
163 #if WITH_LIBSSL > 0
164   vlib_main_t *vm = vlib_get_main ();
165   clib_error_t *error;
166   u8 *tmp = format (0, "%s", mp->name);
167   error = ikev2_set_profile_udp_encap (vm, tmp);
168   vec_free (tmp);
169   if (error)
170     rv = VNET_API_ERROR_UNSPECIFIED;
171 #else
172   rv = VNET_API_ERROR_UNIMPLEMENTED;
173 #endif
174
175   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_UDP_ENCAP);
176 }
177
178 static void
179 vl_api_ikev2_profile_set_ts_t_handler (vl_api_ikev2_profile_set_ts_t * mp)
180 {
181   vl_api_ikev2_profile_set_ts_reply_t *rmp;
182   int rv = 0;
183
184 #if WITH_LIBSSL > 0
185   vlib_main_t *vm = vlib_get_main ();
186   clib_error_t *error;
187   u8 *tmp = format (0, "%s", mp->name);
188   error =
189     ikev2_set_profile_ts (vm, tmp, mp->proto,
190                           clib_net_to_host_u16 (mp->start_port),
191                           clib_net_to_host_u16 (mp->end_port),
192                           (ip4_address_t) mp->start_addr,
193                           (ip4_address_t) mp->end_addr, mp->is_local);
194   vec_free (tmp);
195   if (error)
196     rv = VNET_API_ERROR_UNSPECIFIED;
197 #else
198   rv = VNET_API_ERROR_UNIMPLEMENTED;
199 #endif
200
201   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_TS_REPLY);
202 }
203
204 static void
205 vl_api_ikev2_set_local_key_t_handler (vl_api_ikev2_set_local_key_t * mp)
206 {
207   vl_api_ikev2_set_local_key_reply_t *rmp;
208   int rv = 0;
209
210 #if WITH_LIBSSL > 0
211   vlib_main_t *vm = vlib_get_main ();
212   clib_error_t *error;
213
214   error = ikev2_set_local_key (vm, mp->key_file);
215   if (error)
216     rv = VNET_API_ERROR_UNSPECIFIED;
217 #else
218   rv = VNET_API_ERROR_UNIMPLEMENTED;
219 #endif
220
221   REPLY_MACRO (VL_API_IKEV2_SET_LOCAL_KEY_REPLY);
222 }
223
224 static void
225 vl_api_ikev2_set_responder_t_handler (vl_api_ikev2_set_responder_t * mp)
226 {
227   vl_api_ikev2_set_responder_reply_t *rmp;
228   int rv = 0;
229
230 #if WITH_LIBSSL > 0
231   vlib_main_t *vm = vlib_get_main ();
232   clib_error_t *error;
233
234   u8 *tmp = format (0, "%s", mp->name);
235   ip4_address_t ip4;
236   clib_memcpy (&ip4, mp->address, sizeof (ip4));
237
238   error = ikev2_set_profile_responder (vm, tmp, ntohl (mp->sw_if_index), ip4);
239   vec_free (tmp);
240   if (error)
241     rv = VNET_API_ERROR_UNSPECIFIED;
242 #else
243   rv = VNET_API_ERROR_UNIMPLEMENTED;
244 #endif
245
246   REPLY_MACRO (VL_API_IKEV2_SET_RESPONDER_REPLY);
247 }
248
249 static void
250 vl_api_ikev2_set_ike_transforms_t_handler (vl_api_ikev2_set_ike_transforms_t *
251                                            mp)
252 {
253   vl_api_ikev2_set_ike_transforms_reply_t *rmp;
254   int rv = 0;
255
256 #if WITH_LIBSSL > 0
257   vlib_main_t *vm = vlib_get_main ();
258   clib_error_t *error;
259
260   u8 *tmp = format (0, "%s", mp->name);
261
262   error =
263     ikev2_set_profile_ike_transforms (vm, tmp, ntohl (mp->crypto_alg),
264                                       ntohl (mp->integ_alg),
265                                       ntohl (mp->dh_group),
266                                       ntohl (mp->crypto_key_size));
267   vec_free (tmp);
268   if (error)
269     rv = VNET_API_ERROR_UNSPECIFIED;
270 #else
271   rv = VNET_API_ERROR_UNIMPLEMENTED;
272 #endif
273
274   REPLY_MACRO (VL_API_IKEV2_SET_IKE_TRANSFORMS_REPLY);
275 }
276
277 static void
278 vl_api_ikev2_set_esp_transforms_t_handler (vl_api_ikev2_set_esp_transforms_t *
279                                            mp)
280 {
281   vl_api_ikev2_set_esp_transforms_reply_t *rmp;
282   int rv = 0;
283
284 #if WITH_LIBSSL > 0
285   vlib_main_t *vm = vlib_get_main ();
286   clib_error_t *error;
287
288   u8 *tmp = format (0, "%s", mp->name);
289
290   error =
291     ikev2_set_profile_esp_transforms (vm, tmp, ntohl (mp->crypto_alg),
292                                       ntohl (mp->integ_alg),
293                                       ntohl (mp->dh_group),
294                                       ntohl (mp->crypto_key_size));
295   vec_free (tmp);
296   if (error)
297     rv = VNET_API_ERROR_UNSPECIFIED;
298 #else
299   rv = VNET_API_ERROR_UNIMPLEMENTED;
300 #endif
301
302   REPLY_MACRO (VL_API_IKEV2_SET_ESP_TRANSFORMS_REPLY);
303 }
304
305 static void
306 vl_api_ikev2_set_sa_lifetime_t_handler (vl_api_ikev2_set_sa_lifetime_t * mp)
307 {
308   vl_api_ikev2_set_sa_lifetime_reply_t *rmp;
309   int rv = 0;
310
311 #if WITH_LIBSSL > 0
312   vlib_main_t *vm = vlib_get_main ();
313   clib_error_t *error;
314
315   u8 *tmp = format (0, "%s", mp->name);
316
317   error =
318     ikev2_set_profile_sa_lifetime (vm, tmp,
319                                    clib_net_to_host_u64 (mp->lifetime),
320                                    ntohl (mp->lifetime_jitter),
321                                    ntohl (mp->handover),
322                                    clib_net_to_host_u64
323                                    (mp->lifetime_maxdata));
324   vec_free (tmp);
325   if (error)
326     rv = VNET_API_ERROR_UNSPECIFIED;
327 #else
328   rv = VNET_API_ERROR_UNIMPLEMENTED;
329 #endif
330
331   REPLY_MACRO (VL_API_IKEV2_SET_SA_LIFETIME_REPLY);
332 }
333
334 static void
335   vl_api_ikev2_profile_set_ipsec_udp_port_t_handler
336   (vl_api_ikev2_profile_set_ipsec_udp_port_t * mp)
337 {
338   vl_api_ikev2_profile_set_ipsec_udp_port_reply_t *rmp;
339   int rv = 0;
340
341 #if WITH_LIBSSL > 0
342   vlib_main_t *vm = vlib_get_main ();
343
344   u8 *tmp = format (0, "%s", mp->name);
345
346   rv =
347     ikev2_set_profile_ipsec_udp_port (vm, tmp,
348                                       clib_net_to_host_u16 (mp->port),
349                                       mp->is_set);
350   vec_free (tmp);
351 #else
352   rv = VNET_API_ERROR_UNIMPLEMENTED;
353 #endif
354
355   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_IPSEC_UDP_PORT_REPLY);
356 }
357
358 static void
359   vl_api_ikev2_set_tunnel_interface_t_handler
360   (vl_api_ikev2_set_tunnel_interface_t * mp)
361 {
362   vl_api_ikev2_set_tunnel_interface_reply_t *rmp;
363   int rv = 0;
364
365   VALIDATE_SW_IF_INDEX (mp);
366
367 #if WITH_LIBSSL > 0
368   u8 *tmp = format (0, "%s", mp->name);
369   clib_error_t *error;
370
371   error = ikev2_set_profile_tunnel_interface (vlib_get_main (), tmp,
372                                               ntohl (mp->sw_if_index));
373
374   if (error)
375     rv = VNET_API_ERROR_UNSPECIFIED;
376   vec_free (tmp);
377 #else
378   rv = VNET_API_ERROR_UNIMPLEMENTED;
379 #endif
380
381   BAD_SW_IF_INDEX_LABEL;
382   REPLY_MACRO (VL_API_IKEV2_SET_TUNNEL_INTERFACE_REPLY);
383 }
384
385 static void
386 vl_api_ikev2_initiate_sa_init_t_handler (vl_api_ikev2_initiate_sa_init_t * mp)
387 {
388   vl_api_ikev2_initiate_sa_init_reply_t *rmp;
389   int rv = 0;
390
391 #if WITH_LIBSSL > 0
392   vlib_main_t *vm = vlib_get_main ();
393   clib_error_t *error;
394
395   u8 *tmp = format (0, "%s", mp->name);
396
397   error = ikev2_initiate_sa_init (vm, tmp);
398   vec_free (tmp);
399   if (error)
400     rv = VNET_API_ERROR_UNSPECIFIED;
401 #else
402   rv = VNET_API_ERROR_UNIMPLEMENTED;
403 #endif
404
405   REPLY_MACRO (VL_API_IKEV2_INITIATE_SA_INIT_REPLY);
406 }
407
408 static void
409 vl_api_ikev2_initiate_del_ike_sa_t_handler (vl_api_ikev2_initiate_del_ike_sa_t
410                                             * mp)
411 {
412   vl_api_ikev2_initiate_del_ike_sa_reply_t *rmp;
413   int rv = 0;
414
415 #if WITH_LIBSSL > 0
416   vlib_main_t *vm = vlib_get_main ();
417   clib_error_t *error;
418
419   error = ikev2_initiate_delete_ike_sa (vm, mp->ispi);
420   if (error)
421     rv = VNET_API_ERROR_UNSPECIFIED;
422 #else
423   rv = VNET_API_ERROR_UNIMPLEMENTED;
424 #endif
425
426   REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_IKE_SA_REPLY);
427 }
428
429 static void
430   vl_api_ikev2_initiate_del_child_sa_t_handler
431   (vl_api_ikev2_initiate_del_child_sa_t * mp)
432 {
433   vl_api_ikev2_initiate_del_child_sa_reply_t *rmp;
434   int rv = 0;
435
436 #if WITH_LIBSSL > 0
437   vlib_main_t *vm = vlib_get_main ();
438   clib_error_t *error;
439
440   error = ikev2_initiate_delete_child_sa (vm, mp->ispi);
441   if (error)
442     rv = VNET_API_ERROR_UNSPECIFIED;
443 #else
444   rv = VNET_API_ERROR_UNIMPLEMENTED;
445 #endif
446
447   REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_CHILD_SA_REPLY);
448 }
449
450 static void
451   vl_api_ikev2_initiate_rekey_child_sa_t_handler
452   (vl_api_ikev2_initiate_rekey_child_sa_t * mp)
453 {
454   vl_api_ikev2_initiate_rekey_child_sa_reply_t *rmp;
455   int rv = 0;
456
457 #if WITH_LIBSSL > 0
458   vlib_main_t *vm = vlib_get_main ();
459   clib_error_t *error;
460
461   error = ikev2_initiate_rekey_child_sa (vm, mp->ispi);
462   if (error)
463     rv = VNET_API_ERROR_UNSPECIFIED;
464 #else
465   rv = VNET_API_ERROR_UNIMPLEMENTED;
466 #endif
467
468   REPLY_MACRO (VL_API_IKEV2_INITIATE_REKEY_CHILD_SA_REPLY);
469 }
470
471 #include <ikev2/ikev2.api.c>
472 static clib_error_t *
473 ikev2_api_init (vlib_main_t * vm)
474 {
475   ikev2_main_t *im = &ikev2_main;
476
477   /* Ask for a correctly-sized block of API message decode slots */
478   im->msg_id_base = setup_message_id_table ();
479
480   return 0;
481 }
482
483 VLIB_INIT_FUNCTION (ikev2_api_init);
484
485 /*
486  * fd.io coding-style-patch-verification: ON
487  *
488  * Local Variables:
489  * eval: (c-set-style "gnu")
490  * End:
491  */