FD.io VPP  v19.08.1-401-g8e4ed521a
Vector Packet Processing
ikev2_api.c
Go to the documentation of this file.
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 */
30 
31 #define vl_typedefs /* define message structures */
32 #include <ikev2/ikev2_all_api.h>
33 #undef vl_typedefs
34 
35 #define vl_endianfun /* define message structures */
36 #include <ikev2/ikev2_all_api.h>
37 #undef vl_endianfun
38 
39 /* instantiate all the print functions we know about */
40 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
41 #define vl_printfun
42 #include <ikev2/ikev2_all_api.h>
43 #undef vl_printfun
44 
45 /* Get the API version number */
46 #define vl_api_version(n,v) static u32 api_version=(v);
47 #include <ikev2/ikev2_all_api.h>
48 #undef vl_api_version
49 
51 
52 #define IKEV2_PLUGIN_VERSION_MAJOR 1
53 #define IKEV2_PLUGIN_VERSION_MINOR 0
54 #define REPLY_MSG_ID_BASE ikev2_main.msg_id_base
56 
57 #define foreach_ikev2_api_msg \
58 _(IKEV2_PLUGIN_GET_VERSION, ikev2_plugin_get_version) \
59 _(IKEV2_PROFILE_ADD_DEL, ikev2_profile_add_del) \
60 _(IKEV2_PROFILE_SET_AUTH, ikev2_profile_set_auth) \
61 _(IKEV2_PROFILE_SET_ID, ikev2_profile_set_id) \
62 _(IKEV2_PROFILE_SET_TS, ikev2_profile_set_ts) \
63 _(IKEV2_SET_LOCAL_KEY, ikev2_set_local_key) \
64 _(IKEV2_SET_RESPONDER, ikev2_set_responder) \
65 _(IKEV2_SET_IKE_TRANSFORMS, ikev2_set_ike_transforms) \
66 _(IKEV2_SET_ESP_TRANSFORMS, ikev2_set_esp_transforms) \
67 _(IKEV2_SET_SA_LIFETIME, ikev2_set_sa_lifetime) \
68 _(IKEV2_INITIATE_SA_INIT, ikev2_initiate_sa_init) \
69 _(IKEV2_INITIATE_DEL_IKE_SA, ikev2_initiate_del_ike_sa) \
70 _(IKEV2_INITIATE_DEL_CHILD_SA, ikev2_initiate_del_child_sa) \
71 _(IKEV2_INITIATE_REKEY_CHILD_SA, ikev2_initiate_rekey_child_sa)
72 
73 static void
75  mp)
76 {
77  ikev2_main_t *im = &ikev2_main;
79  int msg_size = sizeof (*rmp);
81 
83  if (!reg)
84  return;
85 
86  rmp = vl_msg_api_alloc (msg_size);
87  clib_memset (rmp, 0, msg_size);
88  rmp->_vl_msg_id =
89  ntohs (VL_API_IKEV2_PLUGIN_GET_VERSION_REPLY + im->msg_id_base);
90  rmp->context = mp->context;
91  rmp->major = htonl (IKEV2_PLUGIN_VERSION_MAJOR);
92  rmp->minor = htonl (IKEV2_PLUGIN_VERSION_MINOR);
93 
94  vl_api_send_msg (reg, (u8 *) rmp);
95 }
96 
97 static void
99 {
100  vl_api_ikev2_profile_add_del_reply_t *rmp;
101  int rv = 0;
102 
103 #if WITH_LIBSSL > 0
105  clib_error_t *error;
106  u8 *tmp = format (0, "%s", mp->name);
107  error = ikev2_add_del_profile (vm, tmp, mp->is_add);
108  vec_free (tmp);
109  if (error)
110  rv = VNET_API_ERROR_UNSPECIFIED;
111 #else
112  rv = VNET_API_ERROR_UNIMPLEMENTED;
113 #endif
114 
115  REPLY_MACRO (VL_API_IKEV2_PROFILE_ADD_DEL_REPLY);
116 }
117 
118 static void
121 {
122  vl_api_ikev2_profile_set_auth_reply_t *rmp;
123  int rv = 0;
124 
125 #if WITH_LIBSSL > 0
127  clib_error_t *error;
128  int data_len = ntohl (mp->data_len);
129  u8 *tmp = format (0, "%s", mp->name);
130  u8 *data = vec_new (u8, data_len);
131  clib_memcpy (data, mp->data, data_len);
132  error = ikev2_set_profile_auth (vm, tmp, mp->auth_method, data, mp->is_hex);
133  vec_free (tmp);
134  vec_free (data);
135  if (error)
136  rv = VNET_API_ERROR_UNSPECIFIED;
137 #else
138  rv = VNET_API_ERROR_UNIMPLEMENTED;
139 #endif
140 
141  REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_AUTH_REPLY);
142 }
143 
144 static void
146 {
147  vl_api_ikev2_profile_add_del_reply_t *rmp;
148  int rv = 0;
149 
150 #if WITH_LIBSSL > 0
152  clib_error_t *error;
153  u8 *tmp = format (0, "%s", mp->name);
154  int data_len = ntohl (mp->data_len);
155  u8 *data = vec_new (u8, data_len);
156  clib_memcpy (data, mp->data, data_len);
157  error = ikev2_set_profile_id (vm, tmp, mp->id_type, data, mp->is_local);
158  vec_free (tmp);
159  vec_free (data);
160  if (error)
161  rv = VNET_API_ERROR_UNSPECIFIED;
162 #else
163  rv = VNET_API_ERROR_UNIMPLEMENTED;
164 #endif
165 
166  REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_ID_REPLY);
167 }
168 
169 static void
171 {
172  vl_api_ikev2_profile_set_ts_reply_t *rmp;
173  int rv = 0;
174 
175 #if WITH_LIBSSL > 0
177  clib_error_t *error;
178  u8 *tmp = format (0, "%s", mp->name);
179  error =
180  ikev2_set_profile_ts (vm, tmp, mp->proto,
181  clib_net_to_host_u16 (mp->start_port),
182  clib_net_to_host_u16 (mp->end_port),
184  (ip4_address_t) mp->end_addr, mp->is_local);
185  vec_free (tmp);
186  if (error)
187  rv = VNET_API_ERROR_UNSPECIFIED;
188 #else
189  rv = VNET_API_ERROR_UNIMPLEMENTED;
190 #endif
191 
192  REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_TS_REPLY);
193 }
194 
195 static void
197 {
198  vl_api_ikev2_profile_set_ts_reply_t *rmp;
199  int rv = 0;
200 
201 #if WITH_LIBSSL > 0
203  clib_error_t *error;
204 
205  error = ikev2_set_local_key (vm, mp->key_file);
206  if (error)
207  rv = VNET_API_ERROR_UNSPECIFIED;
208 #else
209  rv = VNET_API_ERROR_UNIMPLEMENTED;
210 #endif
211 
212  REPLY_MACRO (VL_API_IKEV2_SET_LOCAL_KEY_REPLY);
213 }
214 
215 static void
217 {
218  vl_api_ikev2_set_responder_reply_t *rmp;
219  int rv = 0;
220 
221 #if WITH_LIBSSL > 0
223  clib_error_t *error;
224 
225  u8 *tmp = format (0, "%s", mp->name);
226  ip4_address_t ip4;
227  clib_memcpy (&ip4, mp->address, sizeof (ip4));
228 
229  error = ikev2_set_profile_responder (vm, tmp, ntohl (mp->sw_if_index), ip4);
230  vec_free (tmp);
231  if (error)
232  rv = VNET_API_ERROR_UNSPECIFIED;
233 #else
234  rv = VNET_API_ERROR_UNIMPLEMENTED;
235 #endif
236 
237  REPLY_MACRO (VL_API_IKEV2_SET_RESPONDER_REPLY);
238 }
239 
240 static void
242  mp)
243 {
244  vl_api_ikev2_set_ike_transforms_reply_t *rmp;
245  int rv = 0;
246 
247 #if WITH_LIBSSL > 0
249  clib_error_t *error;
250 
251  u8 *tmp = format (0, "%s", mp->name);
252 
253  error =
254  ikev2_set_profile_ike_transforms (vm, tmp, ntohl (mp->crypto_alg),
255  ntohl (mp->integ_alg),
256  ntohl (mp->dh_group),
257  ntohl (mp->crypto_key_size));
258  vec_free (tmp);
259  if (error)
260  rv = VNET_API_ERROR_UNSPECIFIED;
261 #else
262  rv = VNET_API_ERROR_UNIMPLEMENTED;
263 #endif
264 
265  REPLY_MACRO (VL_API_IKEV2_SET_IKE_TRANSFORMS_REPLY);
266 }
267 
268 static void
270  mp)
271 {
272  vl_api_ikev2_set_esp_transforms_reply_t *rmp;
273  int rv = 0;
274 
275 #if WITH_LIBSSL > 0
277  clib_error_t *error;
278 
279  u8 *tmp = format (0, "%s", mp->name);
280 
281  error =
282  ikev2_set_profile_esp_transforms (vm, tmp, ntohl (mp->crypto_alg),
283  ntohl (mp->integ_alg),
284  ntohl (mp->dh_group),
285  ntohl (mp->crypto_key_size));
286  vec_free (tmp);
287  if (error)
288  rv = VNET_API_ERROR_UNSPECIFIED;
289 #else
290  rv = VNET_API_ERROR_UNIMPLEMENTED;
291 #endif
292 
293  REPLY_MACRO (VL_API_IKEV2_SET_ESP_TRANSFORMS_REPLY);
294 }
295 
296 static void
298 {
299  vl_api_ikev2_set_sa_lifetime_reply_t *rmp;
300  int rv = 0;
301 
302 #if WITH_LIBSSL > 0
304  clib_error_t *error;
305 
306  u8 *tmp = format (0, "%s", mp->name);
307 
308  error =
310  clib_net_to_host_u64 (mp->lifetime),
311  ntohl (mp->lifetime_jitter),
312  ntohl (mp->handover),
313  clib_net_to_host_u64
314  (mp->lifetime_maxdata));
315  vec_free (tmp);
316  if (error)
317  rv = VNET_API_ERROR_UNSPECIFIED;
318 #else
319  rv = VNET_API_ERROR_UNIMPLEMENTED;
320 #endif
321 
322  REPLY_MACRO (VL_API_IKEV2_SET_SA_LIFETIME_REPLY);
323 }
324 
325 static void
327 {
328  vl_api_ikev2_initiate_sa_init_reply_t *rmp;
329  int rv = 0;
330 
331 #if WITH_LIBSSL > 0
333  clib_error_t *error;
334 
335  u8 *tmp = format (0, "%s", mp->name);
336 
337  error = ikev2_initiate_sa_init (vm, tmp);
338  vec_free (tmp);
339  if (error)
340  rv = VNET_API_ERROR_UNSPECIFIED;
341 #else
342  rv = VNET_API_ERROR_UNIMPLEMENTED;
343 #endif
344 
345  REPLY_MACRO (VL_API_IKEV2_INITIATE_SA_INIT_REPLY);
346 }
347 
348 static void
350  * mp)
351 {
352  vl_api_ikev2_initiate_del_ike_sa_reply_t *rmp;
353  int rv = 0;
354 
355 #if WITH_LIBSSL > 0
357  clib_error_t *error;
358 
359  error = ikev2_initiate_delete_ike_sa (vm, mp->ispi);
360  if (error)
361  rv = VNET_API_ERROR_UNSPECIFIED;
362 #else
363  rv = VNET_API_ERROR_UNIMPLEMENTED;
364 #endif
365 
366  REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_IKE_SA_REPLY);
367 }
368 
369 static void
372 {
373  vl_api_ikev2_initiate_del_child_sa_reply_t *rmp;
374  int rv = 0;
375 
376 #if WITH_LIBSSL > 0
378  clib_error_t *error;
379 
380  error = ikev2_initiate_delete_child_sa (vm, mp->ispi);
381  if (error)
382  rv = VNET_API_ERROR_UNSPECIFIED;
383 #else
384  rv = VNET_API_ERROR_UNIMPLEMENTED;
385 #endif
386 
387  REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_CHILD_SA_REPLY);
388 }
389 
390 static void
393 {
394  vl_api_ikev2_initiate_rekey_child_sa_reply_t *rmp;
395  int rv = 0;
396 
397 #if WITH_LIBSSL > 0
399  clib_error_t *error;
400 
401  error = ikev2_initiate_rekey_child_sa (vm, mp->ispi);
402  if (error)
403  rv = VNET_API_ERROR_UNSPECIFIED;
404 #else
405  rv = VNET_API_ERROR_UNIMPLEMENTED;
406 #endif
407 
408  REPLY_MACRO (VL_API_IKEV2_INITIATE_REKEY_CHILD_SA_REPLY);
409 }
410 
411 /*
412  * ikev2_api_hookup
413  * Add vpe's API message handlers to the table.
414  * vlib has already mapped shared memory and
415  * added the client registration handlers.
416  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
417  */
418 #define vl_msg_name_crc_list
419 #include <ikev2/ikev2_all_api.h>
420 #undef vl_msg_name_crc_list
421 
422 static void
424 {
425 #define _(id,n,crc) \
426  vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + im->msg_id_base);
427  foreach_vl_msg_name_crc_ikev2;
428 #undef _
429 }
430 
431 static clib_error_t *
433 {
434  ikev2_main_t *im = &ikev2_main;
435 #define _(N,n) \
436  vl_msg_api_set_handlers(VL_API_##N + im->msg_id_base, #n, \
437  vl_api_##n##_t_handler, \
438  vl_noop_handler, \
439  vl_api_##n##_t_endian, \
440  vl_api_##n##_t_print, \
441  sizeof(vl_api_##n##_t), 1);
443 #undef _
444 
445  return (NULL);
446 }
447 
448 static clib_error_t *
450 {
451  ikev2_main_t *im = &ikev2_main;
452  clib_error_t *error = 0;
453  u8 *name;
454 
455  name = format (0, "ikev2_%08x%c", api_version, 0);
456 
457  /* Ask for a correctly-sized block of API message decode slots */
458  im->msg_id_base = vl_msg_api_get_msg_ids ((char *) name,
460 
461  error = ikev2_plugin_api_hookup (vm);
462 
463  /* Add our API messages to the global name_crc hash table */
465 
466  vec_free (name);
467 
468  return (error);
469 }
470 
472 
473 /*
474  * fd.io coding-style-patch-verification: ON
475  *
476  * Local Variables:
477  * eval: (c-set-style "gnu")
478  * End:
479  */
clib_error_t * ikev2_set_profile_responder(vlib_main_t *vm, u8 *name, u32 sw_if_index, ip4_address_t ip4)
Definition: ikev2.c:2853
static void vl_api_ikev2_initiate_rekey_child_sa_t_handler(vl_api_ikev2_initiate_rekey_child_sa_t *mp)
Definition: ikev2_api.c:392
IKEv2: Set Child SA lifetime, limited by time and/or data.
Definition: ikev2.api:216
clib_error_t * ikev2_add_del_profile(vlib_main_t *vm, u8 *name, int is_add)
Definition: ikev2.c:2712
IKEv2: Add/delete profile.
Definition: ikev2.api:48
clib_error_t * ikev2_initiate_delete_ike_sa(vlib_main_t *vm, u64 ispi)
Definition: ikev2.c:3169
#define NULL
Definition: clib.h:58
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
static void vl_api_send_msg(vl_api_registration_t *rp, u8 *elem)
Definition: api.h:35
clib_error_t * ikev2_set_profile_sa_lifetime(vlib_main_t *vm, u8 *name, u64 lifetime, u32 jitter, u32 handover, u64 maxdata)
Definition: ikev2.c:2924
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
static void vl_api_ikev2_initiate_sa_init_t_handler(vl_api_ikev2_initiate_sa_init_t *mp)
Definition: ikev2_api.c:326
u8 data[128]
Definition: ipsec.api:251
static void vl_api_ikev2_set_sa_lifetime_t_handler(vl_api_ikev2_set_sa_lifetime_t *mp)
Definition: ikev2_api.c:297
void * vl_msg_api_alloc(int nbytes)
static void vl_api_ikev2_initiate_del_ike_sa_t_handler(vl_api_ikev2_initiate_del_ike_sa_t *mp)
Definition: ikev2_api.c:349
unsigned char u8
Definition: types.h:56
#define clib_memcpy(d, s, n)
Definition: string.h:180
IKEv2: Set IKEv2 IKE transforms in SA_INIT proposal (RFC 7296)
Definition: ikev2.api:170
static void vl_api_ikev2_plugin_get_version_t_handler(vl_api_ikev2_plugin_get_version_t *mp)
Definition: ikev2_api.c:74
static void vl_api_ikev2_set_local_key_t_handler(vl_api_ikev2_set_local_key_t *mp)
Definition: ikev2_api.c:196
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
static clib_error_t * ikev2_plugin_api_hookup(vlib_main_t *vm)
Definition: ikev2_api.c:432
clib_error_t * ikev2_initiate_sa_init(vlib_main_t *vm, u8 *name)
Definition: ikev2.c:2947
#define vec_new(T, N)
Create new vector of given type and length (unspecified alignment, no header).
Definition: vec.h:311
#define foreach_ikev2_api_msg
Definition: ikev2_api.c:57
clib_error_t * ikev2_set_profile_auth(vlib_main_t *vm, u8 *name, u8 auth_method, u8 *auth_data, u8 data_hex_format)
Definition: ikev2.c:2743
IKEv2: Initiate the delete Child SA exchange.
Definition: ikev2.api:265
clib_error_t * ikev2_set_profile_esp_transforms(vlib_main_t *vm, u8 *name, ikev2_transform_encr_type_t crypto_alg, ikev2_transform_integ_type_t integ_alg, ikev2_transform_dh_type_t dh_type, u32 crypto_key_size)
Definition: ikev2.c:2899
IKEv2: Set IKEv2 profile local/remote identification.
Definition: ikev2.api:89
static void vl_api_ikev2_profile_set_ts_t_handler(vl_api_ikev2_profile_set_ts_t *mp)
Definition: ikev2_api.c:170
IKEv2: Set IKEv2 profile traffic selector parameters.
Definition: ikev2.api:113
static void vl_api_ikev2_set_ike_transforms_t_handler(vl_api_ikev2_set_ike_transforms_t *mp)
Definition: ikev2_api.c:241
clib_error_t * ikev2_set_profile_id(vlib_main_t *vm, u8 *name, u8 id_type, u8 *data, int is_local)
Definition: ikev2.c:2775
IKEv2: Initiate the rekey Child SA exchange.
Definition: ikev2.api:280
clib_error_t * ikev2_initiate_delete_child_sa(vlib_main_t *vm, u32 ispi)
Definition: ikev2.c:3129
#define REPLY_MACRO(t)
u8 name[64]
Definition: memclnt.api:152
static void vl_api_ikev2_set_responder_t_handler(vl_api_ikev2_set_responder_t *mp)
Definition: ikev2_api.c:216
clib_error_t * ikev2_set_local_key(vlib_main_t *vm, u8 *file)
Definition: ikev2.c:2700
clib_error_t * ikev2_set_profile_ts(vlib_main_t *vm, u8 *name, u8 protocol_id, u16 start_port, u16 end_port, ip4_address_t start_addr, ip4_address_t end_addr, int is_local)
Definition: ikev2.c:2814
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:203
An API client registration, only in vpp/vlib.
Definition: api_common.h:46
clib_error_t * ikev2_initiate_rekey_child_sa(vlib_main_t *vm, u32 ispi)
Definition: ikev2.c:3279
vlib_main_t * vm
Definition: buffer.c:323
static void vl_api_ikev2_initiate_del_child_sa_t_handler(vl_api_ikev2_initiate_del_child_sa_t *mp)
Definition: ikev2_api.c:371
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
IKEv2: Initiate the SA_INIT exchange.
Definition: ikev2.api:235
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:57
IKEv2: Set IKEv2 responder interface and IP address.
Definition: ikev2.api:149
IKEv2: Initiate the delete IKE SA exchange.
Definition: ikev2.api:250
IKEv2: Set IKEv2 profile authentication method.
Definition: ikev2.api:67
static void vl_api_ikev2_profile_set_auth_t_handler(vl_api_ikev2_profile_set_auth_t *mp)
Definition: ikev2_api.c:120
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
ikev2_main_t ikev2_main
Definition: ikev2.c:29
#define IKEV2_PLUGIN_VERSION_MINOR
Definition: ikev2_api.c:53
#define IKEV2_PLUGIN_VERSION_MAJOR
Definition: ikev2_api.c:52
IKEv2: Set IKEv2 local RSA private key.
Definition: ikev2.api:133
static void vl_api_ikev2_profile_set_id_t_handler(vl_api_ikev2_profile_set_id_t *mp)
Definition: ikev2_api.c:145
clib_error_t * ikev2_set_profile_ike_transforms(vlib_main_t *vm, u8 *name, ikev2_transform_encr_type_t crypto_alg, ikev2_transform_integ_type_t integ_alg, ikev2_transform_dh_type_t dh_type, u32 crypto_key_size)
Definition: ikev2.c:2874
Get the plugin version.
Definition: ikev2.api:23
IKEv2: Set IKEv2 ESP transforms in SA_INIT proposal (RFC 7296)
Definition: ikev2.api:193
static void vl_api_ikev2_set_esp_transforms_t_handler(vl_api_ikev2_set_esp_transforms_t *mp)
Definition: ikev2_api.c:269
static void setup_message_id_table(ikev2_main_t *im, api_main_t *am)
Definition: ikev2_api.c:423
static void vl_api_ikev2_profile_add_del_t_handler(vl_api_ikev2_profile_add_del_t *mp)
Definition: ikev2_api.c:98
Reply to get the plugin version.
Definition: ikev2.api:34
api_main_t api_main
Definition: api_shared.c:35
static clib_error_t * ikev2_api_init(vlib_main_t *vm)
Definition: ikev2_api.c:449
u16 vl_msg_api_get_msg_ids(const char *name, int n)
Definition: api_shared.c:957