ikev2: correct byte order in api handlers
[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 <plugins/ikev2/ikev2.api_enum.h>
30 #include <plugins/ikev2/ikev2.api_types.h>
31
32 extern ikev2_main_t ikev2_main;
33
34 #define IKEV2_PLUGIN_VERSION_MAJOR 1
35 #define IKEV2_PLUGIN_VERSION_MINOR 0
36 #define REPLY_MSG_ID_BASE ikev2_main.msg_id_base
37 #include <vlibapi/api_helper_macros.h>
38
39 static void
40 vl_api_ikev2_plugin_get_version_t_handler (vl_api_ikev2_plugin_get_version_t *
41                                            mp)
42 {
43   ikev2_main_t *im = &ikev2_main;
44   vl_api_ikev2_plugin_get_version_reply_t *rmp;
45   int msg_size = sizeof (*rmp);
46   vl_api_registration_t *reg;
47
48   reg = vl_api_client_index_to_registration (mp->client_index);
49   if (!reg)
50     return;
51
52   rmp = vl_msg_api_alloc (msg_size);
53   clib_memset (rmp, 0, msg_size);
54   rmp->_vl_msg_id =
55     ntohs (VL_API_IKEV2_PLUGIN_GET_VERSION_REPLY + im->msg_id_base);
56   rmp->context = mp->context;
57   rmp->major = htonl (IKEV2_PLUGIN_VERSION_MAJOR);
58   rmp->minor = htonl (IKEV2_PLUGIN_VERSION_MINOR);
59
60   vl_api_send_msg (reg, (u8 *) rmp);
61 }
62
63 static void
64 vl_api_ikev2_profile_add_del_t_handler (vl_api_ikev2_profile_add_del_t * mp)
65 {
66   vl_api_ikev2_profile_add_del_reply_t *rmp;
67   int rv = 0;
68
69 #if WITH_LIBSSL > 0
70   vlib_main_t *vm = vlib_get_main ();
71   clib_error_t *error;
72   u8 *tmp = format (0, "%s", mp->name);
73   error = ikev2_add_del_profile (vm, tmp, mp->is_add);
74   vec_free (tmp);
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_ADD_DEL_REPLY);
82 }
83
84 static void
85   vl_api_ikev2_profile_set_auth_t_handler
86   (vl_api_ikev2_profile_set_auth_t * mp)
87 {
88   vl_api_ikev2_profile_set_auth_reply_t *rmp;
89   int rv = 0;
90
91 #if WITH_LIBSSL > 0
92   vlib_main_t *vm = vlib_get_main ();
93   clib_error_t *error;
94   int data_len = ntohl (mp->data_len);
95   u8 *tmp = format (0, "%s", mp->name);
96   u8 *data = vec_new (u8, data_len);
97   clib_memcpy (data, mp->data, data_len);
98   error = ikev2_set_profile_auth (vm, tmp, mp->auth_method, data, mp->is_hex);
99   vec_free (tmp);
100   vec_free (data);
101   if (error)
102     rv = VNET_API_ERROR_UNSPECIFIED;
103 #else
104   rv = VNET_API_ERROR_UNIMPLEMENTED;
105 #endif
106
107   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_AUTH_REPLY);
108 }
109
110 static void
111 vl_api_ikev2_profile_set_id_t_handler (vl_api_ikev2_profile_set_id_t * mp)
112 {
113   vl_api_ikev2_profile_add_del_reply_t *rmp;
114   int rv = 0;
115
116 #if WITH_LIBSSL > 0
117   vlib_main_t *vm = vlib_get_main ();
118   clib_error_t *error;
119   u8 *tmp = format (0, "%s", mp->name);
120   int data_len = ntohl (mp->data_len);
121   u8 *data = vec_new (u8, data_len);
122   clib_memcpy (data, mp->data, data_len);
123   error = ikev2_set_profile_id (vm, tmp, mp->id_type, data, mp->is_local);
124   vec_free (tmp);
125   vec_free (data);
126   if (error)
127     rv = VNET_API_ERROR_UNSPECIFIED;
128 #else
129   rv = VNET_API_ERROR_UNIMPLEMENTED;
130 #endif
131
132   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_ID_REPLY);
133 }
134
135 static void
136 vl_api_ikev2_profile_set_ts_t_handler (vl_api_ikev2_profile_set_ts_t * mp)
137 {
138   vl_api_ikev2_profile_set_ts_reply_t *rmp;
139   int rv = 0;
140
141 #if WITH_LIBSSL > 0
142   vlib_main_t *vm = vlib_get_main ();
143   clib_error_t *error;
144   u8 *tmp = format (0, "%s", mp->name);
145   error =
146     ikev2_set_profile_ts (vm, tmp, mp->proto,
147                           clib_net_to_host_u16 (mp->start_port),
148                           clib_net_to_host_u16 (mp->end_port),
149                           (ip4_address_t) mp->start_addr,
150                           (ip4_address_t) mp->end_addr, mp->is_local);
151   vec_free (tmp);
152   if (error)
153     rv = VNET_API_ERROR_UNSPECIFIED;
154 #else
155   rv = VNET_API_ERROR_UNIMPLEMENTED;
156 #endif
157
158   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_TS_REPLY);
159 }
160
161 static void
162 vl_api_ikev2_set_local_key_t_handler (vl_api_ikev2_set_local_key_t * mp)
163 {
164   vl_api_ikev2_profile_set_ts_reply_t *rmp;
165   int rv = 0;
166
167 #if WITH_LIBSSL > 0
168   vlib_main_t *vm = vlib_get_main ();
169   clib_error_t *error;
170
171   error = ikev2_set_local_key (vm, mp->key_file);
172   if (error)
173     rv = VNET_API_ERROR_UNSPECIFIED;
174 #else
175   rv = VNET_API_ERROR_UNIMPLEMENTED;
176 #endif
177
178   REPLY_MACRO (VL_API_IKEV2_SET_LOCAL_KEY_REPLY);
179 }
180
181 static void
182 vl_api_ikev2_set_responder_t_handler (vl_api_ikev2_set_responder_t * mp)
183 {
184   vl_api_ikev2_set_responder_reply_t *rmp;
185   int rv = 0;
186
187 #if WITH_LIBSSL > 0
188   vlib_main_t *vm = vlib_get_main ();
189   clib_error_t *error;
190
191   u8 *tmp = format (0, "%s", mp->name);
192   ip4_address_t ip4;
193   clib_memcpy (&ip4, mp->address, sizeof (ip4));
194
195   error = ikev2_set_profile_responder (vm, tmp, ntohl (mp->sw_if_index), ip4);
196   vec_free (tmp);
197   if (error)
198     rv = VNET_API_ERROR_UNSPECIFIED;
199 #else
200   rv = VNET_API_ERROR_UNIMPLEMENTED;
201 #endif
202
203   REPLY_MACRO (VL_API_IKEV2_SET_RESPONDER_REPLY);
204 }
205
206 static void
207 vl_api_ikev2_set_ike_transforms_t_handler (vl_api_ikev2_set_ike_transforms_t *
208                                            mp)
209 {
210   vl_api_ikev2_set_ike_transforms_reply_t *rmp;
211   int rv = 0;
212
213 #if WITH_LIBSSL > 0
214   vlib_main_t *vm = vlib_get_main ();
215   clib_error_t *error;
216
217   u8 *tmp = format (0, "%s", mp->name);
218
219   error =
220     ikev2_set_profile_ike_transforms (vm, tmp, ntohl (mp->crypto_alg),
221                                       ntohl (mp->integ_alg),
222                                       ntohl (mp->dh_group),
223                                       ntohl (mp->crypto_key_size));
224   vec_free (tmp);
225   if (error)
226     rv = VNET_API_ERROR_UNSPECIFIED;
227 #else
228   rv = VNET_API_ERROR_UNIMPLEMENTED;
229 #endif
230
231   REPLY_MACRO (VL_API_IKEV2_SET_IKE_TRANSFORMS_REPLY);
232 }
233
234 static void
235 vl_api_ikev2_set_esp_transforms_t_handler (vl_api_ikev2_set_esp_transforms_t *
236                                            mp)
237 {
238   vl_api_ikev2_set_esp_transforms_reply_t *rmp;
239   int rv = 0;
240
241 #if WITH_LIBSSL > 0
242   vlib_main_t *vm = vlib_get_main ();
243   clib_error_t *error;
244
245   u8 *tmp = format (0, "%s", mp->name);
246
247   error =
248     ikev2_set_profile_esp_transforms (vm, tmp, ntohl (mp->crypto_alg),
249                                       ntohl (mp->integ_alg),
250                                       ntohl (mp->dh_group),
251                                       ntohl (mp->crypto_key_size));
252   vec_free (tmp);
253   if (error)
254     rv = VNET_API_ERROR_UNSPECIFIED;
255 #else
256   rv = VNET_API_ERROR_UNIMPLEMENTED;
257 #endif
258
259   REPLY_MACRO (VL_API_IKEV2_SET_ESP_TRANSFORMS_REPLY);
260 }
261
262 static void
263 vl_api_ikev2_set_sa_lifetime_t_handler (vl_api_ikev2_set_sa_lifetime_t * mp)
264 {
265   vl_api_ikev2_set_sa_lifetime_reply_t *rmp;
266   int rv = 0;
267
268 #if WITH_LIBSSL > 0
269   vlib_main_t *vm = vlib_get_main ();
270   clib_error_t *error;
271
272   u8 *tmp = format (0, "%s", mp->name);
273
274   error =
275     ikev2_set_profile_sa_lifetime (vm, tmp,
276                                    clib_net_to_host_u64 (mp->lifetime),
277                                    ntohl (mp->lifetime_jitter),
278                                    ntohl (mp->handover),
279                                    clib_net_to_host_u64
280                                    (mp->lifetime_maxdata));
281   vec_free (tmp);
282   if (error)
283     rv = VNET_API_ERROR_UNSPECIFIED;
284 #else
285   rv = VNET_API_ERROR_UNIMPLEMENTED;
286 #endif
287
288   REPLY_MACRO (VL_API_IKEV2_SET_SA_LIFETIME_REPLY);
289 }
290
291 static void
292 vl_api_ikev2_initiate_sa_init_t_handler (vl_api_ikev2_initiate_sa_init_t * mp)
293 {
294   vl_api_ikev2_initiate_sa_init_reply_t *rmp;
295   int rv = 0;
296
297 #if WITH_LIBSSL > 0
298   vlib_main_t *vm = vlib_get_main ();
299   clib_error_t *error;
300
301   u8 *tmp = format (0, "%s", mp->name);
302
303   error = ikev2_initiate_sa_init (vm, tmp);
304   vec_free (tmp);
305   if (error)
306     rv = VNET_API_ERROR_UNSPECIFIED;
307 #else
308   rv = VNET_API_ERROR_UNIMPLEMENTED;
309 #endif
310
311   REPLY_MACRO (VL_API_IKEV2_INITIATE_SA_INIT_REPLY);
312 }
313
314 static void
315 vl_api_ikev2_initiate_del_ike_sa_t_handler (vl_api_ikev2_initiate_del_ike_sa_t
316                                             * mp)
317 {
318   vl_api_ikev2_initiate_del_ike_sa_reply_t *rmp;
319   int rv = 0;
320
321 #if WITH_LIBSSL > 0
322   vlib_main_t *vm = vlib_get_main ();
323   clib_error_t *error;
324
325   error = ikev2_initiate_delete_ike_sa (vm, mp->ispi);
326   if (error)
327     rv = VNET_API_ERROR_UNSPECIFIED;
328 #else
329   rv = VNET_API_ERROR_UNIMPLEMENTED;
330 #endif
331
332   REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_IKE_SA_REPLY);
333 }
334
335 static void
336   vl_api_ikev2_initiate_del_child_sa_t_handler
337   (vl_api_ikev2_initiate_del_child_sa_t * mp)
338 {
339   vl_api_ikev2_initiate_del_child_sa_reply_t *rmp;
340   int rv = 0;
341
342 #if WITH_LIBSSL > 0
343   vlib_main_t *vm = vlib_get_main ();
344   clib_error_t *error;
345
346   error = ikev2_initiate_delete_child_sa (vm, mp->ispi);
347   if (error)
348     rv = VNET_API_ERROR_UNSPECIFIED;
349 #else
350   rv = VNET_API_ERROR_UNIMPLEMENTED;
351 #endif
352
353   REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_CHILD_SA_REPLY);
354 }
355
356 static void
357   vl_api_ikev2_initiate_rekey_child_sa_t_handler
358   (vl_api_ikev2_initiate_rekey_child_sa_t * mp)
359 {
360   vl_api_ikev2_initiate_rekey_child_sa_reply_t *rmp;
361   int rv = 0;
362
363 #if WITH_LIBSSL > 0
364   vlib_main_t *vm = vlib_get_main ();
365   clib_error_t *error;
366
367   error = ikev2_initiate_rekey_child_sa (vm, mp->ispi);
368   if (error)
369     rv = VNET_API_ERROR_UNSPECIFIED;
370 #else
371   rv = VNET_API_ERROR_UNIMPLEMENTED;
372 #endif
373
374   REPLY_MACRO (VL_API_IKEV2_INITIATE_REKEY_CHILD_SA_REPLY);
375 }
376
377 #include <ikev2/ikev2.api.c>
378 static clib_error_t *
379 ikev2_api_init (vlib_main_t * vm)
380 {
381   ikev2_main_t *im = &ikev2_main;
382
383   /* Ask for a correctly-sized block of API message decode slots */
384   im->msg_id_base = setup_message_id_table ();
385
386   return 0;
387 }
388
389 VLIB_INIT_FUNCTION (ikev2_api_init);
390
391 /*
392  * fd.io coding-style-patch-verification: ON
393  *
394  * Local Variables:
395  * eval: (c-set-style "gnu")
396  * End:
397  */