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;
27 p = hash_get (im->spd_index_by_spd_id, spd_id);
29 return VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
31 return VNET_API_ERROR_NO_SUCH_ENTRY;
33 if (!is_add) /* delete */
36 spd = pool_elt_at_index (im->spds, spd_index);
38 return VNET_API_ERROR_INVALID_VALUE;
40 hash_foreach (k, v, im->spd_index_by_sw_if_index, ({
42 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
49 pool_put (im->spds, spd);
51 else /* create new SPD */
53 pool_get (im->spds, spd);
54 clib_memset (spd, 0, sizeof (*spd));
55 spd_index = spd - im->spds;
57 hash_set (im->spd_index_by_spd_id, spd_id, spd_index);
63 ipsec_set_interface_spd (vlib_main_t * vm, u32 sw_if_index, u32 spd_id,
66 ipsec_main_t *im = &ipsec_main;
67 ip4_ipsec_config_t config;
72 p = hash_get (im->spd_index_by_spd_id, spd_id);
74 return VNET_API_ERROR_SYSCALL_ERROR_1; /* no such spd-id */
78 p = hash_get (im->spd_index_by_sw_if_index, sw_if_index);
80 return VNET_API_ERROR_SYSCALL_ERROR_2; /* spd already assigned */
84 hash_set (im->spd_index_by_sw_if_index, sw_if_index, spd_index);
88 hash_unset (im->spd_index_by_sw_if_index, sw_if_index);
91 /* enable IPsec on TX */
92 vnet_feature_enable_disable ("ip4-output", "ipsec4-output-feature",
93 sw_if_index, is_add, 0, 0);
94 vnet_feature_enable_disable ("ip6-output", "ipsec6-output-feature",
95 sw_if_index, is_add, 0, 0);
97 config.spd_index = spd_index;
99 /* enable IPsec on RX */
100 vnet_feature_enable_disable ("ip4-unicast", "ipsec4-input-feature",
101 sw_if_index, is_add, &config, sizeof (config));
102 vnet_feature_enable_disable ("ip6-unicast", "ipsec6-input-feature",
103 sw_if_index, is_add, &config, sizeof (config));
109 * fd.io coding-style-patch-verification: ON
112 * eval: (c-set-style "gnu")