blob: 58f0f145a5a2c242de0676a59dd31dc3fbbfe46d [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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 */
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000015#ifndef __IPSEC_H__
16#define __IPSEC_H__
17
Matus Fabian694265d2016-08-10 01:55:36 -070018#define IPSEC_FLAG_IPSEC_GRE_TUNNEL (1 << 0)
19
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +000020
21#define foreach_ipsec_output_next \
22_(DROP, "error-drop") \
23_(ESP_ENCRYPT, "esp-encrypt")
24
25#define _(v, s) IPSEC_OUTPUT_NEXT_##v,
26typedef enum
27{
28 foreach_ipsec_output_next
29#undef _
30 IPSEC_OUTPUT_N_NEXT,
31} ipsec_output_next_t;
32
33
34#define foreach_ipsec_input_next \
35_(DROP, "error-drop") \
36_(ESP_DECRYPT, "esp-decrypt")
37
38#define _(v, s) IPSEC_INPUT_NEXT_##v,
39typedef enum
40{
41 foreach_ipsec_input_next
42#undef _
43 IPSEC_INPUT_N_NEXT,
44} ipsec_input_next_t;
45
46
Ed Warnickecb9cada2015-12-08 15:45:58 -070047#define foreach_ipsec_policy_action \
48 _(0, BYPASS, "bypass") \
49 _(1, DISCARD, "discard") \
50 _(2, RESOLVE, "resolve") \
51 _(3, PROTECT, "protect")
52
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070053typedef enum
54{
Ed Warnickecb9cada2015-12-08 15:45:58 -070055#define _(v,f,s) IPSEC_POLICY_ACTION_##f = v,
56 foreach_ipsec_policy_action
57#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070058 IPSEC_POLICY_N_ACTION,
Ed Warnickecb9cada2015-12-08 15:45:58 -070059} ipsec_policy_action_t;
60
Radu Nicolau6929ea92016-11-29 11:00:30 +000061#define foreach_ipsec_crypto_alg \
62 _(0, NONE, "none") \
63 _(1, AES_CBC_128, "aes-cbc-128") \
64 _(2, AES_CBC_192, "aes-cbc-192") \
65 _(3, AES_CBC_256, "aes-cbc-256") \
66 _(4, AES_GCM_128, "aes-gcm-128")
Ed Warnickecb9cada2015-12-08 15:45:58 -070067
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070068typedef enum
69{
Ed Warnickecb9cada2015-12-08 15:45:58 -070070#define _(v,f,s) IPSEC_CRYPTO_ALG_##f = v,
71 foreach_ipsec_crypto_alg
72#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070073 IPSEC_CRYPTO_N_ALG,
Ed Warnickecb9cada2015-12-08 15:45:58 -070074} ipsec_crypto_alg_t;
75
Radu Nicolau6929ea92016-11-29 11:00:30 +000076#define foreach_ipsec_integ_alg \
77 _(0, NONE, "none") \
78 _(1, MD5_96, "md5-96") /* RFC2403 */ \
79 _(2, SHA1_96, "sha1-96") /* RFC2404 */ \
80 _(3, SHA_256_96, "sha-256-96") /* draft-ietf-ipsec-ciph-sha-256-00 */ \
81 _(4, SHA_256_128, "sha-256-128") /* RFC4868 */ \
82 _(5, SHA_384_192, "sha-384-192") /* RFC4868 */ \
83 _(6, SHA_512_256, "sha-512-256") /* RFC4868 */ \
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +000084 _(7, AES_GCM_128, "aes-gcm-128") /* RFC4106 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070085
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070086typedef enum
87{
Ed Warnickecb9cada2015-12-08 15:45:58 -070088#define _(v,f,s) IPSEC_INTEG_ALG_##f = v,
89 foreach_ipsec_integ_alg
90#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070091 IPSEC_INTEG_N_ALG,
Ed Warnickecb9cada2015-12-08 15:45:58 -070092} ipsec_integ_alg_t;
93
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070094typedef enum
95{
96 IPSEC_PROTOCOL_AH = 0,
97 IPSEC_PROTOCOL_ESP = 1
Ed Warnickecb9cada2015-12-08 15:45:58 -070098} ipsec_protocol_t;
99
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700100typedef struct
101{
102 u32 id;
103 u32 spi;
104 ipsec_protocol_t protocol;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700105
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700106 ipsec_crypto_alg_t crypto_alg;
107 u8 crypto_key_len;
108 u8 crypto_key[128];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700110 ipsec_integ_alg_t integ_alg;
111 u8 integ_key_len;
112 u8 integ_key[128];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700114 u8 use_esn;
115 u8 use_anti_replay;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700117 u8 is_tunnel;
118 u8 is_tunnel_ip6;
119 ip46_address_t tunnel_src_addr;
120 ip46_address_t tunnel_dst_addr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121
Radu Nicolau6929ea92016-11-29 11:00:30 +0000122 u32 salt;
123
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700124 /* runtime */
125 u32 seq;
126 u32 seq_hi;
127 u32 last_seq;
128 u32 last_seq_hi;
129 u64 replay_window;
Radu Nicolaucb33dc22017-02-16 16:49:46 +0000130
131 /*lifetime data */
132 u64 total_data_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700133} ipsec_sa_t;
134
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700135typedef struct
136{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137 ip46_address_t start, stop;
138} ip46_address_range_t;
139
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700140typedef struct
141{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700142 u16 start, stop;
143} port_range_t;
144
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700145typedef struct
146{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147 u8 is_add;
148 u8 esn;
149 u8 anti_replay;
150 ip4_address_t local_ip, remote_ip;
151 u32 local_spi;
152 u32 remote_spi;
Matthew Smith2838a232016-06-21 16:05:09 -0500153 ipsec_crypto_alg_t crypto_alg;
154 u8 local_crypto_key_len;
155 u8 local_crypto_key[128];
156 u8 remote_crypto_key_len;
157 u8 remote_crypto_key[128];
158 ipsec_integ_alg_t integ_alg;
159 u8 local_integ_key_len;
160 u8 local_integ_key[128];
161 u8 remote_integ_key_len;
162 u8 remote_integ_key[128];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163} ipsec_add_del_tunnel_args_t;
164
Matus Fabian694265d2016-08-10 01:55:36 -0700165typedef struct
166{
167 u8 is_add;
168 u32 local_sa_id;
169 u32 remote_sa_id;
170 ip4_address_t local_ip;
171 ip4_address_t remote_ip;
172} ipsec_add_del_ipsec_gre_tunnel_args_t;
173
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700174typedef enum
175{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700176 IPSEC_IF_SET_KEY_TYPE_NONE,
177 IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO,
178 IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO,
179 IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG,
180 IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG,
181} ipsec_if_set_key_type_t;
182
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700183typedef struct
184{
185 u32 id;
186 i32 priority;
187 u8 is_outbound;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700188
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700189 // Selector
190 u8 is_ipv6;
191 ip46_address_range_t laddr;
192 ip46_address_range_t raddr;
193 u8 protocol;
194 port_range_t lport;
195 port_range_t rport;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700197 // Policy
198 u8 policy;
199 u32 sa_id;
200 u32 sa_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700201
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700202 // Counter
203 vlib_counter_t counter;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204} ipsec_policy_t;
205
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700206typedef struct
207{
208 u32 id;
209 /* pool of policies */
210 ipsec_policy_t *policies;
211 /* vectors of policy indices */
212 u32 *ipv4_outbound_policies;
213 u32 *ipv6_outbound_policies;
214 u32 *ipv4_inbound_protect_policy_indices;
215 u32 *ipv4_inbound_policy_discard_and_bypass_indices;
216 u32 *ipv6_inbound_protect_policy_indices;
217 u32 *ipv6_inbound_policy_discard_and_bypass_indices;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700218} ipsec_spd_t;
219
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700220typedef struct
221{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700222 u32 spd_index;
223} ip4_ipsec_config_t;
224
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700225typedef struct
226{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700227 u32 spd_index;
228} ip6_ipsec_config_t;
229
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700230typedef struct
231{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700232 u32 input_sa_index;
233 u32 output_sa_index;
234 u32 hw_if_index;
235} ipsec_tunnel_if_t;
236
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700237typedef struct
238{
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000239 i32 (*add_del_sa_sess_cb) (u32 sa_index, u8 is_add);
240 clib_error_t *(*check_support_cb) (ipsec_sa_t * sa);
241} ipsec_main_callbacks_t;
242
243typedef struct
244{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700245 /* pool of tunnel instances */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700246 ipsec_spd_t *spds;
247 ipsec_sa_t *sad;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700248
249 /* pool of tunnel interfaces */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700250 ipsec_tunnel_if_t *tunnel_interfaces;
251 u32 *free_tunnel_if_indices;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700252
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700253 u32 **empty_buffers;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700254
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700255 uword *tunnel_index_by_key;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700256
257 /* convenience */
258 vlib_main_t *vlib_main;
259 vnet_main_t *vnet_main;
260
261 /* next node indices */
262 u32 feature_next_node_index[32];
263
264 /* hashes */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700265 uword *spd_index_by_spd_id;
266 uword *spd_index_by_sw_if_index;
267 uword *sa_index_by_sa_id;
268 uword *ipsec_if_pool_index_by_key;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700269
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000270 /* node indeces */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700271 u32 error_drop_node_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700272 u32 esp_encrypt_node_index;
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000273 u32 esp_decrypt_node_index;
274 /* next node indeces */
275 u32 esp_encrypt_next_index;
276 u32 esp_decrypt_next_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700277
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000278 /* callbacks */
279 ipsec_main_callbacks_t cb;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700280} ipsec_main_t;
281
282ipsec_main_t ipsec_main;
283
Jean-Mickael Guerin8941ec22016-03-04 14:14:21 +0100284extern vlib_node_registration_t esp_encrypt_node;
285extern vlib_node_registration_t esp_decrypt_node;
286extern vlib_node_registration_t ipsec_if_output_node;
287extern vlib_node_registration_t ipsec_if_input_node;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700288
289
290/*
291 * functions
292 */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700293int ipsec_set_interface_spd (vlib_main_t * vm, u32 sw_if_index, u32 spd_id,
294 int is_add);
295int ipsec_add_del_spd (vlib_main_t * vm, u32 spd_id, int is_add);
296int ipsec_add_del_policy (vlib_main_t * vm, ipsec_policy_t * policy,
297 int is_add);
298int ipsec_add_del_sa (vlib_main_t * vm, ipsec_sa_t * new_sa, int is_add);
299int ipsec_set_sa_key (vlib_main_t * vm, ipsec_sa_t * sa_update);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700300
Matus Fabian694265d2016-08-10 01:55:36 -0700301u32 ipsec_get_sa_index_by_sa_id (u32 sa_id);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700302u8 *format_ipsec_if_output_trace (u8 * s, va_list * args);
303u8 *format_ipsec_policy_action (u8 * s, va_list * args);
304u8 *format_ipsec_crypto_alg (u8 * s, va_list * args);
305u8 *format_ipsec_integ_alg (u8 * s, va_list * args);
306u8 *format_ipsec_replay_window (u8 * s, va_list * args);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700307uword unformat_ipsec_policy_action (unformat_input_t * input, va_list * args);
308uword unformat_ipsec_crypto_alg (unformat_input_t * input, va_list * args);
309uword unformat_ipsec_integ_alg (unformat_input_t * input, va_list * args);
310
Matthew Smith2838a232016-06-21 16:05:09 -0500311/*u32 ipsec_add_del_tunnel_if (vnet_main_t * vnm, ipsec_add_del_tunnel_args_t * args); */
312int ipsec_add_del_tunnel_if (ipsec_add_del_tunnel_args_t * args);
Matus Fabian694265d2016-08-10 01:55:36 -0700313int ipsec_add_del_ipsec_gre_tunnel (vnet_main_t * vnm,
314 ipsec_add_del_ipsec_gre_tunnel_args_t *
315 args);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700316int ipsec_set_interface_key (vnet_main_t * vnm, u32 hw_if_index,
317 ipsec_if_set_key_type_t type, u8 alg, u8 * key);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700318
319
320/*
321 * inline functions
322 */
323
324always_inline void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700325ipsec_alloc_empty_buffers (vlib_main_t * vm, ipsec_main_t * im)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700326{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700327 u32 cpu_index = os_get_cpu_number ();
Matthew Smith29d85102016-05-01 14:52:08 -0500328 uword l = vec_len (im->empty_buffers[cpu_index]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700329 uword n_alloc = 0;
330
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700331 if (PREDICT_FALSE (l < VLIB_FRAME_SIZE))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700332 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700333 if (!im->empty_buffers[cpu_index])
334 {
335 vec_alloc (im->empty_buffers[cpu_index], 2 * VLIB_FRAME_SIZE);
336 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700337
Damjan Marion67655492016-11-15 12:50:28 +0100338 n_alloc = vlib_buffer_alloc (vm, im->empty_buffers[cpu_index] + l,
339 2 * VLIB_FRAME_SIZE - l);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700340
Matthew Smith29d85102016-05-01 14:52:08 -0500341 _vec_len (im->empty_buffers[cpu_index]) = l + n_alloc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700342 }
343}
344
Matus Fabian08a6f012016-11-15 06:08:51 -0800345static_always_inline u32
346get_next_output_feature_node_index (vlib_buffer_t * b,
347 vlib_node_runtime_t * nr)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700348{
Matus Fabian08a6f012016-11-15 06:08:51 -0800349 u32 next;
350 u32 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_TX];
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700351 vlib_main_t *vm = vlib_get_main ();
Matus Fabian08a6f012016-11-15 06:08:51 -0800352 vlib_node_t *node = vlib_get_node (vm, nr->node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700353
Matus Fabian08a6f012016-11-15 06:08:51 -0800354 vnet_feature_next (sw_if_index, &next, b);
355 return node->next_nodes[next];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700356}
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700357
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +0000358#endif /* __IPSEC_H__ */
359
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700360/*
361 * fd.io coding-style-patch-verification: ON
362 *
363 * Local Variables:
364 * eval: (c-set-style "gnu")
365 * End:
366 */