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/crypto/crypto.h>
20 #include <vnet/ipsec/ipsec.h>
21 #include <vnet/ipsec/ipsec.api_enum.h>
40 typedef CLIB_PACKED (struct {
43 }) ip4_and_esp_header_t;
45 typedef CLIB_PACKED (struct {
49 }) ip4_and_udp_and_esp_header_t;
51 typedef CLIB_PACKED (struct {
54 }) ip6_and_esp_header_t;
57 * AES counter mode nonce
63 u32 ctr; /* counter: 1 in big-endian for ctr, unused for gcm */
64 } __clib_packed esp_ctr_nonce_t;
66 STATIC_ASSERT_SIZEOF (esp_ctr_nonce_t, 16);
69 * AES GCM Additional Authentication data
71 typedef struct esp_aead_t_
74 * for GCM: when using ESN it's:
75 * SPI, seq-hi, seg-low
80 } __clib_packed esp_aead_t;
82 #define ESP_SEQ_MAX (4294967295UL)
84 u8 *format_esp_header (u8 * s, va_list * args);
86 /* TODO seq increment should be atomic to be accessed by multiple workers */
88 esp_seq_advance (ipsec_sa_t * sa)
90 if (PREDICT_TRUE (ipsec_sa_is_set_USE_ESN (sa)))
92 if (PREDICT_FALSE (sa->seq == ESP_SEQ_MAX))
94 if (PREDICT_FALSE (ipsec_sa_is_set_USE_ANTI_REPLAY (sa) &&
95 sa->seq_hi == ESP_SEQ_MAX))
103 if (PREDICT_FALSE (ipsec_sa_is_set_USE_ANTI_REPLAY (sa) &&
104 sa->seq == ESP_SEQ_MAX))
113 esp_aad_fill (u8 *data, const esp_header_t *esp, const ipsec_sa_t *sa,
118 aad = (esp_aead_t *) data;
119 aad->data[0] = esp->spi;
121 if (ipsec_sa_is_set_USE_ESN (sa))
123 /* SPI, seq-hi, seq-low */
124 aad->data[1] = (u32) clib_host_to_net_u32 (seq_hi);
125 aad->data[2] = esp->seq;
131 aad->data[1] = esp->seq;
137 esp_encrypt_err_to_sa_err (u32 err)
141 case ESP_ENCRYPT_ERROR_HANDOFF:
142 return IPSEC_SA_ERROR_HANDOFF;
143 case ESP_ENCRYPT_ERROR_SEQ_CYCLED:
144 return IPSEC_SA_ERROR_SEQ_CYCLED;
145 case ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR:
146 return IPSEC_SA_ERROR_CRYPTO_ENGINE_ERROR;
147 case ESP_ENCRYPT_ERROR_CRYPTO_QUEUE_FULL:
148 return IPSEC_SA_ERROR_CRYPTO_QUEUE_FULL;
149 case ESP_ENCRYPT_ERROR_NO_BUFFERS:
150 return IPSEC_SA_ERROR_NO_BUFFERS;
151 case ESP_ENCRYPT_ERROR_NO_ENCRYPTION:
152 return IPSEC_SA_ERROR_NO_ENCRYPTION;
158 esp_decrypt_err_to_sa_err (u32 err)
162 case ESP_DECRYPT_ERROR_HANDOFF:
163 return IPSEC_SA_ERROR_HANDOFF;
164 case ESP_DECRYPT_ERROR_DECRYPTION_FAILED:
165 return IPSEC_SA_ERROR_DECRYPTION_FAILED;
166 case ESP_DECRYPT_ERROR_INTEG_ERROR:
167 return IPSEC_SA_ERROR_INTEG_ERROR;
168 case ESP_DECRYPT_ERROR_CRYPTO_ENGINE_ERROR:
169 return IPSEC_SA_ERROR_CRYPTO_ENGINE_ERROR;
170 case ESP_DECRYPT_ERROR_REPLAY:
171 return IPSEC_SA_ERROR_REPLAY;
172 case ESP_DECRYPT_ERROR_RUNT:
173 return IPSEC_SA_ERROR_RUNT;
174 case ESP_DECRYPT_ERROR_NO_BUFFERS:
175 return IPSEC_SA_ERROR_NO_BUFFERS;
176 case ESP_DECRYPT_ERROR_OVERSIZED_HEADER:
177 return IPSEC_SA_ERROR_OVERSIZED_HEADER;
178 case ESP_DECRYPT_ERROR_NO_TAIL_SPACE:
179 return IPSEC_SA_ERROR_NO_TAIL_SPACE;
180 case ESP_DECRYPT_ERROR_TUN_NO_PROTO:
181 return IPSEC_SA_ERROR_TUN_NO_PROTO;
182 case ESP_DECRYPT_ERROR_UNSUP_PAYLOAD:
183 return IPSEC_SA_ERROR_UNSUP_PAYLOAD;
189 esp_encrypt_set_next_index (vlib_buffer_t *b, vlib_node_runtime_t *node,
190 u32 thread_index, u32 err, u16 index, u16 *nexts,
191 u16 drop_next, u32 sa_index)
193 ipsec_set_next_index (b, node, thread_index, err,
194 esp_encrypt_err_to_sa_err (err), index, nexts,
195 drop_next, sa_index);
199 esp_decrypt_set_next_index (vlib_buffer_t *b, vlib_node_runtime_t *node,
200 u32 thread_index, u32 err, u16 index, u16 *nexts,
201 u16 drop_next, u32 sa_index)
203 ipsec_set_next_index (b, node, thread_index, err,
204 esp_decrypt_err_to_sa_err (err), index, nexts,
205 drop_next, sa_index);
209 * The post data structure to for esp_encrypt/decrypt_inline to write to
210 * vib_buffer_t opaque unused field, and for post nodes to pick up after
221 ipsec_sa_flags_t flags;
233 } esp_decrypt_packet_data_t;
235 STATIC_ASSERT_SIZEOF (esp_decrypt_packet_data_t, 3 * sizeof (u64));
236 STATIC_ASSERT_OFFSET_OF (esp_decrypt_packet_data_t, seq, sizeof (u64));
238 /* we are forced to store the decrypt post data into 2 separate places -
239 vlib_opaque and opaque2. */
243 u32 free_buffer_index;
245 } esp_decrypt_packet_data2_t;
250 esp_decrypt_packet_data_t decrypt_data;
253 STATIC_ASSERT (sizeof (esp_post_data_t) <=
254 STRUCT_SIZE_OF (vnet_buffer_opaque_t, unused),
255 "Custom meta-data too large for vnet_buffer_opaque_t");
257 #define esp_post_data(b) \
258 ((esp_post_data_t *)((u8 *)((b)->opaque) \
259 + STRUCT_OFFSET_OF (vnet_buffer_opaque_t, unused)))
261 STATIC_ASSERT (sizeof (esp_decrypt_packet_data2_t) <=
262 STRUCT_SIZE_OF (vnet_buffer_opaque2_t, unused),
263 "Custom meta-data too large for vnet_buffer_opaque2_t");
265 #define esp_post_data2(b) \
266 ((esp_decrypt_packet_data2_t *)((u8 *)((b)->opaque2) \
267 + STRUCT_OFFSET_OF (vnet_buffer_opaque2_t, unused)))
271 /* esp post node index for async crypto */
274 u32 esp4_tun_post_next;
275 u32 esp6_tun_post_next;
276 u32 esp_mpls_tun_post_next;
277 } esp_async_post_next_t;
279 extern esp_async_post_next_t esp_encrypt_async_next;
280 extern esp_async_post_next_t esp_decrypt_async_next;
282 /* when submitting a frame is failed, drop all buffers in the frame */
284 esp_async_recycle_failed_submit (vlib_main_t *vm, vnet_crypto_async_frame_t *f,
285 vlib_node_runtime_t *node, u32 err,
286 u32 ipsec_sa_err, u16 index, u32 *from,
287 u16 *nexts, u16 drop_next_index,
291 u32 n_drop = f->n_elts;
292 u32 *bi = f->buffer_indices;
299 b = vlib_get_buffer (vm, bi[0]);
303 sa_index = vnet_buffer (b)->ipsec.sad_index;
307 sa_index = esp_post_data (b)->decrypt_data.sa_index;
310 ipsec_set_next_index (b, node, vm->thread_index, err, ipsec_sa_err,
311 index, nexts, drop_next_index, sa_index);
319 #endif /* __ESP_H__ */
322 * fd.io coding-style-patch-verification: ON
325 * eval: (c-set-style "gnu")