IPSEC: tunnel encap/decap dual loop speedups
[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 #include <vnet/fib/fib.h>
22
23 #include <vnet/ipsec/ipsec.h>
24 #include <vnet/ipsec/esp.h>
25
26 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
27
28 static u8 *
29 format_ipsec_name (u8 * s, va_list * args)
30 {
31   u32 dev_instance = va_arg (*args, u32);
32   ipsec_main_t *im = &ipsec_main;
33   ipsec_tunnel_if_t *t = im->tunnel_interfaces + dev_instance;
34
35   return format (s, "ipsec%d", t->show_instance);
36 }
37
38 /* Statistics (not really errors) */
39 #define foreach_ipsec_if_tx_error    \
40 _(TX, "good packets transmitted")
41
42 static char *ipsec_if_tx_error_strings[] = {
43 #define _(sym,string) string,
44   foreach_ipsec_if_tx_error
45 #undef _
46 };
47
48 typedef enum
49 {
50 #define _(sym,str) IPSEC_IF_OUTPUT_ERROR_##sym,
51   foreach_ipsec_if_tx_error
52 #undef _
53     IPSEC_IF_TX_N_ERROR,
54 } ipsec_if_tx_error_t;
55
56 typedef struct
57 {
58   u32 spi;
59   u32 seq;
60 } ipsec_if_tx_trace_t;
61
62 u8 *
63 format_ipsec_if_tx_trace (u8 * s, va_list * args)
64 {
65   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
66   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
67   ipsec_if_tx_trace_t *t = va_arg (*args, ipsec_if_tx_trace_t *);
68
69   s = format (s, "IPSec: spi %u seq %u", t->spi, t->seq);
70   return s;
71 }
72
73 static void
74 ipsec_output_trace (vlib_main_t * vm,
75                     vlib_node_runtime_t * node,
76                     vlib_frame_t * frame, const ipsec_tunnel_if_t * t0)
77 {
78   ipsec_main_t *im = &ipsec_main;
79   u32 *from, n_left;
80
81   n_left = frame->n_vectors;
82   from = vlib_frame_vector_args (frame);
83
84   while (n_left > 0)
85     {
86       vlib_buffer_t *b0;
87
88       b0 = vlib_get_buffer (vm, from[0]);
89
90       if (b0->flags & VLIB_BUFFER_IS_TRACED)
91         {
92           ipsec_if_tx_trace_t *tr =
93             vlib_add_trace (vm, node, b0, sizeof (*tr));
94           ipsec_sa_t *sa0 = pool_elt_at_index (im->sad, t0->output_sa_index);
95           tr->spi = sa0->spi;
96           tr->seq = sa0->seq;
97         }
98
99       from += 1;
100       n_left -= 1;
101     }
102 }
103
104 VNET_DEVICE_CLASS_TX_FN (ipsec_device_class) (vlib_main_t * vm,
105                                               vlib_node_runtime_t * node,
106                                               vlib_frame_t * frame)
107 {
108   ipsec_main_t *im = &ipsec_main;
109   u32 *from, n_left;
110   vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
111   const ipsec_tunnel_if_t *t0;
112   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
113   u16 nexts[VLIB_FRAME_SIZE];
114
115   from = vlib_frame_vector_args (frame);
116   t0 = pool_elt_at_index (im->tunnel_interfaces, rd->dev_instance);
117   n_left = frame->n_vectors;
118   b = bufs;
119
120   /* All going to encrypt */
121   clib_memset (nexts, 0, sizeof (nexts));
122
123   if (node->flags & VLIB_NODE_FLAG_TRACE)
124     ipsec_output_trace (vm, node, frame, t0);
125
126   vlib_get_buffers (vm, from, bufs, n_left);
127
128   while (n_left >= 8)
129     {
130       /* Prefetch the buffer header for the N+2 loop iteration */
131       vlib_prefetch_buffer_header (b[4], STORE);
132       vlib_prefetch_buffer_header (b[5], STORE);
133       vlib_prefetch_buffer_header (b[6], STORE);
134       vlib_prefetch_buffer_header (b[7], STORE);
135
136       vnet_buffer (b[0])->ipsec.sad_index = t0->output_sa_index;
137       vnet_buffer (b[1])->ipsec.sad_index = t0->output_sa_index;
138       vnet_buffer (b[2])->ipsec.sad_index = t0->output_sa_index;
139       vnet_buffer (b[3])->ipsec.sad_index = t0->output_sa_index;
140
141       n_left -= 4;
142       b += 4;
143     }
144   while (n_left > 0)
145     {
146       vnet_buffer (b[0])->ipsec.sad_index = t0->output_sa_index;
147
148       n_left -= 1;
149       b += 1;
150     }
151
152   vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
153
154   return frame->n_vectors;
155 }
156
157
158 static clib_error_t *
159 ipsec_admin_up_down_function (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
160 {
161   ipsec_main_t *im = &ipsec_main;
162   clib_error_t *err = 0;
163   ipsec_tunnel_if_t *t;
164   vnet_hw_interface_t *hi;
165   ipsec_sa_t *sa;
166
167   hi = vnet_get_hw_interface (vnm, hw_if_index);
168   t = pool_elt_at_index (im->tunnel_interfaces, hi->hw_instance);
169   t->flags = flags;
170
171   if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
172     {
173       sa = pool_elt_at_index (im->sad, t->input_sa_index);
174
175       err = ipsec_check_support_cb (im, sa);
176       if (err)
177         return err;
178
179       err = ipsec_add_del_sa_sess_cb (im, t->input_sa_index, 1);
180       if (err)
181         return err;
182
183       sa = pool_elt_at_index (im->sad, t->output_sa_index);
184
185       err = ipsec_check_support_cb (im, sa);
186       if (err)
187         return err;
188
189       err = ipsec_add_del_sa_sess_cb (im, t->output_sa_index, 1);
190       if (err)
191         return err;
192
193       vnet_hw_interface_set_flags (vnm, hw_if_index,
194                                    VNET_HW_INTERFACE_FLAG_LINK_UP);
195     }
196   else
197     {
198       vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */ );
199       sa = pool_elt_at_index (im->sad, t->input_sa_index);
200       err = ipsec_add_del_sa_sess_cb (im, t->input_sa_index, 0);
201       if (err)
202         return err;
203       sa = pool_elt_at_index (im->sad, t->output_sa_index);
204       err = ipsec_add_del_sa_sess_cb (im, t->output_sa_index, 0);
205       if (err)
206         return err;
207     }
208
209   return /* no error */ 0;
210 }
211
212
213 /* *INDENT-OFF* */
214 VNET_DEVICE_CLASS (ipsec_device_class) =
215 {
216   .name = "IPSec",
217   .format_device_name = format_ipsec_name,
218   .format_tx_trace = format_ipsec_if_tx_trace,
219   .tx_function_n_errors = IPSEC_IF_TX_N_ERROR,
220   .tx_function_error_strings = ipsec_if_tx_error_strings,
221   .admin_up_down_function = ipsec_admin_up_down_function,
222 };
223 /* *INDENT-ON* */
224
225 /* *INDENT-OFF* */
226 VNET_HW_INTERFACE_CLASS (ipsec_hw_class) =
227 {
228   .name = "IPSec",
229   .build_rewrite = default_build_rewrite,
230   .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
231 };
232 /* *INDENT-ON* */
233
234 static int
235 ipsec_add_del_tunnel_if_rpc_callback (ipsec_add_del_tunnel_args_t * a)
236 {
237   vnet_main_t *vnm = vnet_get_main ();
238   ASSERT (vlib_get_thread_index () == 0);
239
240   return ipsec_add_del_tunnel_if_internal (vnm, a, NULL);
241 }
242
243 int
244 ipsec_add_del_tunnel_if (ipsec_add_del_tunnel_args_t * args)
245 {
246   vl_api_rpc_call_main_thread (ipsec_add_del_tunnel_if_rpc_callback,
247                                (u8 *) args, sizeof (*args));
248   return 0;
249 }
250
251 static u32
252 ipsec_tun_mk_input_sa_id (u32 ti)
253 {
254   return (0x80000000 | ti);
255 }
256
257 static u32
258 ipsec_tun_mk_output_sa_id (u32 ti)
259 {
260   return (0xc0000000 | ti);
261 }
262
263 int
264 ipsec_add_del_tunnel_if_internal (vnet_main_t * vnm,
265                                   ipsec_add_del_tunnel_args_t * args,
266                                   u32 * sw_if_index)
267 {
268   ipsec_tunnel_if_t *t;
269   ipsec_main_t *im = &ipsec_main;
270   vnet_hw_interface_t *hi = NULL;
271   u32 hw_if_index = ~0;
272   uword *p;
273   u32 dev_instance;
274   u32 slot;
275   ipsec_key_t crypto_key, integ_key;
276   ipsec_sa_flags_t flags;
277   int rv;
278
279   u64 key = ((u64) args->remote_ip.ip4.as_u32 << 32 |
280              (u64) clib_host_to_net_u32 (args->remote_spi));
281   p = hash_get (im->ipsec_if_pool_index_by_key, key);
282
283   if (args->is_add)
284     {
285       /* check if same src/dst pair exists */
286       if (p)
287         return VNET_API_ERROR_INVALID_VALUE;
288
289       pool_get_aligned_zero (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES);
290
291       dev_instance = t - im->tunnel_interfaces;
292       if (args->renumber)
293         t->show_instance = args->show_instance;
294       else
295         t->show_instance = dev_instance;
296
297       if (hash_get (im->ipsec_if_real_dev_by_show_dev, t->show_instance))
298         {
299           pool_put (im->tunnel_interfaces, t);
300           return VNET_API_ERROR_INSTANCE_IN_USE;
301         }
302
303       hash_set (im->ipsec_if_real_dev_by_show_dev, t->show_instance,
304                 dev_instance);
305
306       flags = IPSEC_SA_FLAG_IS_TUNNEL;
307       if (args->udp_encap)
308         flags |= IPSEC_SA_FLAG_UDP_ENCAP;
309       if (args->esn)
310         flags |= IPSEC_SA_FLAG_USE_EXTENDED_SEQ_NUM;
311       if (args->anti_replay)
312         flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
313
314       ipsec_mk_key (&crypto_key,
315                     args->remote_crypto_key, args->remote_crypto_key_len);
316       ipsec_mk_key (&integ_key,
317                     args->remote_integ_key, args->remote_integ_key_len);
318
319       rv = ipsec_sa_add (ipsec_tun_mk_input_sa_id (dev_instance),
320                          args->remote_spi,
321                          IPSEC_PROTOCOL_ESP,
322                          args->crypto_alg,
323                          &crypto_key,
324                          args->integ_alg,
325                          &integ_key,
326                          flags,
327                          args->tx_table_id,
328                          &args->remote_ip,
329                          &args->local_ip, &t->input_sa_index);
330
331       if (rv)
332         return VNET_API_ERROR_UNIMPLEMENTED;
333
334       ipsec_mk_key (&crypto_key,
335                     args->local_crypto_key, args->local_crypto_key_len);
336       ipsec_mk_key (&integ_key,
337                     args->local_integ_key, args->local_integ_key_len);
338
339       rv = ipsec_sa_add (ipsec_tun_mk_output_sa_id (dev_instance),
340                          args->local_spi,
341                          IPSEC_PROTOCOL_ESP,
342                          args->crypto_alg,
343                          &crypto_key,
344                          args->integ_alg,
345                          &integ_key,
346                          flags,
347                          args->tx_table_id,
348                          &args->local_ip,
349                          &args->remote_ip, &t->output_sa_index);
350
351       if (rv)
352         return VNET_API_ERROR_UNIMPLEMENTED;
353
354       hash_set (im->ipsec_if_pool_index_by_key, key,
355                 t - im->tunnel_interfaces);
356
357       hw_if_index = vnet_register_interface (vnm, ipsec_device_class.index,
358                                              t - im->tunnel_interfaces,
359                                              ipsec_hw_class.index,
360                                              t - im->tunnel_interfaces);
361
362       hi = vnet_get_hw_interface (vnm, hw_if_index);
363       /* add esp4 as the next-node-index of this tx-node */
364
365       slot = vlib_node_add_next_with_slot
366         (vnm->vlib_main, hi->tx_node_index, im->esp4_encrypt_node_index, 0);
367
368       ASSERT (slot == 0);
369
370       t->hw_if_index = hw_if_index;
371       t->sw_if_index = hi->sw_if_index;
372
373       vnet_feature_enable_disable ("interface-output", "ipsec-if-output",
374                                    hi->sw_if_index, 1, 0, 0);
375
376       /*1st interface, register protocol */
377       if (pool_elts (im->tunnel_interfaces) == 1)
378         ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
379                                ipsec_if_input_node.index);
380
381     }
382   else
383     {
384       /* check if exists */
385       if (!p)
386         return VNET_API_ERROR_INVALID_VALUE;
387
388       t = pool_elt_at_index (im->tunnel_interfaces, p[0]);
389       hi = vnet_get_hw_interface (vnm, t->hw_if_index);
390       vnet_sw_interface_set_flags (vnm, hi->sw_if_index, 0);    /* admin down */
391
392       vnet_feature_enable_disable ("interface-output", "ipsec-if-output",
393                                    hi->sw_if_index, 0, 0, 0);
394
395       vnet_delete_hw_interface (vnm, t->hw_if_index);
396
397       hash_unset (im->ipsec_if_pool_index_by_key, key);
398       hash_unset (im->ipsec_if_real_dev_by_show_dev, t->show_instance);
399
400       pool_put (im->tunnel_interfaces, t);
401
402       /* delete input and output SA */
403       ipsec_sa_del (ipsec_tun_mk_input_sa_id (p[0]));
404       ipsec_sa_del (ipsec_tun_mk_output_sa_id (p[0]));
405     }
406
407   if (sw_if_index)
408     *sw_if_index = hi->sw_if_index;
409
410   return 0;
411 }
412
413 int
414 ipsec_add_del_ipsec_gre_tunnel (vnet_main_t * vnm,
415                                 ipsec_add_del_ipsec_gre_tunnel_args_t * args)
416 {
417   ipsec_tunnel_if_t *t = 0;
418   ipsec_main_t *im = &ipsec_main;
419   uword *p;
420   ipsec_sa_t *sa;
421   u64 key;
422   u32 isa, osa;
423
424   p = hash_get (im->sa_index_by_sa_id, args->local_sa_id);
425   if (!p)
426     return VNET_API_ERROR_INVALID_VALUE;
427   isa = p[0];
428
429   p = hash_get (im->sa_index_by_sa_id, args->remote_sa_id);
430   if (!p)
431     return VNET_API_ERROR_INVALID_VALUE;
432   osa = p[0];
433   sa = pool_elt_at_index (im->sad, p[0]);
434
435   if (sa->is_tunnel)
436     key = ((u64) sa->tunnel_dst_addr.ip4.as_u32 << 32 |
437            (u64) clib_host_to_net_u32 (sa->spi));
438   else
439     key = ((u64) args->remote_ip.as_u32 << 32 |
440            (u64) clib_host_to_net_u32 (sa->spi));
441
442   p = hash_get (im->ipsec_if_pool_index_by_key, key);
443
444   if (args->is_add)
445     {
446       /* check if same src/dst pair exists */
447       if (p)
448         return VNET_API_ERROR_INVALID_VALUE;
449
450       pool_get_aligned (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES);
451       clib_memset (t, 0, sizeof (*t));
452
453       t->input_sa_index = isa;
454       t->output_sa_index = osa;
455       t->hw_if_index = ~0;
456       hash_set (im->ipsec_if_pool_index_by_key, key,
457                 t - im->tunnel_interfaces);
458
459       /*1st interface, register protocol */
460       if (pool_elts (im->tunnel_interfaces) == 1)
461         ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
462                                ipsec_if_input_node.index);
463     }
464   else
465     {
466       /* check if exists */
467       if (!p)
468         return VNET_API_ERROR_INVALID_VALUE;
469
470       t = pool_elt_at_index (im->tunnel_interfaces, p[0]);
471       hash_unset (im->ipsec_if_pool_index_by_key, key);
472       pool_put (im->tunnel_interfaces, t);
473     }
474   return 0;
475 }
476
477 int
478 ipsec_set_interface_key (vnet_main_t * vnm, u32 hw_if_index,
479                          ipsec_if_set_key_type_t type, u8 alg, u8 * key)
480 {
481   ipsec_main_t *im = &ipsec_main;
482   vnet_hw_interface_t *hi;
483   ipsec_tunnel_if_t *t;
484   ipsec_sa_t *sa;
485
486   hi = vnet_get_hw_interface (vnm, hw_if_index);
487   t = pool_elt_at_index (im->tunnel_interfaces, hi->dev_instance);
488
489   if (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP)
490     return VNET_API_ERROR_SYSCALL_ERROR_1;
491
492   if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO)
493     {
494       sa = pool_elt_at_index (im->sad, t->output_sa_index);
495       sa->crypto_alg = alg;
496       ipsec_mk_key (&sa->crypto_key, key, vec_len (key));
497     }
498   else if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG)
499     {
500       sa = pool_elt_at_index (im->sad, t->output_sa_index);
501       sa->integ_alg = alg;
502       ipsec_mk_key (&sa->integ_key, key, vec_len (key));
503     }
504   else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO)
505     {
506       sa = pool_elt_at_index (im->sad, t->input_sa_index);
507       sa->crypto_alg = alg;
508       ipsec_mk_key (&sa->crypto_key, key, vec_len (key));
509     }
510   else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG)
511     {
512       sa = pool_elt_at_index (im->sad, t->input_sa_index);
513       sa->integ_alg = alg;
514       ipsec_mk_key (&sa->integ_key, key, vec_len (key));
515     }
516   else
517     return VNET_API_ERROR_INVALID_VALUE;
518
519   return 0;
520 }
521
522
523 int
524 ipsec_set_interface_sa (vnet_main_t * vnm, u32 hw_if_index, u32 sa_id,
525                         u8 is_outbound)
526 {
527   ipsec_main_t *im = &ipsec_main;
528   vnet_hw_interface_t *hi;
529   ipsec_tunnel_if_t *t;
530   ipsec_sa_t *sa, *old_sa;
531   u32 sa_index, old_sa_index;
532   uword *p;
533
534   hi = vnet_get_hw_interface (vnm, hw_if_index);
535   t = pool_elt_at_index (im->tunnel_interfaces, hi->dev_instance);
536
537   sa_index = ipsec_get_sa_index_by_sa_id (sa_id);
538   if (sa_index == ~0)
539     {
540       clib_warning ("SA with ID %u not found", sa_id);
541       return VNET_API_ERROR_INVALID_VALUE;
542     }
543
544   if (ipsec_is_sa_used (sa_index))
545     {
546       clib_warning ("SA with ID %u is already in use", sa_id);
547       return VNET_API_ERROR_INVALID_VALUE;
548     }
549
550   sa = pool_elt_at_index (im->sad, sa_index);
551   if (sa->is_tunnel_ip6)
552     {
553       clib_warning ("IPsec interface not supported with IPv6 endpoints");
554       return VNET_API_ERROR_UNIMPLEMENTED;
555     }
556
557   if (!is_outbound)
558     {
559       u64 key;
560
561       old_sa_index = t->input_sa_index;
562       old_sa = pool_elt_at_index (im->sad, old_sa_index);
563
564       /* unset old inbound hash entry. packets should stop arriving */
565       key = ((u64) old_sa->tunnel_src_addr.ip4.as_u32 << 32 |
566              (u64) clib_host_to_net_u32 (old_sa->spi));
567       p = hash_get (im->ipsec_if_pool_index_by_key, key);
568       if (p)
569         hash_unset (im->ipsec_if_pool_index_by_key, key);
570
571       /* set new inbound SA, then set new hash entry */
572       t->input_sa_index = sa_index;
573       key = ((u64) sa->tunnel_src_addr.ip4.as_u32 << 32 |
574              (u64) clib_host_to_net_u32 (sa->spi));
575       hash_set (im->ipsec_if_pool_index_by_key, key, hi->dev_instance);
576     }
577   else
578     {
579       old_sa_index = t->output_sa_index;
580       old_sa = pool_elt_at_index (im->sad, old_sa_index);
581       t->output_sa_index = sa_index;
582     }
583
584   /* remove sa_id to sa_index mapping on old SA */
585   if (ipsec_get_sa_index_by_sa_id (old_sa->id) == old_sa_index)
586     hash_unset (im->sa_index_by_sa_id, old_sa->id);
587
588   if (ipsec_add_del_sa_sess_cb (im, old_sa_index, 0))
589     {
590       clib_warning ("IPsec backend add/del callback returned error");
591       return VNET_API_ERROR_SYSCALL_ERROR_1;
592     }
593   pool_put (im->sad, old_sa);
594
595   return 0;
596 }
597
598 clib_error_t *
599 ipsec_tunnel_if_init (vlib_main_t * vm)
600 {
601   ipsec_main_t *im = &ipsec_main;
602
603   im->ipsec_if_pool_index_by_key = hash_create (0, sizeof (uword));
604   im->ipsec_if_real_dev_by_show_dev = hash_create (0, sizeof (uword));
605
606   return 0;
607 }
608
609 VLIB_INIT_FUNCTION (ipsec_tunnel_if_init);
610
611
612 /*
613  * fd.io coding-style-patch-verification: ON
614  *
615  * Local Variables:
616  * eval: (c-set-style "gnu")
617  * End:
618  */