blob: d179233df49885d070123b104e520602f3b54b3d [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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 */
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000015#ifndef __ESP_H__
16#define __ESP_H__
Ed Warnickecb9cada2015-12-08 15:45:58 -070017
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +010018#include <vnet/ip/ip.h>
Damjan Marion91f17dc2019-03-18 18:59:25 +010019#include <vnet/crypto/crypto.h>
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +010020#include <vnet/ipsec/ipsec.h>
21
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070022typedef struct
23{
Neale Ranns41afb332019-07-16 06:19:35 -070024 union
25 {
26 u32 spi;
27 u8 spi_bytes[4];
28 };
Ed Warnickecb9cada2015-12-08 15:45:58 -070029 u32 seq;
30 u8 data[0];
31} esp_header_t;
32
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070033typedef struct
34{
Ed Warnickecb9cada2015-12-08 15:45:58 -070035 u8 pad_length;
36 u8 next_header;
37} esp_footer_t;
38
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070039/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070040typedef CLIB_PACKED (struct {
41 ip4_header_t ip4;
42 esp_header_t esp;
43}) ip4_and_esp_header_t;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070044/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070045
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070046/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070047typedef CLIB_PACKED (struct {
Klement Sekera4b089f22018-04-17 18:04:57 +020048 ip4_header_t ip4;
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* */
55typedef CLIB_PACKED (struct {
Ed Warnickecb9cada2015-12-08 15:45:58 -070056 ip6_header_t ip6;
57 esp_header_t esp;
58}) ip6_and_esp_header_t;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070059/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070060
Neale Ranns47feb112019-04-11 15:14:07 +000061/**
Benoît Ganne490b9272021-01-22 18:03:09 +010062 * AES counter mode nonce
63 */
64typedef 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
71STATIC_ASSERT_SIZEOF (esp_ctr_nonce_t, 16);
72
73/**
Neale Ranns47feb112019-04-11 15:14:07 +000074 * AES GCM Additional Authentication data
75 */
76typedef 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
Damjan Marionc59b9a22019-03-19 15:38:40 +010087#define ESP_SEQ_MAX (4294967295UL)
88#define ESP_MAX_BLOCK_SIZE (16)
Damjan Marionb4fff3a2019-03-25 15:54:40 +010089#define ESP_MAX_IV_SIZE (16)
Neale Ranns1091c4a2019-04-08 14:48:23 +000090#define ESP_MAX_ICV_SIZE (32)
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000091
Sergio Gonzalez Monroydb93cd92017-08-26 15:22:05 +010092u8 *format_esp_header (u8 * s, va_list * args);
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000093
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000094/* TODO seq increment should be atomic to be accessed by multiple workers */
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000095always_inline int
96esp_seq_advance (ipsec_sa_t * sa)
97{
Damjan Marion1e3aa5e2019-03-28 10:58:59 +010098 if (PREDICT_TRUE (ipsec_sa_is_set_USE_ESN (sa)))
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000099 {
100 if (PREDICT_FALSE (sa->seq == ESP_SEQ_MAX))
101 {
Damjan Mariond709cbc2019-03-26 13:16:42 +0100102 if (PREDICT_FALSE (ipsec_sa_is_set_USE_ANTI_REPLAY (sa) &&
103 sa->seq_hi == ESP_SEQ_MAX))
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +0000104 return 1;
105 sa->seq_hi++;
106 }
107 sa->seq++;
108 }
109 else
110 {
Damjan Mariond709cbc2019-03-26 13:16:42 +0100111 if (PREDICT_FALSE (ipsec_sa_is_set_USE_ANTI_REPLAY (sa) &&
112 sa->seq == ESP_SEQ_MAX))
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +0000113 return 1;
114 sa->seq++;
115 }
116
117 return 0;
118}
119
Fan Zhangf5395782020-04-29 14:00:03 +0100120always_inline u16
Neale Ranns5b891102021-06-28 13:31:28 +0000121esp_aad_fill (u8 *data, const esp_header_t *esp, const ipsec_sa_t *sa,
122 u32 seq_hi)
Neale Ranns47feb112019-04-11 15:14:07 +0000123{
124 esp_aead_t *aad;
125
Fan Zhangf5395782020-04-29 14:00:03 +0100126 aad = (esp_aead_t *) data;
Damjan Marion30b8b4a2019-05-29 18:49:25 +0200127 aad->data[0] = esp->spi;
Neale Ranns47feb112019-04-11 15:14:07 +0000128
129 if (ipsec_sa_is_set_USE_ESN (sa))
130 {
131 /* SPI, seq-hi, seq-low */
Neale Ranns5b891102021-06-28 13:31:28 +0000132 aad->data[1] = (u32) clib_host_to_net_u32 (seq_hi);
Damjan Marion30b8b4a2019-05-29 18:49:25 +0200133 aad->data[2] = esp->seq;
Fan Zhangf5395782020-04-29 14:00:03 +0100134 return 12;
Neale Ranns47feb112019-04-11 15:14:07 +0000135 }
136 else
Damjan Marion30b8b4a2019-05-29 18:49:25 +0200137 {
138 /* SPI, seq-low */
139 aad->data[1] = esp->seq;
Fan Zhangf5395782020-04-29 14:00:03 +0100140 return 8;
Damjan Marion30b8b4a2019-05-29 18:49:25 +0200141 }
Neale Ranns47feb112019-04-11 15:14:07 +0000142}
Fan Zhangf5395782020-04-29 14:00:03 +0100143
Fan Zhang18f0e312020-10-19 13:08:34 +0100144/* 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 */
149always_inline void
Neale Rannsf16e9a52021-02-25 19:09:24 +0000150esp_set_next_index (vlib_buffer_t *b, vlib_node_runtime_t *node, u32 err,
151 u16 index, u16 *nexts, u16 drop_next)
Fan Zhang18f0e312020-10-19 13:08:34 +0100152{
Neale Rannsf16e9a52021-02-25 19:09:24 +0000153 nexts[index] = drop_next;
154 b->error = node->errors[err];
Fan Zhang18f0e312020-10-19 13:08:34 +0100155}
156
157/* when submitting a frame is failed, drop all buffers in the frame */
Neale Rannsf16e9a52021-02-25 19:09:24 +0000158always_inline u32
159esp_async_recycle_failed_submit (vlib_main_t *vm, vnet_crypto_async_frame_t *f,
160 vlib_node_runtime_t *node, u32 err, u16 index,
161 u32 *from, u16 *nexts, u16 drop_next_index)
Fan Zhang18f0e312020-10-19 13:08:34 +0100162{
163 u32 n_drop = f->n_elts;
164 u32 *bi = f->buffer_indices;
Neale Rannsf16e9a52021-02-25 19:09:24 +0000165
Fan Zhang18f0e312020-10-19 13:08:34 +0100166 while (n_drop--)
167 {
Neale Rannsf16e9a52021-02-25 19:09:24 +0000168 from[index] = bi[0];
169 esp_set_next_index (vlib_get_buffer (vm, bi[0]), node, err, index, nexts,
170 drop_next_index);
Fan Zhang18f0e312020-10-19 13:08:34 +0100171 bi++;
Neale Rannsf16e9a52021-02-25 19:09:24 +0000172 index++;
Fan Zhang18f0e312020-10-19 13:08:34 +0100173 }
174 vnet_crypto_async_reset_frame (f);
Neale Rannsf16e9a52021-02-25 19:09:24 +0000175
176 return (f->n_elts);
Fan Zhang18f0e312020-10-19 13:08:34 +0100177}
178
Fan Zhangf5395782020-04-29 14:00:03 +0100179/**
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 **/
184typedef struct
185{
186 union
187 {
188 struct
189 {
190 u8 icv_sz;
191 u8 iv_sz;
192 ipsec_sa_flags_t flags;
193 u32 sa_index;
194 };
195 u64 sa_data;
196 };
197
198 u32 seq;
199 i16 current_data;
200 i16 current_length;
201 u16 hdr_sz;
202 u16 is_chain;
Neale Ranns5b891102021-06-28 13:31:28 +0000203 u32 seq_hi;
Fan Zhangf5395782020-04-29 14:00:03 +0100204} esp_decrypt_packet_data_t;
205
206STATIC_ASSERT_SIZEOF (esp_decrypt_packet_data_t, 3 * sizeof (u64));
Benoît Ganne490b9272021-01-22 18:03:09 +0100207STATIC_ASSERT_OFFSET_OF (esp_decrypt_packet_data_t, seq, sizeof (u64));
Fan Zhangf5395782020-04-29 14:00:03 +0100208
209/* we are forced to store the decrypt post data into 2 separate places -
210 vlib_opaque and opaque2. */
211typedef struct
212{
213 vlib_buffer_t *lb;
214 u32 free_buffer_index;
215 u8 icv_removed;
216} esp_decrypt_packet_data2_t;
217
218typedef union
219{
220 u16 next_index;
221 esp_decrypt_packet_data_t decrypt_data;
222} esp_post_data_t;
223
224STATIC_ASSERT (sizeof (esp_post_data_t) <=
225 STRUCT_SIZE_OF (vnet_buffer_opaque_t, unused),
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
232STATIC_ASSERT (sizeof (esp_decrypt_packet_data2_t) <=
233 STRUCT_SIZE_OF (vnet_buffer_opaque2_t, unused),
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
240typedef struct
241{
242 /* esp post node index for async crypto */
243 u32 esp4_post_next;
244 u32 esp6_post_next;
245 u32 esp4_tun_post_next;
246 u32 esp6_tun_post_next;
Neale Ranns4a58e492020-12-21 13:19:10 +0000247 u32 esp_mpls_tun_post_next;
Fan Zhangf5395782020-04-29 14:00:03 +0100248} esp_async_post_next_t;
249
250extern esp_async_post_next_t esp_encrypt_async_next;
251extern esp_async_post_next_t esp_decrypt_async_next;
252
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +0000253#endif /* __ESP_H__ */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700254
255/*
256 * fd.io coding-style-patch-verification: ON
257 *
258 * Local Variables:
259 * eval: (c-set-style "gnu")
260 * End:
261 */