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