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