Allow IPsec interface to have SAs reset
[vpp.git] / src / vnet / ipsec / ipsec_api.c
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
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 WITH_LIBSSL > 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_SA_DUMP, ipsec_sa_dump)                                         \
57 _(IPSEC_SPD_DUMP, ipsec_spd_dump)                                       \
58 _(IPSEC_TUNNEL_IF_ADD_DEL, ipsec_tunnel_if_add_del)                     \
59 _(IPSEC_TUNNEL_IF_SET_KEY, ipsec_tunnel_if_set_key)                     \
60 _(IPSEC_TUNNEL_IF_SET_SA, ipsec_tunnel_if_set_sa)                       \
61 _(IKEV2_PROFILE_ADD_DEL, ikev2_profile_add_del)                         \
62 _(IKEV2_PROFILE_SET_AUTH, ikev2_profile_set_auth)                       \
63 _(IKEV2_PROFILE_SET_ID, ikev2_profile_set_id)                           \
64 _(IKEV2_PROFILE_SET_TS, ikev2_profile_set_ts)                           \
65 _(IKEV2_SET_LOCAL_KEY, ikev2_set_local_key)                             \
66 _(IKEV2_SET_RESPONDER, ikev2_set_responder)                             \
67 _(IKEV2_SET_IKE_TRANSFORMS, ikev2_set_ike_transforms)                   \
68 _(IKEV2_SET_ESP_TRANSFORMS, ikev2_set_esp_transforms)                   \
69 _(IKEV2_SET_SA_LIFETIME, ikev2_set_sa_lifetime)                         \
70 _(IKEV2_INITIATE_SA_INIT, ikev2_initiate_sa_init)                       \
71 _(IKEV2_INITIATE_DEL_IKE_SA, ikev2_initiate_del_ike_sa)                 \
72 _(IKEV2_INITIATE_DEL_CHILD_SA, ikev2_initiate_del_child_sa)             \
73 _(IKEV2_INITIATE_REKEY_CHILD_SA, ikev2_initiate_rekey_child_sa)
74
75 static void vl_api_ipsec_spd_add_del_t_handler
76   (vl_api_ipsec_spd_add_del_t * mp)
77 {
78 #if WITH_LIBSSL == 0
79   clib_warning ("unimplemented");
80 #else
81
82   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
83   vl_api_ipsec_spd_add_del_reply_t *rmp;
84   int rv;
85
86   rv = ipsec_add_del_spd (vm, ntohl (mp->spd_id), mp->is_add);
87
88   REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_REPLY);
89 #endif
90 }
91
92 static void vl_api_ipsec_interface_add_del_spd_t_handler
93   (vl_api_ipsec_interface_add_del_spd_t * mp)
94 {
95   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
96   vl_api_ipsec_interface_add_del_spd_reply_t *rmp;
97   int rv;
98   u32 sw_if_index __attribute__ ((unused));
99   u32 spd_id __attribute__ ((unused));
100
101   sw_if_index = ntohl (mp->sw_if_index);
102   spd_id = ntohl (mp->spd_id);
103
104   VALIDATE_SW_IF_INDEX (mp);
105
106 #if WITH_LIBSSL > 0
107   rv = ipsec_set_interface_spd (vm, sw_if_index, spd_id, mp->is_add);
108 #else
109   rv = VNET_API_ERROR_UNIMPLEMENTED;
110 #endif
111
112   BAD_SW_IF_INDEX_LABEL;
113
114   REPLY_MACRO (VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY);
115 }
116
117 static void vl_api_ipsec_spd_add_del_entry_t_handler
118   (vl_api_ipsec_spd_add_del_entry_t * mp)
119 {
120   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
121   vl_api_ipsec_spd_add_del_entry_reply_t *rmp;
122   int rv;
123
124 #if WITH_LIBSSL > 0
125   ipsec_policy_t p;
126
127   memset (&p, 0, sizeof (p));
128
129   p.id = ntohl (mp->spd_id);
130   p.priority = ntohl (mp->priority);
131   p.is_outbound = mp->is_outbound;
132   p.is_ipv6 = mp->is_ipv6;
133
134   if (mp->is_ipv6 || mp->is_ip_any)
135     {
136       clib_memcpy (&p.raddr.start, mp->remote_address_start, 16);
137       clib_memcpy (&p.raddr.stop, mp->remote_address_stop, 16);
138       clib_memcpy (&p.laddr.start, mp->local_address_start, 16);
139       clib_memcpy (&p.laddr.stop, mp->local_address_stop, 16);
140     }
141   else
142     {
143       clib_memcpy (&p.raddr.start.ip4.data, mp->remote_address_start, 4);
144       clib_memcpy (&p.raddr.stop.ip4.data, mp->remote_address_stop, 4);
145       clib_memcpy (&p.laddr.start.ip4.data, mp->local_address_start, 4);
146       clib_memcpy (&p.laddr.stop.ip4.data, mp->local_address_stop, 4);
147     }
148   p.protocol = mp->protocol;
149   p.rport.start = ntohs (mp->remote_port_start);
150   p.rport.stop = ntohs (mp->remote_port_stop);
151   p.lport.start = ntohs (mp->local_port_start);
152   p.lport.stop = ntohs (mp->local_port_stop);
153   /* policy action resolve unsupported */
154   if (mp->policy == IPSEC_POLICY_ACTION_RESOLVE)
155     {
156       clib_warning ("unsupported action: 'resolve'");
157       rv = VNET_API_ERROR_UNIMPLEMENTED;
158       goto out;
159     }
160   p.policy = mp->policy;
161   p.sa_id = ntohl (mp->sa_id);
162
163   rv = ipsec_add_del_policy (vm, &p, mp->is_add);
164   if (rv)
165     goto out;
166
167   if (mp->is_ip_any)
168     {
169       p.is_ipv6 = 1;
170       rv = ipsec_add_del_policy (vm, &p, mp->is_add);
171     }
172 #else
173   rv = VNET_API_ERROR_UNIMPLEMENTED;
174   goto out;
175 #endif
176
177 out:
178   REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_ENTRY_REPLY);
179 }
180
181 static void vl_api_ipsec_sad_add_del_entry_t_handler
182   (vl_api_ipsec_sad_add_del_entry_t * mp)
183 {
184   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
185   vl_api_ipsec_sad_add_del_entry_reply_t *rmp;
186   int rv;
187 #if WITH_LIBSSL > 0
188   ipsec_main_t *im = &ipsec_main;
189   ipsec_sa_t sa;
190
191   memset (&sa, 0, sizeof (sa));
192
193   sa.id = ntohl (mp->sad_id);
194   sa.spi = ntohl (mp->spi);
195   /* security protocol AH unsupported */
196   if (mp->protocol == IPSEC_PROTOCOL_AH)
197     {
198       clib_warning ("unsupported security protocol 'AH'");
199       rv = VNET_API_ERROR_UNIMPLEMENTED;
200       goto out;
201     }
202   sa.protocol = mp->protocol;
203   /* check for unsupported crypto-alg */
204   if (mp->crypto_algorithm < IPSEC_CRYPTO_ALG_AES_CBC_128 ||
205       mp->crypto_algorithm >= IPSEC_CRYPTO_N_ALG)
206     {
207       clib_warning ("unsupported crypto-alg: '%U'", format_ipsec_crypto_alg,
208                     mp->crypto_algorithm);
209       rv = VNET_API_ERROR_UNIMPLEMENTED;
210       goto out;
211     }
212   sa.crypto_alg = mp->crypto_algorithm;
213   sa.crypto_key_len = mp->crypto_key_length;
214   clib_memcpy (&sa.crypto_key, mp->crypto_key, sizeof (sa.crypto_key));
215   /* check for unsupported integ-alg */
216   if (mp->integrity_algorithm >= IPSEC_INTEG_N_ALG)
217     {
218       clib_warning ("unsupported integ-alg: '%U'", format_ipsec_integ_alg,
219                     mp->integrity_algorithm);
220       rv = VNET_API_ERROR_UNIMPLEMENTED;
221       goto out;
222     }
223
224   sa.integ_alg = mp->integrity_algorithm;
225   sa.integ_key_len = mp->integrity_key_length;
226   clib_memcpy (&sa.integ_key, mp->integrity_key, sizeof (sa.integ_key));
227   sa.use_esn = mp->use_extended_sequence_number;
228   sa.is_tunnel = mp->is_tunnel;
229   sa.is_tunnel_ip6 = mp->is_tunnel_ipv6;
230   if (sa.is_tunnel_ip6)
231     {
232       clib_memcpy (&sa.tunnel_src_addr, mp->tunnel_src_address, 16);
233       clib_memcpy (&sa.tunnel_dst_addr, mp->tunnel_dst_address, 16);
234     }
235   else
236     {
237       clib_memcpy (&sa.tunnel_src_addr.ip4.data, mp->tunnel_src_address, 4);
238       clib_memcpy (&sa.tunnel_dst_addr.ip4.data, mp->tunnel_dst_address, 4);
239     }
240   sa.use_anti_replay = mp->use_anti_replay;
241
242   ASSERT (im->cb.check_support_cb);
243   clib_error_t *err = im->cb.check_support_cb (&sa);
244   if (err)
245     {
246       clib_warning ("%s", err->what);
247       rv = VNET_API_ERROR_UNIMPLEMENTED;
248       goto out;
249     }
250
251   rv = ipsec_add_del_sa (vm, &sa, mp->is_add);
252 #else
253   rv = VNET_API_ERROR_UNIMPLEMENTED;
254   goto out;
255 #endif
256
257 out:
258   REPLY_MACRO (VL_API_IPSEC_SAD_ADD_DEL_ENTRY_REPLY);
259 }
260
261 static void
262 send_ipsec_spd_details (ipsec_policy_t * p, unix_shared_memory_queue_t * q,
263                         u32 context)
264 {
265   vl_api_ipsec_spd_details_t *mp;
266
267   mp = vl_msg_api_alloc (sizeof (*mp));
268   memset (mp, 0, sizeof (*mp));
269   mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_DETAILS);
270   mp->context = context;
271
272   mp->spd_id = htonl (p->id);
273   mp->priority = htonl (p->priority);
274   mp->is_outbound = p->is_outbound;
275   mp->is_ipv6 = p->is_ipv6;
276   if (p->is_ipv6)
277     {
278       memcpy (mp->local_start_addr, &p->laddr.start.ip6, 16);
279       memcpy (mp->local_stop_addr, &p->laddr.stop.ip6, 16);
280       memcpy (mp->remote_start_addr, &p->raddr.start.ip6, 16);
281       memcpy (mp->remote_stop_addr, &p->raddr.stop.ip6, 16);
282     }
283   else
284     {
285       memcpy (mp->local_start_addr, &p->laddr.start.ip4, 4);
286       memcpy (mp->local_stop_addr, &p->laddr.stop.ip4, 4);
287       memcpy (mp->remote_start_addr, &p->raddr.start.ip4, 4);
288       memcpy (mp->remote_stop_addr, &p->raddr.stop.ip4, 4);
289     }
290   mp->local_start_port = htons (p->lport.start);
291   mp->local_stop_port = htons (p->lport.stop);
292   mp->remote_start_port = htons (p->rport.start);
293   mp->remote_stop_port = htons (p->rport.stop);
294   mp->protocol = p->protocol;
295   mp->policy = p->policy;
296   mp->sa_id = htonl (p->sa_id);
297   mp->bytes = clib_host_to_net_u64 (p->counter.bytes);
298   mp->packets = clib_host_to_net_u64 (p->counter.packets);
299
300   vl_msg_api_send_shmem (q, (u8 *) & mp);
301 }
302
303 static void
304 vl_api_ipsec_spd_dump_t_handler (vl_api_ipsec_spd_dump_t * mp)
305 {
306   unix_shared_memory_queue_t *q;
307   ipsec_main_t *im = &ipsec_main;
308   ipsec_policy_t *policy;
309   ipsec_spd_t *spd;
310   uword *p;
311   u32 spd_index;
312 #if WITH_LIBSSL > 0
313   q = vl_api_client_index_to_input_queue (mp->client_index);
314   if (q == 0)
315     return;
316
317   p = hash_get (im->spd_index_by_spd_id, ntohl (mp->spd_id));
318   if (!p)
319     return;
320
321   spd_index = p[0];
322   spd = pool_elt_at_index (im->spds, spd_index);
323
324   /* *INDENT-OFF* */
325   pool_foreach (policy, spd->policies,
326   ({
327     if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == policy->sa_id)
328       send_ipsec_spd_details (policy, q,
329                               mp->context);}
330     ));
331   /* *INDENT-ON* */
332 #else
333   clib_warning ("unimplemented");
334 #endif
335 }
336
337 static void
338 vl_api_ipsec_sa_set_key_t_handler (vl_api_ipsec_sa_set_key_t * mp)
339 {
340   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
341   vl_api_ipsec_sa_set_key_reply_t *rmp;
342   int rv;
343 #if WITH_LIBSSL > 0
344   ipsec_sa_t sa;
345   sa.id = ntohl (mp->sa_id);
346   sa.crypto_key_len = mp->crypto_key_length;
347   clib_memcpy (&sa.crypto_key, mp->crypto_key, sizeof (sa.crypto_key));
348   sa.integ_key_len = mp->integrity_key_length;
349   clib_memcpy (&sa.integ_key, mp->integrity_key, sizeof (sa.integ_key));
350
351   rv = ipsec_set_sa_key (vm, &sa);
352 #else
353   rv = VNET_API_ERROR_UNIMPLEMENTED;
354 #endif
355
356   REPLY_MACRO (VL_API_IPSEC_SA_SET_KEY_REPLY);
357 }
358
359 static void
360 vl_api_ipsec_tunnel_if_add_del_t_handler (vl_api_ipsec_tunnel_if_add_del_t *
361                                           mp)
362 {
363   vl_api_ipsec_tunnel_if_add_del_reply_t *rmp;
364   ipsec_main_t *im = &ipsec_main;
365   vnet_main_t *vnm = im->vnet_main;
366   u32 sw_if_index = ~0;
367   int rv;
368
369 #if WITH_LIBSSL > 0
370   ipsec_add_del_tunnel_args_t tun;
371
372   memset (&tun, 0, sizeof (ipsec_add_del_tunnel_args_t));
373
374   tun.is_add = mp->is_add;
375   tun.esn = mp->esn;
376   tun.anti_replay = mp->anti_replay;
377   tun.local_spi = ntohl (mp->local_spi);
378   tun.remote_spi = ntohl (mp->remote_spi);
379   tun.crypto_alg = mp->crypto_alg;
380   tun.local_crypto_key_len = mp->local_crypto_key_len;
381   tun.remote_crypto_key_len = mp->remote_crypto_key_len;
382   tun.integ_alg = mp->integ_alg;
383   tun.local_integ_key_len = mp->local_integ_key_len;
384   tun.remote_integ_key_len = mp->remote_integ_key_len;
385   memcpy (&tun.local_ip, mp->local_ip, 4);
386   memcpy (&tun.remote_ip, mp->remote_ip, 4);
387   memcpy (&tun.local_crypto_key, &mp->local_crypto_key,
388           mp->local_crypto_key_len);
389   memcpy (&tun.remote_crypto_key, &mp->remote_crypto_key,
390           mp->remote_crypto_key_len);
391   memcpy (&tun.local_integ_key, &mp->local_integ_key,
392           mp->local_integ_key_len);
393   memcpy (&tun.remote_integ_key, &mp->remote_integ_key,
394           mp->remote_integ_key_len);
395
396   rv = ipsec_add_del_tunnel_if_internal (vnm, &tun, &sw_if_index);
397
398 #else
399   rv = VNET_API_ERROR_UNIMPLEMENTED;
400 #endif
401
402   REPLY_MACRO2 (VL_API_IPSEC_TUNNEL_IF_ADD_DEL_REPLY, (
403                                                         {
404                                                         rmp->sw_if_index =
405                                                         htonl (sw_if_index);
406                                                         }));
407 }
408
409 static void
410 send_ipsec_sa_details (ipsec_sa_t * sa, unix_shared_memory_queue_t * q,
411                        u32 context, u32 sw_if_index)
412 {
413   vl_api_ipsec_sa_details_t *mp;
414
415   mp = vl_msg_api_alloc (sizeof (*mp));
416   memset (mp, 0, sizeof (*mp));
417   mp->_vl_msg_id = ntohs (VL_API_IPSEC_SA_DETAILS);
418   mp->context = context;
419
420   mp->sa_id = htonl (sa->id);
421   mp->sw_if_index = htonl (sw_if_index);
422
423   mp->spi = htonl (sa->spi);
424   mp->protocol = sa->protocol;
425
426   mp->crypto_alg = sa->crypto_alg;
427   mp->crypto_key_len = sa->crypto_key_len;
428   memcpy (mp->crypto_key, sa->crypto_key, sa->crypto_key_len);
429
430   mp->integ_alg = sa->integ_alg;
431   mp->integ_key_len = sa->integ_key_len;
432   memcpy (mp->integ_key, sa->integ_key, sa->integ_key_len);
433
434   mp->use_esn = sa->use_esn;
435   mp->use_anti_replay = sa->use_anti_replay;
436
437   mp->is_tunnel = sa->is_tunnel;
438   mp->is_tunnel_ip6 = sa->is_tunnel_ip6;
439
440   if (sa->is_tunnel)
441     {
442       if (sa->is_tunnel_ip6)
443         {
444           memcpy (mp->tunnel_src_addr, &sa->tunnel_src_addr.ip6, 16);
445           memcpy (mp->tunnel_dst_addr, &sa->tunnel_dst_addr.ip6, 16);
446         }
447       else
448         {
449           memcpy (mp->tunnel_src_addr, &sa->tunnel_src_addr.ip4, 4);
450           memcpy (mp->tunnel_dst_addr, &sa->tunnel_dst_addr.ip4, 4);
451         }
452     }
453
454   mp->salt = clib_host_to_net_u32 (sa->salt);
455   mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq));
456   mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->last_seq));
457   if (sa->use_esn)
458     {
459       mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
460       mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->last_seq_hi));
461     }
462   if (sa->use_anti_replay)
463     mp->replay_window = clib_host_to_net_u64 (sa->replay_window);
464   mp->total_data_size = clib_host_to_net_u64 (sa->total_data_size);
465
466   vl_msg_api_send_shmem (q, (u8 *) & mp);
467 }
468
469
470 static void
471 vl_api_ipsec_sa_dump_t_handler (vl_api_ipsec_sa_dump_t * mp)
472 {
473   unix_shared_memory_queue_t *q;
474   ipsec_main_t *im = &ipsec_main;
475   vnet_main_t *vnm = im->vnet_main;
476   ipsec_sa_t *sa;
477   ipsec_tunnel_if_t *t;
478   u32 *sa_index_to_tun_if_index = 0;
479
480 #if WITH_LIBSSL > 0
481   q = vl_api_client_index_to_input_queue (mp->client_index);
482   if (q == 0 || pool_elts (im->sad) == 0)
483     return;
484
485   vec_validate_init_empty (sa_index_to_tun_if_index, vec_len (im->sad) - 1,
486                            ~0);
487
488   /* *INDENT-OFF* */
489   pool_foreach (t, im->tunnel_interfaces,
490   ({
491     vnet_hw_interface_t *hi;
492     u32 sw_if_index = ~0;
493
494     hi = vnet_get_hw_interface (vnm, t->hw_if_index);
495     sw_if_index = hi->sw_if_index;
496     sa_index_to_tun_if_index[t->input_sa_index] = sw_if_index;
497     sa_index_to_tun_if_index[t->output_sa_index] = sw_if_index;
498   }));
499
500   pool_foreach (sa, im->sad,
501   ({
502     if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == sa->id)
503       send_ipsec_sa_details (sa, q, mp->context,
504                              sa_index_to_tun_if_index[sa - im->sad]);
505   }));
506   /* *INDENT-ON* */
507
508   vec_free (sa_index_to_tun_if_index);
509 #else
510   clib_warning ("unimplemented");
511 #endif
512 }
513
514
515 static void
516 vl_api_ipsec_tunnel_if_set_key_t_handler (vl_api_ipsec_tunnel_if_set_key_t *
517                                           mp)
518 {
519   vl_api_ipsec_tunnel_if_set_key_reply_t *rmp;
520   ipsec_main_t *im = &ipsec_main;
521   vnet_main_t *vnm = im->vnet_main;
522   vnet_sw_interface_t *sw;
523   u8 *key = 0;
524   int rv;
525
526 #if WITH_LIBSSL > 0
527   sw = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
528
529   switch (mp->key_type)
530     {
531     case IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO:
532     case IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO:
533       if (mp->alg < IPSEC_CRYPTO_ALG_AES_CBC_128 ||
534           mp->alg > IPSEC_CRYPTO_N_ALG)
535         {
536           rv = VNET_API_ERROR_UNIMPLEMENTED;
537           goto out;
538         }
539       break;
540     case IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG:
541     case IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG:
542       if (mp->alg > IPSEC_INTEG_N_ALG)
543         {
544           rv = VNET_API_ERROR_UNIMPLEMENTED;
545           goto out;
546         }
547       break;
548     case IPSEC_IF_SET_KEY_TYPE_NONE:
549     default:
550       rv = VNET_API_ERROR_UNIMPLEMENTED;
551       goto out;
552       break;
553     }
554
555   key = vec_new (u8, mp->key_len);
556   clib_memcpy (key, mp->key, mp->key_len);
557
558   rv = ipsec_set_interface_key (vnm, sw->hw_if_index, mp->key_type, mp->alg,
559                                 key);
560   vec_free (key);
561 #else
562   clib_warning ("unimplemented");
563 #endif
564
565 out:
566   REPLY_MACRO (VL_API_IPSEC_TUNNEL_IF_SET_KEY_REPLY);
567 }
568
569
570 static void
571 vl_api_ipsec_tunnel_if_set_sa_t_handler (vl_api_ipsec_tunnel_if_set_sa_t * mp)
572 {
573   vl_api_ipsec_tunnel_if_set_sa_reply_t *rmp;
574   ipsec_main_t *im = &ipsec_main;
575   vnet_main_t *vnm = im->vnet_main;
576   vnet_sw_interface_t *sw;
577   int rv;
578
579 #if WITH_LIBSSL > 0
580   sw = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
581
582   rv = ipsec_set_interface_sa (vnm, sw->hw_if_index, ntohl (mp->sa_id),
583                                mp->is_outbound);
584 #else
585   clib_warning ("unimplemented");
586 #endif
587
588   REPLY_MACRO (VL_API_IPSEC_TUNNEL_IF_SET_SA_REPLY);
589 }
590
591
592 static void
593 vl_api_ikev2_profile_add_del_t_handler (vl_api_ikev2_profile_add_del_t * mp)
594 {
595   vl_api_ikev2_profile_add_del_reply_t *rmp;
596   int rv = 0;
597
598 #if WITH_LIBSSL > 0
599   vlib_main_t *vm = vlib_get_main ();
600   clib_error_t *error;
601   u8 *tmp = format (0, "%s", mp->name);
602   error = ikev2_add_del_profile (vm, tmp, mp->is_add);
603   vec_free (tmp);
604   if (error)
605     rv = VNET_API_ERROR_UNSPECIFIED;
606 #else
607   rv = VNET_API_ERROR_UNIMPLEMENTED;
608 #endif
609
610   REPLY_MACRO (VL_API_IKEV2_PROFILE_ADD_DEL_REPLY);
611 }
612
613 static void
614   vl_api_ikev2_profile_set_auth_t_handler
615   (vl_api_ikev2_profile_set_auth_t * mp)
616 {
617   vl_api_ikev2_profile_set_auth_reply_t *rmp;
618   int rv = 0;
619
620 #if WITH_LIBSSL > 0
621   vlib_main_t *vm = vlib_get_main ();
622   clib_error_t *error;
623   u8 *tmp = format (0, "%s", mp->name);
624   u8 *data = vec_new (u8, mp->data_len);
625   clib_memcpy (data, mp->data, mp->data_len);
626   error = ikev2_set_profile_auth (vm, tmp, mp->auth_method, data, mp->is_hex);
627   vec_free (tmp);
628   vec_free (data);
629   if (error)
630     rv = VNET_API_ERROR_UNSPECIFIED;
631 #else
632   rv = VNET_API_ERROR_UNIMPLEMENTED;
633 #endif
634
635   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_AUTH_REPLY);
636 }
637
638 static void
639 vl_api_ikev2_profile_set_id_t_handler (vl_api_ikev2_profile_set_id_t * mp)
640 {
641   vl_api_ikev2_profile_add_del_reply_t *rmp;
642   int rv = 0;
643
644 #if WITH_LIBSSL > 0
645   vlib_main_t *vm = vlib_get_main ();
646   clib_error_t *error;
647   u8 *tmp = format (0, "%s", mp->name);
648   u8 *data = vec_new (u8, mp->data_len);
649   clib_memcpy (data, mp->data, mp->data_len);
650   error = ikev2_set_profile_id (vm, tmp, mp->id_type, data, mp->is_local);
651   vec_free (tmp);
652   vec_free (data);
653   if (error)
654     rv = VNET_API_ERROR_UNSPECIFIED;
655 #else
656   rv = VNET_API_ERROR_UNIMPLEMENTED;
657 #endif
658
659   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_ID_REPLY);
660 }
661
662 static void
663 vl_api_ikev2_profile_set_ts_t_handler (vl_api_ikev2_profile_set_ts_t * mp)
664 {
665   vl_api_ikev2_profile_set_ts_reply_t *rmp;
666   int rv = 0;
667
668 #if WITH_LIBSSL > 0
669   vlib_main_t *vm = vlib_get_main ();
670   clib_error_t *error;
671   u8 *tmp = format (0, "%s", mp->name);
672   error = ikev2_set_profile_ts (vm, tmp, mp->proto, mp->start_port,
673                                 mp->end_port, (ip4_address_t) mp->start_addr,
674                                 (ip4_address_t) mp->end_addr, mp->is_local);
675   vec_free (tmp);
676   if (error)
677     rv = VNET_API_ERROR_UNSPECIFIED;
678 #else
679   rv = VNET_API_ERROR_UNIMPLEMENTED;
680 #endif
681
682   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_TS_REPLY);
683 }
684
685 static void
686 vl_api_ikev2_set_local_key_t_handler (vl_api_ikev2_set_local_key_t * mp)
687 {
688   vl_api_ikev2_profile_set_ts_reply_t *rmp;
689   int rv = 0;
690
691 #if WITH_LIBSSL > 0
692   vlib_main_t *vm = vlib_get_main ();
693   clib_error_t *error;
694
695   error = ikev2_set_local_key (vm, mp->key_file);
696   if (error)
697     rv = VNET_API_ERROR_UNSPECIFIED;
698 #else
699   rv = VNET_API_ERROR_UNIMPLEMENTED;
700 #endif
701
702   REPLY_MACRO (VL_API_IKEV2_SET_LOCAL_KEY_REPLY);
703 }
704
705 static void
706 vl_api_ikev2_set_responder_t_handler (vl_api_ikev2_set_responder_t * mp)
707 {
708   vl_api_ikev2_set_responder_reply_t *rmp;
709   int rv = 0;
710
711 #if WITH_LIBSSL > 0
712   vlib_main_t *vm = vlib_get_main ();
713   clib_error_t *error;
714
715   u8 *tmp = format (0, "%s", mp->name);
716   ip4_address_t ip4;
717   clib_memcpy (&ip4, mp->address, sizeof (ip4));
718
719   error = ikev2_set_profile_responder (vm, tmp, mp->sw_if_index, ip4);
720   vec_free (tmp);
721   if (error)
722     rv = VNET_API_ERROR_UNSPECIFIED;
723 #else
724   rv = VNET_API_ERROR_UNIMPLEMENTED;
725 #endif
726
727   REPLY_MACRO (VL_API_IKEV2_SET_RESPONDER_REPLY);
728 }
729
730 static void
731 vl_api_ikev2_set_ike_transforms_t_handler (vl_api_ikev2_set_ike_transforms_t *
732                                            mp)
733 {
734   vl_api_ikev2_set_ike_transforms_reply_t *rmp;
735   int rv = 0;
736
737 #if WITH_LIBSSL > 0
738   vlib_main_t *vm = vlib_get_main ();
739   clib_error_t *error;
740
741   u8 *tmp = format (0, "%s", mp->name);
742
743   error =
744     ikev2_set_profile_ike_transforms (vm, tmp, mp->crypto_alg, mp->integ_alg,
745                                       mp->dh_group, mp->crypto_key_size);
746   vec_free (tmp);
747   if (error)
748     rv = VNET_API_ERROR_UNSPECIFIED;
749 #else
750   rv = VNET_API_ERROR_UNIMPLEMENTED;
751 #endif
752
753   REPLY_MACRO (VL_API_IKEV2_SET_IKE_TRANSFORMS_REPLY);
754 }
755
756 static void
757 vl_api_ikev2_set_esp_transforms_t_handler (vl_api_ikev2_set_esp_transforms_t *
758                                            mp)
759 {
760   vl_api_ikev2_set_esp_transforms_reply_t *rmp;
761   int rv = 0;
762
763 #if WITH_LIBSSL > 0
764   vlib_main_t *vm = vlib_get_main ();
765   clib_error_t *error;
766
767   u8 *tmp = format (0, "%s", mp->name);
768
769   error =
770     ikev2_set_profile_esp_transforms (vm, tmp, mp->crypto_alg, mp->integ_alg,
771                                       mp->dh_group, mp->crypto_key_size);
772   vec_free (tmp);
773   if (error)
774     rv = VNET_API_ERROR_UNSPECIFIED;
775 #else
776   rv = VNET_API_ERROR_UNIMPLEMENTED;
777 #endif
778
779   REPLY_MACRO (VL_API_IKEV2_SET_ESP_TRANSFORMS_REPLY);
780 }
781
782 static void
783 vl_api_ikev2_set_sa_lifetime_t_handler (vl_api_ikev2_set_sa_lifetime_t * mp)
784 {
785   vl_api_ikev2_set_sa_lifetime_reply_t *rmp;
786   int rv = 0;
787
788 #if WITH_LIBSSL > 0
789   vlib_main_t *vm = vlib_get_main ();
790   clib_error_t *error;
791
792   u8 *tmp = format (0, "%s", mp->name);
793
794   error =
795     ikev2_set_profile_sa_lifetime (vm, tmp, mp->lifetime, mp->lifetime_jitter,
796                                    mp->handover, mp->lifetime_maxdata);
797   vec_free (tmp);
798   if (error)
799     rv = VNET_API_ERROR_UNSPECIFIED;
800 #else
801   rv = VNET_API_ERROR_UNIMPLEMENTED;
802 #endif
803
804   REPLY_MACRO (VL_API_IKEV2_SET_SA_LIFETIME_REPLY);
805 }
806
807 static void
808 vl_api_ikev2_initiate_sa_init_t_handler (vl_api_ikev2_initiate_sa_init_t * mp)
809 {
810   vl_api_ikev2_initiate_sa_init_reply_t *rmp;
811   int rv = 0;
812
813 #if WITH_LIBSSL > 0
814   vlib_main_t *vm = vlib_get_main ();
815   clib_error_t *error;
816
817   u8 *tmp = format (0, "%s", mp->name);
818
819   error = ikev2_initiate_sa_init (vm, tmp);
820   vec_free (tmp);
821   if (error)
822     rv = VNET_API_ERROR_UNSPECIFIED;
823 #else
824   rv = VNET_API_ERROR_UNIMPLEMENTED;
825 #endif
826
827   REPLY_MACRO (VL_API_IKEV2_INITIATE_SA_INIT_REPLY);
828 }
829
830 static void
831 vl_api_ikev2_initiate_del_ike_sa_t_handler (vl_api_ikev2_initiate_del_ike_sa_t
832                                             * mp)
833 {
834   vl_api_ikev2_initiate_del_ike_sa_reply_t *rmp;
835   int rv = 0;
836
837 #if WITH_LIBSSL > 0
838   vlib_main_t *vm = vlib_get_main ();
839   clib_error_t *error;
840
841   error = ikev2_initiate_delete_ike_sa (vm, mp->ispi);
842   if (error)
843     rv = VNET_API_ERROR_UNSPECIFIED;
844 #else
845   rv = VNET_API_ERROR_UNIMPLEMENTED;
846 #endif
847
848   REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_IKE_SA_REPLY);
849 }
850
851 static void
852   vl_api_ikev2_initiate_del_child_sa_t_handler
853   (vl_api_ikev2_initiate_del_child_sa_t * mp)
854 {
855   vl_api_ikev2_initiate_del_child_sa_reply_t *rmp;
856   int rv = 0;
857
858 #if WITH_LIBSSL > 0
859   vlib_main_t *vm = vlib_get_main ();
860   clib_error_t *error;
861
862   error = ikev2_initiate_delete_child_sa (vm, mp->ispi);
863   if (error)
864     rv = VNET_API_ERROR_UNSPECIFIED;
865 #else
866   rv = VNET_API_ERROR_UNIMPLEMENTED;
867 #endif
868
869   REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_CHILD_SA_REPLY);
870 }
871
872 static void
873   vl_api_ikev2_initiate_rekey_child_sa_t_handler
874   (vl_api_ikev2_initiate_rekey_child_sa_t * mp)
875 {
876   vl_api_ikev2_initiate_rekey_child_sa_reply_t *rmp;
877   int rv = 0;
878
879 #if WITH_LIBSSL > 0
880   vlib_main_t *vm = vlib_get_main ();
881   clib_error_t *error;
882
883   error = ikev2_initiate_rekey_child_sa (vm, mp->ispi);
884   if (error)
885     rv = VNET_API_ERROR_UNSPECIFIED;
886 #else
887   rv = VNET_API_ERROR_UNIMPLEMENTED;
888 #endif
889
890   REPLY_MACRO (VL_API_IKEV2_INITIATE_REKEY_CHILD_SA_REPLY);
891 }
892
893 /*
894  * ipsec_api_hookup
895  * Add vpe's API message handlers to the table.
896  * vlib has alread mapped shared memory and
897  * added the client registration handlers.
898  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
899  */
900 #define vl_msg_name_crc_list
901 #include <vnet/vnet_all_api_h.h>
902 #undef vl_msg_name_crc_list
903
904 static void
905 setup_message_id_table (api_main_t * am)
906 {
907 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
908   foreach_vl_msg_name_crc_ipsec;
909 #undef _
910 }
911
912 static clib_error_t *
913 ipsec_api_hookup (vlib_main_t * vm)
914 {
915   api_main_t *am = &api_main;
916
917 #define _(N,n)                                                  \
918     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
919                            vl_api_##n##_t_handler,              \
920                            vl_noop_handler,                     \
921                            vl_api_##n##_t_endian,               \
922                            vl_api_##n##_t_print,                \
923                            sizeof(vl_api_##n##_t), 1);
924   foreach_vpe_api_msg;
925 #undef _
926
927   /*
928    * Set up the (msg_name, crc, message-id) table
929    */
930   setup_message_id_table (am);
931
932   return 0;
933 }
934
935 VLIB_API_INIT_FUNCTION (ipsec_api_hookup);
936
937 /*
938  * fd.io coding-style-patch-verification: ON
939  *
940  * Local Variables:
941  * eval: (c-set-style "gnu")
942  * End:
943  */