blob: 6e5007151b95325f74ca6b44c8cb7cc8157281df [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * esp_encrypt.c : IPSec ESP encrypt node
3 *
4 * Copyright (c) 2015 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <vnet/vnet.h>
19#include <vnet/api_errno.h>
20#include <vnet/ip/ip.h>
Klement Sekera4b089f22018-04-17 18:04:57 +020021#include <vnet/udp/udp.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070022
Damjan Marion91f17dc2019-03-18 18:59:25 +010023#include <vnet/crypto/crypto.h>
24
Ed Warnickecb9cada2015-12-08 15:45:58 -070025#include <vnet/ipsec/ipsec.h>
26#include <vnet/ipsec/esp.h>
27
Ed Warnickecb9cada2015-12-08 15:45:58 -070028#define foreach_esp_encrypt_next \
29_(DROP, "error-drop") \
Sergio Gonzalez Monroy1b6f9022016-12-12 10:37:49 +000030_(IP4_LOOKUP, "ip4-lookup") \
31_(IP6_LOOKUP, "ip6-lookup") \
Ed Warnickecb9cada2015-12-08 15:45:58 -070032_(INTERFACE_OUTPUT, "interface-output")
33
34#define _(v, s) ESP_ENCRYPT_NEXT_##v,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070035typedef enum
36{
Ed Warnickecb9cada2015-12-08 15:45:58 -070037 foreach_esp_encrypt_next
38#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070039 ESP_ENCRYPT_N_NEXT,
Ed Warnickecb9cada2015-12-08 15:45:58 -070040} esp_encrypt_next_t;
41
Damjan Marionc59b9a22019-03-19 15:38:40 +010042#define foreach_esp_encrypt_error \
43 _(RX_PKTS, "ESP pkts received") \
44 _(SEQ_CYCLED, "sequence number cycled (packet dropped)") \
45 _(CRYPTO_ENGINE_ERROR, "crypto engine error (packet dropped)") \
46 _(CHAINED_BUFFER, "chained buffers (packet dropped)") \
47 _(NO_TRAILER_SPACE, "no trailer space (packet dropped)")
Ed Warnickecb9cada2015-12-08 15:45:58 -070048
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070049typedef enum
50{
Ed Warnickecb9cada2015-12-08 15:45:58 -070051#define _(sym,str) ESP_ENCRYPT_ERROR_##sym,
52 foreach_esp_encrypt_error
53#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070054 ESP_ENCRYPT_N_ERROR,
Ed Warnickecb9cada2015-12-08 15:45:58 -070055} esp_encrypt_error_t;
56
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070057static char *esp_encrypt_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -070058#define _(sym,string) string,
59 foreach_esp_encrypt_error
60#undef _
61};
62
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070063typedef struct
64{
Neale Ranns8d7c5022019-02-06 01:41:05 -080065 u32 sa_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -070066 u32 spi;
67 u32 seq;
Klement Sekera4b089f22018-04-17 18:04:57 +020068 u8 udp_encap;
Ed Warnickecb9cada2015-12-08 15:45:58 -070069 ipsec_crypto_alg_t crypto_alg;
70 ipsec_integ_alg_t integ_alg;
71} esp_encrypt_trace_t;
72
73/* packet trace format function */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070074static u8 *
75format_esp_encrypt_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070076{
77 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
78 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070079 esp_encrypt_trace_t *t = va_arg (*args, esp_encrypt_trace_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070080
Neale Ranns8d7c5022019-02-06 01:41:05 -080081 s = format (s, "esp: sa-index %d spi %u seq %u crypto %U integrity %U%s",
82 t->sa_index, t->spi, t->seq,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070083 format_ipsec_crypto_alg, t->crypto_alg,
Klement Sekera4b089f22018-04-17 18:04:57 +020084 format_ipsec_integ_alg, t->integ_alg,
85 t->udp_encap ? " udp-encap-enabled" : "");
Ed Warnickecb9cada2015-12-08 15:45:58 -070086 return s;
87}
88
Damjan Marionc59b9a22019-03-19 15:38:40 +010089/* pad packet in input buffer */
90static_always_inline u8 *
91esp_add_footer_and_icv (vlib_buffer_t * b, u8 block_size, u8 icv_sz)
Ed Warnickecb9cada2015-12-08 15:45:58 -070092{
Damjan Marionc59b9a22019-03-19 15:38:40 +010093 static const u8 pad_data[ESP_MAX_BLOCK_SIZE] = {
94 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
95 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x00, 0x00,
96 };
Ed Warnickecb9cada2015-12-08 15:45:58 -070097
Damjan Marionc59b9a22019-03-19 15:38:40 +010098 u16 min_length = b->current_length + sizeof (esp_footer_t);
99 u16 new_length = round_pow2 (min_length, block_size);
100 u8 pad_bytes = new_length - min_length;
101 esp_footer_t *f = (esp_footer_t *) (vlib_buffer_get_current (b) +
102 new_length - sizeof (esp_footer_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103
Damjan Marionc59b9a22019-03-19 15:38:40 +0100104 if (pad_bytes)
105 clib_memcpy_fast ((u8 *) f - pad_bytes, pad_data, ESP_MAX_BLOCK_SIZE);
106
107 f->pad_length = pad_bytes;
108 b->current_length = new_length + icv_sz;
109 return &f->next_header;
110}
111
112static_always_inline void
113esp_update_ip4_hdr (ip4_header_t * ip4, u16 len, int is_transport, int is_udp)
114{
115 ip_csum_t sum = ip4->checksum;
116 u16 old_len = 0;
117
118 if (is_transport)
119 {
120 u8 prot = is_udp ? IP_PROTOCOL_UDP : IP_PROTOCOL_IPSEC_ESP;
121 old_len = ip4->length;
122 sum = ip_csum_update (sum, ip4->protocol, prot, ip4_header_t, protocol);
123 ip4->protocol = prot;
124 }
125
126 ip4->length = len = clib_net_to_host_u16 (len);
127 sum = ip_csum_update (ip4->checksum, old_len, len, ip4_header_t, length);
128 ip4->checksum = ip_csum_fold (sum);
129}
130
131static_always_inline void
132esp_fill_udp_hdr (ipsec_sa_t * sa, udp_header_t * udp, u16 len)
133{
134 clib_memcpy_fast (udp, &sa->udp_hdr, sizeof (udp_header_t));
135 udp->length = clib_net_to_host_u16 (len);
136}
137
138static_always_inline u8
139ext_hdr_is_pre_esp (u8 nexthdr)
140{
141#ifdef CLIB_HAVE_VEC128
142 static const u8x16 ext_hdr_types = {
143 IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS,
144 IP_PROTOCOL_IPV6_ROUTE,
145 IP_PROTOCOL_IPV6_FRAGMENTATION,
146 };
147
148 return !u8x16_is_all_zero (ext_hdr_types == u8x16_splat (nexthdr));
149#else
150 return ((nexthdr ^ IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) |
151 (nexthdr ^ IP_PROTOCOL_IPV6_ROUTE) |
152 (nexthdr ^ IP_PROTOCOL_IPV6_FRAGMENTATION) != 0);
153#endif
154}
155
156static_always_inline u8
157esp_get_ip6_hdr_len (ip6_header_t * ip6)
158{
159 /* this code assumes that HbH, route and frag headers will be before
160 others, if that is not the case, they will end up encrypted */
161
162 u8 len = sizeof (ip6_header_t);
163 ip6_ext_header_t *p;
164
165 /* if next packet doesn't have ext header */
166 if (ext_hdr_is_pre_esp (ip6->protocol) == 0)
167 return len;
168
169 p = (void *) (ip6 + 1);
170 len += ip6_ext_header_len (p);
171
172 while (ext_hdr_is_pre_esp (p->next_hdr))
173 {
174 len += ip6_ext_header_len (p);
175 p = ip6_ext_next_header (p);
176 }
177
178 return len;
179}
180
181static_always_inline int
182esp_trailer_icv_overflow (vlib_node_runtime_t * node, vlib_buffer_t * b,
183 u16 * next, u16 buffer_data_size)
184{
185 if (b->current_data + b->current_length <= buffer_data_size)
186 return 0;
187
188 b->current_length -= buffer_data_size - b->current_data;
189 b->error = node->errors[ESP_ENCRYPT_ERROR_NO_TRAILER_SPACE];
190 next[0] = ESP_ENCRYPT_NEXT_DROP;
191 return 1;
192}
193
194static_always_inline void
195esp_process_ops (vlib_main_t * vm, vlib_node_runtime_t * node,
196 vnet_crypto_op_t * ops, vlib_buffer_t * b[], u16 * nexts)
197{
198 u32 n_fail, n_ops = vec_len (ops);
199 vnet_crypto_op_t *op = ops;
200
201 if (n_ops == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700202 return;
203
Damjan Marionc59b9a22019-03-19 15:38:40 +0100204 n_fail = n_ops - vnet_crypto_process_ops (vm, op, n_ops);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700205
Damjan Marionc59b9a22019-03-19 15:38:40 +0100206 while (n_fail)
207 {
208 ASSERT (op - ops < n_ops);
209
210 if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
211 {
212 u32 bi = op->user_data;
213 b[bi]->error = node->errors[ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR];
214 nexts[bi] = ESP_ENCRYPT_NEXT_DROP;
215 n_fail--;
216 }
217 op++;
218 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700219}
220
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200221always_inline uword
Damjan Marionc59b9a22019-03-19 15:38:40 +0100222esp_encrypt_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
223 vlib_frame_t * frame, int is_ip6)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700224{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700225 ipsec_main_t *im = &ipsec_main;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100226 ipsec_per_thread_data_t *ptd = vec_elt_at_index (im->ptd, vm->thread_index);
227 u32 *from = vlib_frame_vector_args (frame);
228 u32 n_left = frame->n_vectors;
229 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
Damjan Marionc98275f2019-03-06 14:05:01 +0100230 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100231 u32 thread_index = vm->thread_index;
232 u16 buffer_data_size = vlib_buffer_get_default_data_size (vm);
233 u32 current_sa_index = ~0, current_sa_packets = 0;
234 u32 current_sa_bytes = 0, spi = 0;
235 u8 block_sz = 0, iv_sz = 0, icv_sz = 0;
236 ipsec_sa_t *sa0 = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237
Damjan Marionc59b9a22019-03-19 15:38:40 +0100238 vlib_get_buffers (vm, from, b, n_left);
239 vec_reset_length (ptd->crypto_ops);
240 vec_reset_length (ptd->integ_ops);
241
242 while (n_left > 0)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700243 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100244 u32 sa_index0 = vnet_buffer (b[0])->ipsec.sad_index;
245 dpo_id_t *dpo;
246 esp_header_t *esp;
247 u8 *payload, *next_hdr_ptr;
248 u16 payload_len;
249 u32 hdr_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700250
Damjan Marionc59b9a22019-03-19 15:38:40 +0100251 if (n_left > 2)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700252 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100253 u8 *p;
254 vlib_prefetch_buffer_header (b[2], LOAD);
255 p = vlib_buffer_get_current (b[1]);
256 CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD);
257 p -= CLIB_CACHE_LINE_BYTES;
258 CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700259 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700260
Damjan Marionc59b9a22019-03-19 15:38:40 +0100261 if (vnet_buffer (b[0])->ipsec.sad_index != current_sa_index)
262 {
263 sa0 = pool_elt_at_index (im->sad, sa_index0);
264 current_sa_index = sa_index0;
265 vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
266 sa_index0, current_sa_packets,
267 current_sa_bytes);
268 current_sa_packets = current_sa_bytes = 0;
269 spi = clib_net_to_host_u32 (sa0->spi);
270 block_sz = sa0->crypto_block_size;
271 icv_sz = sa0->integ_trunc_size;
272 iv_sz = sa0->crypto_iv_size;
273 }
274
275 if (vlib_buffer_chain_linearize (vm, b[0]) != 1)
276 {
277 b[0]->error = node->errors[ESP_ENCRYPT_ERROR_CHAINED_BUFFER];
278 next[0] = ESP_ENCRYPT_NEXT_DROP;
279 goto trace;
280 }
281
282 if (PREDICT_FALSE (esp_seq_advance (sa0)))
283 {
284 b[0]->error = node->errors[ESP_ENCRYPT_ERROR_SEQ_CYCLED];
285 next[0] = ESP_ENCRYPT_NEXT_DROP;
286 goto trace;
287 }
288
289 /* space for IV */
290 hdr_len = iv_sz;
291
Damjan Mariond709cbc2019-03-26 13:16:42 +0100292 if (ipsec_sa_is_set_IS_TUNNEL (sa0))
Damjan Marionc59b9a22019-03-19 15:38:40 +0100293 {
294 payload = vlib_buffer_get_current (b[0]);
295 next_hdr_ptr = esp_add_footer_and_icv (b[0], block_sz, icv_sz);
296 payload_len = b[0]->current_length;
297
298 if (esp_trailer_icv_overflow (node, b[0], next, buffer_data_size))
299 goto trace;
300
301 /* ESP header */
302 hdr_len += sizeof (*esp);
303 esp = (esp_header_t *) (payload - hdr_len);
304
305 /* optional UDP header */
Damjan Mariond709cbc2019-03-26 13:16:42 +0100306 if (ipsec_sa_is_set_UDP_ENCAP (sa0))
Damjan Marionc98275f2019-03-06 14:05:01 +0100307 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100308 hdr_len += sizeof (udp_header_t);
309 esp_fill_udp_hdr (sa0, (udp_header_t *) (payload - hdr_len),
310 payload_len + hdr_len);
311 }
312
313 /* IP header */
Damjan Mariond709cbc2019-03-26 13:16:42 +0100314 if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa0))
Damjan Marionc59b9a22019-03-19 15:38:40 +0100315 {
316 ip6_header_t *ip6;
317 u16 len = sizeof (ip6_header_t);
318 hdr_len += len;
319 ip6 = (ip6_header_t *) (payload - hdr_len);
320 clib_memcpy_fast (ip6, &sa0->ip6_hdr, len);
321 *next_hdr_ptr = IP_PROTOCOL_IPV6;
322 len = payload_len + hdr_len - len;
323 ip6->payload_length = clib_net_to_host_u16 (len);
Damjan Marionc98275f2019-03-06 14:05:01 +0100324 }
325 else
326 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100327 ip4_header_t *ip4;
328 u16 len = sizeof (ip4_header_t);
329 hdr_len += len;
330 ip4 = (ip4_header_t *) (payload - hdr_len);
331 clib_memcpy_fast (ip4, &sa0->ip4_hdr, len);
332 *next_hdr_ptr = IP_PROTOCOL_IP_IN_IP;
333 len = payload_len + hdr_len;
334 esp_update_ip4_hdr (ip4, len, /* is_transport */ 0, 0);
Damjan Marionc98275f2019-03-06 14:05:01 +0100335 }
Damjan Marionc98275f2019-03-06 14:05:01 +0100336
Damjan Marionc59b9a22019-03-19 15:38:40 +0100337 dpo = sa0->dpo + IPSEC_PROTOCOL_ESP;
338 next[0] = dpo->dpoi_next_node;
339 vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = dpo->dpoi_index;
Damjan Marionc98275f2019-03-06 14:05:01 +0100340 }
Damjan Marionc59b9a22019-03-19 15:38:40 +0100341 else /* transport mode */
Damjan Marionc98275f2019-03-06 14:05:01 +0100342 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100343 u8 *l2_hdr, l2_len, *ip_hdr, ip_len;
344 udp_header_t *udp = 0;
345 u8 *old_ip_hdr = vlib_buffer_get_current (b[0]);
Damjan Marionc98275f2019-03-06 14:05:01 +0100346
Damjan Marionc59b9a22019-03-19 15:38:40 +0100347 ip_len = is_ip6 ?
348 esp_get_ip6_hdr_len ((ip6_header_t *) old_ip_hdr) :
349 ip4_header_bytes ((ip4_header_t *) old_ip_hdr);
Damjan Marionc98275f2019-03-06 14:05:01 +0100350
Damjan Marionc59b9a22019-03-19 15:38:40 +0100351 vlib_buffer_advance (b[0], ip_len);
352 payload = vlib_buffer_get_current (b[0]);
353 next_hdr_ptr = esp_add_footer_and_icv (b[0], block_sz, icv_sz);
354 payload_len = b[0]->current_length;
355
356 if (esp_trailer_icv_overflow (node, b[0], next, buffer_data_size))
357 goto trace;
358
359 /* ESP header */
360 hdr_len += sizeof (*esp);
361 esp = (esp_header_t *) (payload - hdr_len);
362
363 /* optional UDP header */
Damjan Mariond709cbc2019-03-26 13:16:42 +0100364 if (ipsec_sa_is_set_UDP_ENCAP (sa0))
Damjan Marionc98275f2019-03-06 14:05:01 +0100365 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100366 hdr_len += sizeof (udp_header_t);
367 udp = (udp_header_t *) (payload - hdr_len);
Damjan Marionc98275f2019-03-06 14:05:01 +0100368 }
369
Damjan Marionc59b9a22019-03-19 15:38:40 +0100370 /* IP header */
371 hdr_len += ip_len;
372 ip_hdr = payload - hdr_len;
373
374 /* L2 header */
375 l2_len = vnet_buffer (b[0])->ip.save_rewrite_length;
376 hdr_len += l2_len;
377 l2_hdr = payload - hdr_len;
378
379 /* copy l2 and ip header */
380 clib_memcpy_le32 (l2_hdr, old_ip_hdr - l2_len, l2_len);
381 clib_memcpy_le64 (ip_hdr, old_ip_hdr, ip_len);
382
Damjan Marionc98275f2019-03-06 14:05:01 +0100383 if (is_ip6)
Damjan Marionc59b9a22019-03-19 15:38:40 +0100384 {
385 ip6_header_t *ip6 = (ip6_header_t *) (ip_hdr);
386 *next_hdr_ptr = ip6->protocol;
387 ip6->protocol = IP_PROTOCOL_IPSEC_ESP;
388 ip6->payload_length = payload_len + hdr_len - l2_len - ip_len;
389 }
Damjan Marionc98275f2019-03-06 14:05:01 +0100390 else
Damjan Marionc98275f2019-03-06 14:05:01 +0100391 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100392 u16 len;
393 ip4_header_t *ip4 = (ip4_header_t *) (ip_hdr);
394 *next_hdr_ptr = ip4->protocol;
395 len = payload_len + hdr_len + l2_len;
396 if (udp)
397 {
398 esp_update_ip4_hdr (ip4, len, /* is_transport */ 1, 1);
399 esp_fill_udp_hdr (sa0, udp, len - ip_len);
400 }
401 else
402 esp_update_ip4_hdr (ip4, len, /* is_transport */ 1, 0);
Damjan Marionc98275f2019-03-06 14:05:01 +0100403 }
Damjan Marionc98275f2019-03-06 14:05:01 +0100404
Damjan Marionc59b9a22019-03-19 15:38:40 +0100405 next[0] = ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT;
Damjan Marionc98275f2019-03-06 14:05:01 +0100406 }
407
Damjan Marionc59b9a22019-03-19 15:38:40 +0100408 esp->spi = spi;
409 esp->seq = clib_net_to_host_u32 (sa0->seq);
Damjan Marionc98275f2019-03-06 14:05:01 +0100410
Damjan Marionc59b9a22019-03-19 15:38:40 +0100411 if (sa0->crypto_enc_op_type)
Damjan Marionc98275f2019-03-06 14:05:01 +0100412 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100413 vnet_crypto_op_t *op;
414 vec_add2_aligned (ptd->crypto_ops, op, 1, CLIB_CACHE_LINE_BYTES);
415 op->op = sa0->crypto_enc_op_type;
416 op->iv = payload - iv_sz;
417 op->src = op->dst = payload;
418 op->key = sa0->crypto_key.data;
419 op->len = payload_len - icv_sz;
420 op->flags = VNET_CRYPTO_OP_FLAG_INIT_IV;
421 op->user_data = b - bufs;
Damjan Marionc98275f2019-03-06 14:05:01 +0100422 }
Damjan Marionc59b9a22019-03-19 15:38:40 +0100423
424 if (sa0->integ_op_type)
Damjan Marionc98275f2019-03-06 14:05:01 +0100425 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100426 vnet_crypto_op_t *op;
427 vec_add2_aligned (ptd->integ_ops, op, 1, CLIB_CACHE_LINE_BYTES);
428 op->op = sa0->integ_op_type;
429 op->src = payload - iv_sz - sizeof (esp_header_t);
430 op->dst = payload + payload_len - icv_sz;
431 op->key = sa0->integ_key.data;
432 op->key_len = sa0->integ_key.len;
433 op->hmac_trunc_len = icv_sz;
434 op->len = payload_len - icv_sz + iv_sz + sizeof (esp_header_t);
435 op->flags = 0;
436 op->user_data = b - bufs;
Damjan Mariond709cbc2019-03-26 13:16:42 +0100437 if (ipsec_sa_is_set_USE_EXTENDED_SEQ_NUM (sa0))
Damjan Marionc98275f2019-03-06 14:05:01 +0100438 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100439 u32 seq_hi = clib_net_to_host_u32 (sa0->seq_hi);
440 clib_memcpy_fast (op->dst, &seq_hi, sizeof (seq_hi));
441 op->len += sizeof (seq_hi);
Damjan Marionc98275f2019-03-06 14:05:01 +0100442 }
443 }
444
Damjan Marionc59b9a22019-03-19 15:38:40 +0100445 vlib_buffer_advance (b[0], 0LL - hdr_len);
Damjan Marionc98275f2019-03-06 14:05:01 +0100446
Damjan Marionc59b9a22019-03-19 15:38:40 +0100447 current_sa_packets += 1;
448 current_sa_bytes += payload_len;
449
450 trace:
451 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
Damjan Marionc98275f2019-03-06 14:05:01 +0100452 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100453 esp_encrypt_trace_t *tr = vlib_add_trace (vm, node, b[0],
454 sizeof (*tr));
455 tr->sa_index = sa_index0;
456 tr->spi = sa0->spi;
457 tr->seq = sa0->seq - 1;
Damjan Mariond709cbc2019-03-26 13:16:42 +0100458 tr->udp_encap = ipsec_sa_is_set_UDP_ENCAP (sa0);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100459 tr->crypto_alg = sa0->crypto_alg;
460 tr->integ_alg = sa0->integ_alg;
Damjan Marionc98275f2019-03-06 14:05:01 +0100461 }
Damjan Marionc98275f2019-03-06 14:05:01 +0100462 /* next */
Damjan Marionc59b9a22019-03-19 15:38:40 +0100463 n_left -= 1;
Damjan Marionc98275f2019-03-06 14:05:01 +0100464 next += 1;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100465 b += 1;
Damjan Marionc98275f2019-03-06 14:05:01 +0100466 }
467
Damjan Marionc59b9a22019-03-19 15:38:40 +0100468 vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
469 current_sa_index, current_sa_packets,
470 current_sa_bytes);
Damjan Marionc98275f2019-03-06 14:05:01 +0100471
Damjan Marionc59b9a22019-03-19 15:38:40 +0100472 esp_process_ops (vm, node, ptd->crypto_ops, bufs, nexts);
473 esp_process_ops (vm, node, ptd->integ_ops, bufs, nexts);
474
475 vlib_node_increment_counter (vm, node->node_index,
476 ESP_ENCRYPT_ERROR_RX_PKTS, frame->n_vectors);
477
478 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
479 return frame->n_vectors;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700480}
481
Klement Sekerab8f35442018-10-29 13:38:19 +0100482VLIB_NODE_FN (esp4_encrypt_node) (vlib_main_t * vm,
483 vlib_node_runtime_t * node,
484 vlib_frame_t * from_frame)
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200485{
486 return esp_encrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ );
487}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700488
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700489/* *INDENT-OFF* */
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200490VLIB_REGISTER_NODE (esp4_encrypt_node) = {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200491 .name = "esp4-encrypt",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700492 .vector_size = sizeof (u32),
493 .format_trace = format_esp_encrypt_trace,
494 .type = VLIB_NODE_TYPE_INTERNAL,
495
496 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
497 .error_strings = esp_encrypt_error_strings,
498
499 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
500 .next_nodes = {
501#define _(s,n) [ESP_ENCRYPT_NEXT_##s] = n,
502 foreach_esp_encrypt_next
503#undef _
504 },
505};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700506/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700507
Klement Sekerab8f35442018-10-29 13:38:19 +0100508VLIB_NODE_FN (esp6_encrypt_node) (vlib_main_t * vm,
509 vlib_node_runtime_t * node,
510 vlib_frame_t * from_frame)
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200511{
512 return esp_encrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ );
513}
514
515/* *INDENT-OFF* */
516VLIB_REGISTER_NODE (esp6_encrypt_node) = {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200517 .name = "esp6-encrypt",
518 .vector_size = sizeof (u32),
519 .format_trace = format_esp_encrypt_trace,
520 .type = VLIB_NODE_TYPE_INTERNAL,
521
522 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
523 .error_strings = esp_encrypt_error_strings,
524
525 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
526 .next_nodes = {
527#define _(s,n) [ESP_ENCRYPT_NEXT_##s] = n,
528 foreach_esp_encrypt_next
529#undef _
530 },
531};
532/* *INDENT-ON* */
533
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700534/*
535 * fd.io coding-style-patch-verification: ON
536 *
537 * Local Variables:
538 * eval: (c-set-style "gnu")
539 * End:
540 */