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;
42 hash_foreach (k, v, im->spd_index_by_sw_if_index, ({
44 ipsec_set_interface_spd(vm, k, spd_id, 0);
47 hash_unset (im->spd_index_by_spd_id, spd_id);
48 #define _(s,v) vec_free(spd->policies[IPSEC_SPD_POLICY_##s]);
49 foreach_ipsec_spd_policy_type
52 fp_spd = &spd->fp_spd;
54 if (im->fp_spd_ipv4_out_is_enabled)
56 if (fp_spd->ip4_out_lookup_hash_idx != INDEX_INVALID)
58 clib_bihash_16_8_t *bihash_table =
59 pool_elt_at_index (im->fp_ip4_lookup_hashes_pool,
60 fp_spd->ip4_out_lookup_hash_idx);
62 clib_bihash_free_16_8 (bihash_table);
63 vec_free (fp_spd->name4_out);
64 pool_put_index (im->fp_ip4_lookup_hashes_pool,
65 fp_spd->ip4_out_lookup_hash_idx);
69 if (im->fp_spd_ipv4_in_is_enabled)
71 if (fp_spd->ip4_in_lookup_hash_idx != INDEX_INVALID)
73 clib_bihash_16_8_t *bihash_table = pool_elt_at_index (
74 im->fp_ip4_lookup_hashes_pool, fp_spd->ip4_in_lookup_hash_idx);
76 clib_bihash_free_16_8 (bihash_table);
77 vec_free (fp_spd->name4_in);
78 pool_put_index (im->fp_ip4_lookup_hashes_pool,
79 fp_spd->ip4_in_lookup_hash_idx);
83 if (im->fp_spd_ipv6_out_is_enabled)
85 if (fp_spd->ip6_out_lookup_hash_idx != INDEX_INVALID)
87 clib_bihash_40_8_t *bihash_table =
88 pool_elt_at_index (im->fp_ip6_lookup_hashes_pool,
89 fp_spd->ip6_out_lookup_hash_idx);
91 clib_bihash_free_40_8 (bihash_table);
92 vec_free (fp_spd->name6_out);
93 pool_put_index (im->fp_ip6_lookup_hashes_pool,
94 fp_spd->ip6_out_lookup_hash_idx);
97 if (im->fp_spd_ipv6_in_is_enabled)
99 if (fp_spd->ip6_in_lookup_hash_idx != INDEX_INVALID)
101 clib_bihash_40_8_t *bihash_table = pool_elt_at_index (
102 im->fp_ip6_lookup_hashes_pool, fp_spd->ip6_in_lookup_hash_idx);
104 clib_bihash_free_40_8 (bihash_table);
105 vec_free (fp_spd->name6_in);
106 pool_put_index (im->fp_ip6_lookup_hashes_pool,
107 fp_spd->ip6_in_lookup_hash_idx);
111 pool_put (im->spds, spd);
113 else /* create new SPD */
115 pool_get (im->spds, spd);
116 clib_memset (spd, 0, sizeof (*spd));
117 spd_index = spd - im->spds;
119 hash_set (im->spd_index_by_spd_id, spd_id, spd_index);
121 fp_spd = &spd->fp_spd;
122 fp_spd->ip4_out_lookup_hash_idx = INDEX_INVALID;
123 fp_spd->ip4_in_lookup_hash_idx = INDEX_INVALID;
124 fp_spd->ip6_out_lookup_hash_idx = INDEX_INVALID;
125 fp_spd->ip6_in_lookup_hash_idx = INDEX_INVALID;
127 if (im->fp_spd_ipv4_out_is_enabled)
129 if (pool_elts (im->fp_ip4_lookup_hashes_pool) <
130 pool_max_len (im->fp_ip4_lookup_hashes_pool))
132 clib_bihash_16_8_t *bihash_table;
133 fp_spd->name4_out = format (0, "spd_%u_fp_ip4_out", spd_id);
135 pool_get (im->fp_ip4_lookup_hashes_pool, bihash_table);
136 fp_spd->ip4_out_lookup_hash_idx =
137 bihash_table - im->fp_ip4_lookup_hashes_pool;
138 clib_bihash_init_16_8 (bihash_table, (char *) fp_spd->name4_out,
139 im->fp_lookup_hash_buckets,
140 im->fp_lookup_hash_buckets *
141 IPSEC_FP_IP4_HASH_MEM_PER_BUCKET);
145 if (im->fp_spd_ipv4_in_is_enabled)
147 if (pool_elts (im->fp_ip4_lookup_hashes_pool) <
148 pool_max_len (im->fp_ip4_lookup_hashes_pool))
150 clib_bihash_16_8_t *bihash_table;
151 fp_spd->name4_in = format (0, "spd_%u_fp_ip4_in", spd_id);
153 pool_get (im->fp_ip4_lookup_hashes_pool, bihash_table);
154 fp_spd->ip4_in_lookup_hash_idx =
155 bihash_table - im->fp_ip4_lookup_hashes_pool;
156 clib_bihash_init_16_8 (bihash_table, (char *) fp_spd->name4_in,
157 im->fp_lookup_hash_buckets,
158 im->fp_lookup_hash_buckets *
159 IPSEC_FP_IP4_HASH_MEM_PER_BUCKET);
162 if (im->fp_spd_ipv6_out_is_enabled)
164 if (pool_elts (im->fp_ip6_lookup_hashes_pool) <
165 pool_max_len (im->fp_ip6_lookup_hashes_pool))
167 clib_bihash_40_8_t *bihash_table;
169 fp_spd->name6_out = format (0, "spd_%u_fp_ip6_out", spd_id);
170 pool_get (im->fp_ip6_lookup_hashes_pool, bihash_table);
171 fp_spd->ip6_out_lookup_hash_idx =
172 bihash_table - im->fp_ip6_lookup_hashes_pool;
173 clib_bihash_init_40_8 (bihash_table, (char *) fp_spd->name6_out,
174 im->fp_lookup_hash_buckets,
175 im->fp_lookup_hash_buckets *
176 IPSEC_FP_IP6_HASH_MEM_PER_BUCKET);
179 if (im->fp_spd_ipv6_in_is_enabled)
181 if (pool_elts (im->fp_ip6_lookup_hashes_pool) <
182 pool_max_len (im->fp_ip6_lookup_hashes_pool))
184 clib_bihash_40_8_t *bihash_table;
186 fp_spd->name6_in = format (0, "spd_%u_fp_ip6_in", spd_id);
187 pool_get (im->fp_ip6_lookup_hashes_pool, bihash_table);
188 fp_spd->ip6_in_lookup_hash_idx =
189 bihash_table - im->fp_ip6_lookup_hashes_pool;
190 clib_bihash_init_40_8 (bihash_table, (char *) fp_spd->name6_in,
191 im->fp_lookup_hash_buckets,
192 im->fp_lookup_hash_buckets *
193 IPSEC_FP_IP6_HASH_MEM_PER_BUCKET);
201 ipsec_set_interface_spd (vlib_main_t * vm, u32 sw_if_index, u32 spd_id,
204 ipsec_main_t *im = &ipsec_main;
205 ip4_ipsec_config_t config;
210 p = hash_get (im->spd_index_by_spd_id, spd_id);
212 return VNET_API_ERROR_SYSCALL_ERROR_1; /* no such spd-id */
216 p = hash_get (im->spd_index_by_sw_if_index, sw_if_index);
218 return VNET_API_ERROR_SYSCALL_ERROR_2; /* spd already assigned */
222 hash_set (im->spd_index_by_sw_if_index, sw_if_index, spd_index);
226 hash_unset (im->spd_index_by_sw_if_index, sw_if_index);
229 /* enable IPsec on TX */
230 vnet_feature_enable_disable ("ip4-output", "ipsec4-output-feature",
231 sw_if_index, is_add, 0, 0);
232 vnet_feature_enable_disable ("ip6-output", "ipsec6-output-feature",
233 sw_if_index, is_add, 0, 0);
235 config.spd_index = spd_index;
237 /* enable IPsec on RX */
238 vnet_feature_enable_disable ("ip4-unicast", "ipsec4-input-feature",
239 sw_if_index, is_add, &config, sizeof (config));
240 vnet_feature_enable_disable ("ip6-unicast", "ipsec6-input-feature",
241 sw_if_index, is_add, &config, sizeof (config));
247 * fd.io coding-style-patch-verification: ON
250 * eval: (c-set-style "gnu")