ipsec: remove the set_key API
[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 #include <vnet/ip/ip_types_api.h>
27 #include <vnet/fib/fib.h>
28
29 #include <vnet/vnet_msg_enum.h>
30
31 #if WITH_LIBSSL > 0
32 #include <vnet/ipsec/ipsec.h>
33 #endif /* IPSEC */
34
35 #define vl_typedefs             /* define message structures */
36 #include <vnet/vnet_all_api_h.h>
37 #undef vl_typedefs
38
39 #define vl_endianfun            /* define message structures */
40 #include <vnet/vnet_all_api_h.h>
41 #undef vl_endianfun
42
43 /* instantiate all the print functions we know about */
44 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
45 #define vl_printfun
46 #include <vnet/vnet_all_api_h.h>
47 #undef vl_printfun
48
49 #include <vlibapi/api_helper_macros.h>
50
51 #define foreach_vpe_api_msg                                     \
52 _(IPSEC_SPD_ADD_DEL, ipsec_spd_add_del)                         \
53 _(IPSEC_INTERFACE_ADD_DEL_SPD, ipsec_interface_add_del_spd)     \
54 _(IPSEC_SPD_ENTRY_ADD_DEL, ipsec_spd_entry_add_del)             \
55 _(IPSEC_SAD_ENTRY_ADD_DEL, ipsec_sad_entry_add_del)             \
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_SA, ipsec_tunnel_if_set_sa)               \
62 _(IPSEC_SELECT_BACKEND, ipsec_select_backend)                   \
63 _(IPSEC_BACKEND_DUMP, ipsec_backend_dump)
64
65 static void
66 vl_api_ipsec_spd_add_del_t_handler (vl_api_ipsec_spd_add_del_t * mp)
67 {
68 #if WITH_LIBSSL == 0
69   clib_warning ("unimplemented");
70 #else
71
72   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
73   vl_api_ipsec_spd_add_del_reply_t *rmp;
74   int rv;
75
76   rv = ipsec_add_del_spd (vm, ntohl (mp->spd_id), mp->is_add);
77
78   REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_REPLY);
79 #endif
80 }
81
82 static void vl_api_ipsec_interface_add_del_spd_t_handler
83   (vl_api_ipsec_interface_add_del_spd_t * mp)
84 {
85   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
86   vl_api_ipsec_interface_add_del_spd_reply_t *rmp;
87   int rv;
88   u32 sw_if_index __attribute__ ((unused));
89   u32 spd_id __attribute__ ((unused));
90
91   sw_if_index = ntohl (mp->sw_if_index);
92   spd_id = ntohl (mp->spd_id);
93
94   VALIDATE_SW_IF_INDEX (mp);
95
96 #if WITH_LIBSSL > 0
97   rv = ipsec_set_interface_spd (vm, sw_if_index, spd_id, mp->is_add);
98 #else
99   rv = VNET_API_ERROR_UNIMPLEMENTED;
100 #endif
101
102   BAD_SW_IF_INDEX_LABEL;
103
104   REPLY_MACRO (VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY);
105 }
106
107 static int
108 ipsec_spd_action_decode (vl_api_ipsec_spd_action_t in,
109                          ipsec_policy_action_t * out)
110 {
111   in = clib_net_to_host_u32 (in);
112
113   switch (in)
114     {
115 #define _(v,f,s) case IPSEC_API_SPD_ACTION_##f: \
116       *out = IPSEC_POLICY_ACTION_##f;              \
117       return (0);
118       foreach_ipsec_policy_action
119 #undef _
120     }
121   return (VNET_API_ERROR_UNIMPLEMENTED);
122 }
123
124 static void vl_api_ipsec_spd_entry_add_del_t_handler
125   (vl_api_ipsec_spd_entry_add_del_t * mp)
126 {
127   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
128   vl_api_ipsec_spd_entry_add_del_reply_t *rmp;
129   ip46_type_t itype;
130   u32 stat_index;
131   int rv;
132
133   stat_index = ~0;
134
135 #if WITH_LIBSSL > 0
136   ipsec_policy_t p;
137
138   clib_memset (&p, 0, sizeof (p));
139
140   p.id = ntohl (mp->entry.spd_id);
141   p.priority = ntohl (mp->entry.priority);
142
143   itype = ip_address_decode (&mp->entry.remote_address_start, &p.raddr.start);
144   ip_address_decode (&mp->entry.remote_address_stop, &p.raddr.stop);
145   ip_address_decode (&mp->entry.local_address_start, &p.laddr.start);
146   ip_address_decode (&mp->entry.local_address_stop, &p.laddr.stop);
147
148   p.is_ipv6 = (itype == IP46_TYPE_IP6);
149
150   p.protocol = mp->entry.protocol;
151   /* leave the ports in network order */
152   p.rport.start = mp->entry.remote_port_start;
153   p.rport.stop = mp->entry.remote_port_stop;
154   p.lport.start = mp->entry.local_port_start;
155   p.lport.stop = mp->entry.local_port_stop;
156
157   rv = ipsec_spd_action_decode (mp->entry.policy, &p.policy);
158
159   if (rv)
160     goto out;
161
162   /* policy action resolve unsupported */
163   if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
164     {
165       clib_warning ("unsupported action: 'resolve'");
166       rv = VNET_API_ERROR_UNIMPLEMENTED;
167       goto out;
168     }
169   p.sa_id = ntohl (mp->entry.sa_id);
170   rv =
171     ipsec_policy_mk_type (mp->entry.is_outbound, p.is_ipv6, p.policy,
172                           &p.type);
173   if (rv)
174     goto out;
175
176   rv = ipsec_add_del_policy (vm, &p, mp->is_add, &stat_index);
177   if (rv)
178     goto out;
179
180 #else
181   rv = VNET_API_ERROR_UNIMPLEMENTED;
182   goto out;
183 #endif
184
185 out:
186   /* *INDENT-OFF* */
187   REPLY_MACRO2 (VL_API_IPSEC_SPD_ENTRY_ADD_DEL_REPLY,
188   ({
189     rmp->stat_index = ntohl(stat_index);
190   }));
191   /* *INDENT-ON* */
192 }
193
194 static int
195 ipsec_proto_decode (vl_api_ipsec_proto_t in, ipsec_protocol_t * out)
196 {
197   in = clib_net_to_host_u32 (in);
198
199   switch (in)
200     {
201     case IPSEC_API_PROTO_ESP:
202       *out = IPSEC_PROTOCOL_ESP;
203       return (0);
204     case IPSEC_API_PROTO_AH:
205       *out = IPSEC_PROTOCOL_AH;
206       return (0);
207     }
208   return (VNET_API_ERROR_INVALID_PROTOCOL);
209 }
210
211 static vl_api_ipsec_proto_t
212 ipsec_proto_encode (ipsec_protocol_t p)
213 {
214   switch (p)
215     {
216     case IPSEC_PROTOCOL_ESP:
217       return clib_host_to_net_u32 (IPSEC_API_PROTO_ESP);
218     case IPSEC_PROTOCOL_AH:
219       return clib_host_to_net_u32 (IPSEC_API_PROTO_AH);
220     }
221   return (VNET_API_ERROR_UNIMPLEMENTED);
222 }
223
224 static int
225 ipsec_crypto_algo_decode (vl_api_ipsec_crypto_alg_t in,
226                           ipsec_crypto_alg_t * out)
227 {
228   in = clib_net_to_host_u32 (in);
229
230   switch (in)
231     {
232 #define _(v,f,s) case IPSEC_API_CRYPTO_ALG_##f: \
233       *out = IPSEC_CRYPTO_ALG_##f;              \
234       return (0);
235       foreach_ipsec_crypto_alg
236 #undef _
237     }
238   return (VNET_API_ERROR_INVALID_ALGORITHM);
239 }
240
241 static vl_api_ipsec_crypto_alg_t
242 ipsec_crypto_algo_encode (ipsec_crypto_alg_t c)
243 {
244   switch (c)
245     {
246 #define _(v,f,s) case IPSEC_CRYPTO_ALG_##f:                     \
247       return clib_host_to_net_u32(IPSEC_API_CRYPTO_ALG_##f);
248       foreach_ipsec_crypto_alg
249 #undef _
250     case IPSEC_CRYPTO_N_ALG:
251       break;
252     }
253   ASSERT (0);
254   return (VNET_API_ERROR_UNIMPLEMENTED);
255 }
256
257
258 static int
259 ipsec_integ_algo_decode (vl_api_ipsec_integ_alg_t in, ipsec_integ_alg_t * out)
260 {
261   in = clib_net_to_host_u32 (in);
262
263   switch (in)
264     {
265 #define _(v,f,s) case IPSEC_API_INTEG_ALG_##f:  \
266       *out = IPSEC_INTEG_ALG_##f;               \
267       return (0);
268       foreach_ipsec_integ_alg
269 #undef _
270     }
271   return (VNET_API_ERROR_INVALID_ALGORITHM);
272 }
273
274 static vl_api_ipsec_integ_alg_t
275 ipsec_integ_algo_encode (ipsec_integ_alg_t i)
276 {
277   switch (i)
278     {
279 #define _(v,f,s) case IPSEC_INTEG_ALG_##f:                      \
280       return (clib_host_to_net_u32(IPSEC_API_INTEG_ALG_##f));
281       foreach_ipsec_integ_alg
282 #undef _
283     case IPSEC_INTEG_N_ALG:
284       break;
285     }
286   ASSERT (0);
287   return (VNET_API_ERROR_UNIMPLEMENTED);
288 }
289
290 static void
291 ipsec_key_decode (const vl_api_key_t * key, ipsec_key_t * out)
292 {
293   ipsec_mk_key (out, key->data, key->length);
294 }
295
296 static void
297 ipsec_key_encode (const ipsec_key_t * in, vl_api_key_t * out)
298 {
299   out->length = in->len;
300   clib_memcpy (out->data, in->data, out->length);
301 }
302
303 static ipsec_sa_flags_t
304 ipsec_sa_flags_decode (vl_api_ipsec_sad_flags_t in)
305 {
306   ipsec_sa_flags_t flags = IPSEC_SA_FLAG_NONE;
307   in = clib_net_to_host_u32 (in);
308
309   if (in & IPSEC_API_SAD_FLAG_USE_ESN)
310     flags |= IPSEC_SA_FLAG_USE_ESN;
311   if (in & IPSEC_API_SAD_FLAG_USE_ANTI_REPLAY)
312     flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
313   if (in & IPSEC_API_SAD_FLAG_IS_TUNNEL)
314     flags |= IPSEC_SA_FLAG_IS_TUNNEL;
315   if (in & IPSEC_API_SAD_FLAG_IS_TUNNEL_V6)
316     flags |= IPSEC_SA_FLAG_IS_TUNNEL_V6;
317   if (in & IPSEC_API_SAD_FLAG_UDP_ENCAP)
318     flags |= IPSEC_SA_FLAG_UDP_ENCAP;
319
320   return (flags);
321 }
322
323 static vl_api_ipsec_sad_flags_t
324 ipsec_sad_flags_encode (const ipsec_sa_t * sa)
325 {
326   vl_api_ipsec_sad_flags_t flags = IPSEC_API_SAD_FLAG_NONE;
327
328   if (ipsec_sa_is_set_USE_ESN (sa))
329     flags |= IPSEC_API_SAD_FLAG_USE_ESN;
330   if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa))
331     flags |= IPSEC_API_SAD_FLAG_USE_ANTI_REPLAY;
332   if (ipsec_sa_is_set_IS_TUNNEL (sa))
333     flags |= IPSEC_API_SAD_FLAG_IS_TUNNEL;
334   if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa))
335     flags |= IPSEC_API_SAD_FLAG_IS_TUNNEL_V6;
336   if (ipsec_sa_is_set_UDP_ENCAP (sa))
337     flags |= IPSEC_API_SAD_FLAG_UDP_ENCAP;
338
339   return clib_host_to_net_u32 (flags);
340 }
341
342 static void vl_api_ipsec_sad_entry_add_del_t_handler
343   (vl_api_ipsec_sad_entry_add_del_t * mp)
344 {
345   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
346   vl_api_ipsec_sad_entry_add_del_reply_t *rmp;
347   ip46_address_t tun_src = { }, tun_dst =
348   {
349   };
350   ipsec_key_t crypto_key, integ_key;
351   ipsec_crypto_alg_t crypto_alg;
352   ipsec_integ_alg_t integ_alg;
353   ipsec_protocol_t proto;
354   ipsec_sa_flags_t flags;
355   u32 id, spi, sa_index = ~0;
356   int rv;
357
358 #if WITH_LIBSSL > 0
359
360   id = ntohl (mp->entry.sad_id);
361   spi = ntohl (mp->entry.spi);
362
363   rv = ipsec_proto_decode (mp->entry.protocol, &proto);
364
365   if (rv)
366     goto out;
367
368   rv = ipsec_crypto_algo_decode (mp->entry.crypto_algorithm, &crypto_alg);
369
370   if (rv)
371     goto out;
372
373   rv = ipsec_integ_algo_decode (mp->entry.integrity_algorithm, &integ_alg);
374
375   if (rv)
376     goto out;
377
378   ipsec_key_decode (&mp->entry.crypto_key, &crypto_key);
379   ipsec_key_decode (&mp->entry.integrity_key, &integ_key);
380
381   flags = ipsec_sa_flags_decode (mp->entry.flags);
382
383   ip_address_decode (&mp->entry.tunnel_src, &tun_src);
384   ip_address_decode (&mp->entry.tunnel_dst, &tun_dst);
385
386   if (mp->is_add)
387     rv = ipsec_sa_add (id, spi, proto,
388                        crypto_alg, &crypto_key,
389                        integ_alg, &integ_key, flags,
390                        0, mp->entry.salt, &tun_src, &tun_dst, &sa_index);
391   else
392     rv = ipsec_sa_del (id);
393
394 #else
395   rv = VNET_API_ERROR_UNIMPLEMENTED;
396 #endif
397
398 out:
399   /* *INDENT-OFF* */
400   REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_DEL_REPLY,
401   {
402     rmp->stat_index = htonl (sa_index);
403   });
404   /* *INDENT-ON* */
405 }
406
407 static void
408 send_ipsec_spds_details (ipsec_spd_t * spd, vl_api_registration_t * reg,
409                          u32 context)
410 {
411   vl_api_ipsec_spds_details_t *mp;
412   u32 n_policies = 0;
413
414   mp = vl_msg_api_alloc (sizeof (*mp));
415   clib_memset (mp, 0, sizeof (*mp));
416   mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPDS_DETAILS);
417   mp->context = context;
418
419   mp->spd_id = htonl (spd->id);
420 #define _(s, n) n_policies += vec_len (spd->policies[IPSEC_SPD_POLICY_##s]);
421   foreach_ipsec_spd_policy_type
422 #undef _
423     mp->npolicies = htonl (n_policies);
424
425   vl_api_send_msg (reg, (u8 *) mp);
426 }
427
428 static void
429 vl_api_ipsec_spds_dump_t_handler (vl_api_ipsec_spds_dump_t * mp)
430 {
431   vl_api_registration_t *reg;
432   ipsec_main_t *im = &ipsec_main;
433   ipsec_spd_t *spd;
434 #if WITH_LIBSSL > 0
435   reg = vl_api_client_index_to_registration (mp->client_index);
436   if (!reg)
437     return;
438
439   /* *INDENT-OFF* */
440   pool_foreach (spd, im->spds, ({
441     send_ipsec_spds_details (spd, reg, mp->context);
442   }));
443   /* *INDENT-ON* */
444 #else
445   clib_warning ("unimplemented");
446 #endif
447 }
448
449 vl_api_ipsec_spd_action_t
450 ipsec_spd_action_encode (ipsec_policy_action_t in)
451 {
452   vl_api_ipsec_spd_action_t out = IPSEC_API_SPD_ACTION_BYPASS;
453
454   switch (in)
455     {
456 #define _(v,f,s) case IPSEC_POLICY_ACTION_##f: \
457       out = IPSEC_API_SPD_ACTION_##f;          \
458       break;
459       foreach_ipsec_policy_action
460 #undef _
461     }
462   return (clib_host_to_net_u32 (out));
463 }
464
465 static void
466 send_ipsec_spd_details (ipsec_policy_t * p, vl_api_registration_t * reg,
467                         u32 context)
468 {
469   vl_api_ipsec_spd_details_t *mp;
470
471   mp = vl_msg_api_alloc (sizeof (*mp));
472   clib_memset (mp, 0, sizeof (*mp));
473   mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_DETAILS);
474   mp->context = context;
475
476   mp->entry.spd_id = htonl (p->id);
477   mp->entry.priority = htonl (p->priority);
478   mp->entry.is_outbound = ((p->type == IPSEC_SPD_POLICY_IP6_OUTBOUND) ||
479                            (p->type == IPSEC_SPD_POLICY_IP4_OUTBOUND));
480
481   ip_address_encode (&p->laddr.start, IP46_TYPE_ANY,
482                      &mp->entry.local_address_start);
483   ip_address_encode (&p->laddr.stop, IP46_TYPE_ANY,
484                      &mp->entry.local_address_stop);
485   ip_address_encode (&p->raddr.start, IP46_TYPE_ANY,
486                      &mp->entry.remote_address_start);
487   ip_address_encode (&p->raddr.stop, IP46_TYPE_ANY,
488                      &mp->entry.remote_address_stop);
489   mp->entry.local_port_start = p->lport.start;
490   mp->entry.local_port_stop = p->lport.stop;
491   mp->entry.remote_port_start = p->rport.start;
492   mp->entry.remote_port_stop = p->rport.stop;
493   mp->entry.protocol = p->protocol;
494   mp->entry.policy = ipsec_spd_action_encode (p->policy);
495   mp->entry.sa_id = htonl (p->sa_id);
496
497   vl_api_send_msg (reg, (u8 *) mp);
498 }
499
500 static void
501 vl_api_ipsec_spd_dump_t_handler (vl_api_ipsec_spd_dump_t * mp)
502 {
503   vl_api_registration_t *reg;
504   ipsec_main_t *im = &ipsec_main;
505   ipsec_spd_policy_type_t ptype;
506   ipsec_policy_t *policy;
507   ipsec_spd_t *spd;
508   uword *p;
509   u32 spd_index, *ii;
510 #if WITH_LIBSSL > 0
511   reg = vl_api_client_index_to_registration (mp->client_index);
512   if (!reg)
513     return;
514
515   p = hash_get (im->spd_index_by_spd_id, ntohl (mp->spd_id));
516   if (!p)
517     return;
518
519   spd_index = p[0];
520   spd = pool_elt_at_index (im->spds, spd_index);
521
522   /* *INDENT-OFF* */
523   FOR_EACH_IPSEC_SPD_POLICY_TYPE(ptype) {
524     vec_foreach(ii, spd->policies[ptype])
525       {
526         policy = pool_elt_at_index(im->policies, *ii);
527
528         if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == policy->sa_id)
529           send_ipsec_spd_details (policy, reg, mp->context);
530       }
531   }
532   /* *INDENT-ON* */
533 #else
534   clib_warning ("unimplemented");
535 #endif
536 }
537
538 static void
539 send_ipsec_spd_interface_details (vl_api_registration_t * reg, u32 spd_index,
540                                   u32 sw_if_index, u32 context)
541 {
542   vl_api_ipsec_spd_interface_details_t *mp;
543
544   mp = vl_msg_api_alloc (sizeof (*mp));
545   clib_memset (mp, 0, sizeof (*mp));
546   mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_INTERFACE_DETAILS);
547   mp->context = context;
548
549   mp->spd_index = htonl (spd_index);
550   mp->sw_if_index = htonl (sw_if_index);
551
552   vl_api_send_msg (reg, (u8 *) mp);
553 }
554
555 static void
556 vl_api_ipsec_spd_interface_dump_t_handler (vl_api_ipsec_spd_interface_dump_t *
557                                            mp)
558 {
559   ipsec_main_t *im = &ipsec_main;
560   vl_api_registration_t *reg;
561   u32 k, v, spd_index;
562
563 #if WITH_LIBSSL > 0
564   reg = vl_api_client_index_to_registration (mp->client_index);
565   if (!reg)
566     return;
567
568   if (mp->spd_index_valid)
569     {
570       spd_index = ntohl (mp->spd_index);
571       /* *INDENT-OFF* */
572       hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
573         if (v == spd_index)
574           send_ipsec_spd_interface_details(reg, v, k, mp->context);
575       }));
576       /* *INDENT-ON* */
577     }
578   else
579     {
580       /* *INDENT-OFF* */
581       hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
582         send_ipsec_spd_interface_details(reg, v, k, mp->context);
583       }));
584       /* *INDENT-ON* */
585     }
586
587 #else
588   clib_warning ("unimplemented");
589 #endif
590 }
591
592 static void
593 vl_api_ipsec_tunnel_if_add_del_t_handler (vl_api_ipsec_tunnel_if_add_del_t *
594                                           mp)
595 {
596   vl_api_ipsec_tunnel_if_add_del_reply_t *rmp;
597   ipsec_main_t *im = &ipsec_main;
598   vnet_main_t *vnm = im->vnet_main;
599   u32 sw_if_index = ~0;
600   ip46_type_t itype;
601   int rv;
602
603 #if WITH_LIBSSL > 0
604   ipsec_add_del_tunnel_args_t tun;
605
606   clib_memset (&tun, 0, sizeof (ipsec_add_del_tunnel_args_t));
607
608   tun.is_add = mp->is_add;
609   tun.esn = mp->esn;
610   tun.anti_replay = mp->anti_replay;
611   tun.local_spi = ntohl (mp->local_spi);
612   tun.remote_spi = ntohl (mp->remote_spi);
613   tun.crypto_alg = mp->crypto_alg;
614   tun.local_crypto_key_len = mp->local_crypto_key_len;
615   tun.remote_crypto_key_len = mp->remote_crypto_key_len;
616   tun.integ_alg = mp->integ_alg;
617   tun.local_integ_key_len = mp->local_integ_key_len;
618   tun.remote_integ_key_len = mp->remote_integ_key_len;
619   tun.udp_encap = mp->udp_encap;
620   tun.tx_table_id = ntohl (mp->tx_table_id);
621   tun.salt = mp->salt;
622   itype = ip_address_decode (&mp->local_ip, &tun.local_ip);
623   itype = ip_address_decode (&mp->remote_ip, &tun.remote_ip);
624   tun.is_ip6 = (IP46_TYPE_IP6 == itype);
625   memcpy (&tun.local_crypto_key, &mp->local_crypto_key,
626           mp->local_crypto_key_len);
627   memcpy (&tun.remote_crypto_key, &mp->remote_crypto_key,
628           mp->remote_crypto_key_len);
629   memcpy (&tun.local_integ_key, &mp->local_integ_key,
630           mp->local_integ_key_len);
631   memcpy (&tun.remote_integ_key, &mp->remote_integ_key,
632           mp->remote_integ_key_len);
633   tun.renumber = mp->renumber;
634   tun.show_instance = ntohl (mp->show_instance);
635
636   rv = ipsec_add_del_tunnel_if_internal (vnm, &tun, &sw_if_index);
637
638 #else
639   rv = VNET_API_ERROR_UNIMPLEMENTED;
640 #endif
641
642   /* *INDENT-OFF* */
643   REPLY_MACRO2 (VL_API_IPSEC_TUNNEL_IF_ADD_DEL_REPLY,
644   ({
645     rmp->sw_if_index = htonl (sw_if_index);
646   }));
647   /* *INDENT-ON* */
648 }
649
650 static void
651 send_ipsec_sa_details (ipsec_sa_t * sa, vl_api_registration_t * reg,
652                        u32 context, u32 sw_if_index)
653 {
654   vl_api_ipsec_sa_details_t *mp;
655
656   mp = vl_msg_api_alloc (sizeof (*mp));
657   clib_memset (mp, 0, sizeof (*mp));
658   mp->_vl_msg_id = ntohs (VL_API_IPSEC_SA_DETAILS);
659   mp->context = context;
660
661   mp->entry.sad_id = htonl (sa->id);
662   mp->entry.spi = htonl (sa->spi);
663   mp->entry.protocol = ipsec_proto_encode (sa->protocol);
664   mp->entry.tx_table_id =
665     htonl (fib_table_get_table_id (sa->tx_fib_index, FIB_PROTOCOL_IP4));
666
667   mp->entry.crypto_algorithm = ipsec_crypto_algo_encode (sa->crypto_alg);
668   ipsec_key_encode (&sa->crypto_key, &mp->entry.crypto_key);
669
670   mp->entry.integrity_algorithm = ipsec_integ_algo_encode (sa->integ_alg);
671   ipsec_key_encode (&sa->integ_key, &mp->entry.integrity_key);
672
673   mp->entry.flags = ipsec_sad_flags_encode (sa);
674
675   if (ipsec_sa_is_set_IS_TUNNEL (sa))
676     {
677       ip_address_encode (&sa->tunnel_src_addr, IP46_TYPE_ANY,
678                          &mp->entry.tunnel_src);
679       ip_address_encode (&sa->tunnel_dst_addr, IP46_TYPE_ANY,
680                          &mp->entry.tunnel_dst);
681     }
682
683   mp->sw_if_index = htonl (sw_if_index);
684   mp->salt = clib_host_to_net_u32 (sa->salt);
685   mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq));
686   mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->last_seq));
687   if (ipsec_sa_is_set_USE_ESN (sa))
688     {
689       mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
690       mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->last_seq_hi));
691     }
692   if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa))
693     mp->replay_window = clib_host_to_net_u64 (sa->replay_window);
694
695   vl_api_send_msg (reg, (u8 *) mp);
696 }
697
698
699 static void
700 vl_api_ipsec_sa_dump_t_handler (vl_api_ipsec_sa_dump_t * mp)
701 {
702   vl_api_registration_t *reg;
703   ipsec_main_t *im = &ipsec_main;
704   vnet_main_t *vnm = im->vnet_main;
705   ipsec_sa_t *sa;
706   ipsec_tunnel_if_t *t;
707   u32 *sa_index_to_tun_if_index = 0;
708
709 #if WITH_LIBSSL > 0
710   reg = vl_api_client_index_to_registration (mp->client_index);
711   if (!reg || pool_elts (im->sad) == 0)
712     return;
713
714   vec_validate_init_empty (sa_index_to_tun_if_index, vec_len (im->sad) - 1,
715                            ~0);
716
717   /* *INDENT-OFF* */
718   pool_foreach (t, im->tunnel_interfaces,
719   ({
720     vnet_hw_interface_t *hi;
721     u32 sw_if_index = ~0;
722
723     hi = vnet_get_hw_interface (vnm, t->hw_if_index);
724     sw_if_index = hi->sw_if_index;
725     sa_index_to_tun_if_index[t->input_sa_index] = sw_if_index;
726     sa_index_to_tun_if_index[t->output_sa_index] = sw_if_index;
727   }));
728
729   pool_foreach (sa, im->sad,
730   ({
731     if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == sa->id)
732       send_ipsec_sa_details (sa, reg, mp->context,
733                              sa_index_to_tun_if_index[sa - im->sad]);
734   }));
735   /* *INDENT-ON* */
736
737   vec_free (sa_index_to_tun_if_index);
738 #else
739   clib_warning ("unimplemented");
740 #endif
741 }
742
743 static void
744 vl_api_ipsec_tunnel_if_set_sa_t_handler (vl_api_ipsec_tunnel_if_set_sa_t * mp)
745 {
746   vl_api_ipsec_tunnel_if_set_sa_reply_t *rmp;
747   ipsec_main_t *im = &ipsec_main;
748   vnet_main_t *vnm = im->vnet_main;
749   vnet_sw_interface_t *sw;
750   int rv;
751
752 #if WITH_LIBSSL > 0
753   sw = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
754
755   rv = ipsec_set_interface_sa (vnm, sw->hw_if_index, ntohl (mp->sa_id),
756                                mp->is_outbound);
757 #else
758   clib_warning ("unimplemented");
759 #endif
760
761   REPLY_MACRO (VL_API_IPSEC_TUNNEL_IF_SET_SA_REPLY);
762 }
763
764 static void
765 vl_api_ipsec_backend_dump_t_handler (vl_api_ipsec_backend_dump_t * mp)
766 {
767   vl_api_registration_t *rp;
768   ipsec_main_t *im = &ipsec_main;
769   u32 context = mp->context;
770
771   rp = vl_api_client_index_to_registration (mp->client_index);
772
773   if (rp == 0)
774     {
775       clib_warning ("Client %d AWOL", mp->client_index);
776       return;
777     }
778
779   ipsec_ah_backend_t *ab;
780   ipsec_esp_backend_t *eb;
781   /* *INDENT-OFF* */
782   pool_foreach (ab, im->ah_backends, {
783     vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
784     clib_memset (mp, 0, sizeof (*mp));
785     mp->_vl_msg_id = ntohs (VL_API_IPSEC_BACKEND_DETAILS);
786     mp->context = context;
787     snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (ab->name),
788               ab->name);
789     mp->protocol = ntohl (IPSEC_API_PROTO_AH);
790     mp->index = ab - im->ah_backends;
791     mp->active = mp->index == im->ah_current_backend ? 1 : 0;
792     vl_api_send_msg (rp, (u8 *)mp);
793   });
794   pool_foreach (eb, im->esp_backends, {
795     vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
796     clib_memset (mp, 0, sizeof (*mp));
797     mp->_vl_msg_id = ntohs (VL_API_IPSEC_BACKEND_DETAILS);
798     mp->context = context;
799     snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (eb->name),
800               eb->name);
801     mp->protocol = ntohl (IPSEC_API_PROTO_ESP);
802     mp->index = eb - im->esp_backends;
803     mp->active = mp->index == im->esp_current_backend ? 1 : 0;
804     vl_api_send_msg (rp, (u8 *)mp);
805   });
806   /* *INDENT-ON* */
807 }
808
809 static void
810 vl_api_ipsec_select_backend_t_handler (vl_api_ipsec_select_backend_t * mp)
811 {
812   ipsec_main_t *im = &ipsec_main;
813   vl_api_ipsec_select_backend_reply_t *rmp;
814   ipsec_protocol_t protocol;
815   int rv = 0;
816   if (pool_elts (im->sad) > 0)
817     {
818       rv = VNET_API_ERROR_INSTANCE_IN_USE;
819       goto done;
820     }
821
822   rv = ipsec_proto_decode (mp->protocol, &protocol);
823
824   if (rv)
825     goto done;
826
827 #if WITH_LIBSSL > 0
828   switch (protocol)
829     {
830     case IPSEC_PROTOCOL_ESP:
831       rv = ipsec_select_esp_backend (im, mp->index);
832       break;
833     case IPSEC_PROTOCOL_AH:
834       rv = ipsec_select_ah_backend (im, mp->index);
835       break;
836     default:
837       rv = VNET_API_ERROR_INVALID_PROTOCOL;
838       break;
839     }
840 #else
841   clib_warning ("unimplemented");       /* FIXME */
842 #endif
843 done:
844   REPLY_MACRO (VL_API_IPSEC_SELECT_BACKEND_REPLY);
845 }
846
847 /*
848  * ipsec_api_hookup
849  * Add vpe's API message handlers to the table.
850  * vlib has already mapped shared memory and
851  * added the client registration handlers.
852  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
853  */
854 #define vl_msg_name_crc_list
855 #include <vnet/vnet_all_api_h.h>
856 #undef vl_msg_name_crc_list
857
858 static void
859 setup_message_id_table (api_main_t * am)
860 {
861 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
862   foreach_vl_msg_name_crc_ipsec;
863 #undef _
864 }
865
866 static clib_error_t *
867 ipsec_api_hookup (vlib_main_t * vm)
868 {
869   api_main_t *am = &api_main;
870
871 #define _(N,n)                                                  \
872     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
873                            vl_api_##n##_t_handler,              \
874                            vl_noop_handler,                     \
875                            vl_api_##n##_t_endian,               \
876                            vl_api_##n##_t_print,                \
877                            sizeof(vl_api_##n##_t), 1);
878   foreach_vpe_api_msg;
879 #undef _
880
881   /*
882    * Set up the (msg_name, crc, message-id) table
883    */
884   setup_message_id_table (am);
885
886   return 0;
887 }
888
889 VLIB_API_INIT_FUNCTION (ipsec_api_hookup);
890
891 /*
892  * fd.io coding-style-patch-verification: ON
893  *
894  * Local Variables:
895  * eval: (c-set-style "gnu")
896  * End:
897  */