2 *------------------------------------------------------------------
3 * ipsec_api.c - ipsec api
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
20 #include <vnet/vnet.h>
21 #include <vlibmemory/api.h>
23 #include <vnet/interface.h>
24 #include <vnet/api_errno.h>
25 #include <vnet/ip/ip.h>
27 #include <vnet/vnet_msg_enum.h>
30 #include <vnet/ipsec/ipsec.h>
31 #include <vnet/ipsec/ikev2.h>
34 #define vl_typedefs /* define message structures */
35 #include <vnet/vnet_all_api_h.h>
38 #define vl_endianfun /* define message structures */
39 #include <vnet/vnet_all_api_h.h>
42 /* instantiate all the print functions we know about */
43 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
45 #include <vnet/vnet_all_api_h.h>
48 #include <vlibapi/api_helper_macros.h>
50 #define foreach_vpe_api_msg \
51 _(IPSEC_SPD_ADD_DEL, ipsec_spd_add_del) \
52 _(IPSEC_INTERFACE_ADD_DEL_SPD, ipsec_interface_add_del_spd) \
53 _(IPSEC_SPD_ADD_DEL_ENTRY, ipsec_spd_add_del_entry) \
54 _(IPSEC_SAD_ADD_DEL_ENTRY, ipsec_sad_add_del_entry) \
55 _(IPSEC_SA_SET_KEY, ipsec_sa_set_key) \
56 _(IPSEC_SPD_DUMP, ipsec_spd_dump) \
57 _(IKEV2_PROFILE_ADD_DEL, ikev2_profile_add_del) \
58 _(IKEV2_PROFILE_SET_AUTH, ikev2_profile_set_auth) \
59 _(IKEV2_PROFILE_SET_ID, ikev2_profile_set_id) \
60 _(IKEV2_PROFILE_SET_TS, ikev2_profile_set_ts) \
61 _(IKEV2_SET_LOCAL_KEY, ikev2_set_local_key) \
62 _(IKEV2_SET_RESPONDER, ikev2_set_responder) \
63 _(IKEV2_SET_IKE_TRANSFORMS, ikev2_set_ike_transforms) \
64 _(IKEV2_SET_ESP_TRANSFORMS, ikev2_set_esp_transforms) \
65 _(IKEV2_SET_SA_LIFETIME, ikev2_set_sa_lifetime) \
66 _(IKEV2_INITIATE_SA_INIT, ikev2_initiate_sa_init) \
67 _(IKEV2_INITIATE_DEL_IKE_SA, ikev2_initiate_del_ike_sa) \
68 _(IKEV2_INITIATE_DEL_CHILD_SA, ikev2_initiate_del_child_sa) \
69 _(IKEV2_INITIATE_REKEY_CHILD_SA, ikev2_initiate_rekey_child_sa)
71 static void vl_api_ipsec_spd_add_del_t_handler
72 (vl_api_ipsec_spd_add_del_t * mp)
75 clib_warning ("unimplemented");
78 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
79 vl_api_ipsec_spd_add_del_reply_t *rmp;
82 rv = ipsec_add_del_spd (vm, ntohl (mp->spd_id), mp->is_add);
84 REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_REPLY);
88 static void vl_api_ipsec_interface_add_del_spd_t_handler
89 (vl_api_ipsec_interface_add_del_spd_t * mp)
91 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
92 vl_api_ipsec_interface_add_del_spd_reply_t *rmp;
94 u32 sw_if_index __attribute__ ((unused));
95 u32 spd_id __attribute__ ((unused));
97 sw_if_index = ntohl (mp->sw_if_index);
98 spd_id = ntohl (mp->spd_id);
100 VALIDATE_SW_IF_INDEX (mp);
103 rv = ipsec_set_interface_spd (vm, sw_if_index, spd_id, mp->is_add);
105 rv = VNET_API_ERROR_UNIMPLEMENTED;
108 BAD_SW_IF_INDEX_LABEL;
110 REPLY_MACRO (VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY);
113 static void vl_api_ipsec_spd_add_del_entry_t_handler
114 (vl_api_ipsec_spd_add_del_entry_t * mp)
116 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
117 vl_api_ipsec_spd_add_del_entry_reply_t *rmp;
123 memset (&p, 0, sizeof (p));
125 p.id = ntohl (mp->spd_id);
126 p.priority = ntohl (mp->priority);
127 p.is_outbound = mp->is_outbound;
128 p.is_ipv6 = mp->is_ipv6;
130 if (mp->is_ipv6 || mp->is_ip_any)
132 clib_memcpy (&p.raddr.start, mp->remote_address_start, 16);
133 clib_memcpy (&p.raddr.stop, mp->remote_address_stop, 16);
134 clib_memcpy (&p.laddr.start, mp->local_address_start, 16);
135 clib_memcpy (&p.laddr.stop, mp->local_address_stop, 16);
139 clib_memcpy (&p.raddr.start.ip4.data, mp->remote_address_start, 4);
140 clib_memcpy (&p.raddr.stop.ip4.data, mp->remote_address_stop, 4);
141 clib_memcpy (&p.laddr.start.ip4.data, mp->local_address_start, 4);
142 clib_memcpy (&p.laddr.stop.ip4.data, mp->local_address_stop, 4);
144 p.protocol = mp->protocol;
145 p.rport.start = ntohs (mp->remote_port_start);
146 p.rport.stop = ntohs (mp->remote_port_stop);
147 p.lport.start = ntohs (mp->local_port_start);
148 p.lport.stop = ntohs (mp->local_port_stop);
149 /* policy action resolve unsupported */
150 if (mp->policy == IPSEC_POLICY_ACTION_RESOLVE)
152 clib_warning ("unsupported action: 'resolve'");
153 rv = VNET_API_ERROR_UNIMPLEMENTED;
156 p.policy = mp->policy;
157 p.sa_id = ntohl (mp->sa_id);
159 rv = ipsec_add_del_policy (vm, &p, mp->is_add);
166 rv = ipsec_add_del_policy (vm, &p, mp->is_add);
169 rv = VNET_API_ERROR_UNIMPLEMENTED;
174 REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_ENTRY_REPLY);
177 static void vl_api_ipsec_sad_add_del_entry_t_handler
178 (vl_api_ipsec_sad_add_del_entry_t * mp)
180 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
181 vl_api_ipsec_sad_add_del_entry_reply_t *rmp;
184 ipsec_main_t *im = &ipsec_main;
187 memset (&sa, 0, sizeof (sa));
189 sa.id = ntohl (mp->sad_id);
190 sa.spi = ntohl (mp->spi);
191 /* security protocol AH unsupported */
192 if (mp->protocol == IPSEC_PROTOCOL_AH)
194 clib_warning ("unsupported security protocol 'AH'");
195 rv = VNET_API_ERROR_UNIMPLEMENTED;
198 sa.protocol = mp->protocol;
199 /* check for unsupported crypto-alg */
200 if (mp->crypto_algorithm < IPSEC_CRYPTO_ALG_AES_CBC_128 ||
201 mp->crypto_algorithm >= IPSEC_CRYPTO_N_ALG)
203 clib_warning ("unsupported crypto-alg: '%U'", format_ipsec_crypto_alg,
204 mp->crypto_algorithm);
205 rv = VNET_API_ERROR_UNIMPLEMENTED;
208 sa.crypto_alg = mp->crypto_algorithm;
209 sa.crypto_key_len = mp->crypto_key_length;
210 clib_memcpy (&sa.crypto_key, mp->crypto_key, sizeof (sa.crypto_key));
211 /* check for unsupported integ-alg */
212 if (mp->integrity_algorithm >= IPSEC_INTEG_N_ALG)
214 clib_warning ("unsupported integ-alg: '%U'", format_ipsec_integ_alg,
215 mp->integrity_algorithm);
216 rv = VNET_API_ERROR_UNIMPLEMENTED;
220 sa.integ_alg = mp->integrity_algorithm;
221 sa.integ_key_len = mp->integrity_key_length;
222 clib_memcpy (&sa.integ_key, mp->integrity_key, sizeof (sa.integ_key));
223 sa.use_esn = mp->use_extended_sequence_number;
224 sa.is_tunnel = mp->is_tunnel;
225 sa.is_tunnel_ip6 = mp->is_tunnel_ipv6;
226 if (sa.is_tunnel_ip6)
228 clib_memcpy (&sa.tunnel_src_addr, mp->tunnel_src_address, 16);
229 clib_memcpy (&sa.tunnel_dst_addr, mp->tunnel_dst_address, 16);
233 clib_memcpy (&sa.tunnel_src_addr.ip4.data, mp->tunnel_src_address, 4);
234 clib_memcpy (&sa.tunnel_dst_addr.ip4.data, mp->tunnel_dst_address, 4);
237 ASSERT (im->cb.check_support_cb);
238 clib_error_t *err = im->cb.check_support_cb (&sa);
241 clib_warning ("%s", err->what);
242 rv = VNET_API_ERROR_UNIMPLEMENTED;
246 rv = ipsec_add_del_sa (vm, &sa, mp->is_add);
248 rv = VNET_API_ERROR_UNIMPLEMENTED;
253 REPLY_MACRO (VL_API_IPSEC_SAD_ADD_DEL_ENTRY_REPLY);
257 send_ipsec_spd_details (ipsec_policy_t * p, unix_shared_memory_queue_t * q,
260 vl_api_ipsec_spd_details_t *mp;
262 mp = vl_msg_api_alloc (sizeof (*mp));
263 memset (mp, 0, sizeof (*mp));
264 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_DETAILS);
265 mp->context = context;
267 mp->spd_id = htonl (p->id);
268 mp->priority = htonl (p->priority);
269 mp->is_outbound = p->is_outbound;
270 mp->is_ipv6 = p->is_ipv6;
273 memcpy (mp->local_start_addr, &p->laddr.start.ip6, 16);
274 memcpy (mp->local_stop_addr, &p->laddr.stop.ip6, 16);
275 memcpy (mp->remote_start_addr, &p->raddr.start.ip6, 16);
276 memcpy (mp->remote_stop_addr, &p->raddr.stop.ip6, 16);
280 memcpy (mp->local_start_addr, &p->laddr.start.ip4, 4);
281 memcpy (mp->local_stop_addr, &p->laddr.stop.ip4, 4);
282 memcpy (mp->remote_start_addr, &p->raddr.start.ip4, 4);
283 memcpy (mp->remote_stop_addr, &p->raddr.stop.ip4, 4);
285 mp->local_start_port = htons (p->lport.start);
286 mp->local_stop_port = htons (p->lport.stop);
287 mp->remote_start_port = htons (p->rport.start);
288 mp->remote_stop_port = htons (p->rport.stop);
289 mp->protocol = p->protocol;
290 mp->policy = p->policy;
291 mp->sa_id = htonl (p->sa_id);
292 mp->bytes = clib_host_to_net_u64 (p->counter.bytes);
293 mp->packets = clib_host_to_net_u64 (p->counter.packets);
295 vl_msg_api_send_shmem (q, (u8 *) & mp);
299 vl_api_ipsec_spd_dump_t_handler (vl_api_ipsec_spd_dump_t * mp)
301 unix_shared_memory_queue_t *q;
302 ipsec_main_t *im = &ipsec_main;
303 ipsec_policy_t *policy;
308 q = vl_api_client_index_to_input_queue (mp->client_index);
312 p = hash_get (im->spd_index_by_spd_id, ntohl (mp->spd_id));
317 spd = pool_elt_at_index (im->spds, spd_index);
320 pool_foreach (policy, spd->policies,
322 if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == policy->sa_id)
323 send_ipsec_spd_details (policy, q,
328 clib_warning ("unimplemented");
333 vl_api_ipsec_sa_set_key_t_handler (vl_api_ipsec_sa_set_key_t * mp)
335 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
336 vl_api_ipsec_sa_set_key_reply_t *rmp;
340 sa.id = ntohl (mp->sa_id);
341 sa.crypto_key_len = mp->crypto_key_length;
342 clib_memcpy (&sa.crypto_key, mp->crypto_key, sizeof (sa.crypto_key));
343 sa.integ_key_len = mp->integrity_key_length;
344 clib_memcpy (&sa.integ_key, mp->integrity_key, sizeof (sa.integ_key));
346 rv = ipsec_set_sa_key (vm, &sa);
348 rv = VNET_API_ERROR_UNIMPLEMENTED;
351 REPLY_MACRO (VL_API_IPSEC_SA_SET_KEY_REPLY);
355 vl_api_ikev2_profile_add_del_t_handler (vl_api_ikev2_profile_add_del_t * mp)
357 vl_api_ikev2_profile_add_del_reply_t *rmp;
361 vlib_main_t *vm = vlib_get_main ();
363 u8 *tmp = format (0, "%s", mp->name);
364 error = ikev2_add_del_profile (vm, tmp, mp->is_add);
367 rv = VNET_API_ERROR_UNSPECIFIED;
369 rv = VNET_API_ERROR_UNIMPLEMENTED;
372 REPLY_MACRO (VL_API_IKEV2_PROFILE_ADD_DEL_REPLY);
376 vl_api_ikev2_profile_set_auth_t_handler
377 (vl_api_ikev2_profile_set_auth_t * mp)
379 vl_api_ikev2_profile_set_auth_reply_t *rmp;
383 vlib_main_t *vm = vlib_get_main ();
385 u8 *tmp = format (0, "%s", mp->name);
386 u8 *data = vec_new (u8, mp->data_len);
387 clib_memcpy (data, mp->data, mp->data_len);
388 error = ikev2_set_profile_auth (vm, tmp, mp->auth_method, data, mp->is_hex);
392 rv = VNET_API_ERROR_UNSPECIFIED;
394 rv = VNET_API_ERROR_UNIMPLEMENTED;
397 REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_AUTH_REPLY);
401 vl_api_ikev2_profile_set_id_t_handler (vl_api_ikev2_profile_set_id_t * mp)
403 vl_api_ikev2_profile_add_del_reply_t *rmp;
407 vlib_main_t *vm = vlib_get_main ();
409 u8 *tmp = format (0, "%s", mp->name);
410 u8 *data = vec_new (u8, mp->data_len);
411 clib_memcpy (data, mp->data, mp->data_len);
412 error = ikev2_set_profile_id (vm, tmp, mp->id_type, data, mp->is_local);
416 rv = VNET_API_ERROR_UNSPECIFIED;
418 rv = VNET_API_ERROR_UNIMPLEMENTED;
421 REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_ID_REPLY);
425 vl_api_ikev2_profile_set_ts_t_handler (vl_api_ikev2_profile_set_ts_t * mp)
427 vl_api_ikev2_profile_set_ts_reply_t *rmp;
431 vlib_main_t *vm = vlib_get_main ();
433 u8 *tmp = format (0, "%s", mp->name);
434 error = ikev2_set_profile_ts (vm, tmp, mp->proto, mp->start_port,
435 mp->end_port, (ip4_address_t) mp->start_addr,
436 (ip4_address_t) mp->end_addr, mp->is_local);
439 rv = VNET_API_ERROR_UNSPECIFIED;
441 rv = VNET_API_ERROR_UNIMPLEMENTED;
444 REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_TS_REPLY);
448 vl_api_ikev2_set_local_key_t_handler (vl_api_ikev2_set_local_key_t * mp)
450 vl_api_ikev2_profile_set_ts_reply_t *rmp;
454 vlib_main_t *vm = vlib_get_main ();
457 error = ikev2_set_local_key (vm, mp->key_file);
459 rv = VNET_API_ERROR_UNSPECIFIED;
461 rv = VNET_API_ERROR_UNIMPLEMENTED;
464 REPLY_MACRO (VL_API_IKEV2_SET_LOCAL_KEY_REPLY);
468 vl_api_ikev2_set_responder_t_handler (vl_api_ikev2_set_responder_t * mp)
470 vl_api_ikev2_set_responder_reply_t *rmp;
474 vlib_main_t *vm = vlib_get_main ();
477 u8 *tmp = format (0, "%s", mp->name);
479 clib_memcpy (&ip4, mp->address, sizeof (ip4));
481 error = ikev2_set_profile_responder (vm, tmp, mp->sw_if_index, ip4);
484 rv = VNET_API_ERROR_UNSPECIFIED;
486 rv = VNET_API_ERROR_UNIMPLEMENTED;
489 REPLY_MACRO (VL_API_IKEV2_SET_RESPONDER_REPLY);
493 vl_api_ikev2_set_ike_transforms_t_handler (vl_api_ikev2_set_ike_transforms_t *
496 vl_api_ikev2_set_ike_transforms_reply_t *rmp;
500 vlib_main_t *vm = vlib_get_main ();
503 u8 *tmp = format (0, "%s", mp->name);
506 ikev2_set_profile_ike_transforms (vm, tmp, mp->crypto_alg, mp->integ_alg,
507 mp->dh_group, mp->crypto_key_size);
510 rv = VNET_API_ERROR_UNSPECIFIED;
512 rv = VNET_API_ERROR_UNIMPLEMENTED;
515 REPLY_MACRO (VL_API_IKEV2_SET_IKE_TRANSFORMS_REPLY);
519 vl_api_ikev2_set_esp_transforms_t_handler (vl_api_ikev2_set_esp_transforms_t *
522 vl_api_ikev2_set_esp_transforms_reply_t *rmp;
526 vlib_main_t *vm = vlib_get_main ();
529 u8 *tmp = format (0, "%s", mp->name);
532 ikev2_set_profile_esp_transforms (vm, tmp, mp->crypto_alg, mp->integ_alg,
533 mp->dh_group, mp->crypto_key_size);
536 rv = VNET_API_ERROR_UNSPECIFIED;
538 rv = VNET_API_ERROR_UNIMPLEMENTED;
541 REPLY_MACRO (VL_API_IKEV2_SET_ESP_TRANSFORMS_REPLY);
545 vl_api_ikev2_set_sa_lifetime_t_handler (vl_api_ikev2_set_sa_lifetime_t * mp)
547 vl_api_ikev2_set_sa_lifetime_reply_t *rmp;
551 vlib_main_t *vm = vlib_get_main ();
554 u8 *tmp = format (0, "%s", mp->name);
557 ikev2_set_profile_sa_lifetime (vm, tmp, mp->lifetime, mp->lifetime_jitter,
558 mp->handover, mp->lifetime_maxdata);
561 rv = VNET_API_ERROR_UNSPECIFIED;
563 rv = VNET_API_ERROR_UNIMPLEMENTED;
566 REPLY_MACRO (VL_API_IKEV2_SET_SA_LIFETIME_REPLY);
570 vl_api_ikev2_initiate_sa_init_t_handler (vl_api_ikev2_initiate_sa_init_t * mp)
572 vl_api_ikev2_initiate_sa_init_reply_t *rmp;
576 vlib_main_t *vm = vlib_get_main ();
579 u8 *tmp = format (0, "%s", mp->name);
581 error = ikev2_initiate_sa_init (vm, tmp);
584 rv = VNET_API_ERROR_UNSPECIFIED;
586 rv = VNET_API_ERROR_UNIMPLEMENTED;
589 REPLY_MACRO (VL_API_IKEV2_INITIATE_SA_INIT_REPLY);
593 vl_api_ikev2_initiate_del_ike_sa_t_handler (vl_api_ikev2_initiate_del_ike_sa_t
596 vl_api_ikev2_initiate_del_ike_sa_reply_t *rmp;
600 vlib_main_t *vm = vlib_get_main ();
603 error = ikev2_initiate_delete_ike_sa (vm, mp->ispi);
605 rv = VNET_API_ERROR_UNSPECIFIED;
607 rv = VNET_API_ERROR_UNIMPLEMENTED;
610 REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_IKE_SA_REPLY);
614 vl_api_ikev2_initiate_del_child_sa_t_handler
615 (vl_api_ikev2_initiate_del_child_sa_t * mp)
617 vl_api_ikev2_initiate_del_child_sa_reply_t *rmp;
621 vlib_main_t *vm = vlib_get_main ();
624 error = ikev2_initiate_delete_child_sa (vm, mp->ispi);
626 rv = VNET_API_ERROR_UNSPECIFIED;
628 rv = VNET_API_ERROR_UNIMPLEMENTED;
631 REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_CHILD_SA_REPLY);
635 vl_api_ikev2_initiate_rekey_child_sa_t_handler
636 (vl_api_ikev2_initiate_rekey_child_sa_t * mp)
638 vl_api_ikev2_initiate_rekey_child_sa_reply_t *rmp;
642 vlib_main_t *vm = vlib_get_main ();
645 error = ikev2_initiate_rekey_child_sa (vm, mp->ispi);
647 rv = VNET_API_ERROR_UNSPECIFIED;
649 rv = VNET_API_ERROR_UNIMPLEMENTED;
652 REPLY_MACRO (VL_API_IKEV2_INITIATE_REKEY_CHILD_SA_REPLY);
657 * Add vpe's API message handlers to the table.
658 * vlib has alread mapped shared memory and
659 * added the client registration handlers.
660 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
662 #define vl_msg_name_crc_list
663 #include <vnet/vnet_all_api_h.h>
664 #undef vl_msg_name_crc_list
667 setup_message_id_table (api_main_t * am)
669 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
670 foreach_vl_msg_name_crc_ipsec;
674 static clib_error_t *
675 ipsec_api_hookup (vlib_main_t * vm)
677 api_main_t *am = &api_main;
680 vl_msg_api_set_handlers(VL_API_##N, #n, \
681 vl_api_##n##_t_handler, \
683 vl_api_##n##_t_endian, \
684 vl_api_##n##_t_print, \
685 sizeof(vl_api_##n##_t), 1);
690 * Set up the (msg_name, crc, message-id) table
692 setup_message_id_table (am);
697 VLIB_API_INIT_FUNCTION (ipsec_api_hookup);
700 * fd.io coding-style-patch-verification: ON
703 * eval: (c-set-style "gnu")