dpdk: add ipsec cryptodev support
[vpp.git] / vnet / vnet / ipsec / ipsec.h
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 #ifndef __IPSEC_H__
16 #define __IPSEC_H__
17
18 #define IPSEC_FLAG_IPSEC_GRE_TUNNEL (1 << 0)
19
20 #define foreach_ipsec_policy_action \
21   _(0, BYPASS,  "bypass")          \
22   _(1, DISCARD, "discard")         \
23   _(2, RESOLVE, "resolve")         \
24   _(3, PROTECT, "protect")
25
26 typedef enum
27 {
28 #define _(v,f,s) IPSEC_POLICY_ACTION_##f = v,
29   foreach_ipsec_policy_action
30 #undef _
31     IPSEC_POLICY_N_ACTION,
32 } ipsec_policy_action_t;
33
34 #define foreach_ipsec_crypto_alg \
35   _(0, NONE,  "none")               \
36   _(1, AES_CBC_128, "aes-cbc-128")  \
37   _(2, AES_CBC_192, "aes-cbc-192")  \
38   _(3, AES_CBC_256, "aes-cbc-256")
39
40 typedef enum
41 {
42 #define _(v,f,s) IPSEC_CRYPTO_ALG_##f = v,
43   foreach_ipsec_crypto_alg
44 #undef _
45     IPSEC_CRYPTO_N_ALG,
46 } ipsec_crypto_alg_t;
47
48 #define foreach_ipsec_integ_alg \
49   _(0, NONE,  "none")                                                     \
50   _(1, MD5_96, "md5-96")           /* RFC2403 */                          \
51   _(2, SHA1_96, "sha1-96")         /* RFC2404 */                          \
52   _(3, SHA_256_96, "sha-256-96")   /* draft-ietf-ipsec-ciph-sha-256-00 */ \
53   _(4, SHA_256_128, "sha-256-128") /* RFC4868 */                          \
54   _(5, SHA_384_192, "sha-384-192") /* RFC4868 */                          \
55   _(6, SHA_512_256, "sha-512-256")      /* RFC4868 */
56
57 typedef enum
58 {
59 #define _(v,f,s) IPSEC_INTEG_ALG_##f = v,
60   foreach_ipsec_integ_alg
61 #undef _
62     IPSEC_INTEG_N_ALG,
63 } ipsec_integ_alg_t;
64
65 typedef enum
66 {
67   IPSEC_PROTOCOL_AH = 0,
68   IPSEC_PROTOCOL_ESP = 1
69 } ipsec_protocol_t;
70
71 typedef struct
72 {
73   u32 id;
74   u32 spi;
75   ipsec_protocol_t protocol;
76
77   ipsec_crypto_alg_t crypto_alg;
78   u8 crypto_key_len;
79   u8 crypto_key[128];
80
81   ipsec_integ_alg_t integ_alg;
82   u8 integ_key_len;
83   u8 integ_key[128];
84
85   u8 use_esn;
86   u8 use_anti_replay;
87
88   u8 is_tunnel;
89   u8 is_tunnel_ip6;
90   ip46_address_t tunnel_src_addr;
91   ip46_address_t tunnel_dst_addr;
92
93   /* runtime */
94   u32 seq;
95   u32 seq_hi;
96   u32 last_seq;
97   u32 last_seq_hi;
98   u64 replay_window;
99 } ipsec_sa_t;
100
101 typedef struct
102 {
103   ip46_address_t start, stop;
104 } ip46_address_range_t;
105
106 typedef struct
107 {
108   u16 start, stop;
109 } port_range_t;
110
111 typedef struct
112 {
113   u8 is_add;
114   u8 esn;
115   u8 anti_replay;
116   ip4_address_t local_ip, remote_ip;
117   u32 local_spi;
118   u32 remote_spi;
119   ipsec_crypto_alg_t crypto_alg;
120   u8 local_crypto_key_len;
121   u8 local_crypto_key[128];
122   u8 remote_crypto_key_len;
123   u8 remote_crypto_key[128];
124   ipsec_integ_alg_t integ_alg;
125   u8 local_integ_key_len;
126   u8 local_integ_key[128];
127   u8 remote_integ_key_len;
128   u8 remote_integ_key[128];
129 } ipsec_add_del_tunnel_args_t;
130
131 typedef struct
132 {
133   u8 is_add;
134   u32 local_sa_id;
135   u32 remote_sa_id;
136   ip4_address_t local_ip;
137   ip4_address_t remote_ip;
138 } ipsec_add_del_ipsec_gre_tunnel_args_t;
139
140 typedef enum
141 {
142   IPSEC_IF_SET_KEY_TYPE_NONE,
143   IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO,
144   IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO,
145   IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG,
146   IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG,
147 } ipsec_if_set_key_type_t;
148
149 typedef struct
150 {
151   u32 id;
152   i32 priority;
153   u8 is_outbound;
154
155   // Selector
156   u8 is_ipv6;
157   ip46_address_range_t laddr;
158   ip46_address_range_t raddr;
159   u8 protocol;
160   port_range_t lport;
161   port_range_t rport;
162
163   // Policy
164   u8 policy;
165   u32 sa_id;
166   u32 sa_index;
167
168   // Counter
169   vlib_counter_t counter;
170 } ipsec_policy_t;
171
172 typedef struct
173 {
174   u32 id;
175   /* pool of policies */
176   ipsec_policy_t *policies;
177   /* vectors of policy indices */
178   u32 *ipv4_outbound_policies;
179   u32 *ipv6_outbound_policies;
180   u32 *ipv4_inbound_protect_policy_indices;
181   u32 *ipv4_inbound_policy_discard_and_bypass_indices;
182   u32 *ipv6_inbound_protect_policy_indices;
183   u32 *ipv6_inbound_policy_discard_and_bypass_indices;
184 } ipsec_spd_t;
185
186 typedef struct
187 {
188   u32 spd_index;
189 } ip4_ipsec_config_t;
190
191 typedef struct
192 {
193   u32 spd_index;
194 } ip6_ipsec_config_t;
195
196 typedef struct
197 {
198   u32 input_sa_index;
199   u32 output_sa_index;
200   u32 hw_if_index;
201 } ipsec_tunnel_if_t;
202
203 typedef struct
204 {
205   /* pool of tunnel instances */
206   ipsec_spd_t *spds;
207   ipsec_sa_t *sad;
208
209   /* pool of tunnel interfaces */
210   ipsec_tunnel_if_t *tunnel_interfaces;
211   u32 *free_tunnel_if_indices;
212
213   u32 **empty_buffers;
214
215   uword *tunnel_index_by_key;
216
217   /* convenience */
218   vlib_main_t *vlib_main;
219   vnet_main_t *vnet_main;
220
221   /* next node indices */
222   u32 feature_next_node_index[32];
223
224   /* hashes */
225   uword *spd_index_by_spd_id;
226   uword *spd_index_by_sw_if_index;
227   uword *sa_index_by_sa_id;
228   uword *ipsec_if_pool_index_by_key;
229
230   /* node indexes */
231   u32 error_drop_node_index;
232   u32 ip4_lookup_node_index;
233   u32 esp_encrypt_node_index;
234
235 } ipsec_main_t;
236
237 ipsec_main_t ipsec_main;
238
239 extern vlib_node_registration_t esp_encrypt_node;
240 extern vlib_node_registration_t esp_decrypt_node;
241 extern vlib_node_registration_t ipsec_if_output_node;
242 extern vlib_node_registration_t ipsec_if_input_node;
243
244
245 /*
246  * functions
247  */
248 int ipsec_set_interface_spd (vlib_main_t * vm, u32 sw_if_index, u32 spd_id,
249                              int is_add);
250 int ipsec_add_del_spd (vlib_main_t * vm, u32 spd_id, int is_add);
251 int ipsec_add_del_policy (vlib_main_t * vm, ipsec_policy_t * policy,
252                           int is_add);
253 int ipsec_add_del_sa (vlib_main_t * vm, ipsec_sa_t * new_sa, int is_add);
254 int ipsec_set_sa_key (vlib_main_t * vm, ipsec_sa_t * sa_update);
255
256 u32 ipsec_get_sa_index_by_sa_id (u32 sa_id);
257 u8 *format_ipsec_if_output_trace (u8 * s, va_list * args);
258 u8 *format_ipsec_policy_action (u8 * s, va_list * args);
259 u8 *format_ipsec_crypto_alg (u8 * s, va_list * args);
260 u8 *format_ipsec_integ_alg (u8 * s, va_list * args);
261 u8 *format_ipsec_replay_window (u8 * s, va_list * args);
262 uword unformat_ipsec_policy_action (unformat_input_t * input, va_list * args);
263 uword unformat_ipsec_crypto_alg (unformat_input_t * input, va_list * args);
264 uword unformat_ipsec_integ_alg (unformat_input_t * input, va_list * args);
265
266 /*u32 ipsec_add_del_tunnel_if (vnet_main_t * vnm, ipsec_add_del_tunnel_args_t * args); */
267 int ipsec_add_del_tunnel_if (ipsec_add_del_tunnel_args_t * args);
268 int ipsec_add_del_ipsec_gre_tunnel (vnet_main_t * vnm,
269                                     ipsec_add_del_ipsec_gre_tunnel_args_t *
270                                     args);
271 int ipsec_set_interface_key (vnet_main_t * vnm, u32 hw_if_index,
272                              ipsec_if_set_key_type_t type, u8 alg, u8 * key);
273
274
275 /*
276  *  inline functions
277  */
278
279 always_inline void
280 ipsec_alloc_empty_buffers (vlib_main_t * vm, ipsec_main_t * im)
281 {
282   u32 cpu_index = os_get_cpu_number ();
283   uword l = vec_len (im->empty_buffers[cpu_index]);
284   uword n_alloc = 0;
285
286   if (PREDICT_FALSE (l < VLIB_FRAME_SIZE))
287     {
288       if (!im->empty_buffers[cpu_index])
289         {
290           vec_alloc (im->empty_buffers[cpu_index], 2 * VLIB_FRAME_SIZE);
291         }
292
293       n_alloc = vlib_buffer_alloc (vm, im->empty_buffers[cpu_index] + l,
294                                    2 * VLIB_FRAME_SIZE - l);
295
296       _vec_len (im->empty_buffers[cpu_index]) = l + n_alloc;
297     }
298 }
299
300 static_always_inline u32
301 get_next_output_feature_node_index (vlib_buffer_t * b,
302                                     vlib_node_runtime_t * nr)
303 {
304   u32 next;
305   u32 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_TX];
306   vlib_main_t *vm = vlib_get_main ();
307   vlib_node_t *node = vlib_get_node (vm, nr->node_index);
308
309   vnet_feature_next (sw_if_index, &next, b);
310   return node->next_nodes[next];
311 }
312
313 #endif /* __IPSEC_H__ */
314
315 /*
316  * fd.io coding-style-patch-verification: ON
317  *
318  * Local Variables:
319  * eval: (c-set-style "gnu")
320  * End:
321  */