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