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/ipsec/ipsec_io.h>
20 ipsec_add_del_spd (vlib_main_t * vm, u32 spd_id, int is_add)
22 ipsec_main_t *im = &ipsec_main;
24 ipsec_spd_fp_t *fp_spd = 0;
28 p = hash_get (im->spd_index_by_spd_id, spd_id);
30 return VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
32 return VNET_API_ERROR_NO_SUCH_ENTRY;
34 if (!is_add) /* delete */
37 spd = pool_elt_at_index (im->spds, spd_index);
39 return VNET_API_ERROR_INVALID_VALUE;
41 hash_foreach (k, v, im->spd_index_by_sw_if_index, ({
43 ipsec_set_interface_spd(vm, k, spd_id, 0);
45 hash_unset (im->spd_index_by_spd_id, spd_id);
46 #define _(s,v) vec_free(spd->policies[IPSEC_SPD_POLICY_##s]);
47 foreach_ipsec_spd_policy_type
50 fp_spd = &spd->fp_spd;
52 if (im->fp_spd_ipv4_out_is_enabled)
54 if (fp_spd->ip4_out_lookup_hash_idx != INDEX_INVALID)
56 clib_bihash_16_8_t *bihash_table =
57 pool_elt_at_index (im->fp_ip4_lookup_hashes_pool,
58 fp_spd->ip4_out_lookup_hash_idx);
60 clib_bihash_free_16_8 (bihash_table);
61 vec_free (fp_spd->name4_out);
62 pool_put_index (im->fp_ip4_lookup_hashes_pool,
63 fp_spd->ip4_out_lookup_hash_idx);
67 if (im->fp_spd_ipv4_in_is_enabled)
69 if (fp_spd->ip4_in_lookup_hash_idx != INDEX_INVALID)
71 clib_bihash_16_8_t *bihash_table = pool_elt_at_index (
72 im->fp_ip4_lookup_hashes_pool, fp_spd->ip4_in_lookup_hash_idx);
74 clib_bihash_free_16_8 (bihash_table);
75 vec_free (fp_spd->name4_in);
76 pool_put_index (im->fp_ip4_lookup_hashes_pool,
77 fp_spd->ip4_in_lookup_hash_idx);
81 if (im->fp_spd_ipv6_out_is_enabled)
83 if (fp_spd->ip6_out_lookup_hash_idx != INDEX_INVALID)
85 clib_bihash_40_8_t *bihash_table =
86 pool_elt_at_index (im->fp_ip6_lookup_hashes_pool,
87 fp_spd->ip6_out_lookup_hash_idx);
89 clib_bihash_free_40_8 (bihash_table);
90 vec_free (fp_spd->name6_out);
91 pool_put_index (im->fp_ip6_lookup_hashes_pool,
92 fp_spd->ip6_out_lookup_hash_idx);
95 if (im->fp_spd_ipv6_in_is_enabled)
97 if (fp_spd->ip6_in_lookup_hash_idx != INDEX_INVALID)
99 clib_bihash_40_8_t *bihash_table = pool_elt_at_index (
100 im->fp_ip6_lookup_hashes_pool, fp_spd->ip6_in_lookup_hash_idx);
102 clib_bihash_free_40_8 (bihash_table);
103 vec_free (fp_spd->name6_in);
104 pool_put_index (im->fp_ip6_lookup_hashes_pool,
105 fp_spd->ip6_in_lookup_hash_idx);
109 pool_put (im->spds, spd);
111 else /* create new SPD */
113 pool_get (im->spds, spd);
114 clib_memset (spd, 0, sizeof (*spd));
115 spd_index = spd - im->spds;
117 hash_set (im->spd_index_by_spd_id, spd_id, spd_index);
119 fp_spd = &spd->fp_spd;
120 fp_spd->ip4_out_lookup_hash_idx = INDEX_INVALID;
121 fp_spd->ip4_in_lookup_hash_idx = INDEX_INVALID;
122 fp_spd->ip6_out_lookup_hash_idx = INDEX_INVALID;
123 fp_spd->ip6_in_lookup_hash_idx = INDEX_INVALID;
125 if (im->fp_spd_ipv4_out_is_enabled)
127 if (pool_elts (im->fp_ip4_lookup_hashes_pool) <
128 pool_max_len (im->fp_ip4_lookup_hashes_pool))
130 clib_bihash_16_8_t *bihash_table;
131 fp_spd->name4_out = format (0, "spd_%u_fp_ip4_out", spd_id);
133 pool_get (im->fp_ip4_lookup_hashes_pool, bihash_table);
134 fp_spd->ip4_out_lookup_hash_idx =
135 bihash_table - im->fp_ip4_lookup_hashes_pool;
136 clib_bihash_init_16_8 (bihash_table, (char *) fp_spd->name4_out,
137 im->fp_lookup_hash_buckets,
138 im->fp_lookup_hash_buckets *
139 IPSEC_FP_IP4_HASH_MEM_PER_BUCKET);
143 if (im->fp_spd_ipv4_in_is_enabled)
145 if (pool_elts (im->fp_ip4_lookup_hashes_pool) <
146 pool_max_len (im->fp_ip4_lookup_hashes_pool))
148 clib_bihash_16_8_t *bihash_table;
149 fp_spd->name4_in = format (0, "spd_%u_fp_ip4_in", spd_id);
151 pool_get (im->fp_ip4_lookup_hashes_pool, bihash_table);
152 fp_spd->ip4_in_lookup_hash_idx =
153 bihash_table - im->fp_ip4_lookup_hashes_pool;
154 clib_bihash_init_16_8 (bihash_table, (char *) fp_spd->name4_in,
155 im->fp_lookup_hash_buckets,
156 im->fp_lookup_hash_buckets *
157 IPSEC_FP_IP4_HASH_MEM_PER_BUCKET);
160 if (im->fp_spd_ipv6_out_is_enabled)
162 if (pool_elts (im->fp_ip6_lookup_hashes_pool) <
163 pool_max_len (im->fp_ip6_lookup_hashes_pool))
165 clib_bihash_40_8_t *bihash_table;
167 fp_spd->name6_out = format (0, "spd_%u_fp_ip6_out", spd_id);
168 pool_get (im->fp_ip6_lookup_hashes_pool, bihash_table);
169 fp_spd->ip6_out_lookup_hash_idx =
170 bihash_table - im->fp_ip6_lookup_hashes_pool;
171 clib_bihash_init_40_8 (bihash_table, (char *) fp_spd->name6_out,
172 im->fp_lookup_hash_buckets,
173 im->fp_lookup_hash_buckets *
174 IPSEC_FP_IP6_HASH_MEM_PER_BUCKET);
177 if (im->fp_spd_ipv6_in_is_enabled)
179 if (pool_elts (im->fp_ip6_lookup_hashes_pool) <
180 pool_max_len (im->fp_ip6_lookup_hashes_pool))
182 clib_bihash_40_8_t *bihash_table;
184 fp_spd->name6_in = format (0, "spd_%u_fp_ip6_in", spd_id);
185 pool_get (im->fp_ip6_lookup_hashes_pool, bihash_table);
186 fp_spd->ip6_in_lookup_hash_idx =
187 bihash_table - im->fp_ip6_lookup_hashes_pool;
188 clib_bihash_init_40_8 (bihash_table, (char *) fp_spd->name6_in,
189 im->fp_lookup_hash_buckets,
190 im->fp_lookup_hash_buckets *
191 IPSEC_FP_IP6_HASH_MEM_PER_BUCKET);
199 ipsec_set_interface_spd (vlib_main_t * vm, u32 sw_if_index, u32 spd_id,
202 ipsec_main_t *im = &ipsec_main;
203 ip4_ipsec_config_t config;
208 p = hash_get (im->spd_index_by_spd_id, spd_id);
210 return VNET_API_ERROR_SYSCALL_ERROR_1; /* no such spd-id */
214 p = hash_get (im->spd_index_by_sw_if_index, sw_if_index);
216 return VNET_API_ERROR_SYSCALL_ERROR_2; /* spd already assigned */
220 hash_set (im->spd_index_by_sw_if_index, sw_if_index, spd_index);
224 hash_unset (im->spd_index_by_sw_if_index, sw_if_index);
227 /* enable IPsec on TX */
228 vnet_feature_enable_disable ("ip4-output", "ipsec4-output-feature",
229 sw_if_index, is_add, 0, 0);
230 vnet_feature_enable_disable ("ip6-output", "ipsec6-output-feature",
231 sw_if_index, is_add, 0, 0);
233 config.spd_index = spd_index;
235 /* enable IPsec on RX */
236 vnet_feature_enable_disable ("ip4-unicast", "ipsec4-input-feature",
237 sw_if_index, is_add, &config, sizeof (config));
238 vnet_feature_enable_disable ("ip6-unicast", "ipsec6-input-feature",
239 sw_if_index, is_add, &config, sizeof (config));
245 * fd.io coding-style-patch-verification: ON
248 * eval: (c-set-style "gnu")