FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
esp.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 __ESP_H__
16 #define __ESP_H__
17 
18 #include <vnet/ip/ip.h>
19 #include <vnet/crypto/crypto.h>
20 #include <vnet/ipsec/ipsec.h>
21 
22 typedef struct
23 {
24  union
25  {
27  u8 spi_bytes[4];
28  };
30  u8 data[0];
31 } esp_header_t;
32 
33 typedef struct
34 {
37 } esp_footer_t;
38 
39 /* *INDENT-OFF* */
40 typedef CLIB_PACKED (struct {
42  esp_header_t esp;
43 }) ip4_and_esp_header_t;
44 /* *INDENT-ON* */
45 
46 /* *INDENT-OFF* */
47 typedef CLIB_PACKED (struct {
49  udp_header_t udp;
50  esp_header_t esp;
51 }) ip4_and_udp_and_esp_header_t;
52 /* *INDENT-ON* */
53 
54 /* *INDENT-OFF* */
55 typedef CLIB_PACKED (struct {
57  esp_header_t esp;
58 }) ip6_and_esp_header_t;
59 /* *INDENT-ON* */
60 
61 /**
62  * AES counter mode nonce
63  */
64 typedef struct
65 {
66  u32 salt;
67  u64 iv;
68  u32 ctr; /* counter: 1 in big-endian for ctr, unused for gcm */
69 } __clib_packed esp_ctr_nonce_t;
70 
71 STATIC_ASSERT_SIZEOF (esp_ctr_nonce_t, 16);
72 
73 /**
74  * AES GCM Additional Authentication data
75  */
76 typedef struct esp_aead_t_
77 {
78  /**
79  * for GCM: when using ESN it's:
80  * SPI, seq-hi, seg-low
81  * else
82  * SPI, seq-low
83  */
84  u32 data[3];
85 } __clib_packed esp_aead_t;
86 
87 #define ESP_SEQ_MAX (4294967295UL)
88 #define ESP_MAX_BLOCK_SIZE (16)
89 #define ESP_MAX_IV_SIZE (16)
90 #define ESP_MAX_ICV_SIZE (32)
91 
92 u8 *format_esp_header (u8 * s, va_list * args);
93 
94 /* TODO seq increment should be atomic to be accessed by multiple workers */
95 always_inline int
97 {
98  if (PREDICT_TRUE (ipsec_sa_is_set_USE_ESN (sa)))
99  {
100  if (PREDICT_FALSE (sa->seq == ESP_SEQ_MAX))
101  {
102  if (PREDICT_FALSE (ipsec_sa_is_set_USE_ANTI_REPLAY (sa) &&
103  sa->seq_hi == ESP_SEQ_MAX))
104  return 1;
105  sa->seq_hi++;
106  }
107  sa->seq++;
108  }
109  else
110  {
111  if (PREDICT_FALSE (ipsec_sa_is_set_USE_ANTI_REPLAY (sa) &&
112  sa->seq == ESP_SEQ_MAX))
113  return 1;
114  sa->seq++;
115  }
116 
117  return 0;
118 }
119 
121 esp_aad_fill (u8 *data, const esp_header_t *esp, const ipsec_sa_t *sa,
122  u32 seq_hi)
123 {
124  esp_aead_t *aad;
125 
126  aad = (esp_aead_t *) data;
127  aad->data[0] = esp->spi;
128 
129  if (ipsec_sa_is_set_USE_ESN (sa))
130  {
131  /* SPI, seq-hi, seq-low */
132  aad->data[1] = (u32) clib_host_to_net_u32 (seq_hi);
133  aad->data[2] = esp->seq;
134  return 12;
135  }
136  else
137  {
138  /* SPI, seq-low */
139  aad->data[1] = esp->seq;
140  return 8;
141  }
142 }
143 
144 /* Special case to drop or hand off packets for sync/async modes.
145  *
146  * Different than sync mode, async mode only enqueue drop or hand-off packets
147  * to next nodes.
148  */
149 always_inline void
151  u16 index, u16 *nexts, u16 drop_next)
152 {
153  nexts[index] = drop_next;
154  b->error = node->errors[err];
155 }
156 
157 /* when submitting a frame is failed, drop all buffers in the frame */
161  u32 *from, u16 *nexts, u16 drop_next_index)
162 {
163  u32 n_drop = f->n_elts;
164  u32 *bi = f->buffer_indices;
165 
166  while (n_drop--)
167  {
168  from[index] = bi[0];
170  drop_next_index);
171  bi++;
172  index++;
173  }
175 
176  return (f->n_elts);
177 }
178 
179 /**
180  * The post data structure to for esp_encrypt/decrypt_inline to write to
181  * vib_buffer_t opaque unused field, and for post nodes to pick up after
182  * dequeue.
183  **/
184 typedef struct
185 {
186  union
187  {
188  struct
189  {
194  };
196  };
197 
205 
208 
209 /* we are forced to store the decrypt post data into 2 separate places -
210  vlib_opaque and opaque2. */
211 typedef struct
212 {
217 
218 typedef union
219 {
223 
224 STATIC_ASSERT (sizeof (esp_post_data_t) <=
226  "Custom meta-data too large for vnet_buffer_opaque_t");
227 
228 #define esp_post_data(b) \
229  ((esp_post_data_t *)((u8 *)((b)->opaque) \
230  + STRUCT_OFFSET_OF (vnet_buffer_opaque_t, unused)))
231 
234  "Custom meta-data too large for vnet_buffer_opaque2_t");
235 
236 #define esp_post_data2(b) \
237  ((esp_decrypt_packet_data2_t *)((u8 *)((b)->opaque2) \
238  + STRUCT_OFFSET_OF (vnet_buffer_opaque2_t, unused)))
239 
240 typedef struct
241 {
242  /* esp post node index for async crypto */
249 
252 
253 #endif /* __ESP_H__ */
254 
255 /*
256  * fd.io coding-style-patch-verification: ON
257  *
258  * Local Variables:
259  * eval: (c-set-style "gnu")
260  * End:
261  */
ipsec.h
esp_aead_t_::data
u32 data[3]
for GCM: when using ESN it's: SPI, seq-hi, seg-low else SPI, seq-low
Definition: esp.h:84
CLIB_PACKED
typedef CLIB_PACKED(struct { ip4_header_t ip4;esp_header_t esp;}) ip4_and_esp_header_t
vnet_buffer_opaque2_t
Definition: buffer.h:444
crypto.h
esp_decrypt_packet_data_t::is_chain
u16 is_chain
Definition: esp.h:202
ip4
vl_api_ip4_address_t ip4
Definition: one.api:376
esp_async_post_next_t
Definition: esp.h:240
vlib_get_buffer
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:111
f
vlib_frame_t * f
Definition: interface_output.c:1098
esp_header_t::spi
u32 spi
Definition: esp.h:26
esp_decrypt_packet_data_t::iv_sz
u8 iv_sz
Definition: esp.h:191
esp_async_post_next_t::esp_mpls_tun_post_next
u32 esp_mpls_tun_post_next
Definition: esp.h:247
node
vlib_main_t vlib_node_runtime_t * node
Definition: nat44_ei.c:3047
esp_post_data_t::decrypt_data
esp_decrypt_packet_data_t decrypt_data
Definition: esp.h:221
u16
unsigned short u16
Definition: types.h:57
vnet_crypto_async_frame_t
Definition: crypto.h:358
esp_set_next_index
static void esp_set_next_index(vlib_buffer_t *b, vlib_node_runtime_t *node, u32 err, u16 index, u16 *nexts, u16 drop_next)
Definition: esp.h:150
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
esp_decrypt_packet_data_t::current_length
i16 current_length
Definition: esp.h:200
esp_aead_t_
AES GCM Additional Authentication data.
Definition: esp.h:76
STATIC_ASSERT
STATIC_ASSERT(sizeof(esp_post_data_t)<=STRUCT_SIZE_OF(vnet_buffer_opaque_t, unused), "Custom meta-data too large for vnet_buffer_opaque_t")
esp_header_t::seq
u32 seq
Definition: esp.h:29
udp_header_t
Definition: udp_packet.h:45
ip4_header_t
Definition: ip4_packet.h:87
i16
signed short i16
Definition: types.h:46
ipsec_sa_flags_t
enum ipsec_sad_flags_t_ ipsec_sa_flags_t
esp_encrypt_async_next
esp_async_post_next_t esp_encrypt_async_next
Definition: ipsec.c:30
vlib_buffer_t::error
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:145
ESP_SEQ_MAX
#define ESP_SEQ_MAX
Definition: esp.h:87
PREDICT_FALSE
#define PREDICT_FALSE(x)
Definition: clib.h:124
esp_decrypt_packet_data2_t
Definition: esp.h:211
esp_async_post_next_t::esp4_tun_post_next
u32 esp4_tun_post_next
Definition: esp.h:245
ipsec_sa_t::seq
u32 seq
Definition: ipsec_sa.h:132
esp_async_post_next_t::esp6_tun_post_next
u32 esp6_tun_post_next
Definition: esp.h:246
vnet_crypto_async_reset_frame
static_always_inline void vnet_crypto_async_reset_frame(vnet_crypto_async_frame_t *f)
Definition: crypto.h:650
esp_decrypt_packet_data_t::flags
ipsec_sa_flags_t flags
Definition: esp.h:192
esp_aead_t
struct esp_aead_t_ esp_aead_t
AES GCM Additional Authentication data.
esp_header_t
Definition: esp.h:22
esp_decrypt_packet_data_t::seq_hi
u32 seq_hi
Definition: esp.h:203
esp_decrypt_packet_data2_t::free_buffer_index
u32 free_buffer_index
Definition: esp.h:214
STATIC_ASSERT_SIZEOF
STATIC_ASSERT_SIZEOF(esp_ctr_nonce_t, 16)
esp_decrypt_packet_data_t::hdr_sz
u16 hdr_sz
Definition: esp.h:201
data
u8 data[128]
Definition: ipsec_types.api:95
ipsec_sa_t
Definition: ipsec_sa.h:116
index
u32 index
Definition: flow_types.api:221
always_inline
#define always_inline
Definition: rdma_mlx5dv.h:23
vnet_buffer_opaque_t
Definition: buffer.h:153
esp_decrypt_packet_data2_t::lb
vlib_buffer_t * lb
Definition: esp.h:213
STATIC_ASSERT_OFFSET_OF
STATIC_ASSERT_OFFSET_OF(esp_decrypt_packet_data_t, seq, sizeof(u64))
u64
unsigned long u64
Definition: types.h:89
ip.h
u32
unsigned int u32
Definition: types.h:88
ip6
vl_api_ip6_address_t ip6
Definition: one.api:424
esp_decrypt_packet_data_t::sa_index
u32 sa_index
Definition: esp.h:193
esp_aad_fill
static u16 esp_aad_fill(u8 *data, const esp_header_t *esp, const ipsec_sa_t *sa, u32 seq_hi)
Definition: esp.h:121
iv
static u8 iv[]
Definition: aes_cbc.c:24
ipsec_sa_t::seq_hi
u32 seq_hi
Definition: ipsec_sa.h:133
ip6_header_t
Definition: ip6_packet.h:294
salt
u32 salt
Definition: ipsec_types.api:139
vlib_main_t
Definition: main.h:102
b
vlib_buffer_t ** b
Definition: nat44_ei_out2in.c:717
esp_async_post_next_t::esp4_post_next
u32 esp4_post_next
Definition: esp.h:243
u8
unsigned char u8
Definition: types.h:56
esp_decrypt_packet_data2_t::icv_removed
u8 icv_removed
Definition: esp.h:215
esp_decrypt_packet_data_t::current_data
i16 current_data
Definition: esp.h:199
esp_decrypt_packet_data_t::icv_sz
u8 icv_sz
Definition: esp.h:190
nexts
u16 nexts[VLIB_FRAME_SIZE]
Definition: nat44_ei_out2in.c:718
esp_decrypt_packet_data_t::sa_data
u64 sa_data
Definition: esp.h:195
esp_post_data_t::next_index
u16 next_index
Definition: esp.h:220
vlib_node_runtime_t
Definition: node.h:454
esp_decrypt_packet_data_t::seq
u32 seq
Definition: esp.h:198
format_esp_header
u8 * format_esp_header(u8 *s, va_list *args)
Definition: esp_format.c:23
esp_async_recycle_failed_submit
static u32 esp_async_recycle_failed_submit(vlib_main_t *vm, vnet_crypto_async_frame_t *f, vlib_node_runtime_t *node, u32 err, u16 index, u32 *from, u16 *nexts, u16 drop_next_index)
Definition: esp.h:159
from
from
Definition: nat44_ei_hairpinning.c:415
PREDICT_TRUE
#define PREDICT_TRUE(x)
Definition: clib.h:125
esp_post_data_t
Definition: esp.h:218
esp_decrypt_packet_data_t
The post data structure to for esp_encrypt/decrypt_inline to write to vib_buffer_t opaque unused fiel...
Definition: esp.h:184
esp_decrypt_async_next
esp_async_post_next_t esp_decrypt_async_next
Definition: ipsec.c:31
STRUCT_SIZE_OF
#define STRUCT_SIZE_OF(t, f)
Definition: clib.h:75
esp_async_post_next_t::esp6_post_next
u32 esp6_post_next
Definition: esp.h:244
esp_seq_advance
static int esp_seq_advance(ipsec_sa_t *sa)
Definition: esp.h:96
vlib_buffer_t
VLIB buffer representation.
Definition: buffer.h:111