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