2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <vnet/ipsec/ipsec.h>
17 #include <vnet/fib/fib_table.h>
20 ipsec_call_add_del_callbacks (ipsec_main_t * im, ipsec_sa_t * sa,
21 u32 sa_index, int is_add)
23 ipsec_ah_backend_t *ab;
24 ipsec_esp_backend_t *eb;
27 case IPSEC_PROTOCOL_AH:
28 ab = pool_elt_at_index (im->ah_backends, im->ah_current_backend);
29 if (ab->add_del_sa_sess_cb)
30 return ab->add_del_sa_sess_cb (sa_index, is_add);
32 case IPSEC_PROTOCOL_ESP:
33 eb = pool_elt_at_index (im->esp_backends, im->esp_current_backend);
34 if (eb->add_del_sa_sess_cb)
35 return eb->add_del_sa_sess_cb (sa_index, is_add);
42 ipsec_mk_key (ipsec_key_t * key, const u8 * data, u8 len)
44 memset (key, 0, sizeof (*key));
46 if (len > sizeof (key->data))
47 key->len = sizeof (key->data);
51 memcpy (key->data, data, key->len);
55 * 'stack' (resolve the recursion for) the SA tunnel destination
58 ipsec_sa_stack (ipsec_sa_t * sa)
60 fib_forward_chain_type_t fct;
61 dpo_id_t tmp = DPO_INVALID;
64 fct = fib_forw_chain_type_from_fib_proto ((sa->is_tunnel_ip6 ?
68 fib_entry_contribute_forwarding (sa->fib_entry_index, fct, &tmp);
70 node = vlib_get_node_by_name (vlib_get_main (),
72 (u8 *) "ah6-encrypt" :
73 (u8 *) "ah4-encrypt"));
74 dpo_stack_from_node (node->index, &sa->dpo[IPSEC_PROTOCOL_AH], &tmp);
76 node = vlib_get_node_by_name (vlib_get_main (),
78 (u8 *) "esp6-encrypt" :
79 (u8 *) "esp4-encrypt"));
80 dpo_stack_from_node (node->index, &sa->dpo[IPSEC_PROTOCOL_ESP], &tmp);
86 ipsec_protocol_t proto,
87 ipsec_crypto_alg_t crypto_alg,
88 const ipsec_key_t * ck,
89 ipsec_integ_alg_t integ_alg,
90 const ipsec_key_t * ik,
91 ipsec_sa_flags_t flags,
93 const ip46_address_t * tun_src,
94 const ip46_address_t * tun_dst, u32 * sa_out_index)
96 ipsec_main_t *im = &ipsec_main;
102 p = hash_get (im->sa_index_by_sa_id, id);
104 return VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
106 pool_get_zero (im->sad, sa);
108 fib_node_init (&sa->node, FIB_NODE_TYPE_IPSEC_SA);
109 sa_index = sa - im->sad;
113 sa->protocol = proto;
114 sa->crypto_alg = crypto_alg;
115 clib_memcpy (&sa->crypto_key, ck, sizeof (sa->crypto_key));
116 sa->integ_alg = integ_alg;
117 clib_memcpy (&sa->integ_key, ik, sizeof (sa->integ_key));
118 ip46_address_copy (&sa->tunnel_src_addr, tun_src);
119 ip46_address_copy (&sa->tunnel_dst_addr, tun_dst);
121 if (flags & IPSEC_SA_FLAG_USE_EXTENDED_SEQ_NUM)
123 if (flags & IPSEC_SA_FLAG_USE_ANTI_REPLAY)
124 sa->use_anti_replay = 1;
125 if (flags & IPSEC_SA_FLAG_IS_TUNNEL)
127 if (flags & IPSEC_SA_FLAG_IS_TUNNEL_V6)
128 sa->is_tunnel_ip6 = 1;
129 if (flags & IPSEC_SA_FLAG_UDP_ENCAP)
132 err = ipsec_check_support_cb (im, sa);
135 clib_warning ("%s", err->what);
136 pool_put (im->sad, sa);
137 return VNET_API_ERROR_UNIMPLEMENTED;
140 err = ipsec_call_add_del_callbacks (im, sa, sa_index, 1);
143 pool_put (im->sad, sa);
144 return VNET_API_ERROR_SYSCALL_ERROR_1;
149 fib_protocol_t fproto = (sa->is_tunnel_ip6 ?
150 FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
152 .fp_addr = sa->tunnel_dst_addr,
153 .fp_len = (sa->is_tunnel_ip6 ? 128 : 32),
156 sa->tx_fib_index = fib_table_find (fproto, tx_table_id);
157 if (sa->tx_fib_index == ~((u32) 0))
159 pool_put (im->sad, sa);
160 return VNET_API_ERROR_NO_SUCH_FIB;
163 sa->fib_entry_index = fib_table_entry_special_add (sa->tx_fib_index,
166 FIB_ENTRY_FLAG_NONE);
167 sa->sibling = fib_entry_child_add (sa->fib_entry_index,
168 FIB_NODE_TYPE_IPSEC_SA, sa_index);
171 hash_set (im->sa_index_by_sa_id, sa->id, sa_index);
174 *sa_out_index = sa_index;
180 ipsec_sa_del (u32 id)
182 ipsec_main_t *im = &ipsec_main;
188 p = hash_get (im->sa_index_by_sa_id, id);
191 return VNET_API_ERROR_NO_SUCH_ENTRY;
194 sa = pool_elt_at_index (im->sad, sa_index);
195 if (ipsec_is_sa_used (sa_index))
197 clib_warning ("sa_id %u used in policy", sa->id);
198 /* sa used in policy */
199 return VNET_API_ERROR_SYSCALL_ERROR_1;
201 hash_unset (im->sa_index_by_sa_id, sa->id);
202 err = ipsec_call_add_del_callbacks (im, sa, sa_index, 0);
204 return VNET_API_ERROR_SYSCALL_ERROR_1;
207 fib_entry_child_remove (sa->fib_entry_index, sa->sibling);
208 fib_table_entry_special_remove
210 fib_entry_get_prefix (sa->fib_entry_index), FIB_SOURCE_RR);
211 dpo_reset (&sa->dpo[IPSEC_PROTOCOL_AH]);
212 dpo_reset (&sa->dpo[IPSEC_PROTOCOL_ESP]);
214 pool_put (im->sad, sa);
219 ipsec_is_sa_used (u32 sa_index)
221 ipsec_main_t *im = &ipsec_main;
222 ipsec_tunnel_if_t *t;
226 pool_foreach(p, im->policies, ({
227 if (p->policy == IPSEC_POLICY_ACTION_PROTECT)
229 if (p->sa_index == sa_index)
234 pool_foreach(t, im->tunnel_interfaces, ({
235 if (t->input_sa_index == sa_index)
237 if (t->output_sa_index == sa_index)
246 ipsec_set_sa_key (u32 id, const ipsec_key_t * ck, const ipsec_key_t * ik)
248 ipsec_main_t *im = &ipsec_main;
254 p = hash_get (im->sa_index_by_sa_id, id);
256 return VNET_API_ERROR_SYSCALL_ERROR_1; /* no such sa-id */
259 sa = pool_elt_at_index (im->sad, sa_index);
264 clib_memcpy (&sa->crypto_key, ck, sizeof (sa->crypto_key));
270 clib_memcpy (&sa->integ_key, 0, sizeof (sa->integ_key));
275 err = ipsec_call_add_del_callbacks (im, sa, sa_index, 0);
277 return VNET_API_ERROR_SYSCALL_ERROR_1;
284 ipsec_get_sa_index_by_sa_id (u32 sa_id)
286 ipsec_main_t *im = &ipsec_main;
287 uword *p = hash_get (im->sa_index_by_sa_id, sa_id);
295 * Function definition to get a FIB node from its index
298 ipsec_sa_fib_node_get (fib_node_index_t index)
304 sa = pool_elt_at_index (im->sad, index);
310 * Function definition to inform the FIB node that its last lock has gone.
313 ipsec_sa_last_lock_gone (fib_node_t * node)
316 * The ipsec SA is a root of the graph. As such
317 * it never has children and thus is never locked.
323 ipsec_sa_from_fib_node (fib_node_t * node)
325 ASSERT (FIB_NODE_TYPE_IPSEC_SA == node->fn_type);
326 return ((ipsec_sa_t *) (((char *) node) -
327 STRUCT_OFFSET_OF (ipsec_sa_t, node)));
332 * Function definition to backwalk a FIB node
334 static fib_node_back_walk_rc_t
335 ipsec_sa_back_walk (fib_node_t * node, fib_node_back_walk_ctx_t * ctx)
337 ipsec_sa_stack (ipsec_sa_from_fib_node (node));
339 return (FIB_NODE_BACK_WALK_CONTINUE);
343 * Virtual function table registered by MPLS GRE tunnels
344 * for participation in the FIB object graph.
346 const static fib_node_vft_t ipsec_sa_vft = {
347 .fnv_get = ipsec_sa_fib_node_get,
348 .fnv_last_lock = ipsec_sa_last_lock_gone,
349 .fnv_back_walk = ipsec_sa_back_walk,
352 /* force inclusion from application's main.c */
354 ipsec_sa_interface_init (vlib_main_t * vm)
356 fib_node_register_type (FIB_NODE_TYPE_IPSEC_SA, &ipsec_sa_vft);
361 VLIB_INIT_FUNCTION (ipsec_sa_interface_init);
364 * fd.io coding-style-patch-verification: ON
367 * eval: (c-set-style "gnu")