ipsec: Coverity warnings
[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 #include <vnet/ipip/ipip.h>
29
30 #include <vnet/vnet_msg_enum.h>
31
32 #if WITH_LIBSSL > 0
33 #include <vnet/ipsec/ipsec.h>
34 #include <vnet/ipsec/ipsec_tun.h>
35 #endif /* IPSEC */
36
37 #define vl_typedefs             /* define message structures */
38 #include <vnet/vnet_all_api_h.h>
39 #undef vl_typedefs
40
41 #define vl_endianfun            /* define message structures */
42 #include <vnet/vnet_all_api_h.h>
43 #undef vl_endianfun
44
45 /* instantiate all the print functions we know about */
46 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
47 #define vl_printfun
48 #include <vnet/vnet_all_api_h.h>
49 #undef vl_printfun
50
51 #include <vlibapi/api_helper_macros.h>
52
53 #define foreach_vpe_api_msg                                     \
54 _(IPSEC_SPD_ADD_DEL, ipsec_spd_add_del)                         \
55 _(IPSEC_INTERFACE_ADD_DEL_SPD, ipsec_interface_add_del_spd)     \
56 _(IPSEC_SPD_ENTRY_ADD_DEL, ipsec_spd_entry_add_del)             \
57 _(IPSEC_SAD_ENTRY_ADD_DEL, ipsec_sad_entry_add_del)             \
58 _(IPSEC_SA_DUMP, ipsec_sa_dump)                                 \
59 _(IPSEC_SPDS_DUMP, ipsec_spds_dump)                             \
60 _(IPSEC_SPD_DUMP, ipsec_spd_dump)                               \
61 _(IPSEC_SPD_INTERFACE_DUMP, ipsec_spd_interface_dump)           \
62 _(IPSEC_TUNNEL_IF_ADD_DEL, ipsec_tunnel_if_add_del)             \
63 _(IPSEC_TUNNEL_IF_SET_SA, ipsec_tunnel_if_set_sa)               \
64 _(IPSEC_SELECT_BACKEND, ipsec_select_backend)                   \
65 _(IPSEC_BACKEND_DUMP, ipsec_backend_dump)                       \
66 _(IPSEC_TUNNEL_PROTECT_UPDATE, ipsec_tunnel_protect_update)     \
67 _(IPSEC_TUNNEL_PROTECT_DEL, ipsec_tunnel_protect_del)           \
68 _(IPSEC_TUNNEL_PROTECT_DUMP, ipsec_tunnel_protect_dump)
69
70 static void
71 vl_api_ipsec_spd_add_del_t_handler (vl_api_ipsec_spd_add_del_t * mp)
72 {
73 #if WITH_LIBSSL == 0
74   clib_warning ("unimplemented");
75 #else
76
77   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
78   vl_api_ipsec_spd_add_del_reply_t *rmp;
79   int rv;
80
81   rv = ipsec_add_del_spd (vm, ntohl (mp->spd_id), mp->is_add);
82
83   REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_REPLY);
84 #endif
85 }
86
87 static void vl_api_ipsec_interface_add_del_spd_t_handler
88   (vl_api_ipsec_interface_add_del_spd_t * mp)
89 {
90   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
91   vl_api_ipsec_interface_add_del_spd_reply_t *rmp;
92   int rv;
93   u32 sw_if_index __attribute__ ((unused));
94   u32 spd_id __attribute__ ((unused));
95
96   sw_if_index = ntohl (mp->sw_if_index);
97   spd_id = ntohl (mp->spd_id);
98
99   VALIDATE_SW_IF_INDEX (mp);
100
101 #if WITH_LIBSSL > 0
102   rv = ipsec_set_interface_spd (vm, sw_if_index, spd_id, mp->is_add);
103 #else
104   rv = VNET_API_ERROR_UNIMPLEMENTED;
105 #endif
106
107   BAD_SW_IF_INDEX_LABEL;
108
109   REPLY_MACRO (VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY);
110 }
111
112 static void vl_api_ipsec_tunnel_protect_update_t_handler
113   (vl_api_ipsec_tunnel_protect_update_t * mp)
114 {
115   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
116   vl_api_ipsec_tunnel_protect_update_reply_t *rmp;
117   u32 sw_if_index, ii, *sa_ins = NULL;
118   int rv;
119
120   sw_if_index = ntohl (mp->tunnel.sw_if_index);
121
122   VALIDATE_SW_IF_INDEX (&(mp->tunnel));
123
124 #if WITH_LIBSSL > 0
125
126   for (ii = 0; ii < mp->tunnel.n_sa_in; ii++)
127     vec_add1 (sa_ins, ntohl (mp->tunnel.sa_in[ii]));
128
129   rv = ipsec_tun_protect_update (sw_if_index,
130                                  ntohl (mp->tunnel.sa_out), sa_ins);
131 #else
132   rv = VNET_API_ERROR_UNIMPLEMENTED;
133 #endif
134
135   BAD_SW_IF_INDEX_LABEL;
136
137   REPLY_MACRO (VL_API_IPSEC_TUNNEL_PROTECT_UPDATE_REPLY);
138 }
139
140 static void vl_api_ipsec_tunnel_protect_del_t_handler
141   (vl_api_ipsec_tunnel_protect_del_t * mp)
142 {
143   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
144   vl_api_ipsec_tunnel_protect_del_reply_t *rmp;
145   int rv;
146   u32 sw_if_index;
147
148   sw_if_index = ntohl (mp->sw_if_index);
149
150   VALIDATE_SW_IF_INDEX (mp);
151
152 #if WITH_LIBSSL > 0
153   rv = ipsec_tun_protect_del (sw_if_index);
154 #else
155   rv = VNET_API_ERROR_UNIMPLEMENTED;
156 #endif
157
158   BAD_SW_IF_INDEX_LABEL;
159
160   REPLY_MACRO (VL_API_IPSEC_TUNNEL_PROTECT_DEL_REPLY);
161 }
162
163 typedef struct ipsec_dump_walk_ctx_t_
164 {
165   vl_api_registration_t *reg;
166   u32 context;
167 } ipsec_dump_walk_ctx_t;
168
169 static walk_rc_t
170 send_ipsec_tunnel_protect_details (index_t itpi, void *arg)
171 {
172   ipsec_dump_walk_ctx_t *ctx = arg;
173   vl_api_ipsec_tunnel_protect_details_t *mp;
174   ipsec_tun_protect_t *itp;
175   u32 sai, ii = 0;
176
177   itp = ipsec_tun_protect_get (itpi);
178
179
180   mp = vl_msg_api_alloc (sizeof (*mp) + (sizeof (u32) * itp->itp_n_sa_in));
181   clib_memset (mp, 0, sizeof (*mp));
182   mp->_vl_msg_id = ntohs (VL_API_IPSEC_TUNNEL_PROTECT_DETAILS);
183   mp->context = ctx->context;
184
185   mp->tun.sw_if_index = htonl (itp->itp_sw_if_index);
186
187   mp->tun.sa_out = htonl (itp->itp_out_sa);
188   mp->tun.n_sa_in = itp->itp_n_sa_in;
189   /* *INDENT-OFF* */
190   FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
191   ({
192     mp->tun.sa_in[ii++] = htonl (sai);
193   }));
194   /* *INDENT-ON* */
195
196   vl_api_send_msg (ctx->reg, (u8 *) mp);
197
198   return (WALK_CONTINUE);
199 }
200
201 static void
202 vl_api_ipsec_tunnel_protect_dump_t_handler (vl_api_ipsec_tunnel_protect_dump_t
203                                             * mp)
204 {
205   vl_api_registration_t *reg;
206   u32 sw_if_index;
207
208 #if WITH_LIBSSL > 0
209   reg = vl_api_client_index_to_registration (mp->client_index);
210   if (!reg)
211     return;
212
213   ipsec_dump_walk_ctx_t ctx = {
214     .reg = reg,
215     .context = mp->context,
216   };
217
218   sw_if_index = ntohl (mp->sw_if_index);
219
220   if (~0 == sw_if_index)
221     {
222       ipsec_tun_protect_walk (send_ipsec_tunnel_protect_details, &ctx);
223     }
224   else
225     {
226       index_t itpi;
227
228       itpi = ipsec_tun_protect_find (sw_if_index);
229
230       if (INDEX_INVALID != itpi)
231         send_ipsec_tunnel_protect_details (itpi, &ctx);
232     }
233 #else
234   clib_warning ("unimplemented");
235 #endif
236 }
237
238 static int
239 ipsec_spd_action_decode (vl_api_ipsec_spd_action_t in,
240                          ipsec_policy_action_t * out)
241 {
242   in = clib_net_to_host_u32 (in);
243
244   switch (in)
245     {
246 #define _(v,f,s) case IPSEC_API_SPD_ACTION_##f: \
247       *out = IPSEC_POLICY_ACTION_##f;              \
248       return (0);
249       foreach_ipsec_policy_action
250 #undef _
251     }
252   return (VNET_API_ERROR_UNIMPLEMENTED);
253 }
254
255 static void vl_api_ipsec_spd_entry_add_del_t_handler
256   (vl_api_ipsec_spd_entry_add_del_t * mp)
257 {
258   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
259   vl_api_ipsec_spd_entry_add_del_reply_t *rmp;
260   ip46_type_t itype;
261   u32 stat_index;
262   int rv;
263
264   stat_index = ~0;
265
266 #if WITH_LIBSSL > 0
267   ipsec_policy_t p;
268
269   clib_memset (&p, 0, sizeof (p));
270
271   p.id = ntohl (mp->entry.spd_id);
272   p.priority = ntohl (mp->entry.priority);
273
274   itype = ip_address_decode (&mp->entry.remote_address_start, &p.raddr.start);
275   ip_address_decode (&mp->entry.remote_address_stop, &p.raddr.stop);
276   ip_address_decode (&mp->entry.local_address_start, &p.laddr.start);
277   ip_address_decode (&mp->entry.local_address_stop, &p.laddr.stop);
278
279   p.is_ipv6 = (itype == IP46_TYPE_IP6);
280
281   p.protocol = mp->entry.protocol;
282   p.rport.start = ntohs (mp->entry.remote_port_start);
283   p.rport.stop = ntohs (mp->entry.remote_port_stop);
284   p.lport.start = ntohs (mp->entry.local_port_start);
285   p.lport.stop = ntohs (mp->entry.local_port_stop);
286
287   rv = ipsec_spd_action_decode (mp->entry.policy, &p.policy);
288
289   if (rv)
290     goto out;
291
292   /* policy action resolve unsupported */
293   if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
294     {
295       clib_warning ("unsupported action: 'resolve'");
296       rv = VNET_API_ERROR_UNIMPLEMENTED;
297       goto out;
298     }
299   p.sa_id = ntohl (mp->entry.sa_id);
300   rv =
301     ipsec_policy_mk_type (mp->entry.is_outbound, p.is_ipv6, p.policy,
302                           &p.type);
303   if (rv)
304     goto out;
305
306   rv = ipsec_add_del_policy (vm, &p, mp->is_add, &stat_index);
307   if (rv)
308     goto out;
309
310 #else
311   rv = VNET_API_ERROR_UNIMPLEMENTED;
312   goto out;
313 #endif
314
315 out:
316   /* *INDENT-OFF* */
317   REPLY_MACRO2 (VL_API_IPSEC_SPD_ENTRY_ADD_DEL_REPLY,
318   ({
319     rmp->stat_index = ntohl(stat_index);
320   }));
321   /* *INDENT-ON* */
322 }
323
324 static int
325 ipsec_proto_decode (vl_api_ipsec_proto_t in, ipsec_protocol_t * out)
326 {
327   in = clib_net_to_host_u32 (in);
328
329   switch (in)
330     {
331     case IPSEC_API_PROTO_ESP:
332       *out = IPSEC_PROTOCOL_ESP;
333       return (0);
334     case IPSEC_API_PROTO_AH:
335       *out = IPSEC_PROTOCOL_AH;
336       return (0);
337     }
338   return (VNET_API_ERROR_INVALID_PROTOCOL);
339 }
340
341 static vl_api_ipsec_proto_t
342 ipsec_proto_encode (ipsec_protocol_t p)
343 {
344   switch (p)
345     {
346     case IPSEC_PROTOCOL_ESP:
347       return clib_host_to_net_u32 (IPSEC_API_PROTO_ESP);
348     case IPSEC_PROTOCOL_AH:
349       return clib_host_to_net_u32 (IPSEC_API_PROTO_AH);
350     }
351   return (VNET_API_ERROR_UNIMPLEMENTED);
352 }
353
354 static int
355 ipsec_crypto_algo_decode (vl_api_ipsec_crypto_alg_t in,
356                           ipsec_crypto_alg_t * out)
357 {
358   in = clib_net_to_host_u32 (in);
359
360   switch (in)
361     {
362 #define _(v,f,s) case IPSEC_API_CRYPTO_ALG_##f: \
363       *out = IPSEC_CRYPTO_ALG_##f;              \
364       return (0);
365       foreach_ipsec_crypto_alg
366 #undef _
367     }
368   return (VNET_API_ERROR_INVALID_ALGORITHM);
369 }
370
371 static vl_api_ipsec_crypto_alg_t
372 ipsec_crypto_algo_encode (ipsec_crypto_alg_t c)
373 {
374   switch (c)
375     {
376 #define _(v,f,s) case IPSEC_CRYPTO_ALG_##f:                     \
377       return clib_host_to_net_u32(IPSEC_API_CRYPTO_ALG_##f);
378       foreach_ipsec_crypto_alg
379 #undef _
380     case IPSEC_CRYPTO_N_ALG:
381       break;
382     }
383   ASSERT (0);
384   return (VNET_API_ERROR_UNIMPLEMENTED);
385 }
386
387 static int
388 ipsec_integ_algo_decode (vl_api_ipsec_integ_alg_t in, ipsec_integ_alg_t * out)
389 {
390   in = clib_net_to_host_u32 (in);
391
392   switch (in)
393     {
394 #define _(v,f,s) case IPSEC_API_INTEG_ALG_##f:  \
395       *out = IPSEC_INTEG_ALG_##f;               \
396       return (0);
397       foreach_ipsec_integ_alg
398 #undef _
399     }
400   return (VNET_API_ERROR_INVALID_ALGORITHM);
401 }
402
403 static vl_api_ipsec_integ_alg_t
404 ipsec_integ_algo_encode (ipsec_integ_alg_t i)
405 {
406   switch (i)
407     {
408 #define _(v,f,s) case IPSEC_INTEG_ALG_##f:                      \
409       return (clib_host_to_net_u32(IPSEC_API_INTEG_ALG_##f));
410       foreach_ipsec_integ_alg
411 #undef _
412     case IPSEC_INTEG_N_ALG:
413       break;
414     }
415   ASSERT (0);
416   return (VNET_API_ERROR_UNIMPLEMENTED);
417 }
418
419 static void
420 ipsec_key_decode (const vl_api_key_t * key, ipsec_key_t * out)
421 {
422   ipsec_mk_key (out, key->data, key->length);
423 }
424
425 static void
426 ipsec_key_encode (const ipsec_key_t * in, vl_api_key_t * out)
427 {
428   out->length = in->len;
429   clib_memcpy (out->data, in->data, out->length);
430 }
431
432 static ipsec_sa_flags_t
433 ipsec_sa_flags_decode (vl_api_ipsec_sad_flags_t in)
434 {
435   ipsec_sa_flags_t flags = IPSEC_SA_FLAG_NONE;
436   in = clib_net_to_host_u32 (in);
437
438   if (in & IPSEC_API_SAD_FLAG_USE_ESN)
439     flags |= IPSEC_SA_FLAG_USE_ESN;
440   if (in & IPSEC_API_SAD_FLAG_USE_ANTI_REPLAY)
441     flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
442   if (in & IPSEC_API_SAD_FLAG_IS_TUNNEL)
443     flags |= IPSEC_SA_FLAG_IS_TUNNEL;
444   if (in & IPSEC_API_SAD_FLAG_IS_TUNNEL_V6)
445     flags |= IPSEC_SA_FLAG_IS_TUNNEL_V6;
446   if (in & IPSEC_API_SAD_FLAG_UDP_ENCAP)
447     flags |= IPSEC_SA_FLAG_UDP_ENCAP;
448
449   return (flags);
450 }
451
452 static vl_api_ipsec_sad_flags_t
453 ipsec_sad_flags_encode (const ipsec_sa_t * sa)
454 {
455   vl_api_ipsec_sad_flags_t flags = IPSEC_API_SAD_FLAG_NONE;
456
457   if (ipsec_sa_is_set_USE_ESN (sa))
458     flags |= IPSEC_API_SAD_FLAG_USE_ESN;
459   if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa))
460     flags |= IPSEC_API_SAD_FLAG_USE_ANTI_REPLAY;
461   if (ipsec_sa_is_set_IS_TUNNEL (sa))
462     flags |= IPSEC_API_SAD_FLAG_IS_TUNNEL;
463   if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa))
464     flags |= IPSEC_API_SAD_FLAG_IS_TUNNEL_V6;
465   if (ipsec_sa_is_set_UDP_ENCAP (sa))
466     flags |= IPSEC_API_SAD_FLAG_UDP_ENCAP;
467
468   return clib_host_to_net_u32 (flags);
469 }
470
471 static void vl_api_ipsec_sad_entry_add_del_t_handler
472   (vl_api_ipsec_sad_entry_add_del_t * mp)
473 {
474   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
475   vl_api_ipsec_sad_entry_add_del_reply_t *rmp;
476   ip46_address_t tun_src = { }, tun_dst =
477   {
478   };
479   ipsec_key_t crypto_key, integ_key;
480   ipsec_crypto_alg_t crypto_alg;
481   ipsec_integ_alg_t integ_alg;
482   ipsec_protocol_t proto;
483   ipsec_sa_flags_t flags;
484   u32 id, spi, sa_index = ~0;
485   int rv;
486
487 #if WITH_LIBSSL > 0
488
489   id = ntohl (mp->entry.sad_id);
490   spi = ntohl (mp->entry.spi);
491
492   rv = ipsec_proto_decode (mp->entry.protocol, &proto);
493
494   if (rv)
495     goto out;
496
497   rv = ipsec_crypto_algo_decode (mp->entry.crypto_algorithm, &crypto_alg);
498
499   if (rv)
500     goto out;
501
502   rv = ipsec_integ_algo_decode (mp->entry.integrity_algorithm, &integ_alg);
503
504   if (rv)
505     goto out;
506
507   ipsec_key_decode (&mp->entry.crypto_key, &crypto_key);
508   ipsec_key_decode (&mp->entry.integrity_key, &integ_key);
509
510   flags = ipsec_sa_flags_decode (mp->entry.flags);
511
512   ip_address_decode (&mp->entry.tunnel_src, &tun_src);
513   ip_address_decode (&mp->entry.tunnel_dst, &tun_dst);
514
515   if (mp->is_add)
516     rv = ipsec_sa_add_and_lock (id, spi, proto,
517                                 crypto_alg, &crypto_key,
518                                 integ_alg, &integ_key, flags,
519                                 0, mp->entry.salt, &tun_src, &tun_dst,
520                                 &sa_index);
521   else
522     rv = ipsec_sa_unlock_id (id);
523
524 #else
525   rv = VNET_API_ERROR_UNIMPLEMENTED;
526 #endif
527
528 out:
529   /* *INDENT-OFF* */
530   REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_DEL_REPLY,
531   {
532     rmp->stat_index = htonl (sa_index);
533   });
534   /* *INDENT-ON* */
535 }
536
537 static void
538 send_ipsec_spds_details (ipsec_spd_t * spd, vl_api_registration_t * reg,
539                          u32 context)
540 {
541   vl_api_ipsec_spds_details_t *mp;
542   u32 n_policies = 0;
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_SPDS_DETAILS);
547   mp->context = context;
548
549   mp->spd_id = htonl (spd->id);
550 #define _(s, n) n_policies += vec_len (spd->policies[IPSEC_SPD_POLICY_##s]);
551   foreach_ipsec_spd_policy_type
552 #undef _
553     mp->npolicies = htonl (n_policies);
554
555   vl_api_send_msg (reg, (u8 *) mp);
556 }
557
558 static void
559 vl_api_ipsec_spds_dump_t_handler (vl_api_ipsec_spds_dump_t * mp)
560 {
561   vl_api_registration_t *reg;
562   ipsec_main_t *im = &ipsec_main;
563   ipsec_spd_t *spd;
564 #if WITH_LIBSSL > 0
565   reg = vl_api_client_index_to_registration (mp->client_index);
566   if (!reg)
567     return;
568
569   /* *INDENT-OFF* */
570   pool_foreach (spd, im->spds, ({
571     send_ipsec_spds_details (spd, reg, mp->context);
572   }));
573   /* *INDENT-ON* */
574 #else
575   clib_warning ("unimplemented");
576 #endif
577 }
578
579 vl_api_ipsec_spd_action_t
580 ipsec_spd_action_encode (ipsec_policy_action_t in)
581 {
582   vl_api_ipsec_spd_action_t out = IPSEC_API_SPD_ACTION_BYPASS;
583
584   switch (in)
585     {
586 #define _(v,f,s) case IPSEC_POLICY_ACTION_##f: \
587       out = IPSEC_API_SPD_ACTION_##f;          \
588       break;
589       foreach_ipsec_policy_action
590 #undef _
591     }
592   return (clib_host_to_net_u32 (out));
593 }
594
595 static void
596 send_ipsec_spd_details (ipsec_policy_t * p, vl_api_registration_t * reg,
597                         u32 context)
598 {
599   vl_api_ipsec_spd_details_t *mp;
600
601   mp = vl_msg_api_alloc (sizeof (*mp));
602   clib_memset (mp, 0, sizeof (*mp));
603   mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_DETAILS);
604   mp->context = context;
605
606   mp->entry.spd_id = htonl (p->id);
607   mp->entry.priority = htonl (p->priority);
608   mp->entry.is_outbound = ((p->type == IPSEC_SPD_POLICY_IP6_OUTBOUND) ||
609                            (p->type == IPSEC_SPD_POLICY_IP4_OUTBOUND));
610
611   ip_address_encode (&p->laddr.start, IP46_TYPE_ANY,
612                      &mp->entry.local_address_start);
613   ip_address_encode (&p->laddr.stop, IP46_TYPE_ANY,
614                      &mp->entry.local_address_stop);
615   ip_address_encode (&p->raddr.start, IP46_TYPE_ANY,
616                      &mp->entry.remote_address_start);
617   ip_address_encode (&p->raddr.stop, IP46_TYPE_ANY,
618                      &mp->entry.remote_address_stop);
619   mp->entry.local_port_start = htons (p->lport.start);
620   mp->entry.local_port_stop = htons (p->lport.stop);
621   mp->entry.remote_port_start = htons (p->rport.start);
622   mp->entry.remote_port_stop = htons (p->rport.stop);
623   mp->entry.protocol = p->protocol;
624   mp->entry.policy = ipsec_spd_action_encode (p->policy);
625   mp->entry.sa_id = htonl (p->sa_id);
626
627   vl_api_send_msg (reg, (u8 *) mp);
628 }
629
630 static void
631 vl_api_ipsec_spd_dump_t_handler (vl_api_ipsec_spd_dump_t * mp)
632 {
633   vl_api_registration_t *reg;
634   ipsec_main_t *im = &ipsec_main;
635   ipsec_spd_policy_type_t ptype;
636   ipsec_policy_t *policy;
637   ipsec_spd_t *spd;
638   uword *p;
639   u32 spd_index, *ii;
640 #if WITH_LIBSSL > 0
641   reg = vl_api_client_index_to_registration (mp->client_index);
642   if (!reg)
643     return;
644
645   p = hash_get (im->spd_index_by_spd_id, ntohl (mp->spd_id));
646   if (!p)
647     return;
648
649   spd_index = p[0];
650   spd = pool_elt_at_index (im->spds, spd_index);
651
652   /* *INDENT-OFF* */
653   FOR_EACH_IPSEC_SPD_POLICY_TYPE(ptype) {
654     vec_foreach(ii, spd->policies[ptype])
655       {
656         policy = pool_elt_at_index(im->policies, *ii);
657
658         if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == policy->sa_id)
659           send_ipsec_spd_details (policy, reg, mp->context);
660       }
661   }
662   /* *INDENT-ON* */
663 #else
664   clib_warning ("unimplemented");
665 #endif
666 }
667
668 static void
669 send_ipsec_spd_interface_details (vl_api_registration_t * reg, u32 spd_index,
670                                   u32 sw_if_index, u32 context)
671 {
672   vl_api_ipsec_spd_interface_details_t *mp;
673
674   mp = vl_msg_api_alloc (sizeof (*mp));
675   clib_memset (mp, 0, sizeof (*mp));
676   mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_INTERFACE_DETAILS);
677   mp->context = context;
678
679   mp->spd_index = htonl (spd_index);
680   mp->sw_if_index = htonl (sw_if_index);
681
682   vl_api_send_msg (reg, (u8 *) mp);
683 }
684
685 static void
686 vl_api_ipsec_spd_interface_dump_t_handler (vl_api_ipsec_spd_interface_dump_t *
687                                            mp)
688 {
689   ipsec_main_t *im = &ipsec_main;
690   vl_api_registration_t *reg;
691   u32 k, v, spd_index;
692
693 #if WITH_LIBSSL > 0
694   reg = vl_api_client_index_to_registration (mp->client_index);
695   if (!reg)
696     return;
697
698   if (mp->spd_index_valid)
699     {
700       spd_index = ntohl (mp->spd_index);
701       /* *INDENT-OFF* */
702       hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
703         if (v == spd_index)
704           send_ipsec_spd_interface_details(reg, v, k, mp->context);
705       }));
706       /* *INDENT-ON* */
707     }
708   else
709     {
710       /* *INDENT-OFF* */
711       hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
712         send_ipsec_spd_interface_details(reg, v, k, mp->context);
713       }));
714       /* *INDENT-ON* */
715     }
716
717 #else
718   clib_warning ("unimplemented");
719 #endif
720 }
721
722 static u32
723 ipsec_tun_mk_input_sa_id (u32 ti)
724 {
725   return (0x80000000 | ti);
726 }
727
728 static u32
729 ipsec_tun_mk_output_sa_id (u32 ti)
730 {
731   return (0xc0000000 | ti);
732 }
733
734 static void
735 vl_api_ipsec_tunnel_if_add_del_t_handler (vl_api_ipsec_tunnel_if_add_del_t *
736                                           mp)
737 {
738   vl_api_ipsec_tunnel_if_add_del_reply_t *rmp;
739   u32 sw_if_index = ~0;
740   int rv;
741
742 #if WITH_LIBSSL > 0
743   ip46_address_t local_ip = ip46_address_initializer;
744   ip46_address_t remote_ip = ip46_address_initializer;
745   ipsec_key_t crypto_key, integ_key;
746   ipsec_sa_flags_t flags;
747   ip46_type_t local_ip_type, remote_ip_type;
748   ipip_transport_t transport;
749   u32 fib_index;
750
751   local_ip_type = ip_address_decode (&mp->local_ip, &local_ip);
752   remote_ip_type = ip_address_decode (&mp->remote_ip, &remote_ip);
753   transport = (IP46_TYPE_IP6 == local_ip_type ?
754                IPIP_TRANSPORT_IP6 : IPIP_TRANSPORT_IP4);
755
756   if (local_ip_type != remote_ip_type)
757     {
758       rv = VNET_API_ERROR_INVALID_VALUE;
759       goto done;
760     }
761
762   flags = IPSEC_SA_FLAG_NONE;
763
764   if (mp->udp_encap)
765     flags |= IPSEC_SA_FLAG_UDP_ENCAP;
766   if (mp->esn)
767     flags |= IPSEC_SA_FLAG_USE_ESN;
768   if (mp->anti_replay)
769     flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
770
771   ipsec_mk_key (&crypto_key, mp->remote_crypto_key,
772                 mp->remote_crypto_key_len);
773   ipsec_mk_key (&integ_key, mp->remote_integ_key, mp->remote_integ_key_len);
774   ipsec_mk_key (&crypto_key, mp->local_crypto_key, mp->local_crypto_key_len);
775   ipsec_mk_key (&integ_key, mp->local_integ_key, mp->local_integ_key_len);
776
777   fib_index =
778     fib_table_find (fib_proto_from_ip46 (local_ip_type),
779                     ntohl (mp->tx_table_id));
780
781   if (~0 == fib_index)
782     {
783       rv = VNET_API_ERROR_NO_SUCH_FIB;
784       goto done;
785     }
786
787   if (mp->is_add)
788     {
789       // remote = input, local = output
790       /* create an ip-ip tunnel, then the two SA, then bind them */
791       rv = ipip_add_tunnel (transport,
792                             ntohl (mp->show_instance),
793                             &local_ip,
794                             &remote_ip, fib_index, 0, &sw_if_index);
795
796       if (rv)
797         goto done;
798
799       rv = ipsec_sa_add_and_lock (ipsec_tun_mk_input_sa_id (sw_if_index),
800                                   ntohl (mp->remote_spi),
801                                   IPSEC_PROTOCOL_ESP,
802                                   mp->crypto_alg,
803                                   &crypto_key,
804                                   mp->integ_alg,
805                                   &integ_key,
806                                   (flags | IPSEC_SA_FLAG_IS_INBOUND),
807                                   ntohl (mp->tx_table_id),
808                                   mp->salt, &remote_ip, &local_ip, NULL);
809
810       if (rv)
811         goto done;
812
813       rv = ipsec_sa_add_and_lock (ipsec_tun_mk_output_sa_id (sw_if_index),
814                                   ntohl (mp->local_spi),
815                                   IPSEC_PROTOCOL_ESP,
816                                   mp->crypto_alg,
817                                   &crypto_key,
818                                   mp->integ_alg,
819                                   &integ_key,
820                                   flags,
821                                   ntohl (mp->tx_table_id),
822                                   mp->salt, &local_ip, &remote_ip, NULL);
823
824       if (rv)
825         goto done;
826
827       rv = ipsec_tun_protect_update_one (sw_if_index,
828                                          ipsec_tun_mk_output_sa_id
829                                          (sw_if_index),
830                                          ipsec_tun_mk_input_sa_id
831                                          (sw_if_index));
832       if (rv)
833         goto done;
834
835       /* the SAs are locked as a result of being used for proection,
836        * they cannot be removed from the API, since they cannot be refered
837        * to by the API. unlock them now, so that if the tunnel is rekeyed
838        * they-ll disapper
839        */
840       ipsec_sa_unlock_id (ipsec_tun_mk_input_sa_id (sw_if_index));
841       ipsec_sa_unlock_id (ipsec_tun_mk_output_sa_id (sw_if_index));
842     }
843   else
844     {
845       /* *INDENT-OFF* */
846       ipip_tunnel_key_t key = {
847         .transport = transport,
848         .fib_index = fib_index,
849         .src = local_ip,
850         .dst = remote_ip
851       };
852       /* *INDENT-ON* */
853
854       ipip_tunnel_t *t = ipip_tunnel_db_find (&key);
855
856       if (NULL != t)
857         {
858           rv = ipsec_tun_protect_del (t->sw_if_index);
859           ipip_del_tunnel (t->sw_if_index);
860         }
861       else
862         rv = VNET_API_ERROR_NO_SUCH_ENTRY;
863     }
864
865 #else
866   rv = VNET_API_ERROR_UNIMPLEMENTED;
867 #endif
868 done:
869   /* *INDENT-OFF* */
870   REPLY_MACRO2 (VL_API_IPSEC_TUNNEL_IF_ADD_DEL_REPLY,
871   ({
872     rmp->sw_if_index = htonl (sw_if_index);
873   }));
874   /* *INDENT-ON* */
875 }
876
877 typedef struct ipsec_sa_dump_match_ctx_t_
878 {
879   index_t sai;
880   u32 sw_if_index;
881 } ipsec_sa_dump_match_ctx_t;
882
883 static walk_rc_t
884 ipsec_sa_dump_match_sa (index_t itpi, void *arg)
885 {
886   ipsec_sa_dump_match_ctx_t *ctx = arg;
887   ipsec_tun_protect_t *itp;
888   index_t sai;
889
890   itp = ipsec_tun_protect_get (itpi);
891
892   if (itp->itp_out_sa == ctx->sai)
893     {
894       ctx->sw_if_index = itp->itp_sw_if_index;
895       return (WALK_STOP);
896     }
897   /* *INDENT-OFF* */
898   FOR_EACH_IPSEC_PROTECT_INPUT_SAI (itp, sai,
899   ({
900     if (sai == ctx->sai)
901       {
902         ctx->sw_if_index = itp->itp_sw_if_index;
903         return (WALK_STOP);
904       }
905   }));
906   /* *INDENT-OFF* */
907
908   return (WALK_CONTINUE);
909 }
910
911 static walk_rc_t
912 send_ipsec_sa_details (ipsec_sa_t * sa, void *arg)
913 {
914   ipsec_dump_walk_ctx_t *ctx = arg;
915   vl_api_ipsec_sa_details_t *mp;
916   ipsec_main_t *im = &ipsec_main;
917
918   mp = vl_msg_api_alloc (sizeof (*mp));
919   clib_memset (mp, 0, sizeof (*mp));
920   mp->_vl_msg_id = ntohs (VL_API_IPSEC_SA_DETAILS);
921   mp->context = ctx->context;
922
923   mp->entry.sad_id = htonl (sa->id);
924   mp->entry.spi = htonl (sa->spi);
925   mp->entry.protocol = ipsec_proto_encode (sa->protocol);
926   mp->entry.tx_table_id =
927     htonl (fib_table_get_table_id (sa->tx_fib_index, FIB_PROTOCOL_IP4));
928
929   mp->entry.crypto_algorithm = ipsec_crypto_algo_encode (sa->crypto_alg);
930   ipsec_key_encode (&sa->crypto_key, &mp->entry.crypto_key);
931
932   mp->entry.integrity_algorithm = ipsec_integ_algo_encode (sa->integ_alg);
933   ipsec_key_encode (&sa->integ_key, &mp->entry.integrity_key);
934
935   mp->entry.flags = ipsec_sad_flags_encode (sa);
936   mp->entry.salt = clib_host_to_net_u32 (sa->salt);
937
938   if (ipsec_sa_is_set_IS_PROTECT (sa))
939     {
940       ipsec_sa_dump_match_ctx_t ctx = {
941         .sai = sa - im->sad,
942         .sw_if_index = ~0,
943       };
944       ipsec_tun_protect_walk (ipsec_sa_dump_match_sa, &ctx);
945
946       mp->sw_if_index = htonl (ctx.sw_if_index);
947     }
948   else
949     mp->sw_if_index = ~0;
950
951   if (ipsec_sa_is_set_IS_TUNNEL (sa))
952     {
953       ip_address_encode (&sa->tunnel_src_addr, IP46_TYPE_ANY,
954                          &mp->entry.tunnel_src);
955       ip_address_encode (&sa->tunnel_dst_addr, IP46_TYPE_ANY,
956                          &mp->entry.tunnel_dst);
957     }
958
959   mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq));
960   mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->last_seq));
961   if (ipsec_sa_is_set_USE_ESN (sa))
962     {
963       mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
964       mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->last_seq_hi));
965     }
966   if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa))
967     mp->replay_window = clib_host_to_net_u64 (sa->replay_window);
968
969   vl_api_send_msg (ctx->reg, (u8 *) mp);
970
971   return (WALK_CONTINUE);
972 }
973
974 static void
975 vl_api_ipsec_sa_dump_t_handler (vl_api_ipsec_sa_dump_t * mp)
976 {
977   vl_api_registration_t *reg;
978
979 #if WITH_LIBSSL > 0
980   reg = vl_api_client_index_to_registration (mp->client_index);
981   if (!reg)
982     return;
983
984   ipsec_dump_walk_ctx_t ctx = {
985     .reg = reg,
986     .context = mp->context,
987   };
988
989   ipsec_sa_walk (send_ipsec_sa_details, &ctx);
990
991 #else
992   clib_warning ("unimplemented");
993 #endif
994 }
995
996 static void
997 vl_api_ipsec_tunnel_if_set_sa_t_handler (vl_api_ipsec_tunnel_if_set_sa_t * mp)
998 {
999   vl_api_ipsec_tunnel_if_set_sa_reply_t *rmp;
1000   int rv;
1001
1002 #if WITH_LIBSSL > 0
1003   VALIDATE_SW_IF_INDEX(mp);
1004
1005   if (mp->is_outbound)
1006     rv = ipsec_tun_protect_update_out (ntohl (mp->sw_if_index),
1007                                        ntohl (mp->sa_id));
1008   else
1009     rv = ipsec_tun_protect_update_in (ntohl (mp->sw_if_index),
1010                                        ntohl (mp->sa_id));
1011
1012 #else
1013   clib_warning ("unimplemented");
1014 #endif
1015
1016   BAD_SW_IF_INDEX_LABEL;
1017
1018   REPLY_MACRO (VL_API_IPSEC_TUNNEL_IF_SET_SA_REPLY);
1019 }
1020
1021 static void
1022 vl_api_ipsec_backend_dump_t_handler (vl_api_ipsec_backend_dump_t * mp)
1023 {
1024   vl_api_registration_t *rp;
1025   ipsec_main_t *im = &ipsec_main;
1026   u32 context = mp->context;
1027
1028   rp = vl_api_client_index_to_registration (mp->client_index);
1029
1030   if (rp == 0)
1031     {
1032       clib_warning ("Client %d AWOL", mp->client_index);
1033       return;
1034     }
1035
1036   ipsec_ah_backend_t *ab;
1037   ipsec_esp_backend_t *eb;
1038   /* *INDENT-OFF* */
1039   pool_foreach (ab, im->ah_backends, {
1040     vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
1041     clib_memset (mp, 0, sizeof (*mp));
1042     mp->_vl_msg_id = ntohs (VL_API_IPSEC_BACKEND_DETAILS);
1043     mp->context = context;
1044     snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (ab->name),
1045               ab->name);
1046     mp->protocol = ntohl (IPSEC_API_PROTO_AH);
1047     mp->index = ab - im->ah_backends;
1048     mp->active = mp->index == im->ah_current_backend ? 1 : 0;
1049     vl_api_send_msg (rp, (u8 *)mp);
1050   });
1051   pool_foreach (eb, im->esp_backends, {
1052     vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
1053     clib_memset (mp, 0, sizeof (*mp));
1054     mp->_vl_msg_id = ntohs (VL_API_IPSEC_BACKEND_DETAILS);
1055     mp->context = context;
1056     snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (eb->name),
1057               eb->name);
1058     mp->protocol = ntohl (IPSEC_API_PROTO_ESP);
1059     mp->index = eb - im->esp_backends;
1060     mp->active = mp->index == im->esp_current_backend ? 1 : 0;
1061     vl_api_send_msg (rp, (u8 *)mp);
1062   });
1063   /* *INDENT-ON* */
1064 }
1065
1066 static void
1067 vl_api_ipsec_select_backend_t_handler (vl_api_ipsec_select_backend_t * mp)
1068 {
1069   ipsec_main_t *im = &ipsec_main;
1070   vl_api_ipsec_select_backend_reply_t *rmp;
1071   ipsec_protocol_t protocol;
1072   int rv = 0;
1073   if (pool_elts (im->sad) > 0)
1074     {
1075       rv = VNET_API_ERROR_INSTANCE_IN_USE;
1076       goto done;
1077     }
1078
1079   rv = ipsec_proto_decode (mp->protocol, &protocol);
1080
1081   if (rv)
1082     goto done;
1083
1084 #if WITH_LIBSSL > 0
1085   switch (protocol)
1086     {
1087     case IPSEC_PROTOCOL_ESP:
1088       rv = ipsec_select_esp_backend (im, mp->index);
1089       break;
1090     case IPSEC_PROTOCOL_AH:
1091       rv = ipsec_select_ah_backend (im, mp->index);
1092       break;
1093     default:
1094       rv = VNET_API_ERROR_INVALID_PROTOCOL;
1095       break;
1096     }
1097 #else
1098   clib_warning ("unimplemented");       /* FIXME */
1099 #endif
1100 done:
1101   REPLY_MACRO (VL_API_IPSEC_SELECT_BACKEND_REPLY);
1102 }
1103
1104 /*
1105  * ipsec_api_hookup
1106  * Add vpe's API message handlers to the table.
1107  * vlib has already mapped shared memory and
1108  * added the client registration handlers.
1109  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
1110  */
1111 #define vl_msg_name_crc_list
1112 #include <vnet/vnet_all_api_h.h>
1113 #undef vl_msg_name_crc_list
1114
1115 static void
1116 setup_message_id_table (api_main_t * am)
1117 {
1118 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1119   foreach_vl_msg_name_crc_ipsec;
1120 #undef _
1121 }
1122
1123 static clib_error_t *
1124 ipsec_api_hookup (vlib_main_t * vm)
1125 {
1126   api_main_t *am = &api_main;
1127
1128 #define _(N,n)                                                  \
1129     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
1130                            vl_api_##n##_t_handler,              \
1131                            vl_noop_handler,                     \
1132                            vl_api_##n##_t_endian,               \
1133                            vl_api_##n##_t_print,                \
1134                            sizeof(vl_api_##n##_t), 1);
1135   foreach_vpe_api_msg;
1136 #undef _
1137
1138   /*
1139    * Adding and deleting SAs is MP safe since when they are added/delete
1140    * no traffic is using them
1141    */
1142   am->is_mp_safe[VL_API_IPSEC_SAD_ENTRY_ADD_DEL] = 1;
1143   am->is_mp_safe[VL_API_IPSEC_SAD_ENTRY_ADD_DEL_REPLY] = 1;
1144
1145   /*
1146    * Set up the (msg_name, crc, message-id) table
1147    */
1148   setup_message_id_table (am);
1149
1150   return 0;
1151 }
1152
1153 VLIB_API_INIT_FUNCTION (ipsec_api_hookup);
1154
1155 /*
1156  * fd.io coding-style-patch-verification: ON
1157  *
1158  * Local Variables:
1159  * eval: (c-set-style "gnu")
1160  * End:
1161  */