ikev2: use explicit api types
[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_add_del_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_profile_set_ts_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_initiate_sa_init_t_handler (vl_api_ikev2_initiate_sa_init_t * mp)
294 {
295   vl_api_ikev2_initiate_sa_init_reply_t *rmp;
296   int rv = 0;
297
298 #if WITH_LIBSSL > 0
299   vlib_main_t *vm = vlib_get_main ();
300   clib_error_t *error;
301
302   u8 *tmp = format (0, "%s", mp->name);
303
304   error = ikev2_initiate_sa_init (vm, tmp);
305   vec_free (tmp);
306   if (error)
307     rv = VNET_API_ERROR_UNSPECIFIED;
308 #else
309   rv = VNET_API_ERROR_UNIMPLEMENTED;
310 #endif
311
312   REPLY_MACRO (VL_API_IKEV2_INITIATE_SA_INIT_REPLY);
313 }
314
315 static void
316 vl_api_ikev2_initiate_del_ike_sa_t_handler (vl_api_ikev2_initiate_del_ike_sa_t
317                                             * mp)
318 {
319   vl_api_ikev2_initiate_del_ike_sa_reply_t *rmp;
320   int rv = 0;
321
322 #if WITH_LIBSSL > 0
323   vlib_main_t *vm = vlib_get_main ();
324   clib_error_t *error;
325
326   error = ikev2_initiate_delete_ike_sa (vm, mp->ispi);
327   if (error)
328     rv = VNET_API_ERROR_UNSPECIFIED;
329 #else
330   rv = VNET_API_ERROR_UNIMPLEMENTED;
331 #endif
332
333   REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_IKE_SA_REPLY);
334 }
335
336 static void
337   vl_api_ikev2_initiate_del_child_sa_t_handler
338   (vl_api_ikev2_initiate_del_child_sa_t * mp)
339 {
340   vl_api_ikev2_initiate_del_child_sa_reply_t *rmp;
341   int rv = 0;
342
343 #if WITH_LIBSSL > 0
344   vlib_main_t *vm = vlib_get_main ();
345   clib_error_t *error;
346
347   error = ikev2_initiate_delete_child_sa (vm, mp->ispi);
348   if (error)
349     rv = VNET_API_ERROR_UNSPECIFIED;
350 #else
351   rv = VNET_API_ERROR_UNIMPLEMENTED;
352 #endif
353
354   REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_CHILD_SA_REPLY);
355 }
356
357 static void
358   vl_api_ikev2_initiate_rekey_child_sa_t_handler
359   (vl_api_ikev2_initiate_rekey_child_sa_t * mp)
360 {
361   vl_api_ikev2_initiate_rekey_child_sa_reply_t *rmp;
362   int rv = 0;
363
364 #if WITH_LIBSSL > 0
365   vlib_main_t *vm = vlib_get_main ();
366   clib_error_t *error;
367
368   error = ikev2_initiate_rekey_child_sa (vm, mp->ispi);
369   if (error)
370     rv = VNET_API_ERROR_UNSPECIFIED;
371 #else
372   rv = VNET_API_ERROR_UNIMPLEMENTED;
373 #endif
374
375   REPLY_MACRO (VL_API_IKEV2_INITIATE_REKEY_CHILD_SA_REPLY);
376 }
377
378 #include <ikev2/ikev2.api.c>
379 static clib_error_t *
380 ikev2_api_init (vlib_main_t * vm)
381 {
382   ikev2_main_t *im = &ikev2_main;
383
384   /* Ask for a correctly-sized block of API message decode slots */
385   im->msg_id_base = setup_message_id_table ();
386
387   return 0;
388 }
389
390 VLIB_INIT_FUNCTION (ikev2_api_init);
391
392 /*
393  * fd.io coding-style-patch-verification: ON
394  *
395  * Local Variables:
396  * eval: (c-set-style "gnu")
397  * End:
398  */