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