FD.io VPP  v18.10-34-gcce845e
Vector Packet Processing
ipsec.h
Go to the documentation of this file.
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 #ifndef __IPSEC_H__
16 #define __IPSEC_H__
17 
18 #include <vnet/ip/ip.h>
19 #include <vnet/feature/feature.h>
20 
21 #define IPSEC_FLAG_IPSEC_GRE_TUNNEL (1 << 0)
22 
23 
24 #define foreach_ipsec_output_next \
25 _(DROP, "error-drop") \
26 _(ESP_ENCRYPT, "esp-encrypt") \
27 _(AH_ENCRYPT, "ah-encrypt")
28 
29 #define _(v, s) IPSEC_OUTPUT_NEXT_##v,
30 typedef enum
31 {
33 #undef _
36 
37 
38 #define foreach_ipsec_input_next \
39 _(DROP, "error-drop") \
40 _(ESP_DECRYPT, "esp-decrypt") \
41 _(AH_DECRYPT, "ah-decrypt")
42 
43 #define _(v, s) IPSEC_INPUT_NEXT_##v,
44 typedef enum
45 {
47 #undef _
50 
51 
52 #define foreach_ipsec_policy_action \
53  _(0, BYPASS, "bypass") \
54  _(1, DISCARD, "discard") \
55  _(2, RESOLVE, "resolve") \
56  _(3, PROTECT, "protect")
57 
58 typedef enum
59 {
60 #define _(v,f,s) IPSEC_POLICY_ACTION_##f = v,
62 #undef _
65 
66 #define foreach_ipsec_crypto_alg \
67  _(0, NONE, "none") \
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")
79 
80 typedef enum
81 {
82 #define _(v,f,s) IPSEC_CRYPTO_ALG_##f = v,
84 #undef _
87 
88 #define foreach_ipsec_integ_alg \
89  _(0, NONE, "none") \
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 */
96 
97 typedef enum
98 {
99 #define _(v,f,s) IPSEC_INTEG_ALG_##f = v,
101 #undef _
104 
105 typedef enum
106 {
110 
111 typedef struct
112 {
116 
119  u8 crypto_key[128];
120 
123  u8 integ_key[128];
124 
127 
131  ip46_address_t tunnel_src_addr;
132  ip46_address_t tunnel_dst_addr;
133 
135 
136  /* runtime */
142 
143  /*lifetime data */
145 } ipsec_sa_t;
146 
147 typedef struct
148 {
149  ip46_address_t start, stop;
151 
152 typedef struct
153 {
154  u16 start, stop;
155 } port_range_t;
156 
157 typedef struct
158 {
167  u8 local_crypto_key[128];
169  u8 remote_crypto_key[128];
172  u8 local_integ_key[128];
174  u8 remote_integ_key[128];
179 
180 typedef struct
181 {
188 
189 typedef enum
190 {
197 
198 typedef struct
199 {
203 
204  // Selector
211 
212  // Policy
216 
217  // Counter
220 
221 typedef struct
222 {
224  /* pool of policies */
226  /* vectors of policy indices */
233 } ipsec_spd_t;
234 
235 typedef struct
236 {
239 
240 typedef struct
241 {
244 
245 typedef struct
246 {
247  /* Required for pool_get_aligned */
248  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
254 
255 typedef struct
256 {
257  clib_error_t *(*add_del_sa_sess_cb) (u32 sa_index, u8 is_add);
258  clib_error_t *(*check_support_cb) (ipsec_sa_t * sa);
260 
261 typedef struct
262 {
263  /* pool of tunnel instances */
266 
267  /* pool of tunnel interfaces */
270 
272 
274 
275  /* convenience */
278 
279  /* next node indices */
280  u32 feature_next_node_index[32];
281 
282  /* hashes */
288 
289  /* node indices */
295  /* next node indices */
300 
301  /* callbacks */
303 
304  /* helper for sort function */
306 } ipsec_main_t;
307 
308 extern ipsec_main_t ipsec_main;
309 
315 
316 
317 /*
318  * functions
319  */
321  int is_add);
322 int ipsec_add_del_spd (vlib_main_t * vm, u32 spd_id, int is_add);
324  int is_add);
325 int ipsec_add_del_sa (vlib_main_t * vm, ipsec_sa_t * new_sa, int is_add);
326 int ipsec_set_sa_key (vlib_main_t * vm, ipsec_sa_t * sa_update);
327 
329 u8 ipsec_is_sa_used (u32 sa_index);
330 u8 *format_ipsec_policy_action (u8 * s, va_list * args);
331 u8 *format_ipsec_crypto_alg (u8 * s, va_list * args);
332 u8 *format_ipsec_integ_alg (u8 * s, va_list * args);
333 u8 *format_ipsec_replay_window (u8 * s, va_list * args);
334 uword unformat_ipsec_policy_action (unformat_input_t * input, va_list * args);
335 uword unformat_ipsec_crypto_alg (unformat_input_t * input, va_list * args);
336 uword unformat_ipsec_integ_alg (unformat_input_t * input, va_list * args);
337 
340  u32 * sw_if_index);
344  args);
345 int ipsec_set_interface_key (vnet_main_t * vnm, u32 hw_if_index,
346  ipsec_if_set_key_type_t type, u8 alg, u8 * key);
347 int ipsec_set_interface_sa (vnet_main_t * vnm, u32 hw_if_index, u32 sa_id,
348  u8 is_outbound);
349 
350 
351 /*
352  * inline functions
353  */
354 
355 always_inline void
357 {
358  u32 thread_index = vm->thread_index;
359  uword l = vec_len (im->empty_buffers[thread_index]);
360  uword n_alloc = 0;
361 
362  if (PREDICT_FALSE (l < VLIB_FRAME_SIZE))
363  {
364  if (!im->empty_buffers[thread_index])
365  {
366  vec_alloc (im->empty_buffers[thread_index], 2 * VLIB_FRAME_SIZE);
367  }
368 
369  n_alloc = vlib_buffer_alloc (vm, im->empty_buffers[thread_index] + l,
370  2 * VLIB_FRAME_SIZE - l);
371 
372  _vec_len (im->empty_buffers[thread_index]) = l + n_alloc;
373  }
374 }
375 
378  vlib_node_runtime_t * nr)
379 {
380  u32 next;
382  vlib_node_t *node = vlib_get_node (vm, nr->node_index);
383 
384  vnet_feature_next (&next, b);
385  return node->next_nodes[next];
386 }
387 
388 #endif /* __IPSEC_H__ */
389 
390 /*
391  * fd.io coding-style-patch-verification: ON
392  *
393  * Local Variables:
394  * eval: (c-set-style "gnu")
395  * End:
396  */
u32 * next_nodes
Definition: node.h:339
ip46_address_t stop
Definition: ipsec.h:149
int ipsec_set_interface_key(vnet_main_t *vnm, u32 hw_if_index, ipsec_if_set_key_type_t type, u8 alg, u8 *key)
Definition: ipsec_if.c:486
u32 * ipv6_inbound_protect_policy_indices
Definition: ipsec.h:231
ipsec_spd_t * spds
Definition: ipsec.h:264
int ipsec_set_interface_sa(vnet_main_t *vnm, u32 hw_if_index, u32 sa_id, u8 is_outbound)
Definition: ipsec_if.c:536
u32 ah_decrypt_next_index
Definition: ipsec.h:299
#define CLIB_CACHE_LINE_ALIGN_MARK(mark)
Definition: cache.h:60
u32 * ipv4_inbound_protect_policy_indices
Definition: ipsec.h:229
u32 ah_decrypt_node_index
Definition: ipsec.h:294
ipsec_tunnel_if_t * tunnel_interfaces
Definition: ipsec.h:268
u16 stop
Definition: ipsec.h:154
ip46_address_t tunnel_src_addr
Definition: ipsec.h:131
uword * tunnel_index_by_key
Definition: ipsec.h:273
u32 id
Definition: ipsec.h:113
int ipsec_set_interface_spd(vlib_main_t *vm, u32 sw_if_index, u32 spd_id, int is_add)
Definition: ipsec.c:44
u32 ipsec_get_sa_index_by_sa_id(u32 sa_id)
Definition: ipsec.c:33
i32 priority
Definition: ipsec.h:201
unsigned long u64
Definition: types.h:89
u32 ah_encrypt_node_index
Definition: ipsec.h:293
ipsec_integ_alg_t integ_alg
Definition: ipsec.h:121
u32 thread_index
Definition: main.h:179
Combined counter to hold both packets and byte differences.
Definition: counter_types.h:26
u8 is_tunnel
Definition: ipsec.h:128
u32 * ipv4_outbound_policies
Definition: ipsec.h:227
ipsec_output_next_t
Definition: ipsec.h:30
int ipsec_add_del_spd(vlib_main_t *vm, u32 spd_id, int is_add)
Definition: ipsec.c:93
uword * ipsec_if_pool_index_by_key
Definition: ipsec.h:286
#define vec_alloc(V, N)
Allocate space for N more elements (no header, unspecified alignment)
Definition: vec.h:278
unsigned char u8
Definition: types.h:56
u8 ipsec_is_sa_used(u32 sa_index)
Definition: ipsec.c:386
u32 spi
Definition: ipsec.h:114
port_range_t lport
Definition: ipsec.h:209
u32 seq_hi
Definition: ipsec.h:138
uword * spd_index_by_sw_if_index
Definition: ipsec.h:284
u64 replay_window
Definition: ipsec.h:141
#define static_always_inline
Definition: clib.h:95
u32 esp_encrypt_next_index
Definition: ipsec.h:296
u32 sw_if_index
Definition: vxlan_gbp.api:39
uword unformat_ipsec_integ_alg(unformat_input_t *input, va_list *args)
Definition: ipsec_format.c:108
ipsec_main_t ipsec_main
Definition: ipsec.c:30
u8 use_esn
Definition: ipsec.h:125
ipsec_policy_action_t
Definition: ipsec.h:58
int ipsec_add_del_ipsec_gre_tunnel(vnet_main_t *vnm, ipsec_add_del_ipsec_gre_tunnel_args_t *args)
Definition: ipsec_if.c:424
ip4_address_t remote_ip
Definition: ipsec.h:162
#define foreach_ipsec_input_next
Definition: ipsec.h:38
ipsec_spd_t * spd_to_sort
Definition: ipsec.h:305
ipsec_main_callbacks_t cb
Definition: ipsec.h:302
u32 ah_encrypt_next_index
Definition: ipsec.h:298
unsigned int u32
Definition: types.h:88
#define VLIB_FRAME_SIZE
Definition: node.h:382
u8 * format_ipsec_crypto_alg(u8 *s, va_list *args)
Definition: ipsec_format.c:58
ipsec_policy_t * policies
Definition: ipsec.h:225
u8 udp_encap
Definition: ipsec.h:130
u32 last_seq
Definition: ipsec.h:139
u32 error_drop_node_index
Definition: ipsec.h:290
ipsec_integ_alg_t
Definition: ipsec.h:97
u8 is_tunnel_ip6
Definition: ipsec.h:129
vlib_node_registration_t ah_decrypt_node
(constructor) VLIB_REGISTER_NODE (ah_decrypt_node)
Definition: ah_decrypt.c:344
u32 salt
Definition: ipsec.h:134
vnet_main_t * vnet_main
Definition: ipsec.h:277
struct _unformat_input_t unformat_input_t
unsigned short u16
Definition: types.h:57
u32 last_seq_hi
Definition: ipsec.h:140
uword unformat_ipsec_crypto_alg(unformat_input_t *input, va_list *args)
Definition: ipsec_format.c:76
#define PREDICT_FALSE(x)
Definition: clib.h:107
ipsec_protocol_t
Definition: ipsec.h:105
#define always_inline
Definition: ipsec.h:28
vlib_node_registration_t ah_encrypt_node
(constructor) VLIB_REGISTER_NODE (ah_encrypt_node)
Definition: ah_encrypt.c:59
ip46_address_range_t laddr
Definition: ipsec.h:206
u32 node_index
Node index.
Definition: node.h:494
#define foreach_ipsec_integ_alg
Definition: ipsec.h:88
uword * spd_index_by_spd_id
Definition: ipsec.h:283
ip46_address_t tunnel_dst_addr
Definition: ipsec.h:132
ipsec_if_set_key_type_t
Definition: ipsec.h:189
#define foreach_ipsec_crypto_alg
Definition: ipsec.h:66
ipsec_crypto_alg_t crypto_alg
Definition: ipsec.h:165
vlib_main_t * vm
Definition: buffer.c:294
#define foreach_ipsec_policy_action
Definition: ipsec.h:52
u32 esp_encrypt_node_index
Definition: ipsec.h:291
u32 sa_index
Definition: ipsec.h:215
static_always_inline void vnet_feature_next(u32 *next0, vlib_buffer_t *b0)
Definition: feature.h:246
u32 esp_decrypt_next_index
Definition: ipsec.h:297
vlib_node_registration_t ipsec_if_input_node
(constructor) VLIB_REGISTER_NODE (ipsec_if_input_node)
Definition: ipsec_if_in.c:224
#define foreach_ipsec_output_next
Definition: ipsec.h:24
uword * sa_index_by_sa_id
Definition: ipsec.h:285
static void ipsec_alloc_empty_buffers(vlib_main_t *vm, ipsec_main_t *im)
Definition: ipsec.h:356
u32 output_sa_index
Definition: ipsec.h:250
static_always_inline u32 get_next_output_feature_node_index(vlib_buffer_t *b, vlib_node_runtime_t *nr)
Definition: ipsec.h:377
vlib_main_t * vlib_main
Definition: ipsec.h:276
uword * ipsec_if_real_dev_by_show_dev
Definition: ipsec.h:287
port_range_t rport
Definition: ipsec.h:210
int ipsec_set_sa_key(vlib_main_t *vm, ipsec_sa_t *sa_update)
Definition: ipsec.c:467
ip46_address_range_t raddr
Definition: ipsec.h:207
signed int i32
Definition: types.h:77
u8 * format_ipsec_replay_window(u8 *s, va_list *args)
Definition: ipsec_format.c:122
ipsec_integ_alg_t integ_alg
Definition: ipsec.h:170
ipsec_sa_t * sad
Definition: ipsec.h:265
ipsec_crypto_alg_t
Definition: ipsec.h:80
u64 total_data_size
Definition: ipsec.h:144
u8 integ_key_len
Definition: ipsec.h:122
u32 show_instance
Definition: ipsec.h:252
ipsec_protocol_t protocol
Definition: ipsec.h:115
uword unformat_ipsec_policy_action(unformat_input_t *input, va_list *args)
Definition: ipsec_format.c:44
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
u32 input_sa_index
Definition: ipsec.h:249
u32 seq
Definition: ipsec.h:137
u8 * format_ipsec_integ_alg(u8 *s, va_list *args)
Definition: ipsec_format.c:90
u8 crypto_key_len
Definition: ipsec.h:118
struct _vlib_node_registration vlib_node_registration_t
ipsec_input_next_t
Definition: ipsec.h:44
int ipsec_add_del_policy(vlib_main_t *vm, ipsec_policy_t *policy, int is_add)
Definition: ipsec.c:154
u8 * format_ipsec_policy_action(u8 *s, va_list *args)
Definition: ipsec_format.c:26
int ipsec_add_del_tunnel_if_internal(vnet_main_t *vnm, ipsec_add_del_tunnel_args_t *args, u32 *sw_if_index)
Definition: ipsec_if.c:271
u32 * ipv4_inbound_policy_discard_and_bypass_indices
Definition: ipsec.h:230
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
vlib_counter_t counter
Definition: ipsec.h:218
u8 is_outbound
Definition: ipsec.h:202
u32 * free_tunnel_if_indices
Definition: ipsec.h:269
u32 * ipv6_inbound_policy_discard_and_bypass_indices
Definition: ipsec.h:232
u64 uword
Definition: types.h:112
u32 * ipv6_outbound_policies
Definition: ipsec.h:228
u32 id
Definition: ipsec.h:223
ipsec_crypto_alg_t crypto_alg
Definition: ipsec.h:117
static vlib_node_t * vlib_get_node(vlib_main_t *vm, u32 i)
Get vlib node by index.
Definition: node_funcs.h:59
int ipsec_add_del_tunnel_if(ipsec_add_del_tunnel_args_t *args)
Definition: ipsec_if.c:263
vlib_node_registration_t esp_decrypt_node
(constructor) VLIB_REGISTER_NODE (esp_decrypt_node)
Definition: esp_decrypt.c:419
static u32 vlib_buffer_alloc(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Allocate buffers into supplied array.
Definition: buffer_funcs.h:503
int ipsec_add_del_sa(vlib_main_t *vm, ipsec_sa_t *new_sa, int is_add)
Definition: ipsec.c:416
u32 ** empty_buffers
Definition: ipsec.h:271
u8 use_anti_replay
Definition: ipsec.h:126
vlib_node_registration_t esp_encrypt_node
(constructor) VLIB_REGISTER_NODE (esp_encrypt_node)
Definition: esp_encrypt.c:63
u32 esp_decrypt_node_index
Definition: ipsec.h:292