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