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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <vnet/ip/ip.h>
19 #include <vnet/feature/feature.h>
21 #define IPSEC_FLAG_IPSEC_GRE_TUNNEL (1 << 0)
24 #define foreach_ipsec_output_next \
25 _(DROP, "error-drop") \
26 _(ESP_ENCRYPT, "esp-encrypt") \
27 _(AH_ENCRYPT, "ah-encrypt")
29 #define _(v, s) IPSEC_OUTPUT_NEXT_##v,
32 foreach_ipsec_output_next
35 } ipsec_output_next_t;
38 #define foreach_ipsec_input_next \
39 _(DROP, "error-drop") \
40 _(ESP_DECRYPT, "esp-decrypt") \
41 _(AH_DECRYPT, "ah-decrypt")
43 #define _(v, s) IPSEC_INPUT_NEXT_##v,
46 foreach_ipsec_input_next
52 #define foreach_ipsec_policy_action \
53 _(0, BYPASS, "bypass") \
54 _(1, DISCARD, "discard") \
55 _(2, RESOLVE, "resolve") \
56 _(3, PROTECT, "protect")
60 #define _(v,f,s) IPSEC_POLICY_ACTION_##f = v,
61 foreach_ipsec_policy_action
63 IPSEC_POLICY_N_ACTION,
64 } ipsec_policy_action_t;
66 #define foreach_ipsec_crypto_alg \
68 _(1, AES_CBC_128, "aes-cbc-128") \
69 _(2, AES_CBC_192, "aes-cbc-192") \
70 _(3, AES_CBC_256, "aes-cbc-256") \
71 _(4, AES_CTR_128, "aes-ctr-128") \
72 _(5, AES_CTR_192, "aes-ctr-192") \
73 _(6, AES_CTR_256, "aes-ctr-256") \
74 _(7, AES_GCM_128, "aes-gcm-128") \
75 _(8, AES_GCM_192, "aes-gcm-192") \
76 _(9, AES_GCM_256, "aes-gcm-256") \
77 _(10, DES_CBC, "des-cbc") \
78 _(11, 3DES_CBC, "3des-cbc")
82 #define _(v,f,s) IPSEC_CRYPTO_ALG_##f = v,
83 foreach_ipsec_crypto_alg
88 #define foreach_ipsec_integ_alg \
90 _(1, MD5_96, "md5-96") /* RFC2403 */ \
91 _(2, SHA1_96, "sha1-96") /* RFC2404 */ \
92 _(3, SHA_256_96, "sha-256-96") /* draft-ietf-ipsec-ciph-sha-256-00 */ \
93 _(4, SHA_256_128, "sha-256-128") /* RFC4868 */ \
94 _(5, SHA_384_192, "sha-384-192") /* RFC4868 */ \
95 _(6, SHA_512_256, "sha-512-256") /* RFC4868 */
99 #define _(v,f,s) IPSEC_INTEG_ALG_##f = v,
100 foreach_ipsec_integ_alg
107 IPSEC_PROTOCOL_AH = 0,
108 IPSEC_PROTOCOL_ESP = 1
115 ipsec_protocol_t protocol;
117 ipsec_crypto_alg_t crypto_alg;
121 ipsec_integ_alg_t integ_alg;
130 ip46_address_t tunnel_src_addr;
131 ip46_address_t tunnel_dst_addr;
148 ip46_address_t start, stop;
149 } ip46_address_range_t;
161 ip4_address_t local_ip, remote_ip;
164 ipsec_crypto_alg_t crypto_alg;
165 u8 local_crypto_key_len;
166 u8 local_crypto_key[128];
167 u8 remote_crypto_key_len;
168 u8 remote_crypto_key[128];
169 ipsec_integ_alg_t integ_alg;
170 u8 local_integ_key_len;
171 u8 local_integ_key[128];
172 u8 remote_integ_key_len;
173 u8 remote_integ_key[128];
176 } ipsec_add_del_tunnel_args_t;
183 ip4_address_t local_ip;
184 ip4_address_t remote_ip;
185 } ipsec_add_del_ipsec_gre_tunnel_args_t;
189 IPSEC_IF_SET_KEY_TYPE_NONE,
190 IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO,
191 IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO,
192 IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG,
193 IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG,
194 } ipsec_if_set_key_type_t;
204 ip46_address_range_t laddr;
205 ip46_address_range_t raddr;
216 vlib_counter_t counter;
222 /* pool of policies */
223 ipsec_policy_t *policies;
224 /* vectors of policy indices */
225 u32 *ipv4_outbound_policies;
226 u32 *ipv6_outbound_policies;
227 u32 *ipv4_inbound_protect_policy_indices;
228 u32 *ipv4_inbound_policy_discard_and_bypass_indices;
229 u32 *ipv6_inbound_protect_policy_indices;
230 u32 *ipv6_inbound_policy_discard_and_bypass_indices;
236 } ip4_ipsec_config_t;
241 } ip6_ipsec_config_t;
245 /* Required for pool_get_aligned */
246 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
255 clib_error_t *(*add_del_sa_sess_cb) (u32 sa_index, u8 is_add);
256 clib_error_t *(*check_support_cb) (ipsec_sa_t * sa);
257 } ipsec_main_callbacks_t;
261 /* pool of tunnel instances */
265 /* pool of tunnel interfaces */
266 ipsec_tunnel_if_t *tunnel_interfaces;
267 u32 *free_tunnel_if_indices;
271 uword *tunnel_index_by_key;
274 vlib_main_t *vlib_main;
275 vnet_main_t *vnet_main;
277 /* next node indices */
278 u32 feature_next_node_index[32];
281 uword *spd_index_by_spd_id;
282 uword *spd_index_by_sw_if_index;
283 uword *sa_index_by_sa_id;
284 uword *ipsec_if_pool_index_by_key;
285 uword *ipsec_if_real_dev_by_show_dev;
288 u32 error_drop_node_index;
289 u32 esp_encrypt_node_index;
290 u32 esp_decrypt_node_index;
291 u32 ah_encrypt_node_index;
292 u32 ah_decrypt_node_index;
293 /* next node indeces */
294 u32 esp_encrypt_next_index;
295 u32 esp_decrypt_next_index;
296 u32 ah_encrypt_next_index;
297 u32 ah_decrypt_next_index;
300 ipsec_main_callbacks_t cb;
303 extern ipsec_main_t ipsec_main;
305 extern vlib_node_registration_t esp_encrypt_node;
306 extern vlib_node_registration_t esp_decrypt_node;
307 extern vlib_node_registration_t ah_encrypt_node;
308 extern vlib_node_registration_t ah_decrypt_node;
309 extern vlib_node_registration_t ipsec_if_output_node;
310 extern vlib_node_registration_t ipsec_if_input_node;
316 int ipsec_set_interface_spd (vlib_main_t * vm, u32 sw_if_index, u32 spd_id,
318 int ipsec_add_del_spd (vlib_main_t * vm, u32 spd_id, int is_add);
319 int ipsec_add_del_policy (vlib_main_t * vm, ipsec_policy_t * policy,
321 int ipsec_add_del_sa (vlib_main_t * vm, ipsec_sa_t * new_sa, int is_add);
322 int ipsec_set_sa_key (vlib_main_t * vm, ipsec_sa_t * sa_update);
324 u32 ipsec_get_sa_index_by_sa_id (u32 sa_id);
325 u8 ipsec_is_sa_used (u32 sa_index);
326 u8 *format_ipsec_if_output_trace (u8 * s, va_list * args);
327 u8 *format_ipsec_policy_action (u8 * s, va_list * args);
328 u8 *format_ipsec_crypto_alg (u8 * s, va_list * args);
329 u8 *format_ipsec_integ_alg (u8 * s, va_list * args);
330 u8 *format_ipsec_replay_window (u8 * s, va_list * args);
331 uword unformat_ipsec_policy_action (unformat_input_t * input, va_list * args);
332 uword unformat_ipsec_crypto_alg (unformat_input_t * input, va_list * args);
333 uword unformat_ipsec_integ_alg (unformat_input_t * input, va_list * args);
335 int ipsec_add_del_tunnel_if_internal (vnet_main_t * vnm,
336 ipsec_add_del_tunnel_args_t * args,
338 int ipsec_add_del_tunnel_if (ipsec_add_del_tunnel_args_t * args);
339 int ipsec_add_del_ipsec_gre_tunnel (vnet_main_t * vnm,
340 ipsec_add_del_ipsec_gre_tunnel_args_t *
342 int ipsec_set_interface_key (vnet_main_t * vnm, u32 hw_if_index,
343 ipsec_if_set_key_type_t type, u8 alg, u8 * key);
344 int ipsec_set_interface_sa (vnet_main_t * vnm, u32 hw_if_index, u32 sa_id,
353 ipsec_alloc_empty_buffers (vlib_main_t * vm, ipsec_main_t * im)
355 u32 thread_index = vlib_get_thread_index ();
356 uword l = vec_len (im->empty_buffers[thread_index]);
359 if (PREDICT_FALSE (l < VLIB_FRAME_SIZE))
361 if (!im->empty_buffers[thread_index])
363 vec_alloc (im->empty_buffers[thread_index], 2 * VLIB_FRAME_SIZE);
366 n_alloc = vlib_buffer_alloc (vm, im->empty_buffers[thread_index] + l,
367 2 * VLIB_FRAME_SIZE - l);
369 _vec_len (im->empty_buffers[thread_index]) = l + n_alloc;
373 static_always_inline u32
374 get_next_output_feature_node_index (vlib_buffer_t * b,
375 vlib_node_runtime_t * nr)
378 u32 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_TX];
379 vlib_main_t *vm = vlib_get_main ();
380 vlib_node_t *node = vlib_get_node (vm, nr->node_index);
382 vnet_feature_next (sw_if_index, &next, b);
383 return node->next_nodes[next];
386 #endif /* __IPSEC_H__ */
389 * fd.io coding-style-patch-verification: ON
392 * eval: (c-set-style "gnu")