69aa661683a9a07612eebe71336ac07788e23afd
[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/crypto/crypto.h>
20 #include <vnet/feature/feature.h>
21
22 #include <vppinfra/types.h>
23 #include <vppinfra/cache.h>
24
25 #include <vnet/ipsec/ipsec_spd.h>
26 #include <vnet/ipsec/ipsec_spd_policy.h>
27 #include <vnet/ipsec/ipsec_sa.h>
28
29 #include <vppinfra/bihash_8_16.h>
30
31 #include <vppinfra/bihash_24_16.h>
32
33 #define IPSEC_FP_IP4_HASHES_POOL_SIZE 128
34 #define IPSEC_FP_IP6_HASHES_POOL_SIZE 128
35
36 typedef clib_error_t *(*add_del_sa_sess_cb_t) (u32 sa_index, u8 is_add);
37 typedef clib_error_t *(*check_support_cb_t) (ipsec_sa_t * sa);
38 typedef clib_error_t *(*enable_disable_cb_t) (int is_enable);
39
40 typedef struct
41 {
42   u64 key[2]; // 16 bytes
43   u64 value;
44   i32 bucket_lock;
45   u32 un_used;
46 } ipsec4_hash_kv_16_8_t;
47
48 typedef union
49 {
50   struct
51   {
52     ip4_address_t ip4_addr[2];
53     u16 port[2];
54     u8 proto;
55     u8 pad[3];
56   };
57   ipsec4_hash_kv_16_8_t kv_16_8;
58 } ipsec4_spd_5tuple_t;
59
60 typedef union
61 {
62   struct
63   {
64     ip4_address_t ip4_src_addr;
65     ip4_address_t ip4_dest_addr;
66     ipsec_spd_policy_type_t policy_type;
67     u8 pad[4];
68   }; // 16 bytes total
69   ipsec4_hash_kv_16_8_t kv_16_8;
70 } ipsec4_inbound_spd_tuple_t;
71
72 typedef struct
73 {
74   u8 *name;
75   /* add/del callback */
76   add_del_sa_sess_cb_t add_del_sa_sess_cb;
77   /* check support function */
78   check_support_cb_t check_support_cb;
79   u32 ah4_encrypt_node_index;
80   u32 ah4_decrypt_node_index;
81   u32 ah4_encrypt_next_index;
82   u32 ah4_decrypt_next_index;
83   u32 ah6_encrypt_node_index;
84   u32 ah6_decrypt_node_index;
85   u32 ah6_encrypt_next_index;
86   u32 ah6_decrypt_next_index;
87 } ipsec_ah_backend_t;
88
89 typedef struct
90 {
91   u8 *name;
92   /* add/del callback */
93   add_del_sa_sess_cb_t add_del_sa_sess_cb;
94   /* check support function */
95   check_support_cb_t check_support_cb;
96   /* enable or disable function */
97   enable_disable_cb_t enable_disable_cb;
98   u32 esp4_encrypt_node_index;
99   u32 esp4_decrypt_node_index;
100   u32 esp4_encrypt_next_index;
101   u32 esp4_decrypt_next_index;
102   u32 esp6_encrypt_node_index;
103   u32 esp6_decrypt_node_index;
104   u32 esp6_encrypt_next_index;
105   u32 esp6_decrypt_next_index;
106   u32 esp4_decrypt_tun_node_index;
107   u32 esp4_decrypt_tun_next_index;
108   u32 esp4_encrypt_tun_node_index;
109   u32 esp6_decrypt_tun_node_index;
110   u32 esp6_decrypt_tun_next_index;
111   u32 esp6_encrypt_tun_node_index;
112   u32 esp_mpls_encrypt_tun_node_index;
113 } ipsec_esp_backend_t;
114
115 typedef struct
116 {
117   vnet_crypto_op_id_t enc_op_id;
118   vnet_crypto_op_id_t dec_op_id;
119   vnet_crypto_alg_t alg;
120   u8 iv_size;
121   u8 block_align;
122   u8 icv_size;
123 } ipsec_main_crypto_alg_t;
124
125 typedef struct
126 {
127   vnet_crypto_op_id_t op_id;
128   vnet_crypto_alg_t alg;
129   u8 icv_size;
130 } ipsec_main_integ_alg_t;
131
132 typedef struct
133 {
134   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
135   vnet_crypto_op_t *crypto_ops;
136   vnet_crypto_op_t *integ_ops;
137   vnet_crypto_op_t *chained_crypto_ops;
138   vnet_crypto_op_t *chained_integ_ops;
139   vnet_crypto_op_chunk_t *chunks;
140   vnet_crypto_async_frame_t **async_frames;
141 } ipsec_per_thread_data_t;
142
143 typedef struct
144 {
145   /* pool of tunnel instances */
146   ipsec_spd_t *spds;
147   /* pool of policies */
148   ipsec_policy_t *policies;
149   /* pool of bihash tables for ipv4 ipsec rules */
150   clib_bihash_16_8_t *fp_ip4_lookup_hashes_pool;
151   /* pool of bihash tables for ipv6 ipsec rules */
152   clib_bihash_40_8_t *fp_ip6_lookup_hashes_pool;
153
154   u32 fp_spd_ipv4_out_is_enabled;
155   u32 fp_spd_ipv4_in_is_enabled;
156   u32 fp_spd_ipv6_out_is_enabled;
157   u32 fp_spd_ipv6_in_is_enabled;
158   /* pool of fast path mask types */
159   ipsec_fp_mask_type_entry_t *fp_mask_types;
160   u32 fp_lookup_hash_buckets; /* number of buckets should be power of two */
161
162   /* hash tables of UDP port registrations */
163   uword *udp_port_registrations;
164
165   uword *tunnel_index_by_key;
166
167   /* next_header protocol registration */
168   u16 *next_header_registrations;
169
170   /* convenience */
171   vlib_main_t *vlib_main;
172   vnet_main_t *vnet_main;
173
174   /* hashes */
175   uword *spd_index_by_spd_id;
176   uword *spd_index_by_sw_if_index;
177   uword *sa_index_by_sa_id;
178   uword *ipsec4_if_pool_index_by_key;
179   uword *ipsec6_if_pool_index_by_key;
180   uword *ipsec_if_real_dev_by_show_dev;
181   uword *ipsec_if_by_sw_if_index;
182
183   ipsec4_hash_kv_16_8_t *ipsec4_out_spd_hash_tbl;
184   ipsec4_hash_kv_16_8_t *ipsec4_in_spd_hash_tbl;
185   clib_bihash_8_16_t tun4_protect_by_key;
186   clib_bihash_24_16_t tun6_protect_by_key;
187
188   /* node indices */
189   u32 error_drop_node_index;
190   u32 esp4_encrypt_node_index;
191   u32 esp4_decrypt_node_index;
192   u32 esp4_decrypt_tun_node_index;
193   u32 esp4_encrypt_tun_node_index;
194   u32 ah4_encrypt_node_index;
195   u32 ah4_decrypt_node_index;
196   u32 esp6_encrypt_node_index;
197   u32 esp6_decrypt_node_index;
198   u32 esp6_decrypt_tun_node_index;
199   u32 esp6_encrypt_tun_node_index;
200   u32 esp_mpls_encrypt_tun_node_index;
201   u32 ah6_encrypt_node_index;
202   u32 ah6_decrypt_node_index;
203   /* next node indices */
204   u32 esp4_encrypt_next_index;
205   u32 esp4_decrypt_next_index;
206   u32 esp4_decrypt_tun_next_index;
207   u32 ah4_encrypt_next_index;
208   u32 ah4_decrypt_next_index;
209   u32 esp6_encrypt_next_index;
210   u32 esp6_decrypt_next_index;
211   u32 esp6_decrypt_tun_next_index;
212   u32 ah6_encrypt_next_index;
213   u32 ah6_decrypt_next_index;
214
215   /* pool of ah backends */
216   ipsec_ah_backend_t *ah_backends;
217   /* pool of esp backends */
218   ipsec_esp_backend_t *esp_backends;
219   /* index of current ah backend */
220   u32 ah_current_backend;
221   /* index of current esp backend */
222   u32 esp_current_backend;
223   /* index of default ah backend */
224   u32 ah_default_backend;
225   /* index of default esp backend */
226   u32 esp_default_backend;
227
228   /* crypto alg data */
229   ipsec_main_crypto_alg_t *crypto_algs;
230
231   /* crypto integ data */
232   ipsec_main_integ_alg_t *integ_algs;
233
234   /* per-thread data */
235   ipsec_per_thread_data_t *ptd;
236
237   /** Worker handoff */
238   u32 ah4_enc_fq_index;
239   u32 ah4_dec_fq_index;
240   u32 ah6_enc_fq_index;
241   u32 ah6_dec_fq_index;
242
243   u32 esp4_enc_fq_index;
244   u32 esp4_dec_fq_index;
245   u32 esp6_enc_fq_index;
246   u32 esp6_dec_fq_index;
247   u32 esp4_enc_tun_fq_index;
248   u32 esp6_enc_tun_fq_index;
249   u32 esp_mpls_enc_tun_fq_index;
250   u32 esp4_dec_tun_fq_index;
251   u32 esp6_dec_tun_fq_index;
252
253   /* Number of buckets for flow cache */
254   u32 ipsec4_out_spd_hash_num_buckets;
255   u32 ipsec4_out_spd_flow_cache_entries;
256   u32 epoch_count;
257   u8 output_flow_cache_flag;
258
259   u32 ipsec4_in_spd_hash_num_buckets;
260   u32 ipsec4_in_spd_flow_cache_entries;
261   u32 input_epoch_count;
262   u8 input_flow_cache_flag;
263
264   u8 async_mode;
265   u16 msg_id_base;
266 } ipsec_main_t;
267
268 typedef enum ipsec_format_flags_t_
269 {
270   IPSEC_FORMAT_BRIEF = 0,
271   IPSEC_FORMAT_DETAIL = (1 << 0),
272   IPSEC_FORMAT_INSECURE = (1 << 1),
273 } ipsec_format_flags_t;
274
275 extern ipsec_main_t ipsec_main;
276
277 clib_error_t *ipsec_add_del_sa_sess_cb (ipsec_main_t * im, u32 sa_index,
278                                         u8 is_add);
279
280 clib_error_t *ipsec_check_support_cb (ipsec_main_t * im, ipsec_sa_t * sa);
281
282 extern vlib_node_registration_t ipsec4_tun_input_node;
283 extern vlib_node_registration_t ipsec6_tun_input_node;
284
285 /*
286  * functions
287  */
288
289 /*
290  *  inline functions
291  */
292
293 static_always_inline u32
294 get_next_output_feature_node_index (vlib_buffer_t * b,
295                                     vlib_node_runtime_t * nr)
296 {
297   u32 next;
298   vlib_main_t *vm = vlib_get_main ();
299   vlib_node_t *node = vlib_get_node (vm, nr->node_index);
300
301   vnet_feature_next (&next, b);
302   return node->next_nodes[next];
303 }
304
305 static_always_inline u64
306 ipsec4_hash_16_8 (ipsec4_hash_kv_16_8_t *v)
307 {
308 #ifdef clib_crc32c_uses_intrinsics
309   return clib_crc32c ((u8 *) v->key, 16);
310 #else
311   u64 tmp = v->key[0] ^ v->key[1];
312   return clib_xxhash (tmp);
313 #endif
314 }
315
316 static_always_inline int
317 ipsec4_hash_key_compare_16_8 (u64 *a, u64 *b)
318 {
319 #if defined(CLIB_HAVE_VEC128) && defined(CLIB_HAVE_VEC128_UNALIGNED_LOAD_STORE)
320   u64x2 v;
321   v = u64x2_load_unaligned (a) ^ u64x2_load_unaligned (b);
322   return u64x2_is_all_zero (v);
323 #else
324   return ((a[0] ^ b[0]) | (a[1] ^ b[1])) == 0;
325 #endif
326 }
327
328 /* clib_spinlock_lock is not used to save another memory indirection */
329 static_always_inline void
330 ipsec_spinlock_lock (i32 *lock)
331 {
332   i32 free = 0;
333   while (!clib_atomic_cmp_and_swap_acq_relax_n (lock, &free, 1, 0))
334     {
335       /* atomic load limits number of compare_exchange executions */
336       while (clib_atomic_load_relax_n (lock))
337         CLIB_PAUSE ();
338       /* on failure, compare_exchange writes lock into free */
339       free = 0;
340     }
341 }
342
343 static_always_inline void
344 ipsec_spinlock_unlock (i32 *lock)
345 {
346   /* Make sure all reads/writes are complete before releasing the lock */
347   clib_atomic_release (lock);
348 }
349
350 u32 ipsec_register_ah_backend (vlib_main_t * vm, ipsec_main_t * im,
351                                const char *name,
352                                const char *ah4_encrypt_node_name,
353                                const char *ah4_decrypt_node_name,
354                                const char *ah6_encrypt_node_name,
355                                const char *ah6_decrypt_node_name,
356                                check_support_cb_t ah_check_support_cb,
357                                add_del_sa_sess_cb_t ah_add_del_sa_sess_cb);
358
359 u32 ipsec_register_esp_backend (
360   vlib_main_t *vm, ipsec_main_t *im, const char *name,
361   const char *esp4_encrypt_node_name, const char *esp4_encrypt_tun_node_name,
362   const char *esp4_decrypt_node_name, const char *esp4_decrypt_tun_node_name,
363   const char *esp6_encrypt_node_name, const char *esp6_encrypt_tun_node_name,
364   const char *esp6_decrypt_node_name, const char *esp6_decrypt_tun_node_name,
365   const char *esp_mpls_encrypt_tun_node_name,
366   check_support_cb_t esp_check_support_cb,
367   add_del_sa_sess_cb_t esp_add_del_sa_sess_cb,
368   enable_disable_cb_t enable_disable_cb);
369
370 int ipsec_select_ah_backend (ipsec_main_t * im, u32 ah_backend_idx);
371 int ipsec_select_esp_backend (ipsec_main_t * im, u32 esp_backend_idx);
372
373 clib_error_t *ipsec_rsc_in_use (ipsec_main_t * im);
374 void ipsec_set_async_mode (u32 is_enabled);
375
376 extern void ipsec_register_udp_port (u16 udp_port, u8 is_ip4);
377 extern void ipsec_unregister_udp_port (u16 udp_port, u8 is_ip4);
378
379 extern clib_error_t *ipsec_register_next_header (vlib_main_t *vm,
380                                                  u8 next_header,
381                                                  const char *next_node);
382
383 #endif /* __IPSEC_H__ */
384
385 /*
386  * fd.io coding-style-patch-verification: ON
387  *
388  * Local Variables:
389  * eval: (c-set-style "gnu")
390  * End:
391  */