aecb50dd34670294a5b74ad2c94007247c0b4325
[vpp.git] / src / vnet / ipsec / ipsec_spd.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <vnet/ipsec/ipsec.h>
17 #include <vnet/ipsec/ipsec_io.h>
18
19 int
20 ipsec_add_del_spd (vlib_main_t * vm, u32 spd_id, int is_add)
21 {
22   ipsec_main_t *im = &ipsec_main;
23   ipsec_spd_t *spd = 0;
24   uword *p;
25   u32 spd_index, k, v;
26
27   p = hash_get (im->spd_index_by_spd_id, spd_id);
28   if (p && is_add)
29     return VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
30   if (!p && !is_add)
31     return VNET_API_ERROR_NO_SUCH_ENTRY;
32
33   if (!is_add)                  /* delete */
34     {
35       spd_index = p[0];
36       spd = pool_elt_at_index (im->spds, spd_index);
37       if (!spd)
38         return VNET_API_ERROR_INVALID_VALUE;
39       /* *INDENT-OFF* */
40       hash_foreach (k, v, im->spd_index_by_sw_if_index, ({
41         if (v == spd_index)
42           ipsec_set_interface_spd(vm, k, spd_id, 0);
43       }));
44       /* *INDENT-ON* */
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
48 #undef _
49         if (im->fp_spd_is_enabled)
50       {
51         ipsec_spd_fp_t *fp_spd = &spd->fp_spd;
52
53         clib_bihash_free_16_8 (&fp_spd->fp_ip4_lookup_hash);
54       }
55       pool_put (im->spds, spd);
56     }
57   else                          /* create new SPD */
58     {
59       pool_get (im->spds, spd);
60       clib_memset (spd, 0, sizeof (*spd));
61       spd_index = spd - im->spds;
62       spd->id = spd_id;
63       hash_set (im->spd_index_by_spd_id, spd_id, spd_index);
64       if (im->fp_spd_is_enabled)
65         {
66           ipsec_spd_fp_t *fp_spd = &spd->fp_spd;
67
68           clib_bihash_init_16_8 (
69             &fp_spd->fp_ip4_lookup_hash, "SPD_FP ip4 rules lookup bihash",
70             im->fp_lookup_hash_buckets,
71             im->fp_lookup_hash_buckets * IPSEC_FP_IP4_HASH_MEM_PER_BUCKET);
72         }
73     }
74   return 0;
75 }
76
77 int
78 ipsec_set_interface_spd (vlib_main_t * vm, u32 sw_if_index, u32 spd_id,
79                          int is_add)
80 {
81   ipsec_main_t *im = &ipsec_main;
82   ip4_ipsec_config_t config;
83
84   u32 spd_index;
85   uword *p;
86
87   p = hash_get (im->spd_index_by_spd_id, spd_id);
88   if (!p)
89     return VNET_API_ERROR_SYSCALL_ERROR_1;      /* no such spd-id */
90
91   spd_index = p[0];
92
93   p = hash_get (im->spd_index_by_sw_if_index, sw_if_index);
94   if (p && is_add)
95     return VNET_API_ERROR_SYSCALL_ERROR_2;      /* spd already assigned */
96
97   if (is_add)
98     {
99       hash_set (im->spd_index_by_sw_if_index, sw_if_index, spd_index);
100     }
101   else
102     {
103       hash_unset (im->spd_index_by_sw_if_index, sw_if_index);
104     }
105
106   /* enable IPsec on TX */
107   vnet_feature_enable_disable ("ip4-output", "ipsec4-output-feature",
108                                sw_if_index, is_add, 0, 0);
109   vnet_feature_enable_disable ("ip6-output", "ipsec6-output-feature",
110                                sw_if_index, is_add, 0, 0);
111
112   config.spd_index = spd_index;
113
114   /* enable IPsec on RX */
115   vnet_feature_enable_disable ("ip4-unicast", "ipsec4-input-feature",
116                                sw_if_index, is_add, &config, sizeof (config));
117   vnet_feature_enable_disable ("ip6-unicast", "ipsec6-input-feature",
118                                sw_if_index, is_add, &config, sizeof (config));
119
120   return 0;
121 }
122
123 /*
124  * fd.io coding-style-patch-verification: ON
125  *
126  * Local Variables:
127  * eval: (c-set-style "gnu")
128  * End:
129  */