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