Make IPsec tunnel intf work with IPv4 output features
[vpp.git] / src / vnet / ipsec / ipsec_if.c
1 /*
2  * ipsec_if.c : IPSec interface support
3  *
4  * Copyright (c) 2015 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <vnet/vnet.h>
19 #include <vnet/api_errno.h>
20 #include <vnet/ip/ip.h>
21
22 #include <vnet/ipsec/ipsec.h>
23 #include <vnet/ipsec/esp.h>
24
25 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
26
27 static u8 *
28 format_ipsec_name (u8 * s, va_list * args)
29 {
30   u32 dev_instance = va_arg (*args, u32);
31   return format (s, "ipsec%d", dev_instance);
32 }
33
34 static uword
35 dummy_interface_tx (vlib_main_t * vm,
36                     vlib_node_runtime_t * node, vlib_frame_t * frame)
37 {
38   clib_warning ("you shouldn't be here, leaking buffers...");
39   return frame->n_vectors;
40 }
41
42 static clib_error_t *
43 ipsec_admin_up_down_function (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
44 {
45   ipsec_main_t *im = &ipsec_main;
46   clib_error_t *err = 0;
47   ipsec_tunnel_if_t *t;
48   vnet_hw_interface_t *hi;
49   ipsec_sa_t *sa;
50
51   hi = vnet_get_hw_interface (vnm, hw_if_index);
52   t = pool_elt_at_index (im->tunnel_interfaces, hi->hw_instance);
53
54   if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
55     {
56       ASSERT (im->cb.check_support_cb);
57
58       sa = pool_elt_at_index (im->sad, t->input_sa_index);
59
60       err = im->cb.check_support_cb (sa);
61       if (err)
62         return err;
63
64       if (im->cb.add_del_sa_sess_cb)
65         {
66           err = im->cb.add_del_sa_sess_cb (t->input_sa_index, 1);
67           if (err)
68             return err;
69         }
70
71       sa = pool_elt_at_index (im->sad, t->output_sa_index);
72
73       err = im->cb.check_support_cb (sa);
74       if (err)
75         return err;
76
77       if (im->cb.add_del_sa_sess_cb)
78         {
79           err = im->cb.add_del_sa_sess_cb (t->output_sa_index, 1);
80           if (err)
81             return err;
82         }
83
84       vnet_hw_interface_set_flags (vnm, hw_if_index,
85                                    VNET_HW_INTERFACE_FLAG_LINK_UP);
86     }
87   else
88     {
89       vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */ );
90
91       sa = pool_elt_at_index (im->sad, t->input_sa_index);
92
93       if (im->cb.add_del_sa_sess_cb)
94         {
95           err = im->cb.add_del_sa_sess_cb (t->input_sa_index, 0);
96           if (err)
97             return err;
98         }
99
100       sa = pool_elt_at_index (im->sad, t->output_sa_index);
101
102       if (im->cb.add_del_sa_sess_cb)
103         {
104           err = im->cb.add_del_sa_sess_cb (t->output_sa_index, 0);
105           if (err)
106             return err;
107         }
108     }
109
110   return /* no error */ 0;
111 }
112
113 /* *INDENT-OFF* */
114 VNET_DEVICE_CLASS (ipsec_device_class, static) =
115 {
116   .name = "IPSec",
117   .format_device_name = format_ipsec_name,
118   .format_tx_trace = format_ipsec_if_output_trace,
119   .tx_function = dummy_interface_tx,
120   .admin_up_down_function = ipsec_admin_up_down_function,
121 };
122 /* *INDENT-ON* */
123
124 /* *INDENT-OFF* */
125 VNET_HW_INTERFACE_CLASS (ipsec_hw_class) =
126 {
127   .name = "IPSec",
128   .build_rewrite = default_build_rewrite,
129   .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
130 };
131 /* *INDENT-ON* */
132
133 static int
134 ipsec_add_del_tunnel_if_rpc_callback (ipsec_add_del_tunnel_args_t * a)
135 {
136   vnet_main_t *vnm = vnet_get_main ();
137   ASSERT (vlib_get_thread_index () == 0);
138
139   return ipsec_add_del_tunnel_if_internal (vnm, a, NULL);
140 }
141
142 int
143 ipsec_add_del_tunnel_if (ipsec_add_del_tunnel_args_t * args)
144 {
145   vl_api_rpc_call_main_thread (ipsec_add_del_tunnel_if_rpc_callback,
146                                (u8 *) args, sizeof (*args));
147   return 0;
148 }
149
150 int
151 ipsec_add_del_tunnel_if_internal (vnet_main_t * vnm,
152                                   ipsec_add_del_tunnel_args_t * args,
153                                   u32 * sw_if_index)
154 {
155   ipsec_tunnel_if_t *t;
156   ipsec_main_t *im = &ipsec_main;
157   vnet_hw_interface_t *hi = NULL;
158   u32 hw_if_index = ~0;
159   uword *p;
160   ipsec_sa_t *sa;
161
162   u64 key = (u64) args->remote_ip.as_u32 << 32 | (u64) args->remote_spi;
163   p = hash_get (im->ipsec_if_pool_index_by_key, key);
164
165   if (args->is_add)
166     {
167       /* check if same src/dst pair exists */
168       if (p)
169         return VNET_API_ERROR_INVALID_VALUE;
170
171       pool_get_aligned (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES);
172       memset (t, 0, sizeof (*t));
173
174       pool_get (im->sad, sa);
175       memset (sa, 0, sizeof (*sa));
176       t->input_sa_index = sa - im->sad;
177       sa->spi = args->remote_spi;
178       sa->tunnel_src_addr.ip4.as_u32 = args->remote_ip.as_u32;
179       sa->tunnel_dst_addr.ip4.as_u32 = args->local_ip.as_u32;
180       sa->is_tunnel = 1;
181       sa->use_esn = args->esn;
182       sa->use_anti_replay = args->anti_replay;
183       sa->integ_alg = args->integ_alg;
184       if (args->remote_integ_key_len <= sizeof (args->remote_integ_key))
185         {
186           sa->integ_key_len = args->remote_integ_key_len;
187           clib_memcpy (sa->integ_key, args->remote_integ_key,
188                        args->remote_integ_key_len);
189         }
190       sa->crypto_alg = args->crypto_alg;
191       if (args->remote_crypto_key_len <= sizeof (args->remote_crypto_key))
192         {
193           sa->crypto_key_len = args->remote_crypto_key_len;
194           clib_memcpy (sa->crypto_key, args->remote_crypto_key,
195                        args->remote_crypto_key_len);
196         }
197
198       pool_get (im->sad, sa);
199       memset (sa, 0, sizeof (*sa));
200       t->output_sa_index = sa - im->sad;
201       sa->spi = args->local_spi;
202       sa->tunnel_src_addr.ip4.as_u32 = args->local_ip.as_u32;
203       sa->tunnel_dst_addr.ip4.as_u32 = args->remote_ip.as_u32;
204       sa->is_tunnel = 1;
205       sa->use_esn = args->esn;
206       sa->use_anti_replay = args->anti_replay;
207       sa->integ_alg = args->integ_alg;
208       if (args->local_integ_key_len <= sizeof (args->local_integ_key))
209         {
210           sa->integ_key_len = args->local_integ_key_len;
211           clib_memcpy (sa->integ_key, args->local_integ_key,
212                        args->local_integ_key_len);
213         }
214       sa->crypto_alg = args->crypto_alg;
215       if (args->local_crypto_key_len <= sizeof (args->local_crypto_key))
216         {
217           sa->crypto_key_len = args->local_crypto_key_len;
218           clib_memcpy (sa->crypto_key, args->local_crypto_key,
219                        args->local_crypto_key_len);
220         }
221
222       hash_set (im->ipsec_if_pool_index_by_key, key,
223                 t - im->tunnel_interfaces);
224
225       if (vec_len (im->free_tunnel_if_indices) > 0)
226         {
227           hw_if_index =
228             im->free_tunnel_if_indices[vec_len (im->free_tunnel_if_indices) -
229                                        1];
230           _vec_len (im->free_tunnel_if_indices) -= 1;
231         }
232       else
233         {
234           hw_if_index =
235             vnet_register_interface (vnm, ipsec_device_class.index,
236                                      t - im->tunnel_interfaces,
237                                      ipsec_hw_class.index,
238                                      t - im->tunnel_interfaces);
239         }
240
241       hi = vnet_get_hw_interface (vnm, hw_if_index);
242       hi->output_node_index = ipsec_if_output_node.index;
243       t->hw_if_index = hw_if_index;
244
245       vnet_feature_enable_disable ("interface-output", "ipsec-if-output",
246                                    hi->sw_if_index, 1, 0, 0);
247
248       /*1st interface, register protocol */
249       if (pool_elts (im->tunnel_interfaces) == 1)
250         ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
251                                ipsec_if_input_node.index);
252
253     }
254   else
255     {
256       vnet_interface_main_t *vim = &vnm->interface_main;
257
258       /* check if exists */
259       if (!p)
260         return VNET_API_ERROR_INVALID_VALUE;
261
262       t = pool_elt_at_index (im->tunnel_interfaces, p[0]);
263       hi = vnet_get_hw_interface (vnm, t->hw_if_index);
264       vnet_sw_interface_set_flags (vnm, hi->sw_if_index, 0);    /* admin down */
265
266       vnet_feature_enable_disable ("interface-output", "ipsec-if-output",
267                                    hi->sw_if_index, 0, 0, 0);
268
269       vec_add1 (im->free_tunnel_if_indices, t->hw_if_index);
270
271       vnet_interface_counter_lock (vim);
272       vlib_zero_combined_counter (vim->combined_sw_if_counters +
273                                   VNET_INTERFACE_COUNTER_TX, hi->sw_if_index);
274       vlib_zero_combined_counter (vim->combined_sw_if_counters +
275                                   VNET_INTERFACE_COUNTER_RX, hi->sw_if_index);
276       vnet_interface_counter_unlock (vim);
277
278       /* delete input and output SA */
279       sa = pool_elt_at_index (im->sad, t->input_sa_index);
280
281       pool_put (im->sad, sa);
282
283       sa = pool_elt_at_index (im->sad, t->output_sa_index);
284
285       pool_put (im->sad, sa);
286
287       hash_unset (im->ipsec_if_pool_index_by_key, key);
288       pool_put (im->tunnel_interfaces, t);
289     }
290
291   if (sw_if_index)
292     *sw_if_index = hi->sw_if_index;
293
294   return 0;
295 }
296
297 int
298 ipsec_add_del_ipsec_gre_tunnel (vnet_main_t * vnm,
299                                 ipsec_add_del_ipsec_gre_tunnel_args_t * args)
300 {
301   ipsec_tunnel_if_t *t = 0;
302   ipsec_main_t *im = &ipsec_main;
303   uword *p;
304   ipsec_sa_t *sa;
305   u64 key;
306   u32 isa, osa;
307
308   p = hash_get (im->sa_index_by_sa_id, args->local_sa_id);
309   if (!p)
310     return VNET_API_ERROR_INVALID_VALUE;
311   isa = p[0];
312
313   p = hash_get (im->sa_index_by_sa_id, args->remote_sa_id);
314   if (!p)
315     return VNET_API_ERROR_INVALID_VALUE;
316   osa = p[0];
317   sa = pool_elt_at_index (im->sad, p[0]);
318
319   if (sa->is_tunnel)
320     key = (u64) sa->tunnel_dst_addr.ip4.as_u32 << 32 | (u64) sa->spi;
321   else
322     key = (u64) args->remote_ip.as_u32 << 32 | (u64) sa->spi;
323
324   p = hash_get (im->ipsec_if_pool_index_by_key, key);
325
326   if (args->is_add)
327     {
328       /* check if same src/dst pair exists */
329       if (p)
330         return VNET_API_ERROR_INVALID_VALUE;
331
332       pool_get_aligned (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES);
333       memset (t, 0, sizeof (*t));
334
335       t->input_sa_index = isa;
336       t->output_sa_index = osa;
337       t->hw_if_index = ~0;
338       hash_set (im->ipsec_if_pool_index_by_key, key,
339                 t - im->tunnel_interfaces);
340
341       /*1st interface, register protocol */
342       if (pool_elts (im->tunnel_interfaces) == 1)
343         ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
344                                ipsec_if_input_node.index);
345     }
346   else
347     {
348       /* check if exists */
349       if (!p)
350         return VNET_API_ERROR_INVALID_VALUE;
351
352       t = pool_elt_at_index (im->tunnel_interfaces, p[0]);
353       hash_unset (im->ipsec_if_pool_index_by_key, key);
354       pool_put (im->tunnel_interfaces, t);
355     }
356   return 0;
357 }
358
359 int
360 ipsec_set_interface_key (vnet_main_t * vnm, u32 hw_if_index,
361                          ipsec_if_set_key_type_t type, u8 alg, u8 * key)
362 {
363   ipsec_main_t *im = &ipsec_main;
364   vnet_hw_interface_t *hi;
365   ipsec_tunnel_if_t *t;
366   ipsec_sa_t *sa;
367
368   hi = vnet_get_hw_interface (vnm, hw_if_index);
369   t = pool_elt_at_index (im->tunnel_interfaces, hi->dev_instance);
370
371   if (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP)
372     return VNET_API_ERROR_SYSCALL_ERROR_1;
373
374   if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO)
375     {
376       sa = pool_elt_at_index (im->sad, t->output_sa_index);
377       sa->crypto_alg = alg;
378       sa->crypto_key_len = vec_len (key);
379       clib_memcpy (sa->crypto_key, key, vec_len (key));
380     }
381   else if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG)
382     {
383       sa = pool_elt_at_index (im->sad, t->output_sa_index);
384       sa->integ_alg = alg;
385       sa->integ_key_len = vec_len (key);
386       clib_memcpy (sa->integ_key, key, vec_len (key));
387     }
388   else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO)
389     {
390       sa = pool_elt_at_index (im->sad, t->input_sa_index);
391       sa->crypto_alg = alg;
392       sa->crypto_key_len = vec_len (key);
393       clib_memcpy (sa->crypto_key, key, vec_len (key));
394     }
395   else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG)
396     {
397       sa = pool_elt_at_index (im->sad, t->input_sa_index);
398       sa->integ_alg = alg;
399       sa->integ_key_len = vec_len (key);
400       clib_memcpy (sa->integ_key, key, vec_len (key));
401     }
402   else
403     return VNET_API_ERROR_INVALID_VALUE;
404
405   return 0;
406 }
407
408
409 int
410 ipsec_set_interface_sa (vnet_main_t * vnm, u32 hw_if_index, u32 sa_id,
411                         u8 is_outbound)
412 {
413   ipsec_main_t *im = &ipsec_main;
414   vnet_hw_interface_t *hi;
415   ipsec_tunnel_if_t *t;
416   ipsec_sa_t *sa, *old_sa;
417   u32 sa_index, old_sa_index;
418   uword *p;
419
420   hi = vnet_get_hw_interface (vnm, hw_if_index);
421   t = pool_elt_at_index (im->tunnel_interfaces, hi->dev_instance);
422
423   sa_index = ipsec_get_sa_index_by_sa_id (sa_id);
424   if (sa_index == ~0)
425     {
426       clib_warning ("SA with ID %u not found", sa_id);
427       return VNET_API_ERROR_INVALID_VALUE;
428     }
429
430   if (ipsec_is_sa_used (sa_index))
431     {
432       clib_warning ("SA with ID %u is already in use", sa_id);
433       return VNET_API_ERROR_INVALID_VALUE;
434     }
435
436   sa = pool_elt_at_index (im->sad, sa_index);
437   if (sa->is_tunnel_ip6)
438     {
439       clib_warning ("IPsec interface not supported with IPv6 endpoints");
440       return VNET_API_ERROR_UNIMPLEMENTED;
441     }
442
443   if (!is_outbound)
444     {
445       u64 key;
446
447       old_sa_index = t->input_sa_index;
448       old_sa = pool_elt_at_index (im->sad, old_sa_index);
449
450       /* unset old inbound hash entry. packets should stop arriving */
451       key =
452         (u64) old_sa->tunnel_src_addr.ip4.as_u32 << 32 | (u64) old_sa->spi;
453       p = hash_get (im->ipsec_if_pool_index_by_key, key);
454       if (p)
455         hash_unset (im->ipsec_if_pool_index_by_key, key);
456
457       /* set new inbound SA, then set new hash entry */
458       t->input_sa_index = sa_index;
459       key = (u64) sa->tunnel_src_addr.ip4.as_u32 << 32 | (u64) sa->spi;
460       hash_set (im->ipsec_if_pool_index_by_key, key, hi->dev_instance);
461     }
462   else
463     {
464       old_sa_index = t->output_sa_index;
465       old_sa = pool_elt_at_index (im->sad, old_sa_index);
466       t->output_sa_index = sa_index;
467     }
468
469   /* remove sa_id to sa_index mapping on old SA */
470   if (ipsec_get_sa_index_by_sa_id (old_sa->id) == old_sa_index)
471     hash_unset (im->sa_index_by_sa_id, old_sa->id);
472
473   if (im->cb.add_del_sa_sess_cb)
474     {
475       clib_error_t *err;
476
477       err = im->cb.add_del_sa_sess_cb (old_sa_index, 0);
478       if (err)
479         return VNET_API_ERROR_SYSCALL_ERROR_1;
480     }
481
482   pool_put (im->sad, old_sa);
483
484   return 0;
485 }
486
487
488 clib_error_t *
489 ipsec_tunnel_if_init (vlib_main_t * vm)
490 {
491   ipsec_main_t *im = &ipsec_main;
492
493   im->ipsec_if_pool_index_by_key = hash_create (0, sizeof (uword));
494
495   return 0;
496 }
497
498 VLIB_INIT_FUNCTION (ipsec_tunnel_if_init);
499
500
501 /*
502  * fd.io coding-style-patch-verification: ON
503  *
504  * Local Variables:
505  * eval: (c-set-style "gnu")
506  * End:
507  */