FD.io VPP  v17.04.2-2-ga8f93f8
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 #define IPSEC_FLAG_IPSEC_GRE_TUNNEL (1 << 0)
19 
20 
21 #define foreach_ipsec_output_next \
22 _(DROP, "error-drop") \
23 _(ESP_ENCRYPT, "esp-encrypt")
24 
25 #define _(v, s) IPSEC_OUTPUT_NEXT_##v,
26 typedef enum
27 {
29 #undef _
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,
39 typedef enum
40 {
42 #undef _
45 
46 
47 #define foreach_ipsec_policy_action \
48  _(0, BYPASS, "bypass") \
49  _(1, DISCARD, "discard") \
50  _(2, RESOLVE, "resolve") \
51  _(3, PROTECT, "protect")
52 
53 typedef enum
54 {
55 #define _(v,f,s) IPSEC_POLICY_ACTION_##f = v,
57 #undef _
60 
61 #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")
67 
68 typedef enum
69 {
70 #define _(v,f,s) IPSEC_CRYPTO_ALG_##f = v,
72 #undef _
75 
76 #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 */ \
84  _(7, AES_GCM_128, "aes-gcm-128") /* RFC4106 */
85 
86 typedef enum
87 {
88 #define _(v,f,s) IPSEC_INTEG_ALG_##f = v,
90 #undef _
93 
94 typedef enum
95 {
99 
100 typedef struct
101 {
105 
108  u8 crypto_key[128];
109 
112  u8 integ_key[128];
113 
116 
119  ip46_address_t tunnel_src_addr;
120  ip46_address_t tunnel_dst_addr;
121 
123 
124  /* runtime */
130 
131  /*lifetime data */
133 } ipsec_sa_t;
134 
135 typedef struct
136 {
137  ip46_address_t start, stop;
139 
140 typedef struct
141 {
142  u16 start, stop;
143 } port_range_t;
144 
145 typedef struct
146 {
155  u8 local_crypto_key[128];
157  u8 remote_crypto_key[128];
160  u8 local_integ_key[128];
162  u8 remote_integ_key[128];
164 
165 typedef struct
166 {
173 
174 typedef enum
175 {
182 
183 typedef struct
184 {
188 
189  // Selector
196 
197  // Policy
201 
202  // Counter
205 
206 typedef struct
207 {
209  /* pool of policies */
211  /* vectors of policy indices */
218 } ipsec_spd_t;
219 
220 typedef struct
221 {
224 
225 typedef struct
226 {
229 
230 typedef struct
231 {
236 
237 typedef struct
238 {
239  i32 (*add_del_sa_sess_cb) (u32 sa_index, u8 is_add);
240  clib_error_t *(*check_support_cb) (ipsec_sa_t * sa);
242 
243 typedef struct
244 {
245  /* pool of tunnel instances */
248 
249  /* pool of tunnel interfaces */
252 
254 
256 
257  /* convenience */
260 
261  /* next node indices */
262  u32 feature_next_node_index[32];
263 
264  /* hashes */
269 
270  /* node indeces */
274  /* next node indeces */
277 
278  /* callbacks */
280 } ipsec_main_t;
281 
283 
288 
289 
290 /*
291  * functions
292  */
293 int ipsec_set_interface_spd (vlib_main_t * vm, u32 sw_if_index, u32 spd_id,
294  int is_add);
295 int ipsec_add_del_spd (vlib_main_t * vm, u32 spd_id, int is_add);
297  int is_add);
298 int ipsec_add_del_sa (vlib_main_t * vm, ipsec_sa_t * new_sa, int is_add);
299 int ipsec_set_sa_key (vlib_main_t * vm, ipsec_sa_t * sa_update);
300 
302 u8 *format_ipsec_if_output_trace (u8 * s, va_list * args);
303 u8 *format_ipsec_policy_action (u8 * s, va_list * args);
304 u8 *format_ipsec_crypto_alg (u8 * s, va_list * args);
305 u8 *format_ipsec_integ_alg (u8 * s, va_list * args);
306 u8 *format_ipsec_replay_window (u8 * s, va_list * args);
307 uword unformat_ipsec_policy_action (unformat_input_t * input, va_list * args);
308 uword unformat_ipsec_crypto_alg (unformat_input_t * input, va_list * args);
309 uword unformat_ipsec_integ_alg (unformat_input_t * input, va_list * args);
310 
311 /*u32 ipsec_add_del_tunnel_if (vnet_main_t * vnm, ipsec_add_del_tunnel_args_t * args); */
315  args);
316 int ipsec_set_interface_key (vnet_main_t * vnm, u32 hw_if_index,
317  ipsec_if_set_key_type_t type, u8 alg, u8 * key);
318 
319 
320 /*
321  * inline functions
322  */
323 
324 always_inline void
326 {
327  u32 cpu_index = os_get_cpu_number ();
328  uword l = vec_len (im->empty_buffers[cpu_index]);
329  uword n_alloc = 0;
330 
331  if (PREDICT_FALSE (l < VLIB_FRAME_SIZE))
332  {
333  if (!im->empty_buffers[cpu_index])
334  {
335  vec_alloc (im->empty_buffers[cpu_index], 2 * VLIB_FRAME_SIZE);
336  }
337 
338  n_alloc = vlib_buffer_alloc (vm, im->empty_buffers[cpu_index] + l,
339  2 * VLIB_FRAME_SIZE - l);
340 
341  _vec_len (im->empty_buffers[cpu_index]) = l + n_alloc;
342  }
343 }
344 
347  vlib_node_runtime_t * nr)
348 {
349  u32 next;
350  u32 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_TX];
352  vlib_node_t *node = vlib_get_node (vm, nr->node_index);
353 
354  vnet_feature_next (sw_if_index, &next, b);
355  return node->next_nodes[next];
356 }
357 
358 #endif /* __IPSEC_H__ */
359 
360 /*
361  * fd.io coding-style-patch-verification: ON
362  *
363  * Local Variables:
364  * eval: (c-set-style "gnu")
365  * End:
366  */
u32 * next_nodes
Definition: node.h:288
ip46_address_t stop
Definition: ipsec.h:137
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:322
u32 * ipv6_inbound_protect_policy_indices
Definition: ipsec.h:216
ipsec_spd_t * spds
Definition: ipsec.h:246
u32 * ipv4_inbound_protect_policy_indices
Definition: ipsec.h:214
ipsec_tunnel_if_t * tunnel_interfaces
Definition: ipsec.h:250
u16 stop
Definition: ipsec.h:142
ip46_address_t tunnel_src_addr
Definition: ipsec.h:119
uword * tunnel_index_by_key
Definition: ipsec.h:255
u32 id
Definition: ipsec.h:102
int ipsec_set_interface_spd(vlib_main_t *vm, u32 sw_if_index, u32 spd_id, int is_add)
Definition: ipsec.c:39
vlib_node_registration_t ipsec_if_output_node
(constructor) VLIB_REGISTER_NODE (ipsec_if_output_node)
Definition: ipsec_if_out.c:122
u32 ipsec_get_sa_index_by_sa_id(u32 sa_id)
Definition: ipsec.c:28
i32 priority
Definition: ipsec.h:186
ipsec_integ_alg_t integ_alg
Definition: ipsec.h:110
Combined counter to hold both packets and byte differences.
Definition: counter.h:139
u8 is_tunnel
Definition: ipsec.h:117
struct _vlib_node_registration vlib_node_registration_t
u32 * ipv4_outbound_policies
Definition: ipsec.h:212
ipsec_output_next_t
Definition: ipsec.h:26
int ipsec_add_del_spd(vlib_main_t *vm, u32 spd_id, int is_add)
Definition: ipsec.c:86
uword * ipsec_if_pool_index_by_key
Definition: ipsec.h:268
#define vec_alloc(V, N)
Allocate space for N more elements (no header, unspecified alignment)
Definition: vec.h:279
u32 spi
Definition: ipsec.h:103
port_range_t lport
Definition: ipsec.h:194
u32 seq_hi
Definition: ipsec.h:126
uword * spd_index_by_sw_if_index
Definition: ipsec.h:266
u64 replay_window
Definition: ipsec.h:129
#define static_always_inline
Definition: clib.h:85
u32 esp_encrypt_next_index
Definition: ipsec.h:275
uword unformat_ipsec_integ_alg(unformat_input_t *input, va_list *args)
Definition: ipsec_format.c:108
ipsec_main_t ipsec_main
Definition: ipsec.h:282
u8 use_esn
Definition: ipsec.h:114
ipsec_policy_action_t
Definition: ipsec.h:53
int ipsec_add_del_ipsec_gre_tunnel(vnet_main_t *vnm, ipsec_add_del_ipsec_gre_tunnel_args_t *args)
Definition: ipsec_if.c:260
int i32
Definition: types.h:81
ip4_address_t remote_ip
Definition: ipsec.h:150
#define foreach_ipsec_input_next
Definition: ipsec.h:34
ipsec_main_callbacks_t cb
Definition: ipsec.h:279
unsigned long u64
Definition: types.h:89
u8 * format_ipsec_crypto_alg(u8 *s, va_list *args)
Definition: ipsec_format.c:58
ipsec_policy_t * policies
Definition: ipsec.h:210
u32 last_seq
Definition: ipsec.h:127
u32 error_drop_node_index
Definition: ipsec.h:271
ipsec_integ_alg_t
Definition: ipsec.h:86
u8 is_tunnel_ip6
Definition: ipsec.h:118
u32 salt
Definition: ipsec.h:122
vnet_main_t * vnet_main
Definition: ipsec.h:259
struct _unformat_input_t unformat_input_t
uword os_get_cpu_number(void)
Definition: unix-misc.c:224
u32 last_seq_hi
Definition: ipsec.h:128
uword unformat_ipsec_crypto_alg(unformat_input_t *input, va_list *args)
Definition: ipsec_format.c:76
#define PREDICT_FALSE(x)
Definition: clib.h:97
ipsec_protocol_t
Definition: ipsec.h:94
#define always_inline
Definition: ipsec.h:26
#define VLIB_FRAME_SIZE
Definition: node.h:328
ip46_address_range_t laddr
Definition: ipsec.h:191
u32 node_index
Node index.
Definition: node.h:436
static_always_inline void vnet_feature_next(u32 sw_if_index, u32 *next0, vlib_buffer_t *b0)
Definition: feature.h:221
#define foreach_ipsec_integ_alg
Definition: ipsec.h:76
uword * spd_index_by_spd_id
Definition: ipsec.h:265
ip46_address_t tunnel_dst_addr
Definition: ipsec.h:120
ipsec_if_set_key_type_t
Definition: ipsec.h:174
#define foreach_ipsec_crypto_alg
Definition: ipsec.h:61
ipsec_crypto_alg_t crypto_alg
Definition: ipsec.h:153
vlib_main_t * vm
Definition: buffer.c:276
#define foreach_ipsec_policy_action
Definition: ipsec.h:47
u32 esp_encrypt_node_index
Definition: ipsec.h:272
u32 sa_index
Definition: ipsec.h:200
u32 esp_decrypt_next_index
Definition: ipsec.h:276
vlib_node_registration_t ipsec_if_input_node
(constructor) VLIB_REGISTER_NODE (ipsec_if_input_node)
Definition: ipsec_if_in.c:136
#define foreach_ipsec_output_next
Definition: ipsec.h:21
uword * sa_index_by_sa_id
Definition: ipsec.h:267
static void ipsec_alloc_empty_buffers(vlib_main_t *vm, ipsec_main_t *im)
Definition: ipsec.h:325
u32 output_sa_index
Definition: ipsec.h:233
static_always_inline u32 get_next_output_feature_node_index(vlib_buffer_t *b, vlib_node_runtime_t *nr)
Definition: ipsec.h:346
vlib_main_t * vlib_main
Definition: ipsec.h:258
port_range_t rport
Definition: ipsec.h:195
int ipsec_set_sa_key(vlib_main_t *vm, ipsec_sa_t *sa_update)
Definition: ipsec.c:455
ip46_address_range_t raddr
Definition: ipsec.h:192
unsigned int u32
Definition: types.h:88
u8 * format_ipsec_replay_window(u8 *s, va_list *args)
Definition: ipsec_format.c:122
ipsec_integ_alg_t integ_alg
Definition: ipsec.h:158
ipsec_sa_t * sad
Definition: ipsec.h:247
ipsec_crypto_alg_t
Definition: ipsec.h:68
u64 total_data_size
Definition: ipsec.h:132
u8 integ_key_len
Definition: ipsec.h:111
ipsec_protocol_t protocol
Definition: ipsec.h:104
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:232
u32 seq
Definition: ipsec.h:125
u64 uword
Definition: types.h:112
u8 * format_ipsec_integ_alg(u8 *s, va_list *args)
Definition: ipsec_format.c:90
u8 crypto_key_len
Definition: ipsec.h:107
ipsec_input_next_t
Definition: ipsec.h:39
Definition: defs.h:47
int ipsec_add_del_policy(vlib_main_t *vm, ipsec_policy_t *policy, int is_add)
Definition: ipsec.c:152
unsigned short u16
Definition: types.h:57
u8 * format_ipsec_policy_action(u8 *s, va_list *args)
Definition: ipsec_format.c:26
u32 * ipv4_inbound_policy_discard_and_bypass_indices
Definition: ipsec.h:215
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
vlib_counter_t counter
Definition: ipsec.h:203
u8 is_outbound
Definition: ipsec.h:187
u32 * free_tunnel_if_indices
Definition: ipsec.h:251
u32 * ipv6_inbound_policy_discard_and_bypass_indices
Definition: ipsec.h:217
u32 * ipv6_outbound_policies
Definition: ipsec.h:213
u32 id
Definition: ipsec.h:208
#define vnet_buffer(b)
Definition: buffer.h:294
ipsec_crypto_alg_t crypto_alg
Definition: ipsec.h:106
static vlib_node_t * vlib_get_node(vlib_main_t *vm, u32 i)
Get vlib node by index.
Definition: node_funcs.h:58
int ipsec_add_del_tunnel_if(ipsec_add_del_tunnel_args_t *args)
Definition: ipsec_if.c:108
vlib_node_registration_t esp_decrypt_node
(constructor) VLIB_REGISTER_NODE (esp_decrypt_node)
Definition: esp_decrypt.c:406
static u32 vlib_buffer_alloc(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Allocate buffers into supplied array.
Definition: buffer_funcs.h:245
int ipsec_add_del_sa(vlib_main_t *vm, ipsec_sa_t *new_sa, int is_add)
Definition: ipsec.c:411
u32 ** empty_buffers
Definition: ipsec.h:253
u8 use_anti_replay
Definition: ipsec.h:115
vlib_node_registration_t esp_encrypt_node
(constructor) VLIB_REGISTER_NODE (esp_encrypt_node)
Definition: esp_encrypt.c:61
u8 * format_ipsec_if_output_trace(u8 *s, va_list *args)
Definition: ipsec_if_out.c:50
u32 esp_decrypt_node_index
Definition: ipsec.h:273