ikev2: remove api boilerplate
[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 = ikev2_set_profile_ts (vm, tmp, mp->proto, mp->start_port,
146                                 mp->end_port, (ip4_address_t) mp->start_addr,
147                                 (ip4_address_t) mp->end_addr, mp->is_local);
148   vec_free (tmp);
149   if (error)
150     rv = VNET_API_ERROR_UNSPECIFIED;
151 #else
152   rv = VNET_API_ERROR_UNIMPLEMENTED;
153 #endif
154
155   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_TS_REPLY);
156 }
157
158 static void
159 vl_api_ikev2_set_local_key_t_handler (vl_api_ikev2_set_local_key_t * mp)
160 {
161   vl_api_ikev2_profile_set_ts_reply_t *rmp;
162   int rv = 0;
163
164 #if WITH_LIBSSL > 0
165   vlib_main_t *vm = vlib_get_main ();
166   clib_error_t *error;
167
168   error = ikev2_set_local_key (vm, mp->key_file);
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_SET_LOCAL_KEY_REPLY);
176 }
177
178 static void
179 vl_api_ikev2_set_responder_t_handler (vl_api_ikev2_set_responder_t * mp)
180 {
181   vl_api_ikev2_set_responder_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
188   u8 *tmp = format (0, "%s", mp->name);
189   ip4_address_t ip4;
190   clib_memcpy (&ip4, mp->address, sizeof (ip4));
191
192   error = ikev2_set_profile_responder (vm, tmp, mp->sw_if_index, ip4);
193   vec_free (tmp);
194   if (error)
195     rv = VNET_API_ERROR_UNSPECIFIED;
196 #else
197   rv = VNET_API_ERROR_UNIMPLEMENTED;
198 #endif
199
200   REPLY_MACRO (VL_API_IKEV2_SET_RESPONDER_REPLY);
201 }
202
203 static void
204 vl_api_ikev2_set_ike_transforms_t_handler (vl_api_ikev2_set_ike_transforms_t *
205                                            mp)
206 {
207   vl_api_ikev2_set_ike_transforms_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   u8 *tmp = format (0, "%s", mp->name);
215
216   error =
217     ikev2_set_profile_ike_transforms (vm, tmp, mp->crypto_alg, mp->integ_alg,
218                                       mp->dh_group, mp->crypto_key_size);
219   vec_free (tmp);
220   if (error)
221     rv = VNET_API_ERROR_UNSPECIFIED;
222 #else
223   rv = VNET_API_ERROR_UNIMPLEMENTED;
224 #endif
225
226   REPLY_MACRO (VL_API_IKEV2_SET_IKE_TRANSFORMS_REPLY);
227 }
228
229 static void
230 vl_api_ikev2_set_esp_transforms_t_handler (vl_api_ikev2_set_esp_transforms_t *
231                                            mp)
232 {
233   vl_api_ikev2_set_esp_transforms_reply_t *rmp;
234   int rv = 0;
235
236 #if WITH_LIBSSL > 0
237   vlib_main_t *vm = vlib_get_main ();
238   clib_error_t *error;
239
240   u8 *tmp = format (0, "%s", mp->name);
241
242   error =
243     ikev2_set_profile_esp_transforms (vm, tmp, mp->crypto_alg, mp->integ_alg,
244                                       mp->dh_group, mp->crypto_key_size);
245   vec_free (tmp);
246   if (error)
247     rv = VNET_API_ERROR_UNSPECIFIED;
248 #else
249   rv = VNET_API_ERROR_UNIMPLEMENTED;
250 #endif
251
252   REPLY_MACRO (VL_API_IKEV2_SET_ESP_TRANSFORMS_REPLY);
253 }
254
255 static void
256 vl_api_ikev2_set_sa_lifetime_t_handler (vl_api_ikev2_set_sa_lifetime_t * mp)
257 {
258   vl_api_ikev2_set_sa_lifetime_reply_t *rmp;
259   int rv = 0;
260
261 #if WITH_LIBSSL > 0
262   vlib_main_t *vm = vlib_get_main ();
263   clib_error_t *error;
264
265   u8 *tmp = format (0, "%s", mp->name);
266
267   error =
268     ikev2_set_profile_sa_lifetime (vm, tmp, mp->lifetime, mp->lifetime_jitter,
269                                    mp->handover, mp->lifetime_maxdata);
270   vec_free (tmp);
271   if (error)
272     rv = VNET_API_ERROR_UNSPECIFIED;
273 #else
274   rv = VNET_API_ERROR_UNIMPLEMENTED;
275 #endif
276
277   REPLY_MACRO (VL_API_IKEV2_SET_SA_LIFETIME_REPLY);
278 }
279
280 static void
281 vl_api_ikev2_initiate_sa_init_t_handler (vl_api_ikev2_initiate_sa_init_t * mp)
282 {
283   vl_api_ikev2_initiate_sa_init_reply_t *rmp;
284   int rv = 0;
285
286 #if WITH_LIBSSL > 0
287   vlib_main_t *vm = vlib_get_main ();
288   clib_error_t *error;
289
290   u8 *tmp = format (0, "%s", mp->name);
291
292   error = ikev2_initiate_sa_init (vm, tmp);
293   vec_free (tmp);
294   if (error)
295     rv = VNET_API_ERROR_UNSPECIFIED;
296 #else
297   rv = VNET_API_ERROR_UNIMPLEMENTED;
298 #endif
299
300   REPLY_MACRO (VL_API_IKEV2_INITIATE_SA_INIT_REPLY);
301 }
302
303 static void
304 vl_api_ikev2_initiate_del_ike_sa_t_handler (vl_api_ikev2_initiate_del_ike_sa_t
305                                             * mp)
306 {
307   vl_api_ikev2_initiate_del_ike_sa_reply_t *rmp;
308   int rv = 0;
309
310 #if WITH_LIBSSL > 0
311   vlib_main_t *vm = vlib_get_main ();
312   clib_error_t *error;
313
314   error = ikev2_initiate_delete_ike_sa (vm, mp->ispi);
315   if (error)
316     rv = VNET_API_ERROR_UNSPECIFIED;
317 #else
318   rv = VNET_API_ERROR_UNIMPLEMENTED;
319 #endif
320
321   REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_IKE_SA_REPLY);
322 }
323
324 static void
325   vl_api_ikev2_initiate_del_child_sa_t_handler
326   (vl_api_ikev2_initiate_del_child_sa_t * mp)
327 {
328   vl_api_ikev2_initiate_del_child_sa_reply_t *rmp;
329   int rv = 0;
330
331 #if WITH_LIBSSL > 0
332   vlib_main_t *vm = vlib_get_main ();
333   clib_error_t *error;
334
335   error = ikev2_initiate_delete_child_sa (vm, mp->ispi);
336   if (error)
337     rv = VNET_API_ERROR_UNSPECIFIED;
338 #else
339   rv = VNET_API_ERROR_UNIMPLEMENTED;
340 #endif
341
342   REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_CHILD_SA_REPLY);
343 }
344
345 static void
346   vl_api_ikev2_initiate_rekey_child_sa_t_handler
347   (vl_api_ikev2_initiate_rekey_child_sa_t * mp)
348 {
349   vl_api_ikev2_initiate_rekey_child_sa_reply_t *rmp;
350   int rv = 0;
351
352 #if WITH_LIBSSL > 0
353   vlib_main_t *vm = vlib_get_main ();
354   clib_error_t *error;
355
356   error = ikev2_initiate_rekey_child_sa (vm, mp->ispi);
357   if (error)
358     rv = VNET_API_ERROR_UNSPECIFIED;
359 #else
360   rv = VNET_API_ERROR_UNIMPLEMENTED;
361 #endif
362
363   REPLY_MACRO (VL_API_IKEV2_INITIATE_REKEY_CHILD_SA_REPLY);
364 }
365
366 #include <ikev2/ikev2.api.c>
367 static clib_error_t *
368 ikev2_api_init (vlib_main_t * vm)
369 {
370   ikev2_main_t *im = &ikev2_main;
371
372   /* Ask for a correctly-sized block of API message decode slots */
373   im->msg_id_base = setup_message_id_table ();
374
375   return 0;
376 }
377
378 VLIB_INIT_FUNCTION (ikev2_api_init);
379
380 /*
381  * fd.io coding-style-patch-verification: ON
382  *
383  * Local Variables:
384  * eval: (c-set-style "gnu")
385  * End:
386  */