ipsec: Support the single IPSec interface dump
[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/ipsec/ipsec_types_api.h>
28 #include <vnet/tunnel/tunnel_types_api.h>
29 #include <vnet/fib/fib.h>
30 #include <vnet/ipip/ipip.h>
31 #include <vnet/tunnel/tunnel_types_api.h>
32 #include <vnet/ipsec/ipsec.h>
33 #include <vnet/ipsec/ipsec_tun.h>
34 #include <vnet/ipsec/ipsec_itf.h>
35
36 #include <vnet/format_fns.h>
37 #include <vnet/ipsec/ipsec.api_enum.h>
38 #include <vnet/ipsec/ipsec.api_types.h>
39
40 #define REPLY_MSG_ID_BASE ipsec_main.msg_id_base
41 #include <vlibapi/api_helper_macros.h>
42
43 static void
44 vl_api_ipsec_spd_add_del_t_handler (vl_api_ipsec_spd_add_del_t * mp)
45 {
46   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
47   vl_api_ipsec_spd_add_del_reply_t *rmp;
48   int rv;
49
50   rv = ipsec_add_del_spd (vm, ntohl (mp->spd_id), mp->is_add);
51
52   REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_REPLY);
53 }
54
55 static void vl_api_ipsec_interface_add_del_spd_t_handler
56   (vl_api_ipsec_interface_add_del_spd_t * mp)
57 {
58   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
59   vl_api_ipsec_interface_add_del_spd_reply_t *rmp;
60   int rv;
61   u32 sw_if_index __attribute__ ((unused));
62   u32 spd_id __attribute__ ((unused));
63
64   sw_if_index = ntohl (mp->sw_if_index);
65   spd_id = ntohl (mp->spd_id);
66
67   VALIDATE_SW_IF_INDEX (mp);
68
69   rv = ipsec_set_interface_spd (vm, sw_if_index, spd_id, mp->is_add);
70
71   BAD_SW_IF_INDEX_LABEL;
72
73   REPLY_MACRO (VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY);
74 }
75
76 static void vl_api_ipsec_tunnel_protect_update_t_handler
77   (vl_api_ipsec_tunnel_protect_update_t * mp)
78 {
79   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
80   vl_api_ipsec_tunnel_protect_update_reply_t *rmp;
81   u32 sw_if_index, ii, *sa_ins = NULL;
82   ip_address_t nh;
83   int rv;
84
85   sw_if_index = ntohl (mp->tunnel.sw_if_index);
86
87   VALIDATE_SW_IF_INDEX (&(mp->tunnel));
88
89   for (ii = 0; ii < mp->tunnel.n_sa_in; ii++)
90     vec_add1 (sa_ins, ntohl (mp->tunnel.sa_in[ii]));
91
92   ip_address_decode2 (&mp->tunnel.nh, &nh);
93
94   rv = ipsec_tun_protect_update (sw_if_index, &nh,
95                                  ntohl (mp->tunnel.sa_out), sa_ins);
96
97   BAD_SW_IF_INDEX_LABEL;
98
99   REPLY_MACRO (VL_API_IPSEC_TUNNEL_PROTECT_UPDATE_REPLY);
100 }
101
102 static void vl_api_ipsec_tunnel_protect_del_t_handler
103   (vl_api_ipsec_tunnel_protect_del_t * mp)
104 {
105   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
106   vl_api_ipsec_tunnel_protect_del_reply_t *rmp;
107   ip_address_t nh;
108   u32 sw_if_index;
109   int rv;
110
111   sw_if_index = ntohl (mp->sw_if_index);
112
113   VALIDATE_SW_IF_INDEX (mp);
114
115   ip_address_decode2 (&mp->nh, &nh);
116   rv = ipsec_tun_protect_del (sw_if_index, &nh);
117
118   BAD_SW_IF_INDEX_LABEL;
119
120   REPLY_MACRO (VL_API_IPSEC_TUNNEL_PROTECT_DEL_REPLY);
121 }
122
123 typedef struct ipsec_dump_walk_ctx_t_
124 {
125   vl_api_registration_t *reg;
126   u32 context;
127   u32 sw_if_index;
128 } ipsec_dump_walk_ctx_t;
129
130 static walk_rc_t
131 send_ipsec_tunnel_protect_details (index_t itpi, void *arg)
132 {
133   ipsec_dump_walk_ctx_t *ctx = arg;
134   vl_api_ipsec_tunnel_protect_details_t *mp;
135   ipsec_tun_protect_t *itp;
136   u32 ii = 0;
137   ipsec_sa_t *sa;
138
139   itp = ipsec_tun_protect_get (itpi);
140
141   mp = vl_msg_api_alloc (sizeof (*mp) + (sizeof (u32) * itp->itp_n_sa_in));
142   clib_memset (mp, 0, sizeof (*mp));
143   mp->_vl_msg_id =
144     ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_TUNNEL_PROTECT_DETAILS);
145   mp->context = ctx->context;
146
147   mp->tun.sw_if_index = htonl (itp->itp_sw_if_index);
148   ip_address_encode2 (itp->itp_key, &mp->tun.nh);
149
150   sa = ipsec_sa_get (itp->itp_out_sa);
151   mp->tun.sa_out = htonl (sa->id);
152   mp->tun.n_sa_in = itp->itp_n_sa_in;
153   /* *INDENT-OFF* */
154   FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
155   ({
156     mp->tun.sa_in[ii++] = htonl (sa->id);
157   }));
158   /* *INDENT-ON* */
159
160   vl_api_send_msg (ctx->reg, (u8 *) mp);
161
162   return (WALK_CONTINUE);
163 }
164
165 static void
166 vl_api_ipsec_tunnel_protect_dump_t_handler (vl_api_ipsec_tunnel_protect_dump_t
167                                             * mp)
168 {
169   vl_api_registration_t *reg;
170   u32 sw_if_index;
171
172   reg = vl_api_client_index_to_registration (mp->client_index);
173   if (!reg)
174     return;
175
176   ipsec_dump_walk_ctx_t ctx = {
177     .reg = reg,
178     .context = mp->context,
179   };
180
181   sw_if_index = ntohl (mp->sw_if_index);
182
183   if (~0 == sw_if_index)
184     {
185       ipsec_tun_protect_walk (send_ipsec_tunnel_protect_details, &ctx);
186     }
187   else
188     {
189       ipsec_tun_protect_walk_itf (sw_if_index,
190                                   send_ipsec_tunnel_protect_details, &ctx);
191     }
192 }
193
194 static int
195 ipsec_spd_action_decode (vl_api_ipsec_spd_action_t in,
196                          ipsec_policy_action_t * out)
197 {
198   in = clib_net_to_host_u32 (in);
199
200   switch (in)
201     {
202 #define _(v,f,s) case IPSEC_API_SPD_ACTION_##f: \
203       *out = IPSEC_POLICY_ACTION_##f;              \
204       return (0);
205       foreach_ipsec_policy_action
206 #undef _
207     }
208   return (VNET_API_ERROR_UNIMPLEMENTED);
209 }
210
211 static void vl_api_ipsec_spd_entry_add_del_t_handler
212   (vl_api_ipsec_spd_entry_add_del_t * mp)
213 {
214   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
215   vl_api_ipsec_spd_entry_add_del_reply_t *rmp;
216   ip46_type_t itype;
217   u32 stat_index;
218   int rv;
219
220   stat_index = ~0;
221
222   ipsec_policy_t p;
223
224   clib_memset (&p, 0, sizeof (p));
225
226   p.id = ntohl (mp->entry.spd_id);
227   p.priority = ntohl (mp->entry.priority);
228
229   itype = ip_address_decode (&mp->entry.remote_address_start, &p.raddr.start);
230   ip_address_decode (&mp->entry.remote_address_stop, &p.raddr.stop);
231   ip_address_decode (&mp->entry.local_address_start, &p.laddr.start);
232   ip_address_decode (&mp->entry.local_address_stop, &p.laddr.stop);
233
234   p.is_ipv6 = (itype == IP46_TYPE_IP6);
235
236   p.protocol = mp->entry.protocol;
237   p.rport.start = ntohs (mp->entry.remote_port_start);
238   p.rport.stop = ntohs (mp->entry.remote_port_stop);
239   p.lport.start = ntohs (mp->entry.local_port_start);
240   p.lport.stop = ntohs (mp->entry.local_port_stop);
241
242   rv = ipsec_spd_action_decode (mp->entry.policy, &p.policy);
243
244   if (rv)
245     goto out;
246
247   /* policy action resolve unsupported */
248   if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
249     {
250       clib_warning ("unsupported action: 'resolve'");
251       rv = VNET_API_ERROR_UNIMPLEMENTED;
252       goto out;
253     }
254   p.sa_id = ntohl (mp->entry.sa_id);
255   rv =
256     ipsec_policy_mk_type (mp->entry.is_outbound, p.is_ipv6, p.policy,
257                           &p.type);
258   if (rv)
259     goto out;
260
261   rv = ipsec_add_del_policy (vm, &p, mp->is_add, &stat_index);
262   if (rv)
263     goto out;
264
265 out:
266   /* *INDENT-OFF* */
267   REPLY_MACRO2 (VL_API_IPSEC_SPD_ENTRY_ADD_DEL_REPLY,
268   ({
269     rmp->stat_index = ntohl(stat_index);
270   }));
271   /* *INDENT-ON* */
272 }
273
274 static void vl_api_ipsec_sad_entry_add_del_t_handler
275   (vl_api_ipsec_sad_entry_add_del_t * mp)
276 {
277   vl_api_ipsec_sad_entry_add_del_reply_t *rmp;
278   ipsec_key_t crypto_key, integ_key;
279   ipsec_crypto_alg_t crypto_alg;
280   ipsec_integ_alg_t integ_alg;
281   ipsec_protocol_t proto;
282   ipsec_sa_flags_t flags;
283   u32 id, spi, sa_index = ~0;
284   tunnel_t tun = {
285     .t_flags = TUNNEL_FLAG_NONE,
286     .t_encap_decap_flags = TUNNEL_ENCAP_DECAP_FLAG_NONE,
287     .t_dscp = 0,
288     .t_mode = TUNNEL_MODE_P2P,
289     .t_table_id = 0,
290     .t_hop_limit = 255,
291   };
292   int rv;
293
294   id = ntohl (mp->entry.sad_id);
295   if (!mp->is_add)
296     {
297       rv = ipsec_sa_unlock_id (id);
298       goto out;
299     }
300   spi = ntohl (mp->entry.spi);
301
302   rv = ipsec_proto_decode (mp->entry.protocol, &proto);
303
304   if (rv)
305     goto out;
306
307   rv = ipsec_crypto_algo_decode (mp->entry.crypto_algorithm, &crypto_alg);
308
309   if (rv)
310     goto out;
311
312   rv = ipsec_integ_algo_decode (mp->entry.integrity_algorithm, &integ_alg);
313
314   if (rv)
315     goto out;
316
317   ipsec_key_decode (&mp->entry.crypto_key, &crypto_key);
318   ipsec_key_decode (&mp->entry.integrity_key, &integ_key);
319
320   flags = ipsec_sa_flags_decode (mp->entry.flags);
321
322   ip_address_decode2 (&mp->entry.tunnel_src, &tun.t_src);
323   ip_address_decode2 (&mp->entry.tunnel_dst, &tun.t_dst);
324
325   rv = ipsec_sa_add_and_lock (id, spi, proto, crypto_alg, &crypto_key,
326                               integ_alg, &integ_key, flags, mp->entry.salt,
327                               htons (mp->entry.udp_src_port),
328                               htons (mp->entry.udp_dst_port), &tun, &sa_index);
329
330 out:
331   /* *INDENT-OFF* */
332   REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_DEL_REPLY,
333   {
334     rmp->stat_index = htonl (sa_index);
335   });
336   /* *INDENT-ON* */
337 }
338
339 static void vl_api_ipsec_sad_entry_add_del_v2_t_handler
340   (vl_api_ipsec_sad_entry_add_del_v2_t * mp)
341 {
342   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
343   vl_api_ipsec_sad_entry_add_del_v2_reply_t *rmp;
344   ipsec_key_t crypto_key, integ_key;
345   ipsec_crypto_alg_t crypto_alg;
346   ipsec_integ_alg_t integ_alg;
347   ipsec_protocol_t proto;
348   ipsec_sa_flags_t flags;
349   u32 id, spi, sa_index = ~0;
350   int rv;
351   tunnel_t tun = {
352     .t_flags = TUNNEL_FLAG_NONE,
353     .t_encap_decap_flags = TUNNEL_ENCAP_DECAP_FLAG_NONE,
354     .t_dscp = 0,
355     .t_mode = TUNNEL_MODE_P2P,
356     .t_table_id = htonl (mp->entry.tx_table_id),
357     .t_hop_limit = 255,
358   };
359
360   id = ntohl (mp->entry.sad_id);
361   if (!mp->is_add)
362     {
363       rv = ipsec_sa_unlock_id (id);
364       goto out;
365     }
366
367   spi = ntohl (mp->entry.spi);
368
369   rv = ipsec_proto_decode (mp->entry.protocol, &proto);
370
371   if (rv)
372     goto out;
373
374   rv = ipsec_crypto_algo_decode (mp->entry.crypto_algorithm, &crypto_alg);
375
376   if (rv)
377     goto out;
378
379   rv = ipsec_integ_algo_decode (mp->entry.integrity_algorithm, &integ_alg);
380
381   if (rv)
382     goto out;
383
384   rv = tunnel_encap_decap_flags_decode (mp->entry.tunnel_flags,
385                                         &tun.t_encap_decap_flags);
386
387   if (rv)
388     goto out;
389
390   ipsec_key_decode (&mp->entry.crypto_key, &crypto_key);
391   ipsec_key_decode (&mp->entry.integrity_key, &integ_key);
392
393   flags = ipsec_sa_flags_decode (mp->entry.flags);
394   tun.t_dscp = ip_dscp_decode (mp->entry.dscp);
395
396   ip_address_decode2 (&mp->entry.tunnel_src, &tun.t_src);
397   ip_address_decode2 (&mp->entry.tunnel_dst, &tun.t_dst);
398
399     rv = ipsec_sa_add_and_lock (
400       id, spi, proto, crypto_alg, &crypto_key, integ_alg, &integ_key, flags,
401       mp->entry.salt, htons (mp->entry.udp_src_port),
402       htons (mp->entry.udp_dst_port), &tun, &sa_index);
403
404 out:
405   /* *INDENT-OFF* */
406   REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_DEL_V2_REPLY,
407   {
408     rmp->stat_index = htonl (sa_index);
409   });
410   /* *INDENT-ON* */
411 }
412
413 static int
414 ipsec_sad_entry_add_v3 (const vl_api_ipsec_sad_entry_v3_t *entry,
415                         u32 *sa_index)
416 {
417   ipsec_key_t crypto_key, integ_key;
418   ipsec_crypto_alg_t crypto_alg;
419   ipsec_integ_alg_t integ_alg;
420   ipsec_protocol_t proto;
421   ipsec_sa_flags_t flags;
422   u32 id, spi;
423   tunnel_t tun;
424   int rv;
425
426   id = ntohl (entry->sad_id);
427   spi = ntohl (entry->spi);
428
429   rv = ipsec_proto_decode (entry->protocol, &proto);
430
431   if (rv)
432     return (rv);
433
434   rv = ipsec_crypto_algo_decode (entry->crypto_algorithm, &crypto_alg);
435
436   if (rv)
437     return (rv);
438
439   rv = ipsec_integ_algo_decode (entry->integrity_algorithm, &integ_alg);
440
441   if (rv)
442     return (rv);
443
444   flags = ipsec_sa_flags_decode (entry->flags);
445
446   if (flags & IPSEC_SA_FLAG_IS_TUNNEL)
447     {
448       rv = tunnel_decode (&entry->tunnel, &tun);
449
450       if (rv)
451         return (rv);
452     }
453
454   ipsec_key_decode (&entry->crypto_key, &crypto_key);
455   ipsec_key_decode (&entry->integrity_key, &integ_key);
456
457   return ipsec_sa_add_and_lock (id, spi, proto, crypto_alg, &crypto_key,
458                                 integ_alg, &integ_key, flags, entry->salt,
459                                 htons (entry->udp_src_port),
460                                 htons (entry->udp_dst_port), &tun, sa_index);
461 }
462
463 static void
464 vl_api_ipsec_sad_entry_add_del_v3_t_handler (
465   vl_api_ipsec_sad_entry_add_del_v3_t *mp)
466 {
467   vl_api_ipsec_sad_entry_add_del_v3_reply_t *rmp;
468   u32 id, sa_index = ~0;
469   int rv;
470
471   id = ntohl (mp->entry.sad_id);
472
473   if (!mp->is_add)
474     {
475       rv = ipsec_sa_unlock_id (id);
476     }
477   else
478     {
479       rv = ipsec_sad_entry_add_v3 (&mp->entry, &sa_index);
480     }
481
482   REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_DEL_V3_REPLY,
483                 { rmp->stat_index = htonl (sa_index); });
484 }
485
486 static void
487 vl_api_ipsec_sad_entry_del_t_handler (vl_api_ipsec_sad_entry_del_t *mp)
488 {
489   vl_api_ipsec_sad_entry_del_reply_t *rmp;
490   int rv;
491
492   rv = ipsec_sa_unlock_id (ntohl (mp->id));
493
494   REPLY_MACRO (VL_API_IPSEC_SAD_ENTRY_DEL_REPLY);
495 }
496
497 static void
498 vl_api_ipsec_sad_entry_add_t_handler (vl_api_ipsec_sad_entry_add_t *mp)
499 {
500   vl_api_ipsec_sad_entry_add_reply_t *rmp;
501   u32 sa_index = ~0;
502   int rv;
503
504   rv = ipsec_sad_entry_add_v3 (&mp->entry, &sa_index);
505
506   REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_REPLY,
507                 { rmp->stat_index = htonl (sa_index); });
508 }
509
510 static void
511 send_ipsec_spds_details (ipsec_spd_t * spd, vl_api_registration_t * reg,
512                          u32 context)
513 {
514   vl_api_ipsec_spds_details_t *mp;
515   u32 n_policies = 0;
516
517   mp = vl_msg_api_alloc (sizeof (*mp));
518   clib_memset (mp, 0, sizeof (*mp));
519   mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SPDS_DETAILS);
520   mp->context = context;
521
522   mp->spd_id = htonl (spd->id);
523 #define _(s, n) n_policies += vec_len (spd->policies[IPSEC_SPD_POLICY_##s]);
524   foreach_ipsec_spd_policy_type
525 #undef _
526     mp->npolicies = htonl (n_policies);
527
528   vl_api_send_msg (reg, (u8 *) mp);
529 }
530
531 static void
532 vl_api_ipsec_spds_dump_t_handler (vl_api_ipsec_spds_dump_t * mp)
533 {
534   vl_api_registration_t *reg;
535   ipsec_main_t *im = &ipsec_main;
536   ipsec_spd_t *spd;
537
538   reg = vl_api_client_index_to_registration (mp->client_index);
539   if (!reg)
540     return;
541
542   pool_foreach (spd, im->spds)  {
543     send_ipsec_spds_details (spd, reg, mp->context);
544   }
545 }
546
547 vl_api_ipsec_spd_action_t
548 ipsec_spd_action_encode (ipsec_policy_action_t in)
549 {
550   vl_api_ipsec_spd_action_t out = IPSEC_API_SPD_ACTION_BYPASS;
551
552   switch (in)
553     {
554 #define _(v,f,s) case IPSEC_POLICY_ACTION_##f: \
555       out = IPSEC_API_SPD_ACTION_##f;          \
556       break;
557       foreach_ipsec_policy_action
558 #undef _
559     }
560   return (clib_host_to_net_u32 (out));
561 }
562
563 static void
564 send_ipsec_spd_details (ipsec_policy_t * p, vl_api_registration_t * reg,
565                         u32 context)
566 {
567   vl_api_ipsec_spd_details_t *mp;
568
569   mp = vl_msg_api_alloc (sizeof (*mp));
570   clib_memset (mp, 0, sizeof (*mp));
571   mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SPD_DETAILS);
572   mp->context = context;
573
574   mp->entry.spd_id = htonl (p->id);
575   mp->entry.priority = htonl (p->priority);
576   mp->entry.is_outbound = ((p->type == IPSEC_SPD_POLICY_IP6_OUTBOUND) ||
577                            (p->type == IPSEC_SPD_POLICY_IP4_OUTBOUND));
578
579   ip_address_encode (&p->laddr.start, IP46_TYPE_ANY,
580                      &mp->entry.local_address_start);
581   ip_address_encode (&p->laddr.stop, IP46_TYPE_ANY,
582                      &mp->entry.local_address_stop);
583   ip_address_encode (&p->raddr.start, IP46_TYPE_ANY,
584                      &mp->entry.remote_address_start);
585   ip_address_encode (&p->raddr.stop, IP46_TYPE_ANY,
586                      &mp->entry.remote_address_stop);
587   mp->entry.local_port_start = htons (p->lport.start);
588   mp->entry.local_port_stop = htons (p->lport.stop);
589   mp->entry.remote_port_start = htons (p->rport.start);
590   mp->entry.remote_port_stop = htons (p->rport.stop);
591   mp->entry.protocol = p->protocol;
592   mp->entry.policy = ipsec_spd_action_encode (p->policy);
593   mp->entry.sa_id = htonl (p->sa_id);
594
595   vl_api_send_msg (reg, (u8 *) mp);
596 }
597
598 static void
599 vl_api_ipsec_spd_dump_t_handler (vl_api_ipsec_spd_dump_t * mp)
600 {
601   vl_api_registration_t *reg;
602   ipsec_main_t *im = &ipsec_main;
603   ipsec_spd_policy_type_t ptype;
604   ipsec_policy_t *policy;
605   ipsec_spd_t *spd;
606   uword *p;
607   u32 spd_index, *ii;
608
609   reg = vl_api_client_index_to_registration (mp->client_index);
610   if (!reg)
611     return;
612
613   p = hash_get (im->spd_index_by_spd_id, ntohl (mp->spd_id));
614   if (!p)
615     return;
616
617   spd_index = p[0];
618   spd = pool_elt_at_index (im->spds, spd_index);
619
620   FOR_EACH_IPSEC_SPD_POLICY_TYPE(ptype) {
621     vec_foreach(ii, spd->policies[ptype])
622       {
623         policy = pool_elt_at_index(im->policies, *ii);
624
625         if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == policy->sa_id)
626           send_ipsec_spd_details (policy, reg, mp->context);
627       }
628   }
629 }
630
631 static void
632 send_ipsec_spd_interface_details (vl_api_registration_t * reg, u32 spd_index,
633                                   u32 sw_if_index, u32 context)
634 {
635   vl_api_ipsec_spd_interface_details_t *mp;
636
637   mp = vl_msg_api_alloc (sizeof (*mp));
638   clib_memset (mp, 0, sizeof (*mp));
639   mp->_vl_msg_id =
640     ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SPD_INTERFACE_DETAILS);
641   mp->context = context;
642
643   mp->spd_index = htonl (spd_index);
644   mp->sw_if_index = htonl (sw_if_index);
645
646   vl_api_send_msg (reg, (u8 *) mp);
647 }
648
649 static void
650 vl_api_ipsec_spd_interface_dump_t_handler (vl_api_ipsec_spd_interface_dump_t *
651                                            mp)
652 {
653   ipsec_main_t *im = &ipsec_main;
654   vl_api_registration_t *reg;
655   u32 k, v, spd_index;
656
657   reg = vl_api_client_index_to_registration (mp->client_index);
658   if (!reg)
659     return;
660
661   if (mp->spd_index_valid)
662     {
663       spd_index = ntohl (mp->spd_index);
664       /* *INDENT-OFF* */
665       hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
666         if (v == spd_index)
667           send_ipsec_spd_interface_details(reg, v, k, mp->context);
668       }));
669       /* *INDENT-ON* */
670     }
671   else
672     {
673       hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
674         send_ipsec_spd_interface_details(reg, v, k, mp->context);
675       }));
676     }
677 }
678
679 static void
680 vl_api_ipsec_itf_create_t_handler (vl_api_ipsec_itf_create_t * mp)
681 {
682   vl_api_ipsec_itf_create_reply_t *rmp;
683   tunnel_mode_t mode;
684   u32 sw_if_index = ~0;
685   int rv;
686
687   rv = tunnel_mode_decode (mp->itf.mode, &mode);
688
689   if (!rv)
690     rv = ipsec_itf_create (ntohl (mp->itf.user_instance), mode, &sw_if_index);
691
692   /* *INDENT-OFF* */
693   REPLY_MACRO2 (VL_API_IPSEC_ITF_CREATE_REPLY,
694   ({
695     rmp->sw_if_index = htonl (sw_if_index);
696   }));
697   /* *INDENT-ON* */
698 }
699
700 static void
701 vl_api_ipsec_itf_delete_t_handler (vl_api_ipsec_itf_delete_t * mp)
702 {
703   vl_api_ipsec_itf_delete_reply_t *rmp;
704   int rv;
705
706   rv = ipsec_itf_delete (ntohl (mp->sw_if_index));
707
708   REPLY_MACRO (VL_API_IPSEC_ITF_DELETE_REPLY);
709 }
710
711 static walk_rc_t
712 send_ipsec_itf_details (ipsec_itf_t *itf, void *arg)
713 {
714   ipsec_dump_walk_ctx_t *ctx = arg;
715   vl_api_ipsec_itf_details_t *mp;
716
717   if (~0 != ctx->sw_if_index && ctx->sw_if_index != itf->ii_sw_if_index)
718     return (WALK_CONTINUE);
719
720   mp = vl_msg_api_alloc (sizeof (*mp));
721   clib_memset (mp, 0, sizeof (*mp));
722   mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_ITF_DETAILS);
723   mp->context = ctx->context;
724
725   mp->itf.mode = tunnel_mode_encode (itf->ii_mode);
726   mp->itf.user_instance = htonl (itf->ii_user_instance);
727   mp->itf.sw_if_index = htonl (itf->ii_sw_if_index);
728   vl_api_send_msg (ctx->reg, (u8 *) mp);
729
730   return (WALK_CONTINUE);
731 }
732
733 static void
734 vl_api_ipsec_itf_dump_t_handler (vl_api_ipsec_itf_dump_t * mp)
735 {
736   vl_api_registration_t *reg;
737
738   reg = vl_api_client_index_to_registration (mp->client_index);
739   if (!reg)
740     return;
741
742   ipsec_dump_walk_ctx_t ctx = {
743     .reg = reg,
744     .context = mp->context,
745     .sw_if_index = ntohl (mp->sw_if_index),
746   };
747
748   ipsec_itf_walk (send_ipsec_itf_details, &ctx);
749 }
750
751 typedef struct ipsec_sa_dump_match_ctx_t_
752 {
753   index_t sai;
754   u32 sw_if_index;
755 } ipsec_sa_dump_match_ctx_t;
756
757 static walk_rc_t
758 ipsec_sa_dump_match_sa (index_t itpi, void *arg)
759 {
760   ipsec_sa_dump_match_ctx_t *ctx = arg;
761   ipsec_tun_protect_t *itp;
762   index_t sai;
763
764   itp = ipsec_tun_protect_get (itpi);
765
766   if (itp->itp_out_sa == ctx->sai)
767     {
768       ctx->sw_if_index = itp->itp_sw_if_index;
769       return (WALK_STOP);
770     }
771
772   FOR_EACH_IPSEC_PROTECT_INPUT_SAI (itp, sai,
773   ({
774     if (sai == ctx->sai)
775       {
776         ctx->sw_if_index = itp->itp_sw_if_index;
777         return (WALK_STOP);
778       }
779   }));
780
781   return (WALK_CONTINUE);
782 }
783
784 static walk_rc_t
785 send_ipsec_sa_details (ipsec_sa_t * sa, void *arg)
786 {
787   ipsec_dump_walk_ctx_t *ctx = arg;
788   vl_api_ipsec_sa_details_t *mp;
789
790   mp = vl_msg_api_alloc (sizeof (*mp));
791   clib_memset (mp, 0, sizeof (*mp));
792   mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SA_DETAILS);
793   mp->context = ctx->context;
794
795   mp->entry.sad_id = htonl (sa->id);
796   mp->entry.spi = htonl (sa->spi);
797   mp->entry.protocol = ipsec_proto_encode (sa->protocol);
798   mp->entry.tx_table_id = htonl (sa->tunnel.t_table_id);
799
800   mp->entry.crypto_algorithm = ipsec_crypto_algo_encode (sa->crypto_alg);
801   ipsec_key_encode (&sa->crypto_key, &mp->entry.crypto_key);
802
803   mp->entry.integrity_algorithm = ipsec_integ_algo_encode (sa->integ_alg);
804   ipsec_key_encode (&sa->integ_key, &mp->entry.integrity_key);
805
806   mp->entry.flags = ipsec_sad_flags_encode (sa);
807   mp->entry.salt = clib_host_to_net_u32 (sa->salt);
808
809   if (ipsec_sa_is_set_IS_PROTECT (sa))
810     {
811       ipsec_sa_dump_match_ctx_t ctx = {
812         .sai = sa - ipsec_sa_pool,
813         .sw_if_index = ~0,
814       };
815       ipsec_tun_protect_walk (ipsec_sa_dump_match_sa, &ctx);
816
817       mp->sw_if_index = htonl (ctx.sw_if_index);
818     }
819   else
820     mp->sw_if_index = ~0;
821
822   if (ipsec_sa_is_set_IS_TUNNEL (sa))
823     {
824       ip_address_encode2 (&sa->tunnel.t_src, &mp->entry.tunnel_src);
825       ip_address_encode2 (&sa->tunnel.t_dst, &mp->entry.tunnel_dst);
826     }
827   if (ipsec_sa_is_set_UDP_ENCAP (sa))
828     {
829       mp->entry.udp_src_port = sa->udp_hdr.src_port;
830       mp->entry.udp_dst_port = sa->udp_hdr.dst_port;
831     }
832
833   mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq));
834   mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->seq));
835   if (ipsec_sa_is_set_USE_ESN (sa))
836     {
837       mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
838       mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
839     }
840   if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa))
841     mp->replay_window = clib_host_to_net_u64 (sa->replay_window);
842
843   mp->stat_index = clib_host_to_net_u32 (sa->stat_index);
844
845   vl_api_send_msg (ctx->reg, (u8 *) mp);
846
847   return (WALK_CONTINUE);
848 }
849
850 static void
851 vl_api_ipsec_sa_dump_t_handler (vl_api_ipsec_sa_dump_t * mp)
852 {
853   vl_api_registration_t *reg;
854
855   reg = vl_api_client_index_to_registration (mp->client_index);
856   if (!reg)
857     return;
858
859   ipsec_dump_walk_ctx_t ctx = {
860     .reg = reg,
861     .context = mp->context,
862   };
863
864   ipsec_sa_walk (send_ipsec_sa_details, &ctx);
865 }
866
867 static walk_rc_t
868 send_ipsec_sa_v2_details (ipsec_sa_t * sa, void *arg)
869 {
870   ipsec_dump_walk_ctx_t *ctx = arg;
871   vl_api_ipsec_sa_v2_details_t *mp;
872
873   mp = vl_msg_api_alloc (sizeof (*mp));
874   clib_memset (mp, 0, sizeof (*mp));
875   mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SA_V2_DETAILS);
876   mp->context = ctx->context;
877
878   mp->entry.sad_id = htonl (sa->id);
879   mp->entry.spi = htonl (sa->spi);
880   mp->entry.protocol = ipsec_proto_encode (sa->protocol);
881   mp->entry.tx_table_id = htonl (sa->tunnel.t_table_id);
882
883   mp->entry.crypto_algorithm = ipsec_crypto_algo_encode (sa->crypto_alg);
884   ipsec_key_encode (&sa->crypto_key, &mp->entry.crypto_key);
885
886   mp->entry.integrity_algorithm = ipsec_integ_algo_encode (sa->integ_alg);
887   ipsec_key_encode (&sa->integ_key, &mp->entry.integrity_key);
888
889   mp->entry.flags = ipsec_sad_flags_encode (sa);
890   mp->entry.salt = clib_host_to_net_u32 (sa->salt);
891
892   if (ipsec_sa_is_set_IS_PROTECT (sa))
893     {
894       ipsec_sa_dump_match_ctx_t ctx = {
895         .sai = sa - ipsec_sa_pool,
896         .sw_if_index = ~0,
897       };
898       ipsec_tun_protect_walk (ipsec_sa_dump_match_sa, &ctx);
899
900       mp->sw_if_index = htonl (ctx.sw_if_index);
901     }
902   else
903     mp->sw_if_index = ~0;
904
905   if (ipsec_sa_is_set_IS_TUNNEL (sa))
906     {
907       ip_address_encode2 (&sa->tunnel.t_src, &mp->entry.tunnel_src);
908       ip_address_encode2 (&sa->tunnel.t_dst, &mp->entry.tunnel_dst);
909     }
910   if (ipsec_sa_is_set_UDP_ENCAP (sa))
911     {
912       mp->entry.udp_src_port = sa->udp_hdr.src_port;
913       mp->entry.udp_dst_port = sa->udp_hdr.dst_port;
914     }
915
916   mp->entry.tunnel_flags =
917     tunnel_encap_decap_flags_encode (sa->tunnel.t_encap_decap_flags);
918   mp->entry.dscp = ip_dscp_encode (sa->tunnel.t_dscp);
919
920   mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq));
921   mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->seq));
922   if (ipsec_sa_is_set_USE_ESN (sa))
923     {
924       mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
925       mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
926     }
927   if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa))
928     mp->replay_window = clib_host_to_net_u64 (sa->replay_window);
929
930   mp->stat_index = clib_host_to_net_u32 (sa->stat_index);
931
932   vl_api_send_msg (ctx->reg, (u8 *) mp);
933
934   return (WALK_CONTINUE);
935 }
936
937 static void
938 vl_api_ipsec_sa_v2_dump_t_handler (vl_api_ipsec_sa_v2_dump_t *mp)
939 {
940   vl_api_registration_t *reg;
941
942   reg = vl_api_client_index_to_registration (mp->client_index);
943   if (!reg)
944     return;
945
946   ipsec_dump_walk_ctx_t ctx = {
947     .reg = reg,
948     .context = mp->context,
949   };
950
951   ipsec_sa_walk (send_ipsec_sa_v2_details, &ctx);
952 }
953
954 static walk_rc_t
955 send_ipsec_sa_v3_details (ipsec_sa_t *sa, void *arg)
956 {
957   ipsec_dump_walk_ctx_t *ctx = arg;
958   vl_api_ipsec_sa_v3_details_t *mp;
959
960   mp = vl_msg_api_alloc (sizeof (*mp));
961   clib_memset (mp, 0, sizeof (*mp));
962   mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_SA_V3_DETAILS);
963   mp->context = ctx->context;
964
965   mp->entry.sad_id = htonl (sa->id);
966   mp->entry.spi = htonl (sa->spi);
967   mp->entry.protocol = ipsec_proto_encode (sa->protocol);
968
969   mp->entry.crypto_algorithm = ipsec_crypto_algo_encode (sa->crypto_alg);
970   ipsec_key_encode (&sa->crypto_key, &mp->entry.crypto_key);
971
972   mp->entry.integrity_algorithm = ipsec_integ_algo_encode (sa->integ_alg);
973   ipsec_key_encode (&sa->integ_key, &mp->entry.integrity_key);
974
975   mp->entry.flags = ipsec_sad_flags_encode (sa);
976   mp->entry.salt = clib_host_to_net_u32 (sa->salt);
977
978   if (ipsec_sa_is_set_IS_PROTECT (sa))
979     {
980       ipsec_sa_dump_match_ctx_t ctx = {
981         .sai = sa - ipsec_sa_pool,
982         .sw_if_index = ~0,
983       };
984       ipsec_tun_protect_walk (ipsec_sa_dump_match_sa, &ctx);
985
986       mp->sw_if_index = htonl (ctx.sw_if_index);
987     }
988   else
989     mp->sw_if_index = ~0;
990
991   if (ipsec_sa_is_set_IS_TUNNEL (sa))
992     tunnel_encode (&sa->tunnel, &mp->entry.tunnel);
993
994   if (ipsec_sa_is_set_UDP_ENCAP (sa))
995     {
996       mp->entry.udp_src_port = sa->udp_hdr.src_port;
997       mp->entry.udp_dst_port = sa->udp_hdr.dst_port;
998     }
999
1000   mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq));
1001   mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->seq));
1002   if (ipsec_sa_is_set_USE_ESN (sa))
1003     {
1004       mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
1005       mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
1006     }
1007   if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa))
1008     mp->replay_window = clib_host_to_net_u64 (sa->replay_window);
1009
1010   mp->stat_index = clib_host_to_net_u32 (sa->stat_index);
1011
1012   vl_api_send_msg (ctx->reg, (u8 *) mp);
1013
1014   return (WALK_CONTINUE);
1015 }
1016
1017 static void
1018 vl_api_ipsec_sa_v3_dump_t_handler (vl_api_ipsec_sa_v3_dump_t *mp)
1019 {
1020   vl_api_registration_t *reg;
1021
1022   reg = vl_api_client_index_to_registration (mp->client_index);
1023   if (!reg)
1024     return;
1025
1026   ipsec_dump_walk_ctx_t ctx = {
1027     .reg = reg,
1028     .context = mp->context,
1029   };
1030
1031   ipsec_sa_walk (send_ipsec_sa_v3_details, &ctx);
1032 }
1033
1034 static void
1035 vl_api_ipsec_backend_dump_t_handler (vl_api_ipsec_backend_dump_t * mp)
1036 {
1037   vl_api_registration_t *rp;
1038   ipsec_main_t *im = &ipsec_main;
1039   u32 context = mp->context;
1040
1041   rp = vl_api_client_index_to_registration (mp->client_index);
1042
1043   if (rp == 0)
1044     {
1045       clib_warning ("Client %d AWOL", mp->client_index);
1046       return;
1047     }
1048
1049   ipsec_ah_backend_t *ab;
1050   ipsec_esp_backend_t *eb;
1051   /* *INDENT-OFF* */
1052   pool_foreach (ab, im->ah_backends) {
1053     vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
1054     clib_memset (mp, 0, sizeof (*mp));
1055     mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_BACKEND_DETAILS);
1056     mp->context = context;
1057     snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (ab->name),
1058               ab->name);
1059     mp->protocol = ntohl (IPSEC_API_PROTO_AH);
1060     mp->index = ab - im->ah_backends;
1061     mp->active = mp->index == im->ah_current_backend ? 1 : 0;
1062     vl_api_send_msg (rp, (u8 *)mp);
1063   }
1064   pool_foreach (eb, im->esp_backends) {
1065     vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
1066     clib_memset (mp, 0, sizeof (*mp));
1067     mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IPSEC_BACKEND_DETAILS);
1068     mp->context = context;
1069     snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (eb->name),
1070               eb->name);
1071     mp->protocol = ntohl (IPSEC_API_PROTO_ESP);
1072     mp->index = eb - im->esp_backends;
1073     mp->active = mp->index == im->esp_current_backend ? 1 : 0;
1074     vl_api_send_msg (rp, (u8 *)mp);
1075   }
1076   /* *INDENT-ON* */
1077 }
1078
1079 static void
1080 vl_api_ipsec_select_backend_t_handler (vl_api_ipsec_select_backend_t * mp)
1081 {
1082   ipsec_main_t *im = &ipsec_main;
1083   vl_api_ipsec_select_backend_reply_t *rmp;
1084   ipsec_protocol_t protocol;
1085   int rv = 0;
1086   if (pool_elts (ipsec_sa_pool) > 0)
1087     {
1088       rv = VNET_API_ERROR_INSTANCE_IN_USE;
1089       goto done;
1090     }
1091
1092   rv = ipsec_proto_decode (mp->protocol, &protocol);
1093
1094   if (rv)
1095     goto done;
1096
1097   switch (protocol)
1098     {
1099     case IPSEC_PROTOCOL_ESP:
1100       rv = ipsec_select_esp_backend (im, mp->index);
1101       break;
1102     case IPSEC_PROTOCOL_AH:
1103       rv = ipsec_select_ah_backend (im, mp->index);
1104       break;
1105     default:
1106       rv = VNET_API_ERROR_INVALID_PROTOCOL;
1107       break;
1108     }
1109 done:
1110   REPLY_MACRO (VL_API_IPSEC_SELECT_BACKEND_REPLY);
1111 }
1112
1113 static void
1114 vl_api_ipsec_set_async_mode_t_handler (vl_api_ipsec_set_async_mode_t * mp)
1115 {
1116   vl_api_ipsec_set_async_mode_reply_t *rmp;
1117   int rv = 0;
1118
1119   ipsec_set_async_mode (mp->async_enable);
1120
1121   REPLY_MACRO (VL_API_IPSEC_SET_ASYNC_MODE_REPLY);
1122 }
1123
1124 #include <vnet/ipsec/ipsec.api.c>
1125 static clib_error_t *
1126 ipsec_api_hookup (vlib_main_t * vm)
1127 {
1128   /*
1129    * Set up the (msg_name, crc, message-id) table
1130    */
1131   REPLY_MSG_ID_BASE = setup_message_id_table ();
1132
1133   return 0;
1134 }
1135
1136 VLIB_API_INIT_FUNCTION (ipsec_api_hookup);
1137
1138 /*
1139  * fd.io coding-style-patch-verification: ON
1140  *
1141  * Local Variables:
1142  * eval: (c-set-style "gnu")
1143  * End:
1144  */