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