blob: 30cc5bd24217823bb56b4e09aab9e71cb99f826a [file] [log] [blame]
Pavel Kotucek9c7ef032016-12-21 07:46:45 +01001/*
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
23#include <vnet/interface.h>
24#include <vnet/api_errno.h>
25#include <vnet/ip/ip.h>
26
27#include <vnet/vnet_msg_enum.h>
28
29#if IPSEC > 0
30#include <vnet/ipsec/ipsec.h>
31#include <vnet/ipsec/ikev2.h>
32#endif /* IPSEC */
33
34#define vl_typedefs /* define message structures */
35#include <vnet/vnet_all_api_h.h>
36#undef vl_typedefs
37
38#define vl_endianfun /* define message structures */
39#include <vnet/vnet_all_api_h.h>
40#undef vl_endianfun
41
42/* instantiate all the print functions we know about */
43#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
44#define vl_printfun
45#include <vnet/vnet_all_api_h.h>
46#undef vl_printfun
47
48#include <vlibapi/api_helper_macros.h>
49
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
63static void vl_api_ipsec_spd_add_del_t_handler
64 (vl_api_ipsec_spd_add_del_t * mp)
65{
66#if IPSEC == 0
67 clib_warning ("unimplemented");
68#else
69
70 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
71 vl_api_ipsec_spd_add_del_reply_t *rmp;
72 int rv;
73
74#if DPDK > 0
75 rv = ipsec_add_del_spd (vm, ntohl (mp->spd_id), mp->is_add);
76#else
77 rv = VNET_API_ERROR_UNIMPLEMENTED;
78#endif
79
80 REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_REPLY);
81#endif
82}
83
84static void vl_api_ipsec_interface_add_del_spd_t_handler
85 (vl_api_ipsec_interface_add_del_spd_t * mp)
86{
87 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
88 vl_api_ipsec_interface_add_del_spd_reply_t *rmp;
89 int rv;
90 u32 sw_if_index __attribute__ ((unused));
91 u32 spd_id __attribute__ ((unused));
92
93 sw_if_index = ntohl (mp->sw_if_index);
94 spd_id = ntohl (mp->spd_id);
95
96 VALIDATE_SW_IF_INDEX (mp);
97
98#if IPSEC > 0
99 rv = ipsec_set_interface_spd (vm, sw_if_index, spd_id, mp->is_add);
100#else
101 rv = VNET_API_ERROR_UNIMPLEMENTED;
102#endif
103
104 BAD_SW_IF_INDEX_LABEL;
105
106 REPLY_MACRO (VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY);
107}
108
109static void vl_api_ipsec_spd_add_del_entry_t_handler
110 (vl_api_ipsec_spd_add_del_entry_t * mp)
111{
112 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
113 vl_api_ipsec_spd_add_del_entry_reply_t *rmp;
114 int rv;
115
116#if IPSEC > 0
117 ipsec_policy_t p;
118
119 memset (&p, 0, sizeof (p));
120
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;
125
126 if (mp->is_ipv6 || mp->is_ip_any)
127 {
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);
132 }
133 else
134 {
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);
139 }
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)
147 {
148 clib_warning ("unsupported action: 'resolve'");
149 rv = VNET_API_ERROR_UNIMPLEMENTED;
150 goto out;
151 }
152 p.policy = mp->policy;
153 p.sa_id = ntohl (mp->sa_id);
154
155 rv = ipsec_add_del_policy (vm, &p, mp->is_add);
156 if (rv)
157 goto out;
158
159 if (mp->is_ip_any)
160 {
161 p.is_ipv6 = 1;
162 rv = ipsec_add_del_policy (vm, &p, mp->is_add);
163 }
164#else
165 rv = VNET_API_ERROR_UNIMPLEMENTED;
166 goto out;
167#endif
168
169out:
170 REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_ENTRY_REPLY);
171}
172
173static void vl_api_ipsec_sad_add_del_entry_t_handler
174 (vl_api_ipsec_sad_add_del_entry_t * mp)
175{
176 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
177 vl_api_ipsec_sad_add_del_entry_reply_t *rmp;
178 int rv;
179#if IPSEC > 0
180 ipsec_sa_t sa;
181
182 memset (&sa, 0, sizeof (sa));
183
184 sa.id = ntohl (mp->sad_id);
185 sa.spi = ntohl (mp->spi);
186 /* security protocol AH unsupported */
187 if (mp->protocol == IPSEC_PROTOCOL_AH)
188 {
189 clib_warning ("unsupported security protocol 'AH'");
190 rv = VNET_API_ERROR_UNIMPLEMENTED;
191 goto out;
192 }
193 sa.protocol = mp->protocol;
194 /* check for unsupported crypto-alg */
195 if (mp->crypto_algorithm < IPSEC_CRYPTO_ALG_AES_CBC_128 ||
196 mp->crypto_algorithm >= IPSEC_CRYPTO_N_ALG)
197 {
198 clib_warning ("unsupported crypto-alg: '%U'", format_ipsec_crypto_alg,
199 mp->crypto_algorithm);
200 rv = VNET_API_ERROR_UNIMPLEMENTED;
201 goto out;
202 }
203 sa.crypto_alg = mp->crypto_algorithm;
204 sa.crypto_key_len = mp->crypto_key_length;
205 clib_memcpy (&sa.crypto_key, mp->crypto_key, sizeof (sa.crypto_key));
206 /* check for unsupported integ-alg */
207#if DPDK_CRYPTO==1
208 if (mp->integrity_algorithm < IPSEC_INTEG_ALG_NONE ||
209#else
210 if (mp->integrity_algorithm < IPSEC_INTEG_ALG_SHA1_96 ||
211#endif
212 mp->integrity_algorithm >= IPSEC_INTEG_N_ALG)
213 {
214 clib_warning ("unsupported integ-alg: '%U'", format_ipsec_integ_alg,
215 mp->integrity_algorithm);
216 rv = VNET_API_ERROR_UNIMPLEMENTED;
217 goto out;
218 }
219
220#if DPDK_CRYPTO==1
221 /*Special cases, aes-gcm-128 encryption */
222 if (mp->crypto_algorithm == IPSEC_CRYPTO_ALG_AES_GCM_128)
223 {
224 if (mp->integrity_algorithm != IPSEC_INTEG_ALG_NONE
225 && mp->integrity_algorithm != IPSEC_INTEG_ALG_AES_GCM_128)
226 {
227 clib_warning
228 ("unsupported: aes-gcm-128 crypto-alg needs none as integ-alg");
229 rv = VNET_API_ERROR_UNIMPLEMENTED;
230 goto out;
231 }
232 else /*set integ-alg internally to aes-gcm-128 */
233 mp->integrity_algorithm = IPSEC_INTEG_ALG_AES_GCM_128;
234 }
235 else if (mp->integrity_algorithm == IPSEC_INTEG_ALG_AES_GCM_128)
236 {
237 clib_warning ("unsupported integ-alg: aes-gcm-128");
238 rv = VNET_API_ERROR_UNIMPLEMENTED;
239 goto out;
240 }
241 else if (mp->integrity_algorithm == IPSEC_INTEG_ALG_NONE)
242 {
243 clib_warning ("unsupported integ-alg: none");
244 rv = VNET_API_ERROR_UNIMPLEMENTED;
245 goto out;
246 }
247#endif
248
249 sa.integ_alg = mp->integrity_algorithm;
250 sa.integ_key_len = mp->integrity_key_length;
251 clib_memcpy (&sa.integ_key, mp->integrity_key, sizeof (sa.integ_key));
252 sa.use_esn = mp->use_extended_sequence_number;
253 sa.is_tunnel = mp->is_tunnel;
254 sa.is_tunnel_ip6 = mp->is_tunnel_ipv6;
255 if (sa.is_tunnel_ip6)
256 {
257 clib_memcpy (&sa.tunnel_src_addr, mp->tunnel_src_address, 16);
258 clib_memcpy (&sa.tunnel_dst_addr, mp->tunnel_dst_address, 16);
259 }
260 else
261 {
262 clib_memcpy (&sa.tunnel_src_addr.ip4.data, mp->tunnel_src_address, 4);
263 clib_memcpy (&sa.tunnel_dst_addr.ip4.data, mp->tunnel_dst_address, 4);
264 }
265
266 rv = ipsec_add_del_sa (vm, &sa, mp->is_add);
267#else
268 rv = VNET_API_ERROR_UNIMPLEMENTED;
269 goto out;
270#endif
271
272out:
273 REPLY_MACRO (VL_API_IPSEC_SAD_ADD_DEL_ENTRY_REPLY);
274}
275
276static void
277send_ipsec_spd_details (ipsec_policy_t * p, unix_shared_memory_queue_t * q,
278 u32 context)
279{
280 vl_api_ipsec_spd_details_t *mp;
281
282 mp = vl_msg_api_alloc (sizeof (*mp));
283 memset (mp, 0, sizeof (*mp));
284 mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_DETAILS);
285 mp->context = context;
286
287 mp->spd_id = htonl (p->id);
288 mp->priority = htonl (p->priority);
289 mp->is_outbound = p->is_outbound;
290 mp->is_ipv6 = p->is_ipv6;
291 if (p->is_ipv6)
292 {
293 memcpy (mp->local_start_addr, &p->laddr.start.ip6, 16);
294 memcpy (mp->local_stop_addr, &p->laddr.stop.ip6, 16);
295 memcpy (mp->remote_start_addr, &p->raddr.start.ip6, 16);
296 memcpy (mp->remote_stop_addr, &p->raddr.stop.ip6, 16);
297 }
298 else
299 {
300 memcpy (mp->local_start_addr, &p->laddr.start.ip4, 4);
301 memcpy (mp->local_stop_addr, &p->laddr.stop.ip4, 4);
302 memcpy (mp->remote_start_addr, &p->raddr.start.ip4, 4);
303 memcpy (mp->remote_stop_addr, &p->raddr.stop.ip4, 4);
304 }
305 mp->local_start_port = htons (p->lport.start);
306 mp->local_stop_port = htons (p->lport.stop);
307 mp->remote_start_port = htons (p->rport.start);
308 mp->remote_stop_port = htons (p->rport.stop);
309 mp->protocol = p->protocol;
310 mp->policy = p->policy;
311 mp->sa_id = htonl (p->sa_id);
312 mp->bytes = clib_host_to_net_u64 (p->counter.bytes);
313 mp->packets = clib_host_to_net_u64 (p->counter.packets);
314
315 vl_msg_api_send_shmem (q, (u8 *) & mp);
316}
317
318static void
319vl_api_ipsec_spd_dump_t_handler (vl_api_ipsec_spd_dump_t * mp)
320{
321 unix_shared_memory_queue_t *q;
322 ipsec_main_t *im = &ipsec_main;
323 ipsec_policy_t *policy;
324 ipsec_spd_t *spd;
325 uword *p;
326 u32 spd_index;
327#if IPSEC > 0
328 q = vl_api_client_index_to_input_queue (mp->client_index);
329 if (q == 0)
330 return;
331
332 p = hash_get (im->spd_index_by_spd_id, ntohl (mp->spd_id));
333 if (!p)
334 return;
335
336 spd_index = p[0];
337 spd = pool_elt_at_index (im->spds, spd_index);
338
339 /* *INDENT-OFF* */
340 pool_foreach (policy, spd->policies,
341 ({
342 if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == policy->sa_id)
343 send_ipsec_spd_details (policy, q,
344 mp->context);}
345 ));
346 /* *INDENT-ON* */
347#else
348 clib_warning ("unimplemented");
349#endif
350}
351
352static void
353vl_api_ipsec_sa_set_key_t_handler (vl_api_ipsec_sa_set_key_t * mp)
354{
355 vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
356 vl_api_ipsec_sa_set_key_reply_t *rmp;
357 int rv;
358#if IPSEC > 0
359 ipsec_sa_t sa;
360 sa.id = ntohl (mp->sa_id);
361 sa.crypto_key_len = mp->crypto_key_length;
362 clib_memcpy (&sa.crypto_key, mp->crypto_key, sizeof (sa.crypto_key));
363 sa.integ_key_len = mp->integrity_key_length;
364 clib_memcpy (&sa.integ_key, mp->integrity_key, sizeof (sa.integ_key));
365
366 rv = ipsec_set_sa_key (vm, &sa);
367#else
368 rv = VNET_API_ERROR_UNIMPLEMENTED;
369#endif
370
371 REPLY_MACRO (VL_API_IPSEC_SA_SET_KEY_REPLY);
372}
373
374static void
375vl_api_ikev2_profile_add_del_t_handler (vl_api_ikev2_profile_add_del_t * mp)
376{
377 vl_api_ikev2_profile_add_del_reply_t *rmp;
378 int rv = 0;
379
380#if IPSEC > 0
381 vlib_main_t *vm = vlib_get_main ();
382 clib_error_t *error;
383 u8 *tmp = format (0, "%s", mp->name);
384 error = ikev2_add_del_profile (vm, tmp, mp->is_add);
385 vec_free (tmp);
386 if (error)
387 rv = VNET_API_ERROR_UNSPECIFIED;
388#else
389 rv = VNET_API_ERROR_UNIMPLEMENTED;
390#endif
391
392 REPLY_MACRO (VL_API_IKEV2_PROFILE_ADD_DEL_REPLY);
393}
394
395static void
396 vl_api_ikev2_profile_set_auth_t_handler
397 (vl_api_ikev2_profile_set_auth_t * mp)
398{
399 vl_api_ikev2_profile_set_auth_reply_t *rmp;
400 int rv = 0;
401
402#if IPSEC > 0
403 vlib_main_t *vm = vlib_get_main ();
404 clib_error_t *error;
405 u8 *tmp = format (0, "%s", mp->name);
406 u8 *data = vec_new (u8, mp->data_len);
407 clib_memcpy (data, mp->data, mp->data_len);
408 error = ikev2_set_profile_auth (vm, tmp, mp->auth_method, data, mp->is_hex);
409 vec_free (tmp);
410 vec_free (data);
411 if (error)
412 rv = VNET_API_ERROR_UNSPECIFIED;
413#else
414 rv = VNET_API_ERROR_UNIMPLEMENTED;
415#endif
416
417 REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_AUTH_REPLY);
418}
419
420static void
421vl_api_ikev2_profile_set_id_t_handler (vl_api_ikev2_profile_set_id_t * mp)
422{
423 vl_api_ikev2_profile_add_del_reply_t *rmp;
424 int rv = 0;
425
426#if IPSEC > 0
427 vlib_main_t *vm = vlib_get_main ();
428 clib_error_t *error;
429 u8 *tmp = format (0, "%s", mp->name);
430 u8 *data = vec_new (u8, mp->data_len);
431 clib_memcpy (data, mp->data, mp->data_len);
432 error = ikev2_set_profile_id (vm, tmp, mp->id_type, data, mp->is_local);
433 vec_free (tmp);
434 vec_free (data);
435 if (error)
436 rv = VNET_API_ERROR_UNSPECIFIED;
437#else
438 rv = VNET_API_ERROR_UNIMPLEMENTED;
439#endif
440
441 REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_ID_REPLY);
442}
443
444static void
445vl_api_ikev2_profile_set_ts_t_handler (vl_api_ikev2_profile_set_ts_t * mp)
446{
447 vl_api_ikev2_profile_set_ts_reply_t *rmp;
448 int rv = 0;
449
450#if IPSEC > 0
451 vlib_main_t *vm = vlib_get_main ();
452 clib_error_t *error;
453 u8 *tmp = format (0, "%s", mp->name);
454 error = ikev2_set_profile_ts (vm, tmp, mp->proto, mp->start_port,
455 mp->end_port, (ip4_address_t) mp->start_addr,
456 (ip4_address_t) mp->end_addr, mp->is_local);
457 vec_free (tmp);
458 if (error)
459 rv = VNET_API_ERROR_UNSPECIFIED;
460#else
461 rv = VNET_API_ERROR_UNIMPLEMENTED;
462#endif
463
464 REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_TS_REPLY);
465}
466
467static void
468vl_api_ikev2_set_local_key_t_handler (vl_api_ikev2_set_local_key_t * mp)
469{
470 vl_api_ikev2_profile_set_ts_reply_t *rmp;
471 int rv = 0;
472
473#if IPSEC > 0
474 vlib_main_t *vm = vlib_get_main ();
475 clib_error_t *error;
476
477 error = ikev2_set_local_key (vm, mp->key_file);
478 if (error)
479 rv = VNET_API_ERROR_UNSPECIFIED;
480#else
481 rv = VNET_API_ERROR_UNIMPLEMENTED;
482#endif
483
484 REPLY_MACRO (VL_API_IKEV2_SET_LOCAL_KEY_REPLY);
485}
486
487/*
488 * ipsec_api_hookup
489 * Add vpe's API message handlers to the table.
490 * vlib has alread mapped shared memory and
491 * added the client registration handlers.
492 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
493 */
494#define vl_msg_name_crc_list
495#include <vnet/vnet_all_api_h.h>
496#undef vl_msg_name_crc_list
497
498static void
499setup_message_id_table (api_main_t * am)
500{
501#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
502 foreach_vl_msg_name_crc_ipsec;
503#undef _
504}
505
506static clib_error_t *
507ipsec_api_hookup (vlib_main_t * vm)
508{
509 api_main_t *am = &api_main;
510
511#define _(N,n) \
512 vl_msg_api_set_handlers(VL_API_##N, #n, \
513 vl_api_##n##_t_handler, \
514 vl_noop_handler, \
515 vl_api_##n##_t_endian, \
516 vl_api_##n##_t_print, \
517 sizeof(vl_api_##n##_t), 1);
518 foreach_vpe_api_msg;
519#undef _
520
521 /*
522 * Set up the (msg_name, crc, message-id) table
523 */
524 setup_message_id_table (am);
525
526 return 0;
527}
528
529VLIB_API_INIT_FUNCTION (ipsec_api_hookup);
530
531/*
532 * fd.io coding-style-patch-verification: ON
533 *
534 * Local Variables:
535 * eval: (c-set-style "gnu")
536 * End:
537 */