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