ipsec: handle UDP keepalives
[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 #include <vnet/udp/udp.h>
23 #include <vnet/adj/adj_midchain.h>
24
25 #include <vnet/ipsec/ipsec.h>
26 #include <vnet/ipsec/esp.h>
27
28 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
29
30 static u8 *
31 format_ipsec_name (u8 * s, va_list * args)
32 {
33   u32 dev_instance = va_arg (*args, u32);
34   ipsec_main_t *im = &ipsec_main;
35   ipsec_tunnel_if_t *t = im->tunnel_interfaces + dev_instance;
36
37   return format (s, "ipsec%d", t->show_instance);
38 }
39
40 /* Statistics (not really errors) */
41 #define foreach_ipsec_if_tx_error    \
42 _(TX, "good packets transmitted")
43
44 static void
45 ipsec_if_tunnel_stack (adj_index_t ai)
46 {
47   ipsec_main_t *ipm = &ipsec_main;
48   ipsec_tunnel_if_t *it;
49   ip_adjacency_t *adj;
50   u32 sw_if_index;
51
52   adj = adj_get (ai);
53   sw_if_index = adj->rewrite_header.sw_if_index;
54
55   if ((vec_len (ipm->ipsec_if_by_sw_if_index) <= sw_if_index) ||
56       (~0 == ipm->ipsec_if_by_sw_if_index[sw_if_index]))
57     return;
58
59   it = pool_elt_at_index (ipm->tunnel_interfaces,
60                           ipm->ipsec_if_by_sw_if_index[sw_if_index]);
61
62   if (!vnet_hw_interface_is_link_up (vnet_get_main (), it->hw_if_index))
63     {
64       adj_midchain_delegate_unstack (ai);
65     }
66   else
67     {
68       ipsec_sa_t *sa;
69
70       sa = ipsec_sa_get (it->output_sa_index);
71
72       /* *INDENT-OFF* */
73       fib_prefix_t pfx = {
74         .fp_addr = sa->tunnel_dst_addr,
75         .fp_len = (ipsec_sa_is_set_IS_TUNNEL_V6(sa) ? 128 : 32),
76         .fp_proto = (ipsec_sa_is_set_IS_TUNNEL_V6(sa) ?
77                      FIB_PROTOCOL_IP6 :
78                      FIB_PROTOCOL_IP4),
79       };
80       /* *INDENT-ON* */
81
82       adj_midchain_delegate_stack (ai, sa->tx_fib_index, &pfx);
83     }
84 }
85
86 /**
87  * @brief Call back when restacking all adjacencies on a IPSec interface
88  */
89 static adj_walk_rc_t
90 ipsec_if_adj_walk_cb (adj_index_t ai, void *ctx)
91 {
92   ipsec_if_tunnel_stack (ai);
93
94   return (ADJ_WALK_RC_CONTINUE);
95 }
96
97 static void
98 ipsec_if_tunnel_restack (ipsec_tunnel_if_t * it)
99 {
100   fib_protocol_t proto;
101
102   /*
103    * walk all the adjacencies on the IPSec interface and restack them
104    */
105   FOR_EACH_FIB_IP_PROTOCOL (proto)
106   {
107     adj_nbr_walk (it->sw_if_index, proto, ipsec_if_adj_walk_cb, NULL);
108   }
109 }
110
111 static clib_error_t *
112 ipsec_admin_up_down_function (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
113 {
114   ipsec_main_t *im = &ipsec_main;
115   clib_error_t *err = 0;
116   ipsec_tunnel_if_t *t;
117   vnet_hw_interface_t *hi;
118   ipsec_sa_t *sa;
119
120   hi = vnet_get_hw_interface (vnm, hw_if_index);
121   t = pool_elt_at_index (im->tunnel_interfaces, hi->hw_instance);
122   t->flags = flags;
123
124   if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
125     {
126       sa = pool_elt_at_index (im->sad, t->input_sa_index);
127
128       err = ipsec_check_support_cb (im, sa);
129       if (err)
130         return err;
131
132       err = ipsec_add_del_sa_sess_cb (im, t->input_sa_index, 1);
133       if (err)
134         return err;
135
136       sa = pool_elt_at_index (im->sad, t->output_sa_index);
137
138       err = ipsec_check_support_cb (im, sa);
139       if (err)
140         return err;
141
142       err = ipsec_add_del_sa_sess_cb (im, t->output_sa_index, 1);
143       if (err)
144         return err;
145
146       vnet_hw_interface_set_flags (vnm, hw_if_index,
147                                    VNET_HW_INTERFACE_FLAG_LINK_UP);
148     }
149   else
150     {
151       vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */ );
152       sa = pool_elt_at_index (im->sad, t->input_sa_index);
153       err = ipsec_add_del_sa_sess_cb (im, t->input_sa_index, 0);
154       if (err)
155         return err;
156       sa = pool_elt_at_index (im->sad, t->output_sa_index);
157       err = ipsec_add_del_sa_sess_cb (im, t->output_sa_index, 0);
158       if (err)
159         return err;
160     }
161
162   ipsec_if_tunnel_restack (t);
163
164   return (NULL);
165 }
166
167 static u8 *
168 ipsec_if_build_rewrite (vnet_main_t * vnm,
169                         u32 sw_if_index,
170                         vnet_link_t link_type, const void *dst_address)
171 {
172   return (NULL);
173 }
174
175 static void
176 ipsec_if_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
177 {
178   adj_nbr_midchain_update_rewrite
179     (ai, NULL, NULL, ADJ_FLAG_MIDCHAIN_IP_STACK,
180      ipsec_if_build_rewrite (vnm, sw_if_index, adj_get_link_type (ai), NULL));
181
182   ipsec_if_tunnel_stack (ai);
183 }
184
185 /* *INDENT-OFF* */
186 VNET_DEVICE_CLASS (ipsec_device_class) =
187 {
188   .name = "IPSec",
189   .format_device_name = format_ipsec_name,
190   .admin_up_down_function = ipsec_admin_up_down_function,
191 };
192 /* *INDENT-ON* */
193
194 /* *INDENT-OFF* */
195 VNET_HW_INTERFACE_CLASS (ipsec_hw_class) =
196 {
197   .name = "IPSec",
198   .build_rewrite = default_build_rewrite,
199   .update_adjacency = ipsec_if_update_adj,
200   .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
201 };
202 /* *INDENT-ON* */
203
204 static int
205 ipsec_add_del_tunnel_if_rpc_callback (ipsec_add_del_tunnel_args_t * a)
206 {
207   vnet_main_t *vnm = vnet_get_main ();
208   ASSERT (vlib_get_thread_index () == 0);
209
210   return ipsec_add_del_tunnel_if_internal (vnm, a, NULL);
211 }
212
213 int
214 ipsec_add_del_tunnel_if (ipsec_add_del_tunnel_args_t * args)
215 {
216   vl_api_rpc_call_main_thread (ipsec_add_del_tunnel_if_rpc_callback,
217                                (u8 *) args, sizeof (*args));
218   return 0;
219 }
220
221 static u32
222 ipsec_tun_mk_input_sa_id (u32 ti)
223 {
224   return (0x80000000 | ti);
225 }
226
227 static u32
228 ipsec_tun_mk_output_sa_id (u32 ti)
229 {
230   return (0xc0000000 | ti);
231 }
232
233 static void
234 ipsec_tunnel_feature_set (ipsec_main_t * im, ipsec_tunnel_if_t * t, u8 enable)
235 {
236   u8 arc;
237   u32 esp4_feature_index, esp6_feature_index;
238   ipsec_sa_t *sa;
239
240   sa = ipsec_sa_get (t->output_sa_index);
241   if (sa->crypto_alg == IPSEC_CRYPTO_ALG_NONE)
242     {
243       esp4_feature_index = im->esp4_no_crypto_tun_feature_index;
244       esp6_feature_index = im->esp6_no_crypto_tun_feature_index;
245     }
246   else
247     {
248       esp4_feature_index = im->esp4_encrypt_tun_feature_index;
249       esp6_feature_index = im->esp6_encrypt_tun_feature_index;
250     }
251
252   arc = vnet_get_feature_arc_index ("ip4-output");
253
254   vnet_feature_enable_disable_with_index (arc, esp4_feature_index,
255                                           t->sw_if_index, enable,
256                                           &t->output_sa_index,
257                                           sizeof (t->output_sa_index));
258
259   arc = vnet_get_feature_arc_index ("ip6-output");
260
261   vnet_feature_enable_disable_with_index (arc, esp6_feature_index,
262                                           t->sw_if_index, enable,
263                                           &t->output_sa_index,
264                                           sizeof (t->output_sa_index));
265 }
266
267 int
268 ipsec_add_del_tunnel_if_internal (vnet_main_t * vnm,
269                                   ipsec_add_del_tunnel_args_t * args,
270                                   u32 * sw_if_index)
271 {
272   ipsec_tunnel_if_t *t;
273   ipsec_main_t *im = &ipsec_main;
274   vnet_hw_interface_t *hi = NULL;
275   u32 hw_if_index = ~0;
276   uword *p;
277   u32 dev_instance;
278   ipsec_key_t crypto_key, integ_key;
279   ipsec_sa_flags_t flags;
280   int rv;
281   int is_ip6 = args->is_ip6;
282   ipsec4_tunnel_key_t key4;
283   ipsec6_tunnel_key_t key6;
284
285   if (!is_ip6)
286     {
287       key4.remote_ip.as_u32 = args->remote_ip.ip4.as_u32;
288       key4.spi = clib_host_to_net_u32 (args->remote_spi);
289       p = hash_get (im->ipsec4_if_pool_index_by_key, key4.as_u64);
290     }
291   else
292     {
293       key6.remote_ip = args->remote_ip.ip6;
294       key6.spi = clib_host_to_net_u32 (args->remote_spi);
295       p = hash_get_mem (im->ipsec6_if_pool_index_by_key, &key6);
296     }
297
298   if (args->is_add)
299     {
300       /* check if same src/dst pair exists */
301       if (p)
302         return VNET_API_ERROR_INVALID_VALUE;
303
304       pool_get_aligned_zero (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES);
305
306       dev_instance = t - im->tunnel_interfaces;
307       if (args->renumber)
308         t->show_instance = args->show_instance;
309       else
310         t->show_instance = dev_instance;
311
312       if (hash_get (im->ipsec_if_real_dev_by_show_dev, t->show_instance))
313         {
314           pool_put (im->tunnel_interfaces, t);
315           return VNET_API_ERROR_INSTANCE_IN_USE;
316         }
317
318       hash_set (im->ipsec_if_real_dev_by_show_dev, t->show_instance,
319                 dev_instance);
320
321       flags = IPSEC_SA_FLAG_IS_TUNNEL;
322       if (args->is_ip6)
323         flags |= IPSEC_SA_FLAG_IS_TUNNEL_V6;
324       if (args->udp_encap)
325         flags |= IPSEC_SA_FLAG_UDP_ENCAP;
326       if (args->esn)
327         flags |= IPSEC_SA_FLAG_USE_ESN;
328       if (args->anti_replay)
329         flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
330
331       ipsec_mk_key (&crypto_key,
332                     args->remote_crypto_key, args->remote_crypto_key_len);
333       ipsec_mk_key (&integ_key,
334                     args->remote_integ_key, args->remote_integ_key_len);
335
336       rv = ipsec_sa_add_and_lock (ipsec_tun_mk_input_sa_id (dev_instance),
337                                   args->remote_spi,
338                                   IPSEC_PROTOCOL_ESP,
339                                   args->crypto_alg,
340                                   &crypto_key,
341                                   args->integ_alg,
342                                   &integ_key,
343                                   (flags | IPSEC_SA_FLAG_IS_INBOUND),
344                                   args->tx_table_id,
345                                   args->salt,
346                                   &args->remote_ip,
347                                   &args->local_ip, &t->input_sa_index);
348
349       if (rv)
350         return rv;
351
352       ipsec_mk_key (&crypto_key,
353                     args->local_crypto_key, args->local_crypto_key_len);
354       ipsec_mk_key (&integ_key,
355                     args->local_integ_key, args->local_integ_key_len);
356
357       rv = ipsec_sa_add_and_lock (ipsec_tun_mk_output_sa_id (dev_instance),
358                                   args->local_spi,
359                                   IPSEC_PROTOCOL_ESP,
360                                   args->crypto_alg,
361                                   &crypto_key,
362                                   args->integ_alg,
363                                   &integ_key,
364                                   flags,
365                                   args->tx_table_id,
366                                   args->salt,
367                                   &args->local_ip,
368                                   &args->remote_ip, &t->output_sa_index);
369
370       if (rv)
371         return rv;
372
373       /* copy the key */
374       if (is_ip6)
375         hash_set_mem_alloc (&im->ipsec6_if_pool_index_by_key, &key6,
376                             t - im->tunnel_interfaces);
377       else
378         {
379           hash_set (im->ipsec4_if_pool_index_by_key, key4.as_u64,
380                     t - im->tunnel_interfaces);
381           if (1 == hash_elts (im->ipsec4_if_pool_index_by_key))
382             udp_register_dst_port (vlib_get_main (),
383                                    UDP_DST_PORT_ipsec,
384                                    ipsec4_if_input_node.index, 1);
385         }
386
387       hw_if_index = vnet_register_interface (vnm, ipsec_device_class.index,
388                                              t - im->tunnel_interfaces,
389                                              ipsec_hw_class.index,
390                                              t - im->tunnel_interfaces);
391
392       hi = vnet_get_hw_interface (vnm, hw_if_index);
393
394       t->hw_if_index = hw_if_index;
395       t->sw_if_index = hi->sw_if_index;
396
397       /* Standard default jumbo MTU. */
398       vnet_sw_interface_set_mtu (vnm, t->sw_if_index, 9000);
399
400       /* Add the new tunnel to the DB of tunnels per sw_if_index ... */
401       vec_validate_init_empty (im->ipsec_if_by_sw_if_index, t->sw_if_index,
402                                ~0);
403       im->ipsec_if_by_sw_if_index[t->sw_if_index] = dev_instance;
404
405       ipsec_tunnel_feature_set (im, t, 1);
406
407       /*1st interface, register protocol */
408       if (pool_elts (im->tunnel_interfaces) == 1)
409         {
410           ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
411                                  ipsec4_if_input_node.index);
412           ip6_register_protocol (IP_PROTOCOL_IPSEC_ESP,
413                                  ipsec6_if_input_node.index);
414         }
415
416     }
417   else
418     {
419       u32 ti;
420
421       /* check if exists */
422       if (!p)
423         return VNET_API_ERROR_INVALID_VALUE;
424
425       ti = p[0];
426       t = pool_elt_at_index (im->tunnel_interfaces, ti);
427       hi = vnet_get_hw_interface (vnm, t->hw_if_index);
428       vnet_sw_interface_set_flags (vnm, hi->sw_if_index, 0);    /* admin down */
429
430       ipsec_tunnel_feature_set (im, t, 0);
431       vnet_delete_hw_interface (vnm, t->hw_if_index);
432
433       if (is_ip6)
434         hash_unset_mem_free (&im->ipsec6_if_pool_index_by_key, &key6);
435       else
436         {
437           hash_unset (im->ipsec4_if_pool_index_by_key, key4.as_u64);
438           if (0 == hash_elts (im->ipsec4_if_pool_index_by_key))
439             udp_unregister_dst_port (vlib_get_main (), UDP_DST_PORT_ipsec, 1);
440         }
441       hash_unset (im->ipsec_if_real_dev_by_show_dev, t->show_instance);
442
443       im->ipsec_if_by_sw_if_index[t->sw_if_index] = ~0;
444
445       /* delete input and output SA */
446       ipsec_sa_unlock (t->input_sa_index);
447       ipsec_sa_unlock (t->output_sa_index);
448
449       pool_put (im->tunnel_interfaces, t);
450     }
451
452   if (sw_if_index)
453     *sw_if_index = hi->sw_if_index;
454
455   return 0;
456 }
457
458 int
459 ipsec_set_interface_sa (vnet_main_t * vnm, u32 hw_if_index, u32 sa_id,
460                         u8 is_outbound)
461 {
462   ipsec_main_t *im = &ipsec_main;
463   vnet_hw_interface_t *hi;
464   ipsec_tunnel_if_t *t;
465   ipsec_sa_t *sa, *old_sa;
466   u32 sa_index, old_sa_index;
467   uword *p;
468
469   hi = vnet_get_hw_interface (vnm, hw_if_index);
470   t = pool_elt_at_index (im->tunnel_interfaces, hi->dev_instance);
471
472   sa_index = ipsec_sa_find_and_lock (sa_id);
473
474   if (INDEX_INVALID == sa_index)
475     {
476       clib_warning ("SA with ID %u not found", sa_id);
477       return VNET_API_ERROR_NO_SUCH_ENTRY;
478     }
479
480   sa = pool_elt_at_index (im->sad, sa_index);
481
482   if (!is_outbound)
483     {
484       old_sa_index = t->input_sa_index;
485       old_sa = pool_elt_at_index (im->sad, old_sa_index);
486
487       if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa) ^
488           ipsec_sa_is_set_IS_TUNNEL_V6 (old_sa))
489         {
490           clib_warning ("IPsec interface SA endpoints type can't be changed");
491           return VNET_API_ERROR_INVALID_VALUE;
492         }
493
494       if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa))
495         {
496           ipsec6_tunnel_key_t key;
497
498           /* unset old inbound hash entry. packets should stop arriving */
499           key.remote_ip = old_sa->tunnel_src_addr.ip6;
500           key.spi = clib_host_to_net_u32 (old_sa->spi);
501
502           p = hash_get_mem (im->ipsec6_if_pool_index_by_key, &key);
503           if (p)
504             hash_unset_mem_free (&im->ipsec6_if_pool_index_by_key, &key);
505
506           /* set new inbound SA, then set new hash entry */
507           t->input_sa_index = sa_index;
508           key.remote_ip = sa->tunnel_src_addr.ip6;
509           key.spi = clib_host_to_net_u32 (sa->spi);
510
511           hash_set_mem_alloc (&im->ipsec6_if_pool_index_by_key, &key,
512                               hi->dev_instance);
513         }
514       else
515         {
516           ipsec4_tunnel_key_t key;
517
518           /* unset old inbound hash entry. packets should stop arriving */
519           key.remote_ip.as_u32 = old_sa->tunnel_src_addr.ip4.as_u32;
520           key.spi = clib_host_to_net_u32 (old_sa->spi);
521
522           p = hash_get (im->ipsec4_if_pool_index_by_key, key.as_u64);
523           if (p)
524             hash_unset (im->ipsec4_if_pool_index_by_key, key.as_u64);
525
526           /* set new inbound SA, then set new hash entry */
527           t->input_sa_index = sa_index;
528           key.remote_ip.as_u32 = sa->tunnel_src_addr.ip4.as_u32;
529           key.spi = clib_host_to_net_u32 (sa->spi);
530
531           hash_set (im->ipsec4_if_pool_index_by_key, key.as_u64,
532                     hi->dev_instance);
533         }
534     }
535   else
536     {
537       old_sa_index = t->output_sa_index;
538       old_sa = pool_elt_at_index (im->sad, old_sa_index);
539
540       if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa) ^
541           ipsec_sa_is_set_IS_TUNNEL_V6 (old_sa))
542         {
543           clib_warning ("IPsec interface SA endpoints type can't be changed");
544           return VNET_API_ERROR_INVALID_VALUE;
545         }
546
547       /*
548        * re-enable the feature to get the new SA in
549        * the workers are stopped so no packets are sent in the clear
550        */
551       ipsec_tunnel_feature_set (im, t, 0);
552       t->output_sa_index = sa_index;
553       ipsec_tunnel_feature_set (im, t, 1);
554     }
555
556   /* remove sa_id to sa_index mapping on old SA */
557   hash_unset (im->sa_index_by_sa_id, old_sa->id);
558
559   if (ipsec_add_del_sa_sess_cb (im, old_sa_index, 0))
560     {
561       clib_warning ("IPsec backend add/del callback returned error");
562       return VNET_API_ERROR_SYSCALL_ERROR_1;
563     }
564
565   ipsec_sa_unlock (old_sa_index);
566
567   return 0;
568 }
569
570 clib_error_t *
571 ipsec_tunnel_if_init (vlib_main_t * vm)
572 {
573   ipsec_main_t *im = &ipsec_main;
574
575   /* initialize the ipsec-if ip4 hash */
576   im->ipsec4_if_pool_index_by_key =
577     hash_create (0, sizeof (ipsec4_tunnel_key_t));
578   /* initialize the ipsec-if ip6 hash */
579   im->ipsec6_if_pool_index_by_key = hash_create_mem (0,
580                                                      sizeof
581                                                      (ipsec6_tunnel_key_t),
582                                                      sizeof (uword));
583   im->ipsec_if_real_dev_by_show_dev = hash_create (0, sizeof (uword));
584
585   /* set up feature nodes to drop outbound packets with no crypto alg set */
586   ipsec_add_feature ("ip4-output", "esp4-no-crypto",
587                      &im->esp4_no_crypto_tun_feature_index);
588   ipsec_add_feature ("ip6-output", "esp6-no-crypto",
589                      &im->esp6_no_crypto_tun_feature_index);
590
591   return 0;
592 }
593
594 VLIB_INIT_FUNCTION (ipsec_tunnel_if_init);
595
596
597 /*
598  * fd.io coding-style-patch-verification: ON
599  *
600  * Local Variables:
601  * eval: (c-set-style "gnu")
602  * End:
603  */