Line data Source code
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 : #include <vnet/ip/ip_types_api.h>
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 :
34 : #define vl_endianfun /* define message structures */
35 : #include <plugins/ikev2/ikev2.api.h>
36 : #include <plugins/ikev2/ikev2_types.api.h>
37 : #undef vl_endianfun
38 :
39 : extern ikev2_main_t ikev2_main;
40 :
41 : #define IKEV2_PLUGIN_VERSION_MAJOR 1
42 : #define IKEV2_PLUGIN_VERSION_MINOR 0
43 : #define REPLY_MSG_ID_BASE ikev2_main.msg_id_base
44 : #include <vlibapi/api_helper_macros.h>
45 :
46 : #define IKEV2_MAX_DATA_LEN (1 << 10)
47 :
48 : static u32
49 45 : ikev2_encode_sa_index (u32 sai, u32 ti)
50 : {
51 45 : return (ti << 16) | sai;
52 : }
53 :
54 : static void
55 135 : ikev2_decode_sa_index (u32 api_sai, u32 * sai, u32 * ti)
56 : {
57 135 : *sai = api_sai & 0xffff;
58 135 : *ti = api_sai >> 16;
59 135 : }
60 :
61 : static void
62 22 : cp_ike_transforms (vl_api_ikev2_ike_transforms_t * vl_api_ts,
63 : ikev2_transforms_set * ts)
64 : {
65 22 : vl_api_ts->crypto_alg = ts->crypto_alg;
66 22 : vl_api_ts->integ_alg = ts->integ_alg;
67 22 : vl_api_ts->dh_group = ts->dh_type;
68 22 : vl_api_ts->crypto_key_size = ts->crypto_key_size;
69 22 : }
70 :
71 : static void
72 22 : cp_esp_transforms (vl_api_ikev2_esp_transforms_t * vl_api_ts,
73 : ikev2_transforms_set * ts)
74 : {
75 22 : vl_api_ts->crypto_alg = ts->crypto_alg;
76 22 : vl_api_ts->integ_alg = ts->integ_alg;
77 22 : vl_api_ts->crypto_key_size = ts->crypto_key_size;
78 22 : }
79 :
80 : static void
81 134 : cp_id (vl_api_ikev2_id_t * vl_api_id, ikev2_id_t * id)
82 : {
83 134 : if (!id->data)
84 1 : return;
85 :
86 133 : int size_data = 0;
87 133 : vl_api_id->type = id->type;
88 133 : size_data = sizeof (vl_api_id->data) - 1; // size without zero ending character
89 133 : if (vec_len (id->data) < size_data)
90 133 : size_data = vec_len (id->data);
91 :
92 133 : vl_api_id->data_len = size_data;
93 133 : clib_memcpy (vl_api_id->data, id->data, size_data);
94 : }
95 :
96 : static void
97 98 : cp_ts (vl_api_ikev2_ts_t * vl_api_ts, ikev2_ts_t * ts, u8 is_local)
98 : {
99 98 : vl_api_ts->is_local = is_local;
100 98 : vl_api_ts->protocol_id = ts->protocol_id;
101 98 : vl_api_ts->start_port = ts->start_port;
102 98 : vl_api_ts->end_port = ts->end_port;
103 98 : ip_address_encode2 (&ts->start_addr, &vl_api_ts->start_addr);
104 98 : ip_address_encode2 (&ts->end_addr, &vl_api_ts->end_addr);
105 98 : }
106 :
107 : static void
108 22 : cp_auth (vl_api_ikev2_auth_t * vl_api_auth, ikev2_auth_t * auth)
109 : {
110 22 : vl_api_auth->method = auth->method;
111 22 : vl_api_auth->data_len = vec_len (auth->data);
112 22 : vl_api_auth->hex = auth->hex;
113 22 : clib_memcpy (&vl_api_auth->data, auth->data, vec_len (auth->data));
114 22 : }
115 :
116 : static void
117 22 : cp_responder (vl_api_ikev2_responder_t * vl_api_responder,
118 : ikev2_responder_t * responder)
119 : {
120 22 : vl_api_responder->sw_if_index = responder->sw_if_index;
121 22 : ip_address_encode2 (&responder->addr, &vl_api_responder->addr);
122 22 : }
123 :
124 : void
125 249 : cp_sa_transform (vl_api_ikev2_sa_transform_t * vl_tr,
126 : ikev2_sa_transform_t * tr)
127 : {
128 249 : vl_tr->transform_type = tr->type;
129 249 : vl_tr->key_len = tr->key_len;
130 249 : vl_tr->key_trunc = tr->key_trunc;
131 249 : vl_tr->block_size = tr->block_size;
132 249 : vl_tr->dh_group = tr->dh_group;
133 249 : vl_tr->transform_id = tr->encr_type;
134 249 : }
135 :
136 : static void
137 22 : send_profile (ikev2_profile_t * profile, vl_api_registration_t * reg,
138 : u32 context)
139 : {
140 22 : vl_api_ikev2_profile_details_t *rmp = 0;
141 :
142 22 : rmp = vl_msg_api_alloc (sizeof (*rmp) + vec_len (profile->auth.data));
143 22 : clib_memset (rmp, 0, sizeof (*rmp) + vec_len (profile->auth.data));
144 22 : ikev2_main_t *im = &ikev2_main;
145 22 : rmp->_vl_msg_id = ntohs (VL_API_IKEV2_PROFILE_DETAILS + im->msg_id_base);
146 22 : rmp->context = context;
147 :
148 22 : int size_data = sizeof (rmp->profile.name) - 1;
149 22 : if (vec_len (profile->name) < size_data)
150 22 : size_data = vec_len (profile->name);
151 22 : clib_memcpy (rmp->profile.name, profile->name, size_data);
152 :
153 22 : cp_ike_transforms (&rmp->profile.ike_ts, &profile->ike_ts);
154 22 : cp_esp_transforms (&rmp->profile.esp_ts, &profile->esp_ts);
155 :
156 22 : cp_id (&rmp->profile.loc_id, &profile->loc_id);
157 22 : cp_id (&rmp->profile.rem_id, &profile->rem_id);
158 :
159 22 : cp_ts (&rmp->profile.rem_ts, &profile->rem_ts, 0 /* is_local */ );
160 22 : cp_ts (&rmp->profile.loc_ts, &profile->loc_ts, 1 /* is_local */ );
161 :
162 22 : cp_auth (&rmp->profile.auth, &profile->auth);
163 :
164 22 : cp_responder (&rmp->profile.responder, &profile->responder);
165 :
166 22 : rmp->profile.udp_encap = profile->udp_encap;
167 22 : rmp->profile.tun_itf = profile->tun_itf;
168 22 : rmp->profile.natt_disabled = profile->natt_disabled;
169 22 : rmp->profile.ipsec_over_udp_port = profile->ipsec_over_udp_port;
170 :
171 22 : rmp->profile.lifetime = profile->lifetime;
172 22 : rmp->profile.lifetime_maxdata = profile->lifetime_maxdata;
173 22 : rmp->profile.lifetime_jitter = profile->lifetime_jitter;
174 22 : rmp->profile.handover = profile->handover;
175 :
176 22 : vl_api_ikev2_profile_t_endian (&rmp->profile);
177 :
178 22 : vl_api_send_msg (reg, (u8 *) rmp);
179 22 : }
180 :
181 : static void
182 40 : vl_api_ikev2_profile_dump_t_handler (vl_api_ikev2_profile_dump_t * mp)
183 : {
184 40 : ikev2_main_t *im = &ikev2_main;
185 : ikev2_profile_t *profile;
186 : vl_api_registration_t *reg;
187 40 : reg = vl_api_client_index_to_registration (mp->client_index);
188 40 : if (!reg)
189 0 : return;
190 :
191 : /* *INDENT-OFF* */
192 62 : pool_foreach (profile, im->profiles)
193 : {
194 22 : send_profile (profile, reg, mp->context);
195 : }
196 : /* *INDENT-ON* */
197 : }
198 :
199 : static void
200 45 : ikev2_copy_stats (vl_api_ikev2_sa_stats_t *dst, const ikev2_stats_t *src)
201 : {
202 45 : dst->n_rekey_req = src->n_rekey_req;
203 45 : dst->n_keepalives = src->n_keepalives;
204 45 : dst->n_retransmit = src->n_retransmit;
205 45 : dst->n_init_sa_retransmit = src->n_init_retransmit;
206 45 : dst->n_sa_init_req = src->n_sa_init_req;
207 45 : dst->n_sa_auth_req = src->n_sa_auth_req;
208 45 : }
209 :
210 : static void
211 45 : send_sa (ikev2_sa_t * sa, vl_api_ikev2_sa_dump_t * mp, u32 api_sa_index)
212 : {
213 45 : vl_api_ikev2_sa_details_t *rmp = 0;
214 45 : int rv = 0;
215 : ikev2_sa_transform_t *tr;
216 :
217 : /* *INDENT-OFF* */
218 45 : REPLY_MACRO2_ZERO (VL_API_IKEV2_SA_DETAILS,
219 : {
220 : vl_api_ikev2_sa_t *rsa = &rmp->sa;
221 : vl_api_ikev2_keys_t* k = &rsa->keys;
222 : rsa->profile_index = rsa->profile_index;
223 : rsa->sa_index = api_sa_index;
224 : ip_address_encode2 (&sa->iaddr, &rsa->iaddr);
225 : ip_address_encode2 (&sa->raddr, &rsa->raddr);
226 : rsa->ispi = sa->ispi;
227 : rsa->rspi = sa->rspi;
228 : cp_id(&rsa->i_id, &sa->i_id);
229 : cp_id(&rsa->r_id, &sa->r_id);
230 :
231 : tr = ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
232 : if (tr)
233 : cp_sa_transform (&rsa->encryption, tr);
234 :
235 : tr = ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
236 : if (tr)
237 : cp_sa_transform (&rsa->prf, tr);
238 :
239 : tr = ikev2_sa_get_td_for_type (sa->r_proposals,
240 : IKEV2_TRANSFORM_TYPE_INTEG);
241 : if (tr)
242 : cp_sa_transform (&rsa->integrity, tr);
243 :
244 : tr = ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_DH);
245 : if (tr)
246 : cp_sa_transform (&rsa->dh, tr);
247 :
248 : k->sk_d_len = vec_len (sa->sk_d);
249 : clib_memcpy (&k->sk_d, sa->sk_d, k->sk_d_len);
250 :
251 : k->sk_ai_len = vec_len (sa->sk_ai);
252 : clib_memcpy (&k->sk_ai, sa->sk_ai, k->sk_ai_len);
253 :
254 : k->sk_ar_len = vec_len (sa->sk_ar);
255 : clib_memcpy (&k->sk_ar, sa->sk_ar, k->sk_ar_len);
256 :
257 : k->sk_ei_len = vec_len (sa->sk_ei);
258 : clib_memcpy (&k->sk_ei, sa->sk_ei, k->sk_ei_len);
259 :
260 : k->sk_er_len = vec_len (sa->sk_er);
261 : clib_memcpy (&k->sk_er, sa->sk_er, k->sk_er_len);
262 :
263 : k->sk_pi_len = vec_len (sa->sk_pi);
264 : clib_memcpy (&k->sk_pi, sa->sk_pi, k->sk_pi_len);
265 :
266 : k->sk_pr_len = vec_len (sa->sk_pr);
267 : clib_memcpy (&k->sk_pr, sa->sk_pr, k->sk_pr_len);
268 :
269 : ikev2_copy_stats (&rsa->stats, &sa->stats);
270 :
271 : vl_api_ikev2_sa_t_endian(rsa);
272 : });
273 : /* *INDENT-ON* */
274 : }
275 :
276 : static void
277 64 : vl_api_ikev2_sa_dump_t_handler (vl_api_ikev2_sa_dump_t * mp)
278 : {
279 64 : ikev2_main_t *km = &ikev2_main;
280 : ikev2_main_per_thread_data_t *tkm;
281 : ikev2_sa_t *sa;
282 :
283 128 : vec_foreach (tkm, km->per_thread_data)
284 : {
285 : /* *INDENT-OFF* */
286 109 : pool_foreach (sa, tkm->sas)
287 : {
288 45 : u32 api_sa_index = ikev2_encode_sa_index (sa - tkm->sas,
289 45 : tkm - km->per_thread_data);
290 45 : send_sa (sa, mp, api_sa_index);
291 : }
292 : /* *INDENT-ON* */
293 : }
294 64 : }
295 :
296 :
297 : static void
298 27 : send_child_sa (ikev2_child_sa_t * child,
299 : vl_api_ikev2_child_sa_dump_t * mp, u32 child_sa_index,
300 : u32 sa_index)
301 : {
302 27 : vl_api_ikev2_child_sa_details_t *rmp = 0;
303 27 : int rv = 0;
304 : ikev2_sa_transform_t *tr;
305 :
306 : /* *INDENT-OFF* */
307 27 : REPLY_MACRO2_ZERO (VL_API_IKEV2_CHILD_SA_DETAILS,
308 : {
309 : vl_api_ikev2_keys_t *k = &rmp->child_sa.keys;
310 : rmp->child_sa.child_sa_index = child_sa_index;
311 : rmp->child_sa.sa_index = sa_index;
312 : rmp->child_sa.i_spi =
313 : child->i_proposals ? child->i_proposals[0].spi : 0;
314 : rmp->child_sa.r_spi =
315 : child->r_proposals ? child->r_proposals[0].spi : 0;
316 :
317 : tr = ikev2_sa_get_td_for_type (child->r_proposals,
318 : IKEV2_TRANSFORM_TYPE_ENCR);
319 : if (tr)
320 : cp_sa_transform (&rmp->child_sa.encryption, tr);
321 :
322 : tr = ikev2_sa_get_td_for_type (child->r_proposals,
323 : IKEV2_TRANSFORM_TYPE_INTEG);
324 : if (tr)
325 : cp_sa_transform (&rmp->child_sa.integrity, tr);
326 :
327 : tr = ikev2_sa_get_td_for_type (child->r_proposals,
328 : IKEV2_TRANSFORM_TYPE_ESN);
329 : if (tr)
330 : cp_sa_transform (&rmp->child_sa.esn, tr);
331 :
332 : k->sk_ei_len = vec_len (child->sk_ei);
333 : clib_memcpy (&k->sk_ei, child->sk_ei, k->sk_ei_len);
334 :
335 : k->sk_er_len = vec_len (child->sk_er);
336 : clib_memcpy (&k->sk_er, child->sk_er, k->sk_er_len);
337 :
338 : if (vec_len (child->sk_ai))
339 : {
340 : k->sk_ai_len = vec_len (child->sk_ai);
341 : clib_memcpy (&k->sk_ai, child->sk_ai,
342 : k->sk_ai_len);
343 :
344 : k->sk_ar_len = vec_len (child->sk_ar);
345 : clib_memcpy (&k->sk_ar, child->sk_ar,
346 : k->sk_ar_len);
347 : }
348 :
349 : vl_api_ikev2_child_sa_t_endian (&rmp->child_sa);
350 : });
351 : /* *INDENT-ON* */
352 : }
353 :
354 : static void
355 27 : vl_api_ikev2_child_sa_dump_t_handler (vl_api_ikev2_child_sa_dump_t * mp)
356 : {
357 27 : ikev2_main_t *im = &ikev2_main;
358 : ikev2_main_per_thread_data_t *tkm;
359 : ikev2_sa_t *sa;
360 : ikev2_child_sa_t *child;
361 27 : u32 sai = ~0, ti = ~0;
362 :
363 27 : ikev2_decode_sa_index (clib_net_to_host_u32 (mp->sa_index), &sai, &ti);
364 :
365 27 : if (vec_len (im->per_thread_data) <= ti)
366 0 : return;
367 :
368 27 : tkm = vec_elt_at_index (im->per_thread_data, ti);
369 :
370 27 : if (pool_len (tkm->sas) <= sai || pool_is_free_index (tkm->sas, sai))
371 0 : return;
372 :
373 27 : sa = pool_elt_at_index (tkm->sas, sai);
374 :
375 54 : vec_foreach (child, sa->childs)
376 : {
377 27 : u32 child_sa_index = child - sa->childs;
378 27 : send_child_sa (child, mp, child_sa_index, sai);
379 : }
380 : }
381 :
382 : static void
383 54 : vl_api_ikev2_traffic_selector_dump_t_handler
384 : (vl_api_ikev2_traffic_selector_dump_t * mp)
385 : {
386 54 : ikev2_main_t *im = &ikev2_main;
387 : ikev2_main_per_thread_data_t *tkm;
388 : ikev2_sa_t *sa;
389 : ikev2_child_sa_t *child;
390 : ikev2_ts_t *ts;
391 54 : u32 sai = ~0, ti = ~0;
392 :
393 54 : u32 api_sa_index = clib_net_to_host_u32 (mp->sa_index);
394 54 : u32 child_sa_index = clib_net_to_host_u32 (mp->child_sa_index);
395 54 : ikev2_decode_sa_index (api_sa_index, &sai, &ti);
396 :
397 54 : if (vec_len (im->per_thread_data) <= ti)
398 0 : return;
399 :
400 54 : tkm = vec_elt_at_index (im->per_thread_data, ti);
401 :
402 54 : if (pool_len (tkm->sas) <= sai || pool_is_free_index (tkm->sas, sai))
403 0 : return;
404 :
405 54 : sa = pool_elt_at_index (tkm->sas, sai);
406 :
407 54 : if (vec_len (sa->childs) <= child_sa_index)
408 0 : return;
409 :
410 54 : child = vec_elt_at_index (sa->childs, child_sa_index);
411 :
412 108 : vec_foreach (ts, mp->is_initiator ? child->tsi : child->tsr)
413 : {
414 54 : vl_api_ikev2_traffic_selector_details_t *rmp = 0;
415 54 : int rv = 0;
416 :
417 : /* *INDENT-OFF* */
418 54 : REPLY_MACRO2_ZERO (VL_API_IKEV2_TRAFFIC_SELECTOR_DETAILS,
419 : {
420 : rmp->ts.sa_index = api_sa_index;
421 : rmp->ts.child_sa_index = child_sa_index;
422 : cp_ts (&rmp->ts, ts, mp->is_initiator);
423 : vl_api_ikev2_ts_t_endian (&rmp->ts);
424 : });
425 : /* *INDENT-ON* */
426 : }
427 : }
428 :
429 : static void
430 54 : vl_api_ikev2_nonce_get_t_handler (vl_api_ikev2_nonce_get_t * mp)
431 : {
432 54 : ikev2_main_t *im = &ikev2_main;
433 : ikev2_main_per_thread_data_t *tkm;
434 : ikev2_sa_t *sa;
435 54 : u32 sai = ~0, ti = ~0;
436 :
437 54 : ikev2_decode_sa_index (clib_net_to_host_u32 (mp->sa_index), &sai, &ti);
438 :
439 54 : if (vec_len (im->per_thread_data) <= ti)
440 0 : return;
441 :
442 54 : tkm = vec_elt_at_index (im->per_thread_data, ti);
443 :
444 54 : if (pool_len (tkm->sas) <= sai || pool_is_free_index (tkm->sas, sai))
445 0 : return;
446 :
447 54 : sa = pool_elt_at_index (tkm->sas, sai);
448 :
449 54 : u8 *nonce = mp->is_initiator ? sa->i_nonce : sa->r_nonce;
450 54 : vl_api_ikev2_nonce_get_reply_t *rmp = 0;
451 54 : int data_len = vec_len (nonce);
452 54 : int rv = 0;
453 :
454 : /* *INDENT-OFF* */
455 54 : REPLY_MACRO3_ZERO (VL_API_IKEV2_NONCE_GET_REPLY, data_len,
456 : {
457 : rmp->data_len = clib_host_to_net_u32 (data_len);
458 : clib_memcpy (rmp->nonce, nonce, data_len);
459 : });
460 : /* *INDENT-ON* */
461 : }
462 :
463 : static void
464 0 : vl_api_ikev2_plugin_get_version_t_handler (vl_api_ikev2_plugin_get_version_t *
465 : mp)
466 : {
467 0 : ikev2_main_t *im = &ikev2_main;
468 : vl_api_ikev2_plugin_get_version_reply_t *rmp;
469 0 : int msg_size = sizeof (*rmp);
470 : vl_api_registration_t *reg;
471 :
472 0 : reg = vl_api_client_index_to_registration (mp->client_index);
473 0 : if (!reg)
474 0 : return;
475 :
476 0 : rmp = vl_msg_api_alloc (msg_size);
477 0 : clib_memset (rmp, 0, msg_size);
478 0 : rmp->_vl_msg_id =
479 0 : ntohs (VL_API_IKEV2_PLUGIN_GET_VERSION_REPLY + im->msg_id_base);
480 0 : rmp->context = mp->context;
481 0 : rmp->major = htonl (IKEV2_PLUGIN_VERSION_MAJOR);
482 0 : rmp->minor = htonl (IKEV2_PLUGIN_VERSION_MINOR);
483 :
484 0 : vl_api_send_msg (reg, (u8 *) rmp);
485 : }
486 :
487 : static void
488 2 : vl_api_ikev2_profile_set_liveness_t_handler
489 : (vl_api_ikev2_profile_set_liveness_t * mp)
490 : {
491 : vl_api_ikev2_profile_set_liveness_reply_t *rmp;
492 2 : int rv = 0;
493 : clib_error_t *error;
494 2 : error = ikev2_set_liveness_params (clib_net_to_host_u32 (mp->period),
495 : clib_net_to_host_u32 (mp->max_retries));
496 2 : if (error)
497 : {
498 0 : ikev2_log_error ("%U", format_clib_error, error);
499 0 : clib_error_free (error);
500 0 : rv = VNET_API_ERROR_UNSPECIFIED;
501 : }
502 2 : REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_LIVENESS_REPLY);
503 : }
504 :
505 : static void
506 42 : vl_api_ikev2_profile_add_del_t_handler (vl_api_ikev2_profile_add_del_t * mp)
507 : {
508 : vl_api_ikev2_profile_add_del_reply_t *rmp;
509 42 : int rv = 0;
510 42 : vlib_main_t *vm = vlib_get_main ();
511 : clib_error_t *error;
512 42 : u8 *tmp = format (0, "%s", mp->name);
513 42 : error = ikev2_add_del_profile (vm, tmp, mp->is_add);
514 42 : vec_free (tmp);
515 42 : if (error)
516 : {
517 0 : ikev2_log_error ("%U", format_clib_error, error);
518 0 : clib_error_free (error);
519 0 : rv = VNET_API_ERROR_UNSPECIFIED;
520 : }
521 42 : REPLY_MACRO (VL_API_IKEV2_PROFILE_ADD_DEL_REPLY);
522 : }
523 :
524 : static void
525 22 : vl_api_ikev2_profile_set_auth_t_handler
526 : (vl_api_ikev2_profile_set_auth_t * mp)
527 : {
528 : vl_api_ikev2_profile_set_auth_reply_t *rmp;
529 22 : int rv = 0;
530 22 : vlib_main_t *vm = vlib_get_main ();
531 : clib_error_t *error;
532 22 : int data_len = ntohl (mp->data_len);
533 22 : if (data_len > 0 && data_len <= IKEV2_MAX_DATA_LEN)
534 22 : {
535 22 : u8 *tmp = format (0, "%s", mp->name);
536 22 : u8 *data = vec_new (u8, data_len);
537 22 : clib_memcpy (data, mp->data, data_len);
538 : error =
539 22 : ikev2_set_profile_auth (vm, tmp, mp->auth_method, data, mp->is_hex);
540 22 : vec_free (tmp);
541 22 : vec_free (data);
542 22 : if (error)
543 : {
544 0 : ikev2_log_error ("%U", format_clib_error, error);
545 0 : clib_error_free (error);
546 0 : rv = VNET_API_ERROR_UNSPECIFIED;
547 : }
548 : }
549 : else
550 0 : rv = VNET_API_ERROR_INVALID_VALUE;
551 22 : REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_AUTH_REPLY);
552 : }
553 :
554 : static void
555 43 : vl_api_ikev2_profile_set_id_t_handler (vl_api_ikev2_profile_set_id_t * mp)
556 : {
557 : vl_api_ikev2_profile_set_id_reply_t *rmp;
558 43 : int rv = 0;
559 43 : vlib_main_t *vm = vlib_get_main ();
560 : clib_error_t *error;
561 43 : u8 *tmp = format (0, "%s", mp->name);
562 43 : int data_len = ntohl (mp->data_len);
563 43 : if (data_len > 0 && data_len <= IKEV2_MAX_DATA_LEN)
564 43 : {
565 43 : u8 *data = vec_new (u8, data_len);
566 43 : clib_memcpy (data, mp->data, data_len);
567 43 : error = ikev2_set_profile_id (vm, tmp, mp->id_type, data, mp->is_local);
568 43 : vec_free (tmp);
569 43 : vec_free (data);
570 43 : if (error)
571 : {
572 0 : ikev2_log_error ("%U", format_clib_error, error);
573 0 : clib_error_free (error);
574 0 : rv = VNET_API_ERROR_UNSPECIFIED;
575 : }
576 : }
577 : else
578 0 : rv = VNET_API_ERROR_INVALID_VALUE;
579 :
580 43 : REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_ID_REPLY);
581 : }
582 :
583 : static void
584 2 : vl_api_ikev2_profile_set_udp_encap_t_handler
585 : (vl_api_ikev2_profile_set_udp_encap_t * mp)
586 : {
587 : vl_api_ikev2_profile_set_udp_encap_reply_t *rmp;
588 2 : int rv = 0;
589 2 : vlib_main_t *vm = vlib_get_main ();
590 : clib_error_t *error;
591 2 : u8 *tmp = format (0, "%s", mp->name);
592 2 : error = ikev2_set_profile_udp_encap (vm, tmp);
593 2 : vec_free (tmp);
594 2 : if (error)
595 : {
596 0 : ikev2_log_error ("%U", format_clib_error, error);
597 0 : clib_error_free (error);
598 0 : rv = VNET_API_ERROR_UNSPECIFIED;
599 : }
600 2 : REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_UDP_ENCAP_REPLY);
601 : }
602 :
603 : static void
604 44 : vl_api_ikev2_profile_set_ts_t_handler (vl_api_ikev2_profile_set_ts_t * mp)
605 : {
606 : vl_api_ikev2_profile_set_ts_reply_t *rmp;
607 44 : int rv = 0;
608 44 : vlib_main_t *vm = vlib_get_main ();
609 : clib_error_t *error;
610 44 : u8 *tmp = format (0, "%s", mp->name);
611 : ip_address_t start_addr, end_addr;
612 44 : ip_address_decode2 (&mp->ts.start_addr, &start_addr);
613 44 : ip_address_decode2 (&mp->ts.end_addr, &end_addr);
614 : error =
615 44 : ikev2_set_profile_ts (vm, tmp, mp->ts.protocol_id,
616 44 : clib_net_to_host_u16 (mp->ts.start_port),
617 44 : clib_net_to_host_u16 (mp->ts.end_port),
618 44 : start_addr, end_addr, mp->ts.is_local);
619 44 : vec_free (tmp);
620 44 : if (error)
621 : {
622 0 : ikev2_log_error ("%U", format_clib_error, error);
623 0 : clib_error_free (error);
624 0 : rv = VNET_API_ERROR_UNSPECIFIED;
625 : }
626 44 : REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_TS_REPLY);
627 : }
628 :
629 : static void
630 1 : vl_api_ikev2_set_local_key_t_handler (vl_api_ikev2_set_local_key_t * mp)
631 : {
632 : vl_api_ikev2_set_local_key_reply_t *rmp;
633 1 : int rv = 0;
634 1 : vlib_main_t *vm = vlib_get_main ();
635 : clib_error_t *error;
636 :
637 1 : error = ikev2_set_local_key (vm, mp->key_file);
638 1 : if (error)
639 : {
640 0 : ikev2_log_error ("%U", format_clib_error, error);
641 0 : clib_error_free (error);
642 0 : rv = VNET_API_ERROR_UNSPECIFIED;
643 : }
644 1 : REPLY_MACRO (VL_API_IKEV2_SET_LOCAL_KEY_REPLY);
645 : }
646 :
647 : static void
648 4 : vl_api_ikev2_set_responder_hostname_t_handler (
649 : vl_api_ikev2_set_responder_hostname_t *mp)
650 : {
651 : vl_api_ikev2_set_responder_hostname_reply_t *rmp;
652 4 : int rv = 0;
653 4 : vlib_main_t *vm = vlib_get_main ();
654 : clib_error_t *error;
655 :
656 4 : u8 *tmp = format (0, "%s", mp->name);
657 4 : u8 *hn = format (0, "%s", mp->hostname);
658 4 : u32 sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
659 :
660 4 : error = ikev2_set_profile_responder_hostname (vm, tmp, hn, sw_if_index);
661 4 : vec_free (tmp);
662 4 : vec_free (hn);
663 :
664 4 : if (error)
665 : {
666 0 : ikev2_log_error ("%U", format_clib_error, error);
667 0 : clib_error_free (error);
668 0 : rv = VNET_API_ERROR_UNSPECIFIED;
669 : }
670 4 : REPLY_MACRO (VL_API_IKEV2_SET_RESPONDER_HOSTNAME_REPLY);
671 : }
672 :
673 : static void
674 4 : vl_api_ikev2_set_responder_t_handler (vl_api_ikev2_set_responder_t * mp)
675 : {
676 : vl_api_ikev2_set_responder_reply_t *rmp;
677 4 : int rv = 0;
678 4 : vlib_main_t *vm = vlib_get_main ();
679 : clib_error_t *error;
680 :
681 4 : u8 *tmp = format (0, "%s", mp->name);
682 : ip_address_t ip;
683 4 : ip_address_decode2 (&mp->responder.addr, &ip);
684 4 : u32 sw_if_index = clib_net_to_host_u32 (mp->responder.sw_if_index);
685 :
686 4 : error = ikev2_set_profile_responder (vm, tmp, sw_if_index, ip);
687 4 : vec_free (tmp);
688 4 : if (error)
689 : {
690 0 : ikev2_log_error ("%U", format_clib_error, error);
691 0 : clib_error_free (error);
692 0 : rv = VNET_API_ERROR_UNSPECIFIED;
693 : }
694 4 : REPLY_MACRO (VL_API_IKEV2_SET_RESPONDER_REPLY);
695 : }
696 :
697 : static void
698 8 : vl_api_ikev2_set_ike_transforms_t_handler (vl_api_ikev2_set_ike_transforms_t *
699 : mp)
700 : {
701 : vl_api_ikev2_set_ike_transforms_reply_t *rmp;
702 8 : int rv = 0;
703 8 : vlib_main_t *vm = vlib_get_main ();
704 : clib_error_t *error;
705 :
706 8 : u8 *tmp = format (0, "%s", mp->name);
707 :
708 : error =
709 8 : ikev2_set_profile_ike_transforms (vm, tmp, mp->tr.crypto_alg,
710 8 : mp->tr.integ_alg,
711 8 : mp->tr.dh_group,
712 : ntohl (mp->tr.crypto_key_size));
713 8 : vec_free (tmp);
714 8 : if (error)
715 : {
716 0 : ikev2_log_error ("%U", format_clib_error, error);
717 0 : clib_error_free (error);
718 0 : rv = VNET_API_ERROR_UNSPECIFIED;
719 : }
720 8 : REPLY_MACRO (VL_API_IKEV2_SET_IKE_TRANSFORMS_REPLY);
721 : }
722 :
723 : static void
724 8 : vl_api_ikev2_set_esp_transforms_t_handler (vl_api_ikev2_set_esp_transforms_t *
725 : mp)
726 : {
727 : vl_api_ikev2_set_esp_transforms_reply_t *rmp;
728 8 : int rv = 0;
729 8 : vlib_main_t *vm = vlib_get_main ();
730 : clib_error_t *error;
731 :
732 8 : u8 *tmp = format (0, "%s", mp->name);
733 :
734 : error =
735 8 : ikev2_set_profile_esp_transforms (vm, tmp, mp->tr.crypto_alg,
736 8 : mp->tr.integ_alg,
737 : ntohl (mp->tr.crypto_key_size));
738 8 : vec_free (tmp);
739 8 : if (error)
740 : {
741 0 : ikev2_log_error ("%U", format_clib_error, error);
742 0 : clib_error_free (error);
743 0 : rv = VNET_API_ERROR_UNSPECIFIED;
744 : }
745 8 : REPLY_MACRO (VL_API_IKEV2_SET_ESP_TRANSFORMS_REPLY);
746 : }
747 :
748 : static void
749 1 : vl_api_ikev2_set_sa_lifetime_t_handler (vl_api_ikev2_set_sa_lifetime_t * mp)
750 : {
751 : vl_api_ikev2_set_sa_lifetime_reply_t *rmp;
752 1 : int rv = 0;
753 1 : vlib_main_t *vm = vlib_get_main ();
754 : clib_error_t *error;
755 :
756 1 : u8 *tmp = format (0, "%s", mp->name);
757 :
758 : error =
759 1 : ikev2_set_profile_sa_lifetime (vm, tmp,
760 : clib_net_to_host_u64 (mp->lifetime),
761 : ntohl (mp->lifetime_jitter),
762 : ntohl (mp->handover),
763 : clib_net_to_host_u64
764 : (mp->lifetime_maxdata));
765 1 : vec_free (tmp);
766 1 : if (error)
767 : {
768 0 : ikev2_log_error ("%U", format_clib_error, error);
769 0 : clib_error_free (error);
770 0 : rv = VNET_API_ERROR_UNSPECIFIED;
771 : }
772 1 : REPLY_MACRO (VL_API_IKEV2_SET_SA_LIFETIME_REPLY);
773 : }
774 :
775 : static void
776 2 : vl_api_ikev2_profile_set_ipsec_udp_port_t_handler
777 : (vl_api_ikev2_profile_set_ipsec_udp_port_t * mp)
778 : {
779 : vl_api_ikev2_profile_set_ipsec_udp_port_reply_t *rmp;
780 2 : int rv = 0;
781 2 : vlib_main_t *vm = vlib_get_main ();
782 :
783 2 : u8 *tmp = format (0, "%s", mp->name);
784 :
785 : rv =
786 2 : ikev2_set_profile_ipsec_udp_port (vm, tmp,
787 2 : clib_net_to_host_u16 (mp->port),
788 2 : mp->is_set);
789 2 : vec_free (tmp);
790 2 : REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_IPSEC_UDP_PORT_REPLY);
791 : }
792 :
793 : static void
794 1 : vl_api_ikev2_set_tunnel_interface_t_handler
795 : (vl_api_ikev2_set_tunnel_interface_t * mp)
796 : {
797 : vl_api_ikev2_set_tunnel_interface_reply_t *rmp;
798 1 : int rv = 0;
799 :
800 1 : VALIDATE_SW_IF_INDEX (mp);
801 :
802 1 : u8 *tmp = format (0, "%s", mp->name);
803 : clib_error_t *error;
804 :
805 1 : error = ikev2_set_profile_tunnel_interface (vlib_get_main (), tmp,
806 : ntohl (mp->sw_if_index));
807 :
808 1 : if (error)
809 : {
810 0 : ikev2_log_error ("%U", format_clib_error, error);
811 0 : clib_error_free (error);
812 0 : rv = VNET_API_ERROR_UNSPECIFIED;
813 : }
814 1 : vec_free (tmp);
815 1 : BAD_SW_IF_INDEX_LABEL;
816 1 : REPLY_MACRO (VL_API_IKEV2_SET_TUNNEL_INTERFACE_REPLY);
817 : }
818 :
819 : static void
820 6 : vl_api_ikev2_initiate_sa_init_t_handler (vl_api_ikev2_initiate_sa_init_t * mp)
821 : {
822 : vl_api_ikev2_initiate_sa_init_reply_t *rmp;
823 6 : int rv = 0;
824 6 : vlib_main_t *vm = vlib_get_main ();
825 : clib_error_t *error;
826 :
827 6 : u8 *tmp = format (0, "%s", mp->name);
828 :
829 6 : error = ikev2_initiate_sa_init (vm, tmp);
830 6 : vec_free (tmp);
831 6 : if (error)
832 : {
833 0 : ikev2_log_error ("%U", format_clib_error, error);
834 0 : clib_error_free (error);
835 0 : rv = VNET_API_ERROR_UNSPECIFIED;
836 : }
837 6 : REPLY_MACRO (VL_API_IKEV2_INITIATE_SA_INIT_REPLY);
838 : }
839 :
840 : static void
841 6 : vl_api_ikev2_initiate_del_ike_sa_t_handler (vl_api_ikev2_initiate_del_ike_sa_t
842 : * mp)
843 : {
844 : vl_api_ikev2_initiate_del_ike_sa_reply_t *rmp;
845 6 : int rv = 0;
846 6 : vlib_main_t *vm = vlib_get_main ();
847 : clib_error_t *error;
848 :
849 6 : error = ikev2_initiate_delete_ike_sa (vm, mp->ispi);
850 6 : if (error)
851 : {
852 0 : ikev2_log_error ("%U", format_clib_error, error);
853 0 : clib_error_free (error);
854 0 : rv = VNET_API_ERROR_UNSPECIFIED;
855 : }
856 6 : REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_IKE_SA_REPLY);
857 : }
858 :
859 : static void
860 0 : vl_api_ikev2_initiate_del_child_sa_t_handler
861 : (vl_api_ikev2_initiate_del_child_sa_t * mp)
862 : {
863 : vl_api_ikev2_initiate_del_child_sa_reply_t *rmp;
864 0 : int rv = 0;
865 0 : vlib_main_t *vm = vlib_get_main ();
866 : clib_error_t *error;
867 :
868 0 : error = ikev2_initiate_delete_child_sa (vm, mp->ispi);
869 0 : if (error)
870 : {
871 0 : ikev2_log_error ("%U", format_clib_error, error);
872 0 : clib_error_free (error);
873 0 : rv = VNET_API_ERROR_UNSPECIFIED;
874 : }
875 0 : REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_CHILD_SA_REPLY);
876 : }
877 :
878 : static void
879 1 : vl_api_ikev2_profile_disable_natt_t_handler
880 : (vl_api_ikev2_profile_disable_natt_t * mp)
881 : {
882 : vl_api_ikev2_profile_disable_natt_reply_t *rmp;
883 1 : int rv = 0;
884 : clib_error_t *error;
885 :
886 1 : u8 *tmp = format (0, "%s", mp->name);
887 1 : error = ikev2_profile_natt_disable (tmp);
888 1 : vec_free (tmp);
889 1 : if (error)
890 : {
891 0 : ikev2_log_error ("%U", format_clib_error, error);
892 0 : clib_error_free (error);
893 0 : rv = VNET_API_ERROR_UNSPECIFIED;
894 : }
895 1 : REPLY_MACRO (VL_API_IKEV2_PROFILE_DISABLE_NATT_REPLY);
896 : }
897 :
898 : static void
899 3 : vl_api_ikev2_initiate_rekey_child_sa_t_handler
900 : (vl_api_ikev2_initiate_rekey_child_sa_t * mp)
901 : {
902 : vl_api_ikev2_initiate_rekey_child_sa_reply_t *rmp;
903 3 : int rv = 0;
904 3 : vlib_main_t *vm = vlib_get_main ();
905 : clib_error_t *error;
906 :
907 3 : error = ikev2_initiate_rekey_child_sa (vm, mp->ispi);
908 3 : if (error)
909 : {
910 0 : ikev2_log_error ("%U", format_clib_error, error);
911 0 : clib_error_free (error);
912 0 : rv = VNET_API_ERROR_UNSPECIFIED;
913 : }
914 3 : REPLY_MACRO (VL_API_IKEV2_INITIATE_REKEY_CHILD_SA_REPLY);
915 : }
916 :
917 : #include <ikev2/ikev2.api.c>
918 : static clib_error_t *
919 559 : ikev2_api_init (vlib_main_t * vm)
920 : {
921 559 : ikev2_main_t *im = &ikev2_main;
922 :
923 : /* Ask for a correctly-sized block of API message decode slots */
924 559 : im->msg_id_base = setup_message_id_table ();
925 :
926 559 : return 0;
927 : }
928 :
929 1119 : VLIB_INIT_FUNCTION (ikev2_api_init);
930 :
931 : /*
932 : * fd.io coding-style-patch-verification: ON
933 : *
934 : * Local Variables:
935 : * eval: (c-set-style "gnu")
936 : * End:
937 : */
|