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