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)
63 static void vl_api_ipsec_spd_add_del_t_handler
64 (vl_api_ipsec_spd_add_del_t * mp)
67 clib_warning ("unimplemented");
70 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
71 vl_api_ipsec_spd_add_del_reply_t *rmp;
75 rv = ipsec_add_del_spd (vm, ntohl (mp->spd_id), mp->is_add);
77 rv = VNET_API_ERROR_UNIMPLEMENTED;
80 REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_REPLY);
84 static void vl_api_ipsec_interface_add_del_spd_t_handler
85 (vl_api_ipsec_interface_add_del_spd_t * mp)
87 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
88 vl_api_ipsec_interface_add_del_spd_reply_t *rmp;
90 u32 sw_if_index __attribute__ ((unused));
91 u32 spd_id __attribute__ ((unused));
93 sw_if_index = ntohl (mp->sw_if_index);
94 spd_id = ntohl (mp->spd_id);
96 VALIDATE_SW_IF_INDEX (mp);
99 rv = ipsec_set_interface_spd (vm, sw_if_index, spd_id, mp->is_add);
101 rv = VNET_API_ERROR_UNIMPLEMENTED;
104 BAD_SW_IF_INDEX_LABEL;
106 REPLY_MACRO (VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY);
109 static void vl_api_ipsec_spd_add_del_entry_t_handler
110 (vl_api_ipsec_spd_add_del_entry_t * mp)
112 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
113 vl_api_ipsec_spd_add_del_entry_reply_t *rmp;
119 memset (&p, 0, sizeof (p));
121 p.id = ntohl (mp->spd_id);
122 p.priority = ntohl (mp->priority);
123 p.is_outbound = mp->is_outbound;
124 p.is_ipv6 = mp->is_ipv6;
126 if (mp->is_ipv6 || mp->is_ip_any)
128 clib_memcpy (&p.raddr.start, mp->remote_address_start, 16);
129 clib_memcpy (&p.raddr.stop, mp->remote_address_stop, 16);
130 clib_memcpy (&p.laddr.start, mp->local_address_start, 16);
131 clib_memcpy (&p.laddr.stop, mp->local_address_stop, 16);
135 clib_memcpy (&p.raddr.start.ip4.data, mp->remote_address_start, 4);
136 clib_memcpy (&p.raddr.stop.ip4.data, mp->remote_address_stop, 4);
137 clib_memcpy (&p.laddr.start.ip4.data, mp->local_address_start, 4);
138 clib_memcpy (&p.laddr.stop.ip4.data, mp->local_address_stop, 4);
140 p.protocol = mp->protocol;
141 p.rport.start = ntohs (mp->remote_port_start);
142 p.rport.stop = ntohs (mp->remote_port_stop);
143 p.lport.start = ntohs (mp->local_port_start);
144 p.lport.stop = ntohs (mp->local_port_stop);
145 /* policy action resolve unsupported */
146 if (mp->policy == IPSEC_POLICY_ACTION_RESOLVE)
148 clib_warning ("unsupported action: 'resolve'");
149 rv = VNET_API_ERROR_UNIMPLEMENTED;
152 p.policy = mp->policy;
153 p.sa_id = ntohl (mp->sa_id);
155 rv = ipsec_add_del_policy (vm, &p, mp->is_add);
162 rv = ipsec_add_del_policy (vm, &p, mp->is_add);
165 rv = VNET_API_ERROR_UNIMPLEMENTED;
170 REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_ENTRY_REPLY);
173 static void vl_api_ipsec_sad_add_del_entry_t_handler
174 (vl_api_ipsec_sad_add_del_entry_t * mp)
176 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
177 vl_api_ipsec_sad_add_del_entry_reply_t *rmp;
180 ipsec_main_t *im = &ipsec_main;
183 memset (&sa, 0, sizeof (sa));
185 sa.id = ntohl (mp->sad_id);
186 sa.spi = ntohl (mp->spi);
187 /* security protocol AH unsupported */
188 if (mp->protocol == IPSEC_PROTOCOL_AH)
190 clib_warning ("unsupported security protocol 'AH'");
191 rv = VNET_API_ERROR_UNIMPLEMENTED;
194 sa.protocol = mp->protocol;
195 /* check for unsupported crypto-alg */
196 if (mp->crypto_algorithm < IPSEC_CRYPTO_ALG_AES_CBC_128 ||
197 mp->crypto_algorithm >= IPSEC_CRYPTO_N_ALG)
199 clib_warning ("unsupported crypto-alg: '%U'", format_ipsec_crypto_alg,
200 mp->crypto_algorithm);
201 rv = VNET_API_ERROR_UNIMPLEMENTED;
204 sa.crypto_alg = mp->crypto_algorithm;
205 sa.crypto_key_len = mp->crypto_key_length;
206 clib_memcpy (&sa.crypto_key, mp->crypto_key, sizeof (sa.crypto_key));
207 /* check for unsupported integ-alg */
208 if (mp->integrity_algorithm < IPSEC_INTEG_ALG_NONE ||
209 mp->integrity_algorithm >= IPSEC_INTEG_N_ALG)
211 clib_warning ("unsupported integ-alg: '%U'", format_ipsec_integ_alg,
212 mp->integrity_algorithm);
213 rv = VNET_API_ERROR_UNIMPLEMENTED;
217 sa.integ_alg = mp->integrity_algorithm;
218 sa.integ_key_len = mp->integrity_key_length;
219 clib_memcpy (&sa.integ_key, mp->integrity_key, sizeof (sa.integ_key));
220 sa.use_esn = mp->use_extended_sequence_number;
221 sa.is_tunnel = mp->is_tunnel;
222 sa.is_tunnel_ip6 = mp->is_tunnel_ipv6;
223 if (sa.is_tunnel_ip6)
225 clib_memcpy (&sa.tunnel_src_addr, mp->tunnel_src_address, 16);
226 clib_memcpy (&sa.tunnel_dst_addr, mp->tunnel_dst_address, 16);
230 clib_memcpy (&sa.tunnel_src_addr.ip4.data, mp->tunnel_src_address, 4);
231 clib_memcpy (&sa.tunnel_dst_addr.ip4.data, mp->tunnel_dst_address, 4);
234 ASSERT (im->cb.check_support_cb);
235 clib_error_t *err = im->cb.check_support_cb (&sa);
238 clib_warning ("%s", err->what);
239 rv = VNET_API_ERROR_UNIMPLEMENTED;
243 rv = ipsec_add_del_sa (vm, &sa, mp->is_add);
245 rv = VNET_API_ERROR_UNIMPLEMENTED;
250 REPLY_MACRO (VL_API_IPSEC_SAD_ADD_DEL_ENTRY_REPLY);
254 send_ipsec_spd_details (ipsec_policy_t * p, unix_shared_memory_queue_t * q,
257 vl_api_ipsec_spd_details_t *mp;
259 mp = vl_msg_api_alloc (sizeof (*mp));
260 memset (mp, 0, sizeof (*mp));
261 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_DETAILS);
262 mp->context = context;
264 mp->spd_id = htonl (p->id);
265 mp->priority = htonl (p->priority);
266 mp->is_outbound = p->is_outbound;
267 mp->is_ipv6 = p->is_ipv6;
270 memcpy (mp->local_start_addr, &p->laddr.start.ip6, 16);
271 memcpy (mp->local_stop_addr, &p->laddr.stop.ip6, 16);
272 memcpy (mp->remote_start_addr, &p->raddr.start.ip6, 16);
273 memcpy (mp->remote_stop_addr, &p->raddr.stop.ip6, 16);
277 memcpy (mp->local_start_addr, &p->laddr.start.ip4, 4);
278 memcpy (mp->local_stop_addr, &p->laddr.stop.ip4, 4);
279 memcpy (mp->remote_start_addr, &p->raddr.start.ip4, 4);
280 memcpy (mp->remote_stop_addr, &p->raddr.stop.ip4, 4);
282 mp->local_start_port = htons (p->lport.start);
283 mp->local_stop_port = htons (p->lport.stop);
284 mp->remote_start_port = htons (p->rport.start);
285 mp->remote_stop_port = htons (p->rport.stop);
286 mp->protocol = p->protocol;
287 mp->policy = p->policy;
288 mp->sa_id = htonl (p->sa_id);
289 mp->bytes = clib_host_to_net_u64 (p->counter.bytes);
290 mp->packets = clib_host_to_net_u64 (p->counter.packets);
292 vl_msg_api_send_shmem (q, (u8 *) & mp);
296 vl_api_ipsec_spd_dump_t_handler (vl_api_ipsec_spd_dump_t * mp)
298 unix_shared_memory_queue_t *q;
299 ipsec_main_t *im = &ipsec_main;
300 ipsec_policy_t *policy;
305 q = vl_api_client_index_to_input_queue (mp->client_index);
309 p = hash_get (im->spd_index_by_spd_id, ntohl (mp->spd_id));
314 spd = pool_elt_at_index (im->spds, spd_index);
317 pool_foreach (policy, spd->policies,
319 if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == policy->sa_id)
320 send_ipsec_spd_details (policy, q,
325 clib_warning ("unimplemented");
330 vl_api_ipsec_sa_set_key_t_handler (vl_api_ipsec_sa_set_key_t * mp)
332 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
333 vl_api_ipsec_sa_set_key_reply_t *rmp;
337 sa.id = ntohl (mp->sa_id);
338 sa.crypto_key_len = mp->crypto_key_length;
339 clib_memcpy (&sa.crypto_key, mp->crypto_key, sizeof (sa.crypto_key));
340 sa.integ_key_len = mp->integrity_key_length;
341 clib_memcpy (&sa.integ_key, mp->integrity_key, sizeof (sa.integ_key));
343 rv = ipsec_set_sa_key (vm, &sa);
345 rv = VNET_API_ERROR_UNIMPLEMENTED;
348 REPLY_MACRO (VL_API_IPSEC_SA_SET_KEY_REPLY);
352 vl_api_ikev2_profile_add_del_t_handler (vl_api_ikev2_profile_add_del_t * mp)
354 vl_api_ikev2_profile_add_del_reply_t *rmp;
358 vlib_main_t *vm = vlib_get_main ();
360 u8 *tmp = format (0, "%s", mp->name);
361 error = ikev2_add_del_profile (vm, tmp, mp->is_add);
364 rv = VNET_API_ERROR_UNSPECIFIED;
366 rv = VNET_API_ERROR_UNIMPLEMENTED;
369 REPLY_MACRO (VL_API_IKEV2_PROFILE_ADD_DEL_REPLY);
373 vl_api_ikev2_profile_set_auth_t_handler
374 (vl_api_ikev2_profile_set_auth_t * mp)
376 vl_api_ikev2_profile_set_auth_reply_t *rmp;
380 vlib_main_t *vm = vlib_get_main ();
382 u8 *tmp = format (0, "%s", mp->name);
383 u8 *data = vec_new (u8, mp->data_len);
384 clib_memcpy (data, mp->data, mp->data_len);
385 error = ikev2_set_profile_auth (vm, tmp, mp->auth_method, data, mp->is_hex);
389 rv = VNET_API_ERROR_UNSPECIFIED;
391 rv = VNET_API_ERROR_UNIMPLEMENTED;
394 REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_AUTH_REPLY);
398 vl_api_ikev2_profile_set_id_t_handler (vl_api_ikev2_profile_set_id_t * mp)
400 vl_api_ikev2_profile_add_del_reply_t *rmp;
404 vlib_main_t *vm = vlib_get_main ();
406 u8 *tmp = format (0, "%s", mp->name);
407 u8 *data = vec_new (u8, mp->data_len);
408 clib_memcpy (data, mp->data, mp->data_len);
409 error = ikev2_set_profile_id (vm, tmp, mp->id_type, data, mp->is_local);
413 rv = VNET_API_ERROR_UNSPECIFIED;
415 rv = VNET_API_ERROR_UNIMPLEMENTED;
418 REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_ID_REPLY);
422 vl_api_ikev2_profile_set_ts_t_handler (vl_api_ikev2_profile_set_ts_t * mp)
424 vl_api_ikev2_profile_set_ts_reply_t *rmp;
428 vlib_main_t *vm = vlib_get_main ();
430 u8 *tmp = format (0, "%s", mp->name);
431 error = ikev2_set_profile_ts (vm, tmp, mp->proto, mp->start_port,
432 mp->end_port, (ip4_address_t) mp->start_addr,
433 (ip4_address_t) mp->end_addr, mp->is_local);
436 rv = VNET_API_ERROR_UNSPECIFIED;
438 rv = VNET_API_ERROR_UNIMPLEMENTED;
441 REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_TS_REPLY);
445 vl_api_ikev2_set_local_key_t_handler (vl_api_ikev2_set_local_key_t * mp)
447 vl_api_ikev2_profile_set_ts_reply_t *rmp;
451 vlib_main_t *vm = vlib_get_main ();
454 error = ikev2_set_local_key (vm, mp->key_file);
456 rv = VNET_API_ERROR_UNSPECIFIED;
458 rv = VNET_API_ERROR_UNIMPLEMENTED;
461 REPLY_MACRO (VL_API_IKEV2_SET_LOCAL_KEY_REPLY);
466 * Add vpe's API message handlers to the table.
467 * vlib has alread mapped shared memory and
468 * added the client registration handlers.
469 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
471 #define vl_msg_name_crc_list
472 #include <vnet/vnet_all_api_h.h>
473 #undef vl_msg_name_crc_list
476 setup_message_id_table (api_main_t * am)
478 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
479 foreach_vl_msg_name_crc_ipsec;
483 static clib_error_t *
484 ipsec_api_hookup (vlib_main_t * vm)
486 api_main_t *am = &api_main;
489 vl_msg_api_set_handlers(VL_API_##N, #n, \
490 vl_api_##n##_t_handler, \
492 vl_api_##n##_t_endian, \
493 vl_api_##n##_t_print, \
494 sizeof(vl_api_##n##_t), 1);
499 * Set up the (msg_name, crc, message-id) table
501 setup_message_id_table (am);
506 VLIB_API_INIT_FUNCTION (ipsec_api_hookup);
509 * fd.io coding-style-patch-verification: ON
512 * eval: (c-set-style "gnu")