9f64634c51e1fe6c18bdc88b19173981fb3c2c9b
[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 GRE 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 th GRE 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_tunnel_if_t * t, u8 enable)
235 {
236   vnet_feature_enable_disable ("ip4-output",
237                                "esp4-encrypt-tun",
238                                t->sw_if_index, enable,
239                                &t->output_sa_index,
240                                sizeof (t->output_sa_index));
241   vnet_feature_enable_disable ("ip6-output",
242                                "esp6-encrypt-tun",
243                                t->sw_if_index, enable,
244                                &t->output_sa_index,
245                                sizeof (t->output_sa_index));
246 }
247
248 int
249 ipsec_add_del_tunnel_if_internal (vnet_main_t * vnm,
250                                   ipsec_add_del_tunnel_args_t * args,
251                                   u32 * sw_if_index)
252 {
253   ipsec_tunnel_if_t *t;
254   ipsec_main_t *im = &ipsec_main;
255   vnet_hw_interface_t *hi = NULL;
256   u32 hw_if_index = ~0;
257   uword *p;
258   u32 dev_instance;
259   ipsec_key_t crypto_key, integ_key;
260   ipsec_sa_flags_t flags;
261   int rv;
262   int is_ip6 = args->is_ip6;
263   ipsec4_tunnel_key_t key4;
264   ipsec6_tunnel_key_t key6;
265
266   if (!is_ip6)
267     {
268       key4.remote_ip = args->remote_ip.ip4.as_u32;
269       key4.spi = clib_host_to_net_u32 (args->remote_spi);
270       p = hash_get (im->ipsec4_if_pool_index_by_key, key4.as_u64);
271     }
272   else
273     {
274       key6.remote_ip = args->remote_ip.ip6;
275       key6.spi = clib_host_to_net_u32 (args->remote_spi);
276       p = hash_get_mem (im->ipsec6_if_pool_index_by_key, &key6);
277     }
278
279   if (args->is_add)
280     {
281       /* check if same src/dst pair exists */
282       if (p)
283         return VNET_API_ERROR_INVALID_VALUE;
284
285       pool_get_aligned_zero (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES);
286
287       dev_instance = t - im->tunnel_interfaces;
288       if (args->renumber)
289         t->show_instance = args->show_instance;
290       else
291         t->show_instance = dev_instance;
292
293       if (hash_get (im->ipsec_if_real_dev_by_show_dev, t->show_instance))
294         {
295           pool_put (im->tunnel_interfaces, t);
296           return VNET_API_ERROR_INSTANCE_IN_USE;
297         }
298
299       hash_set (im->ipsec_if_real_dev_by_show_dev, t->show_instance,
300                 dev_instance);
301
302       flags = IPSEC_SA_FLAG_IS_TUNNEL;
303       if (args->is_ip6)
304         flags |= IPSEC_SA_FLAG_IS_TUNNEL_V6;
305       if (args->udp_encap)
306         flags |= IPSEC_SA_FLAG_UDP_ENCAP;
307       if (args->esn)
308         flags |= IPSEC_SA_FLAG_USE_ESN;
309       if (args->anti_replay)
310         flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
311
312       ipsec_mk_key (&crypto_key,
313                     args->remote_crypto_key, args->remote_crypto_key_len);
314       ipsec_mk_key (&integ_key,
315                     args->remote_integ_key, args->remote_integ_key_len);
316
317       rv = ipsec_sa_add (ipsec_tun_mk_input_sa_id (dev_instance),
318                          args->remote_spi,
319                          IPSEC_PROTOCOL_ESP,
320                          args->crypto_alg,
321                          &crypto_key,
322                          args->integ_alg,
323                          &integ_key,
324                          (flags | IPSEC_SA_FLAG_IS_INBOUND),
325                          args->tx_table_id,
326                          args->salt,
327                          &args->remote_ip,
328                          &args->local_ip, &t->input_sa_index);
329
330       if (rv)
331         return VNET_API_ERROR_INVALID_SRC_ADDRESS;
332
333       ipsec_mk_key (&crypto_key,
334                     args->local_crypto_key, args->local_crypto_key_len);
335       ipsec_mk_key (&integ_key,
336                     args->local_integ_key, args->local_integ_key_len);
337
338       rv = ipsec_sa_add (ipsec_tun_mk_output_sa_id (dev_instance),
339                          args->local_spi,
340                          IPSEC_PROTOCOL_ESP,
341                          args->crypto_alg,
342                          &crypto_key,
343                          args->integ_alg,
344                          &integ_key,
345                          flags,
346                          args->tx_table_id,
347                          args->salt,
348                          &args->local_ip,
349                          &args->remote_ip, &t->output_sa_index);
350
351       if (rv)
352         return VNET_API_ERROR_INVALID_DST_ADDRESS;
353
354       /* copy the key */
355       if (is_ip6)
356         hash_set_mem_alloc (&im->ipsec6_if_pool_index_by_key, &key6,
357                             t - im->tunnel_interfaces);
358       else
359         hash_set (im->ipsec4_if_pool_index_by_key, key4.as_u64,
360                   t - im->tunnel_interfaces);
361
362       hw_if_index = vnet_register_interface (vnm, ipsec_device_class.index,
363                                              t - im->tunnel_interfaces,
364                                              ipsec_hw_class.index,
365                                              t - im->tunnel_interfaces);
366
367       hi = vnet_get_hw_interface (vnm, hw_if_index);
368
369       t->hw_if_index = hw_if_index;
370       t->sw_if_index = hi->sw_if_index;
371
372       /* Standard default jumbo MTU. */
373       vnet_sw_interface_set_mtu (vnm, t->sw_if_index, 9000);
374
375       /* Add the new tunnel to the DB of tunnels per sw_if_index ... */
376       vec_validate_init_empty (im->ipsec_if_by_sw_if_index, t->sw_if_index,
377                                ~0);
378       im->ipsec_if_by_sw_if_index[t->sw_if_index] = dev_instance;
379
380       ipsec_tunnel_feature_set (t, 1);
381
382       /*1st interface, register protocol */
383       if (pool_elts (im->tunnel_interfaces) == 1)
384         {
385           ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
386                                  ipsec4_if_input_node.index);
387           ip6_register_protocol (IP_PROTOCOL_IPSEC_ESP,
388                                  ipsec6_if_input_node.index);
389         }
390
391     }
392   else
393     {
394       u32 ti;
395
396       /* check if exists */
397       if (!p)
398         return VNET_API_ERROR_INVALID_VALUE;
399
400       ti = p[0];
401       t = pool_elt_at_index (im->tunnel_interfaces, ti);
402       hi = vnet_get_hw_interface (vnm, t->hw_if_index);
403       vnet_sw_interface_set_flags (vnm, hi->sw_if_index, 0);    /* admin down */
404
405       ipsec_tunnel_feature_set (t, 0);
406       vnet_delete_hw_interface (vnm, t->hw_if_index);
407
408       if (is_ip6)
409         hash_unset_mem_free (&im->ipsec6_if_pool_index_by_key, &key6);
410       else
411         hash_unset (im->ipsec4_if_pool_index_by_key, key4.as_u64);
412
413       hash_unset (im->ipsec_if_real_dev_by_show_dev, t->show_instance);
414       im->ipsec_if_by_sw_if_index[t->sw_if_index] = ~0;
415
416       pool_put (im->tunnel_interfaces, t);
417
418       /* delete input and output SA */
419       ipsec_sa_del (ipsec_tun_mk_input_sa_id (ti));
420       ipsec_sa_del (ipsec_tun_mk_output_sa_id (ti));
421     }
422
423   if (sw_if_index)
424     *sw_if_index = hi->sw_if_index;
425
426   return 0;
427 }
428
429 int
430 ipsec_add_del_ipsec_gre_tunnel (vnet_main_t * vnm,
431                                 const ipsec_gre_tunnel_add_del_args_t * args)
432 {
433   ipsec_tunnel_if_t *t = 0;
434   ipsec_main_t *im = &ipsec_main;
435   uword *p;
436   ipsec_sa_t *sa;
437   ipsec4_tunnel_key_t key;
438   u32 isa, osa;
439
440   p = hash_get (im->sa_index_by_sa_id, args->local_sa_id);
441   if (!p)
442     return VNET_API_ERROR_INVALID_VALUE;
443   osa = p[0];
444   sa = pool_elt_at_index (im->sad, p[0]);
445   ipsec_sa_set_IS_GRE (sa);
446
447   p = hash_get (im->sa_index_by_sa_id, args->remote_sa_id);
448   if (!p)
449     return VNET_API_ERROR_INVALID_VALUE;
450   isa = p[0];
451   sa = pool_elt_at_index (im->sad, p[0]);
452   ipsec_sa_set_IS_GRE (sa);
453
454   /* we form the key from the input/remote SA whose tunnel is srouce
455    * at the remote end */
456   if (ipsec_sa_is_set_IS_TUNNEL (sa))
457     {
458       key.remote_ip = sa->tunnel_src_addr.ip4.as_u32;
459       key.spi = clib_host_to_net_u32 (sa->spi);
460     }
461   else
462     {
463       key.remote_ip = args->src.as_u32;
464       key.spi = clib_host_to_net_u32 (sa->spi);
465     }
466
467   p = hash_get (im->ipsec4_if_pool_index_by_key, key.as_u64);
468
469   if (args->is_add)
470     {
471       /* check if same src/dst pair exists */
472       if (p)
473         return VNET_API_ERROR_INVALID_VALUE;
474
475       pool_get_aligned (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES);
476       clib_memset (t, 0, sizeof (*t));
477
478       t->input_sa_index = isa;
479       t->output_sa_index = osa;
480       t->hw_if_index = ~0;
481       hash_set (im->ipsec4_if_pool_index_by_key, key.as_u64,
482                 t - im->tunnel_interfaces);
483
484       /*1st interface, register protocol */
485       if (pool_elts (im->tunnel_interfaces) == 1)
486         {
487           ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
488                                  ipsec4_if_input_node.index);
489           /* TBD, GRE IPSec6
490            *
491            ip6_register_protocol (IP_PROTOCOL_IPSEC_ESP,
492            ipsec6_if_input_node.index);
493            */
494         }
495     }
496   else
497     {
498       /* check if exists */
499       if (!p)
500         return VNET_API_ERROR_INVALID_VALUE;
501
502       t = pool_elt_at_index (im->tunnel_interfaces, p[0]);
503       hash_unset (im->ipsec4_if_pool_index_by_key, key.as_u64);
504       pool_put (im->tunnel_interfaces, t);
505     }
506   return 0;
507 }
508
509 int
510 ipsec_set_interface_key (vnet_main_t * vnm, u32 hw_if_index,
511                          ipsec_if_set_key_type_t type, u8 alg, u8 * key)
512 {
513   vlib_main_t *vm = vlib_get_main ();
514   ipsec_main_t *im = &ipsec_main;
515   vnet_hw_interface_t *hi;
516   ipsec_tunnel_if_t *t;
517   ipsec_sa_t *sa;
518
519   hi = vnet_get_hw_interface (vnm, hw_if_index);
520   t = pool_elt_at_index (im->tunnel_interfaces, hi->dev_instance);
521
522   if (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP)
523     return VNET_API_ERROR_SYSCALL_ERROR_1;
524
525   if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO)
526     {
527       sa = pool_elt_at_index (im->sad, t->output_sa_index);
528       ipsec_sa_set_crypto_alg (sa, alg);
529       ipsec_mk_key (&sa->crypto_key, key, vec_len (key));
530       sa->crypto_calg = im->crypto_algs[alg].alg;
531       vnet_crypto_key_modify (vm, sa->crypto_key_index, sa->crypto_calg,
532                               key, vec_len (key));
533     }
534   else if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG)
535     {
536       sa = pool_elt_at_index (im->sad, t->output_sa_index);
537       ipsec_sa_set_integ_alg (sa, alg);
538       ipsec_mk_key (&sa->integ_key, key, vec_len (key));
539       sa->integ_calg = im->integ_algs[alg].alg;
540       vnet_crypto_key_modify (vm, sa->integ_key_index, sa->integ_calg,
541                               key, vec_len (key));
542     }
543   else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO)
544     {
545       sa = pool_elt_at_index (im->sad, t->input_sa_index);
546       ipsec_sa_set_crypto_alg (sa, alg);
547       ipsec_mk_key (&sa->crypto_key, key, vec_len (key));
548       sa->crypto_calg = im->crypto_algs[alg].alg;
549       vnet_crypto_key_modify (vm, sa->crypto_key_index, sa->crypto_calg,
550                               key, vec_len (key));
551     }
552   else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG)
553     {
554       sa = pool_elt_at_index (im->sad, t->input_sa_index);
555       ipsec_sa_set_integ_alg (sa, alg);
556       ipsec_mk_key (&sa->integ_key, key, vec_len (key));
557       sa->integ_calg = im->integ_algs[alg].alg;
558       vnet_crypto_key_modify (vm, sa->integ_key_index, sa->integ_calg,
559                               key, vec_len (key));
560     }
561   else
562     return VNET_API_ERROR_INVALID_VALUE;
563
564   return 0;
565 }
566
567
568 int
569 ipsec_set_interface_sa (vnet_main_t * vnm, u32 hw_if_index, u32 sa_id,
570                         u8 is_outbound)
571 {
572   ipsec_main_t *im = &ipsec_main;
573   vnet_hw_interface_t *hi;
574   ipsec_tunnel_if_t *t;
575   ipsec_sa_t *sa, *old_sa;
576   u32 sa_index, old_sa_index;
577   uword *p;
578
579   hi = vnet_get_hw_interface (vnm, hw_if_index);
580   t = pool_elt_at_index (im->tunnel_interfaces, hi->dev_instance);
581
582   sa_index = ipsec_get_sa_index_by_sa_id (sa_id);
583   if (sa_index == ~0)
584     {
585       clib_warning ("SA with ID %u not found", sa_id);
586       return VNET_API_ERROR_INVALID_VALUE;
587     }
588
589   if (ipsec_is_sa_used (sa_index))
590     {
591       clib_warning ("SA with ID %u is already in use", sa_id);
592       return VNET_API_ERROR_INVALID_VALUE;
593     }
594
595   sa = pool_elt_at_index (im->sad, sa_index);
596
597   if (!is_outbound)
598     {
599       old_sa_index = t->input_sa_index;
600       old_sa = pool_elt_at_index (im->sad, old_sa_index);
601
602       if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa) ^
603           ipsec_sa_is_set_IS_TUNNEL_V6 (old_sa))
604         {
605           clib_warning ("IPsec interface SA endpoints type can't be changed");
606           return VNET_API_ERROR_INVALID_VALUE;
607         }
608
609       if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa))
610         {
611           ipsec6_tunnel_key_t key;
612
613           /* unset old inbound hash entry. packets should stop arriving */
614           key.remote_ip = old_sa->tunnel_src_addr.ip6;
615           key.spi = clib_host_to_net_u32 (old_sa->spi);
616
617           p = hash_get_mem (im->ipsec6_if_pool_index_by_key, &key);
618           if (p)
619             hash_unset_mem_free (&im->ipsec6_if_pool_index_by_key, &key);
620
621           /* set new inbound SA, then set new hash entry */
622           t->input_sa_index = sa_index;
623           key.remote_ip = sa->tunnel_src_addr.ip6;
624           key.spi = clib_host_to_net_u32 (sa->spi);
625
626           hash_set_mem_alloc (&im->ipsec6_if_pool_index_by_key, &key,
627                               hi->dev_instance);
628         }
629       else
630         {
631           ipsec4_tunnel_key_t key;
632
633           /* unset old inbound hash entry. packets should stop arriving */
634           key.remote_ip = old_sa->tunnel_src_addr.ip4.as_u32;
635           key.spi = clib_host_to_net_u32 (old_sa->spi);
636
637           p = hash_get (im->ipsec4_if_pool_index_by_key, key.as_u64);
638           if (p)
639             hash_unset (im->ipsec4_if_pool_index_by_key, key.as_u64);
640
641           /* set new inbound SA, then set new hash entry */
642           t->input_sa_index = sa_index;
643           key.remote_ip = sa->tunnel_src_addr.ip4.as_u32;
644           key.spi = clib_host_to_net_u32 (sa->spi);
645
646           hash_set (im->ipsec4_if_pool_index_by_key, key.as_u64,
647                     hi->dev_instance);
648         }
649     }
650   else
651     {
652       old_sa_index = t->output_sa_index;
653       old_sa = pool_elt_at_index (im->sad, old_sa_index);
654
655       if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa) ^
656           ipsec_sa_is_set_IS_TUNNEL_V6 (old_sa))
657         {
658           clib_warning ("IPsec interface SA endpoints type can't be changed");
659           return VNET_API_ERROR_INVALID_VALUE;
660         }
661
662       /*
663        * re-enable the feature to get the new SA in
664        * the workers are stopped so no packets are sent in the clear
665        */
666       ipsec_tunnel_feature_set (t, 0);
667       t->output_sa_index = sa_index;
668       ipsec_tunnel_feature_set (t, 1);
669     }
670
671   /* remove sa_id to sa_index mapping on old SA */
672   if (ipsec_get_sa_index_by_sa_id (old_sa->id) == old_sa_index)
673     hash_unset (im->sa_index_by_sa_id, old_sa->id);
674
675   if (ipsec_add_del_sa_sess_cb (im, old_sa_index, 0))
676     {
677       clib_warning ("IPsec backend add/del callback returned error");
678       return VNET_API_ERROR_SYSCALL_ERROR_1;
679     }
680   ipsec_sa_del (old_sa->id);
681
682   return 0;
683 }
684
685
686 clib_error_t *
687 ipsec_tunnel_if_init (vlib_main_t * vm)
688 {
689   ipsec_main_t *im = &ipsec_main;
690
691   /* initialize the ipsec-if ip4 hash */
692   im->ipsec4_if_pool_index_by_key =
693     hash_create (0, sizeof (ipsec4_tunnel_key_t));
694   /* initialize the ipsec-if ip6 hash */
695   im->ipsec6_if_pool_index_by_key = hash_create_mem (0,
696                                                      sizeof
697                                                      (ipsec6_tunnel_key_t),
698                                                      sizeof (uword));
699   im->ipsec_if_real_dev_by_show_dev = hash_create (0, sizeof (uword));
700
701   udp_register_dst_port (vm, UDP_DST_PORT_ipsec, ipsec4_if_input_node.index,
702                          1);
703   return 0;
704 }
705
706 VLIB_INIT_FUNCTION (ipsec_tunnel_if_init);
707
708
709 /*
710  * fd.io coding-style-patch-verification: ON
711  *
712  * Local Variables:
713  * eval: (c-set-style "gnu")
714  * End:
715  */