ikev2: fix incorrect reply messages
[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_add_del_t_handler (vl_api_ikev2_profile_add_del_t * mp)
66 {
67   vl_api_ikev2_profile_add_del_reply_t *rmp;
68   int rv = 0;
69
70 #if WITH_LIBSSL > 0
71   vlib_main_t *vm = vlib_get_main ();
72   clib_error_t *error;
73   u8 *tmp = format (0, "%s", mp->name);
74   error = ikev2_add_del_profile (vm, tmp, mp->is_add);
75   vec_free (tmp);
76   if (error)
77     rv = VNET_API_ERROR_UNSPECIFIED;
78 #else
79   rv = VNET_API_ERROR_UNIMPLEMENTED;
80 #endif
81
82   REPLY_MACRO (VL_API_IKEV2_PROFILE_ADD_DEL_REPLY);
83 }
84
85 static void
86   vl_api_ikev2_profile_set_auth_t_handler
87   (vl_api_ikev2_profile_set_auth_t * mp)
88 {
89   vl_api_ikev2_profile_set_auth_reply_t *rmp;
90   int rv = 0;
91
92 #if WITH_LIBSSL > 0
93   vlib_main_t *vm = vlib_get_main ();
94   clib_error_t *error;
95   int data_len = ntohl (mp->data_len);
96   u8 *tmp = format (0, "%s", mp->name);
97   u8 *data = vec_new (u8, data_len);
98   clib_memcpy (data, mp->data, data_len);
99   error = ikev2_set_profile_auth (vm, tmp, mp->auth_method, data, mp->is_hex);
100   vec_free (tmp);
101   vec_free (data);
102   if (error)
103     rv = VNET_API_ERROR_UNSPECIFIED;
104 #else
105   rv = VNET_API_ERROR_UNIMPLEMENTED;
106 #endif
107
108   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_AUTH_REPLY);
109 }
110
111 static void
112 vl_api_ikev2_profile_set_id_t_handler (vl_api_ikev2_profile_set_id_t * mp)
113 {
114   vl_api_ikev2_profile_set_id_reply_t *rmp;
115   int rv = 0;
116
117 #if WITH_LIBSSL > 0
118   vlib_main_t *vm = vlib_get_main ();
119   clib_error_t *error;
120   u8 *tmp = format (0, "%s", mp->name);
121   int data_len = ntohl (mp->data_len);
122   u8 *data = vec_new (u8, data_len);
123   clib_memcpy (data, mp->data, data_len);
124   error = ikev2_set_profile_id (vm, tmp, mp->id_type, data, mp->is_local);
125   vec_free (tmp);
126   vec_free (data);
127   if (error)
128     rv = VNET_API_ERROR_UNSPECIFIED;
129 #else
130   rv = VNET_API_ERROR_UNIMPLEMENTED;
131 #endif
132
133   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_ID_REPLY);
134 }
135
136 static void
137 vl_api_ikev2_profile_set_ts_t_handler (vl_api_ikev2_profile_set_ts_t * mp)
138 {
139   vl_api_ikev2_profile_set_ts_reply_t *rmp;
140   int rv = 0;
141
142 #if WITH_LIBSSL > 0
143   vlib_main_t *vm = vlib_get_main ();
144   clib_error_t *error;
145   u8 *tmp = format (0, "%s", mp->name);
146   error =
147     ikev2_set_profile_ts (vm, tmp, mp->proto,
148                           clib_net_to_host_u16 (mp->start_port),
149                           clib_net_to_host_u16 (mp->end_port),
150                           (ip4_address_t) mp->start_addr,
151                           (ip4_address_t) mp->end_addr, mp->is_local);
152   vec_free (tmp);
153   if (error)
154     rv = VNET_API_ERROR_UNSPECIFIED;
155 #else
156   rv = VNET_API_ERROR_UNIMPLEMENTED;
157 #endif
158
159   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_TS_REPLY);
160 }
161
162 static void
163 vl_api_ikev2_set_local_key_t_handler (vl_api_ikev2_set_local_key_t * mp)
164 {
165   vl_api_ikev2_set_local_key_reply_t *rmp;
166   int rv = 0;
167
168 #if WITH_LIBSSL > 0
169   vlib_main_t *vm = vlib_get_main ();
170   clib_error_t *error;
171
172   error = ikev2_set_local_key (vm, mp->key_file);
173   if (error)
174     rv = VNET_API_ERROR_UNSPECIFIED;
175 #else
176   rv = VNET_API_ERROR_UNIMPLEMENTED;
177 #endif
178
179   REPLY_MACRO (VL_API_IKEV2_SET_LOCAL_KEY_REPLY);
180 }
181
182 static void
183 vl_api_ikev2_set_responder_t_handler (vl_api_ikev2_set_responder_t * mp)
184 {
185   vl_api_ikev2_set_responder_reply_t *rmp;
186   int rv = 0;
187
188 #if WITH_LIBSSL > 0
189   vlib_main_t *vm = vlib_get_main ();
190   clib_error_t *error;
191
192   u8 *tmp = format (0, "%s", mp->name);
193   ip4_address_t ip4;
194   clib_memcpy (&ip4, mp->address, sizeof (ip4));
195
196   error = ikev2_set_profile_responder (vm, tmp, ntohl (mp->sw_if_index), ip4);
197   vec_free (tmp);
198   if (error)
199     rv = VNET_API_ERROR_UNSPECIFIED;
200 #else
201   rv = VNET_API_ERROR_UNIMPLEMENTED;
202 #endif
203
204   REPLY_MACRO (VL_API_IKEV2_SET_RESPONDER_REPLY);
205 }
206
207 static void
208 vl_api_ikev2_set_ike_transforms_t_handler (vl_api_ikev2_set_ike_transforms_t *
209                                            mp)
210 {
211   vl_api_ikev2_set_ike_transforms_reply_t *rmp;
212   int rv = 0;
213
214 #if WITH_LIBSSL > 0
215   vlib_main_t *vm = vlib_get_main ();
216   clib_error_t *error;
217
218   u8 *tmp = format (0, "%s", mp->name);
219
220   error =
221     ikev2_set_profile_ike_transforms (vm, tmp, ntohl (mp->crypto_alg),
222                                       ntohl (mp->integ_alg),
223                                       ntohl (mp->dh_group),
224                                       ntohl (mp->crypto_key_size));
225   vec_free (tmp);
226   if (error)
227     rv = VNET_API_ERROR_UNSPECIFIED;
228 #else
229   rv = VNET_API_ERROR_UNIMPLEMENTED;
230 #endif
231
232   REPLY_MACRO (VL_API_IKEV2_SET_IKE_TRANSFORMS_REPLY);
233 }
234
235 static void
236 vl_api_ikev2_set_esp_transforms_t_handler (vl_api_ikev2_set_esp_transforms_t *
237                                            mp)
238 {
239   vl_api_ikev2_set_esp_transforms_reply_t *rmp;
240   int rv = 0;
241
242 #if WITH_LIBSSL > 0
243   vlib_main_t *vm = vlib_get_main ();
244   clib_error_t *error;
245
246   u8 *tmp = format (0, "%s", mp->name);
247
248   error =
249     ikev2_set_profile_esp_transforms (vm, tmp, ntohl (mp->crypto_alg),
250                                       ntohl (mp->integ_alg),
251                                       ntohl (mp->dh_group),
252                                       ntohl (mp->crypto_key_size));
253   vec_free (tmp);
254   if (error)
255     rv = VNET_API_ERROR_UNSPECIFIED;
256 #else
257   rv = VNET_API_ERROR_UNIMPLEMENTED;
258 #endif
259
260   REPLY_MACRO (VL_API_IKEV2_SET_ESP_TRANSFORMS_REPLY);
261 }
262
263 static void
264 vl_api_ikev2_set_sa_lifetime_t_handler (vl_api_ikev2_set_sa_lifetime_t * mp)
265 {
266   vl_api_ikev2_set_sa_lifetime_reply_t *rmp;
267   int rv = 0;
268
269 #if WITH_LIBSSL > 0
270   vlib_main_t *vm = vlib_get_main ();
271   clib_error_t *error;
272
273   u8 *tmp = format (0, "%s", mp->name);
274
275   error =
276     ikev2_set_profile_sa_lifetime (vm, tmp,
277                                    clib_net_to_host_u64 (mp->lifetime),
278                                    ntohl (mp->lifetime_jitter),
279                                    ntohl (mp->handover),
280                                    clib_net_to_host_u64
281                                    (mp->lifetime_maxdata));
282   vec_free (tmp);
283   if (error)
284     rv = VNET_API_ERROR_UNSPECIFIED;
285 #else
286   rv = VNET_API_ERROR_UNIMPLEMENTED;
287 #endif
288
289   REPLY_MACRO (VL_API_IKEV2_SET_SA_LIFETIME_REPLY);
290 }
291
292 static void
293   vl_api_ikev2_set_tunnel_interface_t_handler
294   (vl_api_ikev2_set_tunnel_interface_t * mp)
295 {
296   vl_api_ikev2_set_tunnel_interface_reply_t *rmp;
297   int rv = 0;
298
299   VALIDATE_SW_IF_INDEX (mp);
300
301 #if WITH_LIBSSL > 0
302   u8 *tmp = format (0, "%s", mp->name);
303   clib_error_t *error;
304
305   error = ikev2_set_profile_tunnel_interface (vlib_get_main (), tmp,
306                                               ntohl (mp->sw_if_index));
307
308   if (error)
309     rv = VNET_API_ERROR_UNSPECIFIED;
310   vec_free (tmp);
311 #else
312   rv = VNET_API_ERROR_UNIMPLEMENTED;
313 #endif
314
315   BAD_SW_IF_INDEX_LABEL;
316   REPLY_MACRO (VL_API_IKEV2_SET_TUNNEL_INTERFACE_REPLY);
317 }
318
319 static void
320 vl_api_ikev2_initiate_sa_init_t_handler (vl_api_ikev2_initiate_sa_init_t * mp)
321 {
322   vl_api_ikev2_initiate_sa_init_reply_t *rmp;
323   int rv = 0;
324
325 #if WITH_LIBSSL > 0
326   vlib_main_t *vm = vlib_get_main ();
327   clib_error_t *error;
328
329   u8 *tmp = format (0, "%s", mp->name);
330
331   error = ikev2_initiate_sa_init (vm, tmp);
332   vec_free (tmp);
333   if (error)
334     rv = VNET_API_ERROR_UNSPECIFIED;
335 #else
336   rv = VNET_API_ERROR_UNIMPLEMENTED;
337 #endif
338
339   REPLY_MACRO (VL_API_IKEV2_INITIATE_SA_INIT_REPLY);
340 }
341
342 static void
343 vl_api_ikev2_initiate_del_ike_sa_t_handler (vl_api_ikev2_initiate_del_ike_sa_t
344                                             * mp)
345 {
346   vl_api_ikev2_initiate_del_ike_sa_reply_t *rmp;
347   int rv = 0;
348
349 #if WITH_LIBSSL > 0
350   vlib_main_t *vm = vlib_get_main ();
351   clib_error_t *error;
352
353   error = ikev2_initiate_delete_ike_sa (vm, mp->ispi);
354   if (error)
355     rv = VNET_API_ERROR_UNSPECIFIED;
356 #else
357   rv = VNET_API_ERROR_UNIMPLEMENTED;
358 #endif
359
360   REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_IKE_SA_REPLY);
361 }
362
363 static void
364   vl_api_ikev2_initiate_del_child_sa_t_handler
365   (vl_api_ikev2_initiate_del_child_sa_t * mp)
366 {
367   vl_api_ikev2_initiate_del_child_sa_reply_t *rmp;
368   int rv = 0;
369
370 #if WITH_LIBSSL > 0
371   vlib_main_t *vm = vlib_get_main ();
372   clib_error_t *error;
373
374   error = ikev2_initiate_delete_child_sa (vm, mp->ispi);
375   if (error)
376     rv = VNET_API_ERROR_UNSPECIFIED;
377 #else
378   rv = VNET_API_ERROR_UNIMPLEMENTED;
379 #endif
380
381   REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_CHILD_SA_REPLY);
382 }
383
384 static void
385   vl_api_ikev2_initiate_rekey_child_sa_t_handler
386   (vl_api_ikev2_initiate_rekey_child_sa_t * mp)
387 {
388   vl_api_ikev2_initiate_rekey_child_sa_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   error = ikev2_initiate_rekey_child_sa (vm, mp->ispi);
396   if (error)
397     rv = VNET_API_ERROR_UNSPECIFIED;
398 #else
399   rv = VNET_API_ERROR_UNIMPLEMENTED;
400 #endif
401
402   REPLY_MACRO (VL_API_IKEV2_INITIATE_REKEY_CHILD_SA_REPLY);
403 }
404
405 #include <ikev2/ikev2.api.c>
406 static clib_error_t *
407 ikev2_api_init (vlib_main_t * vm)
408 {
409   ikev2_main_t *im = &ikev2_main;
410
411   /* Ask for a correctly-sized block of API message decode slots */
412   im->msg_id_base = setup_message_id_table ();
413
414   return 0;
415 }
416
417 VLIB_INIT_FUNCTION (ikev2_api_init);
418
419 /*
420  * fd.io coding-style-patch-verification: ON
421  *
422  * Local Variables:
423  * eval: (c-set-style "gnu")
424  * End:
425  */