blob: 041b268975d82286c0e18fc10c72c550ef25d574 [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
Guillaume Solignac8f818cc2019-05-15 12:02:33 +020081 s =
82 format (s,
83 "esp: sa-index %d spi %u (0x%08x) seq %u crypto %U integrity %U%s",
84 t->sa_index, t->spi, t->spi, t->seq, format_ipsec_crypto_alg,
85 t->crypto_alg, format_ipsec_integ_alg, t->integ_alg,
86 t->udp_encap ? " udp-encap-enabled" : "");
Ed Warnickecb9cada2015-12-08 15:45:58 -070087 return s;
88}
89
Damjan Marionc59b9a22019-03-19 15:38:40 +010090/* pad packet in input buffer */
91static_always_inline u8 *
92esp_add_footer_and_icv (vlib_buffer_t * b, u8 block_size, u8 icv_sz)
Ed Warnickecb9cada2015-12-08 15:45:58 -070093{
Damjan Marionc59b9a22019-03-19 15:38:40 +010094 static const u8 pad_data[ESP_MAX_BLOCK_SIZE] = {
95 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
96 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x00, 0x00,
97 };
Ed Warnickecb9cada2015-12-08 15:45:58 -070098
Damjan Marionc59b9a22019-03-19 15:38:40 +010099 u16 min_length = b->current_length + sizeof (esp_footer_t);
100 u16 new_length = round_pow2 (min_length, block_size);
101 u8 pad_bytes = new_length - min_length;
102 esp_footer_t *f = (esp_footer_t *) (vlib_buffer_get_current (b) +
103 new_length - sizeof (esp_footer_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700104
Damjan Marionc59b9a22019-03-19 15:38:40 +0100105 if (pad_bytes)
106 clib_memcpy_fast ((u8 *) f - pad_bytes, pad_data, ESP_MAX_BLOCK_SIZE);
107
108 f->pad_length = pad_bytes;
109 b->current_length = new_length + icv_sz;
110 return &f->next_header;
111}
112
113static_always_inline void
114esp_update_ip4_hdr (ip4_header_t * ip4, u16 len, int is_transport, int is_udp)
115{
Neale Ranns1b582b82019-04-18 19:49:13 -0700116 ip_csum_t sum;
117 u16 old_len;
118
119 len = clib_net_to_host_u16 (len);
120 old_len = ip4->length;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100121
122 if (is_transport)
123 {
124 u8 prot = is_udp ? IP_PROTOCOL_UDP : IP_PROTOCOL_IPSEC_ESP;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100125
Neale Ranns1b582b82019-04-18 19:49:13 -0700126 sum = ip_csum_update (ip4->checksum, ip4->protocol,
127 prot, ip4_header_t, protocol);
128 ip4->protocol = prot;
129
130 sum = ip_csum_update (sum, old_len, len, ip4_header_t, length);
131 }
132 else
133 sum = ip_csum_update (ip4->checksum, old_len, len, ip4_header_t, length);
134
135 ip4->length = len;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100136 ip4->checksum = ip_csum_fold (sum);
137}
138
139static_always_inline void
140esp_fill_udp_hdr (ipsec_sa_t * sa, udp_header_t * udp, u16 len)
141{
142 clib_memcpy_fast (udp, &sa->udp_hdr, sizeof (udp_header_t));
143 udp->length = clib_net_to_host_u16 (len);
144}
145
146static_always_inline u8
147ext_hdr_is_pre_esp (u8 nexthdr)
148{
149#ifdef CLIB_HAVE_VEC128
150 static const u8x16 ext_hdr_types = {
151 IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS,
152 IP_PROTOCOL_IPV6_ROUTE,
153 IP_PROTOCOL_IPV6_FRAGMENTATION,
154 };
155
156 return !u8x16_is_all_zero (ext_hdr_types == u8x16_splat (nexthdr));
157#else
158 return ((nexthdr ^ IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) |
159 (nexthdr ^ IP_PROTOCOL_IPV6_ROUTE) |
160 (nexthdr ^ IP_PROTOCOL_IPV6_FRAGMENTATION) != 0);
161#endif
162}
163
164static_always_inline u8
165esp_get_ip6_hdr_len (ip6_header_t * ip6)
166{
167 /* this code assumes that HbH, route and frag headers will be before
168 others, if that is not the case, they will end up encrypted */
169
170 u8 len = sizeof (ip6_header_t);
171 ip6_ext_header_t *p;
172
173 /* if next packet doesn't have ext header */
174 if (ext_hdr_is_pre_esp (ip6->protocol) == 0)
175 return len;
176
177 p = (void *) (ip6 + 1);
178 len += ip6_ext_header_len (p);
179
180 while (ext_hdr_is_pre_esp (p->next_hdr))
181 {
182 len += ip6_ext_header_len (p);
183 p = ip6_ext_next_header (p);
184 }
185
186 return len;
187}
188
189static_always_inline int
190esp_trailer_icv_overflow (vlib_node_runtime_t * node, vlib_buffer_t * b,
191 u16 * next, u16 buffer_data_size)
192{
193 if (b->current_data + b->current_length <= buffer_data_size)
194 return 0;
195
196 b->current_length -= buffer_data_size - b->current_data;
197 b->error = node->errors[ESP_ENCRYPT_ERROR_NO_TRAILER_SPACE];
198 next[0] = ESP_ENCRYPT_NEXT_DROP;
199 return 1;
200}
201
202static_always_inline void
203esp_process_ops (vlib_main_t * vm, vlib_node_runtime_t * node,
204 vnet_crypto_op_t * ops, vlib_buffer_t * b[], u16 * nexts)
205{
206 u32 n_fail, n_ops = vec_len (ops);
207 vnet_crypto_op_t *op = ops;
208
209 if (n_ops == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700210 return;
211
Damjan Marionc59b9a22019-03-19 15:38:40 +0100212 n_fail = n_ops - vnet_crypto_process_ops (vm, op, n_ops);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700213
Damjan Marionc59b9a22019-03-19 15:38:40 +0100214 while (n_fail)
215 {
216 ASSERT (op - ops < n_ops);
217
218 if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
219 {
220 u32 bi = op->user_data;
221 b[bi]->error = node->errors[ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR];
222 nexts[bi] = ESP_ENCRYPT_NEXT_DROP;
223 n_fail--;
224 }
225 op++;
226 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700227}
228
Damjan Mariond97918e2019-04-25 18:28:31 +0200229typedef struct
230{
231 u32 salt;
232 u64 iv;
233} __clib_packed esp_gcm_nonce_t;
234
235STATIC_ASSERT_SIZEOF (esp_gcm_nonce_t, 12);
236
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200237always_inline uword
Damjan Marionc59b9a22019-03-19 15:38:40 +0100238esp_encrypt_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
Neale Ranns25edf142019-03-22 08:12:48 +0000239 vlib_frame_t * frame, int is_ip6, int is_tun)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700240{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241 ipsec_main_t *im = &ipsec_main;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100242 ipsec_per_thread_data_t *ptd = vec_elt_at_index (im->ptd, vm->thread_index);
243 u32 *from = vlib_frame_vector_args (frame);
244 u32 n_left = frame->n_vectors;
245 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
Damjan Marionc98275f2019-03-06 14:05:01 +0100246 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
Damjan Mariond97918e2019-04-25 18:28:31 +0200247 esp_gcm_nonce_t nonces[VLIB_FRAME_SIZE], *nonce = nonces;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100248 u32 thread_index = vm->thread_index;
249 u16 buffer_data_size = vlib_buffer_get_default_data_size (vm);
250 u32 current_sa_index = ~0, current_sa_packets = 0;
251 u32 current_sa_bytes = 0, spi = 0;
252 u8 block_sz = 0, iv_sz = 0, icv_sz = 0;
253 ipsec_sa_t *sa0 = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700254
Damjan Marionc59b9a22019-03-19 15:38:40 +0100255 vlib_get_buffers (vm, from, b, n_left);
256 vec_reset_length (ptd->crypto_ops);
257 vec_reset_length (ptd->integ_ops);
258
259 while (n_left > 0)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700260 {
Zhiyong Yang1cff6432019-04-30 05:33:53 -0400261 u32 sa_index0;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100262 dpo_id_t *dpo;
263 esp_header_t *esp;
264 u8 *payload, *next_hdr_ptr;
265 u16 payload_len;
266 u32 hdr_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700267
Damjan Marionc59b9a22019-03-19 15:38:40 +0100268 if (n_left > 2)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700269 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100270 u8 *p;
271 vlib_prefetch_buffer_header (b[2], LOAD);
272 p = vlib_buffer_get_current (b[1]);
273 CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD);
274 p -= CLIB_CACHE_LINE_BYTES;
275 CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700276 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700277
Neale Ranns25edf142019-03-22 08:12:48 +0000278 if (is_tun)
279 {
280 /* we are on a ipsec tunnel's feature arc */
281 u32 next0;
282 sa_index0 = *(u32 *) vnet_feature_next_with_data (&next0, b[0],
283 sizeof
284 (sa_index0));
285 next[0] = next0;
286 }
287 else
288 sa_index0 = vnet_buffer (b[0])->ipsec.sad_index;
289
290 if (sa_index0 != current_sa_index)
Damjan Marionc59b9a22019-03-19 15:38:40 +0100291 {
Damjan Marion21f265f2019-06-05 15:45:50 +0200292 if (current_sa_packets)
293 vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
294 current_sa_index,
295 current_sa_packets,
296 current_sa_bytes);
297 current_sa_packets = current_sa_bytes = 0;
298
Damjan Marionc59b9a22019-03-19 15:38:40 +0100299 sa0 = pool_elt_at_index (im->sad, sa_index0);
300 current_sa_index = sa_index0;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100301 spi = clib_net_to_host_u32 (sa0->spi);
302 block_sz = sa0->crypto_block_size;
Damjan Marion7c22ff72019-04-04 12:25:44 +0200303 icv_sz = sa0->integ_icv_size;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100304 iv_sz = sa0->crypto_iv_size;
305 }
306
307 if (vlib_buffer_chain_linearize (vm, b[0]) != 1)
308 {
309 b[0]->error = node->errors[ESP_ENCRYPT_ERROR_CHAINED_BUFFER];
310 next[0] = ESP_ENCRYPT_NEXT_DROP;
311 goto trace;
312 }
313
314 if (PREDICT_FALSE (esp_seq_advance (sa0)))
315 {
316 b[0]->error = node->errors[ESP_ENCRYPT_ERROR_SEQ_CYCLED];
317 next[0] = ESP_ENCRYPT_NEXT_DROP;
318 goto trace;
319 }
320
321 /* space for IV */
322 hdr_len = iv_sz;
323
Damjan Mariond709cbc2019-03-26 13:16:42 +0100324 if (ipsec_sa_is_set_IS_TUNNEL (sa0))
Damjan Marionc59b9a22019-03-19 15:38:40 +0100325 {
326 payload = vlib_buffer_get_current (b[0]);
327 next_hdr_ptr = esp_add_footer_and_icv (b[0], block_sz, icv_sz);
328 payload_len = b[0]->current_length;
329
330 if (esp_trailer_icv_overflow (node, b[0], next, buffer_data_size))
331 goto trace;
332
333 /* ESP header */
334 hdr_len += sizeof (*esp);
335 esp = (esp_header_t *) (payload - hdr_len);
336
337 /* optional UDP header */
Damjan Mariond709cbc2019-03-26 13:16:42 +0100338 if (ipsec_sa_is_set_UDP_ENCAP (sa0))
Damjan Marionc98275f2019-03-06 14:05:01 +0100339 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100340 hdr_len += sizeof (udp_header_t);
341 esp_fill_udp_hdr (sa0, (udp_header_t *) (payload - hdr_len),
342 payload_len + hdr_len);
343 }
344
345 /* IP header */
Damjan Mariond709cbc2019-03-26 13:16:42 +0100346 if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa0))
Damjan Marionc59b9a22019-03-19 15:38:40 +0100347 {
348 ip6_header_t *ip6;
349 u16 len = sizeof (ip6_header_t);
350 hdr_len += len;
351 ip6 = (ip6_header_t *) (payload - hdr_len);
352 clib_memcpy_fast (ip6, &sa0->ip6_hdr, len);
Neale Ranns987aea82019-03-27 13:40:35 +0000353 *next_hdr_ptr = (is_ip6 ?
354 IP_PROTOCOL_IPV6 : IP_PROTOCOL_IP_IN_IP);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100355 len = payload_len + hdr_len - len;
356 ip6->payload_length = clib_net_to_host_u16 (len);
Damjan Marionc98275f2019-03-06 14:05:01 +0100357 }
358 else
359 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100360 ip4_header_t *ip4;
361 u16 len = sizeof (ip4_header_t);
362 hdr_len += len;
363 ip4 = (ip4_header_t *) (payload - hdr_len);
364 clib_memcpy_fast (ip4, &sa0->ip4_hdr, len);
Neale Ranns987aea82019-03-27 13:40:35 +0000365 *next_hdr_ptr = (is_ip6 ?
366 IP_PROTOCOL_IPV6 : IP_PROTOCOL_IP_IN_IP);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100367 len = payload_len + hdr_len;
368 esp_update_ip4_hdr (ip4, len, /* is_transport */ 0, 0);
Damjan Marionc98275f2019-03-06 14:05:01 +0100369 }
Damjan Marionc98275f2019-03-06 14:05:01 +0100370
Neale Ranns72f2a3a2019-06-17 15:43:38 +0000371 dpo = &sa0->dpo;
Neale Ranns25edf142019-03-22 08:12:48 +0000372 if (!is_tun)
373 {
374 next[0] = dpo->dpoi_next_node;
375 vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = dpo->dpoi_index;
376 }
Damjan Marionc98275f2019-03-06 14:05:01 +0100377 }
Damjan Marionc59b9a22019-03-19 15:38:40 +0100378 else /* transport mode */
Damjan Marionc98275f2019-03-06 14:05:01 +0100379 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100380 u8 *l2_hdr, l2_len, *ip_hdr, ip_len;
381 udp_header_t *udp = 0;
382 u8 *old_ip_hdr = vlib_buffer_get_current (b[0]);
Damjan Marionc98275f2019-03-06 14:05:01 +0100383
Damjan Marionc59b9a22019-03-19 15:38:40 +0100384 ip_len = is_ip6 ?
385 esp_get_ip6_hdr_len ((ip6_header_t *) old_ip_hdr) :
386 ip4_header_bytes ((ip4_header_t *) old_ip_hdr);
Damjan Marionc98275f2019-03-06 14:05:01 +0100387
Damjan Marionc59b9a22019-03-19 15:38:40 +0100388 vlib_buffer_advance (b[0], ip_len);
389 payload = vlib_buffer_get_current (b[0]);
390 next_hdr_ptr = esp_add_footer_and_icv (b[0], block_sz, icv_sz);
391 payload_len = b[0]->current_length;
392
393 if (esp_trailer_icv_overflow (node, b[0], next, buffer_data_size))
394 goto trace;
395
396 /* ESP header */
397 hdr_len += sizeof (*esp);
398 esp = (esp_header_t *) (payload - hdr_len);
399
400 /* optional UDP header */
Damjan Mariond709cbc2019-03-26 13:16:42 +0100401 if (ipsec_sa_is_set_UDP_ENCAP (sa0))
Damjan Marionc98275f2019-03-06 14:05:01 +0100402 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100403 hdr_len += sizeof (udp_header_t);
404 udp = (udp_header_t *) (payload - hdr_len);
Damjan Marionc98275f2019-03-06 14:05:01 +0100405 }
406
Damjan Marionc59b9a22019-03-19 15:38:40 +0100407 /* IP header */
408 hdr_len += ip_len;
409 ip_hdr = payload - hdr_len;
410
411 /* L2 header */
Neale Rannsc87b66c2019-02-07 07:26:12 -0800412 if (!is_tun)
413 {
414 l2_len = vnet_buffer (b[0])->ip.save_rewrite_length;
415 hdr_len += l2_len;
416 l2_hdr = payload - hdr_len;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100417
Neale Rannsc87b66c2019-02-07 07:26:12 -0800418 /* copy l2 and ip header */
419 clib_memcpy_le32 (l2_hdr, old_ip_hdr - l2_len, l2_len);
420 }
421 else
422 l2_len = 0;
423
Damjan Marionc59b9a22019-03-19 15:38:40 +0100424 clib_memcpy_le64 (ip_hdr, old_ip_hdr, ip_len);
425
Damjan Marionc98275f2019-03-06 14:05:01 +0100426 if (is_ip6)
Damjan Marionc59b9a22019-03-19 15:38:40 +0100427 {
428 ip6_header_t *ip6 = (ip6_header_t *) (ip_hdr);
429 *next_hdr_ptr = ip6->protocol;
430 ip6->protocol = IP_PROTOCOL_IPSEC_ESP;
Neale Rannsd207fd72019-04-18 17:18:12 -0700431 ip6->payload_length =
432 clib_host_to_net_u16 (payload_len + hdr_len - l2_len -
433 ip_len);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100434 }
Damjan Marionc98275f2019-03-06 14:05:01 +0100435 else
Damjan Marionc98275f2019-03-06 14:05:01 +0100436 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100437 u16 len;
438 ip4_header_t *ip4 = (ip4_header_t *) (ip_hdr);
439 *next_hdr_ptr = ip4->protocol;
Neale Ranns1b582b82019-04-18 19:49:13 -0700440 len = payload_len + hdr_len - l2_len;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100441 if (udp)
442 {
443 esp_update_ip4_hdr (ip4, len, /* is_transport */ 1, 1);
444 esp_fill_udp_hdr (sa0, udp, len - ip_len);
445 }
446 else
447 esp_update_ip4_hdr (ip4, len, /* is_transport */ 1, 0);
Damjan Marionc98275f2019-03-06 14:05:01 +0100448 }
Damjan Marionc98275f2019-03-06 14:05:01 +0100449
Neale Rannsc87b66c2019-02-07 07:26:12 -0800450 if (!is_tun)
451 next[0] = ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT;
Damjan Marionc98275f2019-03-06 14:05:01 +0100452 }
453
Damjan Marionc59b9a22019-03-19 15:38:40 +0100454 esp->spi = spi;
455 esp->seq = clib_net_to_host_u32 (sa0->seq);
Damjan Marionc98275f2019-03-06 14:05:01 +0100456
Damjan Marion060bfb92019-03-29 13:47:54 +0100457 if (sa0->crypto_enc_op_id)
Damjan Marionc98275f2019-03-06 14:05:01 +0100458 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100459 vnet_crypto_op_t *op;
460 vec_add2_aligned (ptd->crypto_ops, op, 1, CLIB_CACHE_LINE_BYTES);
Damjan Marion060bfb92019-03-29 13:47:54 +0100461 vnet_crypto_op_init (op, sa0->crypto_enc_op_id);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100462 op->src = op->dst = payload;
Damjan Mariond1bed682019-04-24 15:20:35 +0200463 op->key_index = sa0->crypto_key_index;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100464 op->len = payload_len - icv_sz;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100465 op->user_data = b - bufs;
Neale Ranns47feb112019-04-11 15:14:07 +0000466
467 if (ipsec_sa_is_set_IS_AEAD (sa0))
468 {
469 /*
470 * construct the AAD in a scratch space in front
471 * of the IP header.
472 */
473 op->aad = payload - hdr_len - sizeof (esp_aead_t);
474
475 esp_aad_fill (op, esp, sa0);
476
477 op->tag = payload + op->len;
478 op->tag_len = 16;
Damjan Mariond97918e2019-04-25 18:28:31 +0200479
480 u64 *iv = (u64 *) (payload - iv_sz);
481 nonce->salt = sa0->salt;
482 nonce->iv = *iv = clib_host_to_net_u64 (sa0->gcm_iv_counter++);
483 op->iv = (u8 *) nonce;
484 nonce++;
485 }
486 else
487 {
488 op->iv = payload - iv_sz;
489 op->flags = VNET_CRYPTO_OP_FLAG_INIT_IV;
Neale Ranns47feb112019-04-11 15:14:07 +0000490 }
Damjan Marionc98275f2019-03-06 14:05:01 +0100491 }
Damjan Marionc59b9a22019-03-19 15:38:40 +0100492
Damjan Marion060bfb92019-03-29 13:47:54 +0100493 if (sa0->integ_op_id)
Damjan Marionc98275f2019-03-06 14:05:01 +0100494 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100495 vnet_crypto_op_t *op;
496 vec_add2_aligned (ptd->integ_ops, op, 1, CLIB_CACHE_LINE_BYTES);
Damjan Marion060bfb92019-03-29 13:47:54 +0100497 vnet_crypto_op_init (op, sa0->integ_op_id);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100498 op->src = payload - iv_sz - sizeof (esp_header_t);
Damjan Marion060bfb92019-03-29 13:47:54 +0100499 op->digest = payload + payload_len - icv_sz;
Damjan Mariond1bed682019-04-24 15:20:35 +0200500 op->key_index = sa0->integ_key_index;
Damjan Marion060bfb92019-03-29 13:47:54 +0100501 op->digest_len = icv_sz;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100502 op->len = payload_len - icv_sz + iv_sz + sizeof (esp_header_t);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100503 op->user_data = b - bufs;
Damjan Marion1e3aa5e2019-03-28 10:58:59 +0100504 if (ipsec_sa_is_set_USE_ESN (sa0))
Damjan Marionc98275f2019-03-06 14:05:01 +0100505 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100506 u32 seq_hi = clib_net_to_host_u32 (sa0->seq_hi);
Neale Ranns49e7ef62019-04-10 17:24:29 +0000507 clib_memcpy_fast (op->digest, &seq_hi, sizeof (seq_hi));
Damjan Marionc59b9a22019-03-19 15:38:40 +0100508 op->len += sizeof (seq_hi);
Damjan Marionc98275f2019-03-06 14:05:01 +0100509 }
510 }
511
Damjan Marionc59b9a22019-03-19 15:38:40 +0100512 vlib_buffer_advance (b[0], 0LL - hdr_len);
Damjan Marionc98275f2019-03-06 14:05:01 +0100513
Damjan Marionc59b9a22019-03-19 15:38:40 +0100514 current_sa_packets += 1;
515 current_sa_bytes += payload_len;
516
517 trace:
518 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
Damjan Marionc98275f2019-03-06 14:05:01 +0100519 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100520 esp_encrypt_trace_t *tr = vlib_add_trace (vm, node, b[0],
521 sizeof (*tr));
522 tr->sa_index = sa_index0;
523 tr->spi = sa0->spi;
524 tr->seq = sa0->seq - 1;
Damjan Mariond709cbc2019-03-26 13:16:42 +0100525 tr->udp_encap = ipsec_sa_is_set_UDP_ENCAP (sa0);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100526 tr->crypto_alg = sa0->crypto_alg;
527 tr->integ_alg = sa0->integ_alg;
Damjan Marionc98275f2019-03-06 14:05:01 +0100528 }
Damjan Marionc98275f2019-03-06 14:05:01 +0100529 /* next */
Damjan Marionc59b9a22019-03-19 15:38:40 +0100530 n_left -= 1;
Damjan Marionc98275f2019-03-06 14:05:01 +0100531 next += 1;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100532 b += 1;
Damjan Marionc98275f2019-03-06 14:05:01 +0100533 }
534
Damjan Marionc59b9a22019-03-19 15:38:40 +0100535 vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
536 current_sa_index, current_sa_packets,
537 current_sa_bytes);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100538 esp_process_ops (vm, node, ptd->crypto_ops, bufs, nexts);
539 esp_process_ops (vm, node, ptd->integ_ops, bufs, nexts);
540
541 vlib_node_increment_counter (vm, node->node_index,
542 ESP_ENCRYPT_ERROR_RX_PKTS, frame->n_vectors);
543
544 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
545 return frame->n_vectors;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700546}
547
Klement Sekerab8f35442018-10-29 13:38:19 +0100548VLIB_NODE_FN (esp4_encrypt_node) (vlib_main_t * vm,
549 vlib_node_runtime_t * node,
550 vlib_frame_t * from_frame)
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200551{
Neale Ranns25edf142019-03-22 08:12:48 +0000552 return esp_encrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ , 0);
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200553}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700554
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700555/* *INDENT-OFF* */
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200556VLIB_REGISTER_NODE (esp4_encrypt_node) = {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200557 .name = "esp4-encrypt",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700558 .vector_size = sizeof (u32),
559 .format_trace = format_esp_encrypt_trace,
560 .type = VLIB_NODE_TYPE_INTERNAL,
561
562 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
563 .error_strings = esp_encrypt_error_strings,
564
565 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
566 .next_nodes = {
567#define _(s,n) [ESP_ENCRYPT_NEXT_##s] = n,
568 foreach_esp_encrypt_next
569#undef _
570 },
571};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700572/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700573
Klement Sekerab8f35442018-10-29 13:38:19 +0100574VLIB_NODE_FN (esp6_encrypt_node) (vlib_main_t * vm,
575 vlib_node_runtime_t * node,
576 vlib_frame_t * from_frame)
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200577{
Neale Ranns25edf142019-03-22 08:12:48 +0000578 return esp_encrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ , 0);
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200579}
580
581/* *INDENT-OFF* */
582VLIB_REGISTER_NODE (esp6_encrypt_node) = {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200583 .name = "esp6-encrypt",
584 .vector_size = sizeof (u32),
585 .format_trace = format_esp_encrypt_trace,
586 .type = VLIB_NODE_TYPE_INTERNAL,
587
588 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
589 .error_strings = esp_encrypt_error_strings,
590
591 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
592 .next_nodes = {
593#define _(s,n) [ESP_ENCRYPT_NEXT_##s] = n,
594 foreach_esp_encrypt_next
595#undef _
596 },
597};
598/* *INDENT-ON* */
599
Neale Ranns25edf142019-03-22 08:12:48 +0000600VLIB_NODE_FN (esp4_encrypt_tun_node) (vlib_main_t * vm,
601 vlib_node_runtime_t * node,
602 vlib_frame_t * from_frame)
603{
604 return esp_encrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ , 1);
605}
606
607/* *INDENT-OFF* */
608VLIB_REGISTER_NODE (esp4_encrypt_tun_node) = {
609 .name = "esp4-encrypt-tun",
610 .vector_size = sizeof (u32),
611 .format_trace = format_esp_encrypt_trace,
612 .type = VLIB_NODE_TYPE_INTERNAL,
613
614 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
615 .error_strings = esp_encrypt_error_strings,
Neale Rannsd7603d92019-03-28 08:56:10 +0000616
617 .n_next_nodes = 1,
618 .next_nodes = {
619 [ESP_ENCRYPT_NEXT_DROP] = "ip4-drop",
620 },
Neale Ranns25edf142019-03-22 08:12:48 +0000621};
622
623VNET_FEATURE_INIT (esp4_encrypt_tun_feat_node, static) =
624{
625 .arc_name = "ip4-output",
626 .node_name = "esp4-encrypt-tun",
Neale Rannsea5bb772019-04-02 05:43:34 -0700627 .runs_before = VNET_FEATURES ("adj-midchain-tx"),
Neale Ranns25edf142019-03-22 08:12:48 +0000628};
Neale Rannsc87b66c2019-02-07 07:26:12 -0800629
630VNET_FEATURE_INIT (esp4_ethernet_encrypt_tun_feat_node, static) =
631{
632 .arc_name = "ethernet-output",
633 .node_name = "esp4-encrypt-tun",
634 .runs_before = VNET_FEATURES ("adj-midchain-tx", "adj-midchain-tx-no-count"),
635};
Neale Ranns25edf142019-03-22 08:12:48 +0000636/* *INDENT-ON* */
637
638VLIB_NODE_FN (esp6_encrypt_tun_node) (vlib_main_t * vm,
639 vlib_node_runtime_t * node,
640 vlib_frame_t * from_frame)
641{
642 return esp_encrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ , 1);
643}
644
645/* *INDENT-OFF* */
646VLIB_REGISTER_NODE (esp6_encrypt_tun_node) = {
647 .name = "esp6-encrypt-tun",
648 .vector_size = sizeof (u32),
649 .format_trace = format_esp_encrypt_trace,
650 .type = VLIB_NODE_TYPE_INTERNAL,
651
652 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
653 .error_strings = esp_encrypt_error_strings,
Neale Rannsd7603d92019-03-28 08:56:10 +0000654
655 .n_next_nodes = 1,
656 .next_nodes = {
657 [ESP_ENCRYPT_NEXT_DROP] = "ip6-drop",
658 },
Neale Ranns25edf142019-03-22 08:12:48 +0000659};
660
661VNET_FEATURE_INIT (esp6_encrypt_tun_feat_node, static) =
662{
663 .arc_name = "ip6-output",
664 .node_name = "esp6-encrypt-tun",
Neale Rannsea5bb772019-04-02 05:43:34 -0700665 .runs_before = VNET_FEATURES ("adj-midchain-tx"),
Neale Ranns25edf142019-03-22 08:12:48 +0000666};
667/* *INDENT-ON* */
668
Matthew Smith401aedf2019-07-08 14:45:04 -0500669typedef struct
670{
671 u32 sa_index;
672} esp_no_crypto_trace_t;
673
674static u8 *
675format_esp_no_crypto_trace (u8 * s, va_list * args)
676{
677 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
678 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
679 esp_no_crypto_trace_t *t = va_arg (*args, esp_no_crypto_trace_t *);
680
681 s = format (s, "esp-no-crypto: sa-index %u", t->sa_index);
682
683 return s;
684}
685
686enum
687{
688 ESP_NO_CRYPTO_NEXT_DROP,
689 ESP_NO_CRYPTO_N_NEXT,
690};
691
692enum
693{
694 ESP_NO_CRYPTO_ERROR_RX_PKTS,
695};
696
697static char *esp_no_crypto_error_strings[] = {
698 "Outbound ESP packets received",
699};
700
701always_inline uword
702esp_no_crypto_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
703 vlib_frame_t * frame)
704{
705 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
706 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
707 u32 *from = vlib_frame_vector_args (frame);
708 u32 n_left = frame->n_vectors;
709
710 vlib_get_buffers (vm, from, b, n_left);
711
712 while (n_left > 0)
713 {
714 u32 next0;
715 u32 sa_index0;
716
717 /* packets are always going to be dropped, but get the sa_index */
718 sa_index0 = *(u32 *) vnet_feature_next_with_data (&next0, b[0],
719 sizeof (sa_index0));
720
721 next[0] = ESP_NO_CRYPTO_NEXT_DROP;
722
723 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
724 {
725 esp_no_crypto_trace_t *tr = vlib_add_trace (vm, node, b[0],
726 sizeof (*tr));
727 tr->sa_index = sa_index0;
728 }
729
730 n_left -= 1;
731 next += 1;
732 b += 1;
733 }
734
735 vlib_node_increment_counter (vm, node->node_index,
736 ESP_NO_CRYPTO_ERROR_RX_PKTS, frame->n_vectors);
737
738 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
739
740 return frame->n_vectors;
741}
742
743VLIB_NODE_FN (esp4_no_crypto_tun_node) (vlib_main_t * vm,
744 vlib_node_runtime_t * node,
745 vlib_frame_t * from_frame)
746{
747 return esp_no_crypto_inline (vm, node, from_frame);
748}
749
750/* *INDENT-OFF* */
751VLIB_REGISTER_NODE (esp4_no_crypto_tun_node) =
752{
753 .name = "esp4-no-crypto",
754 .vector_size = sizeof (u32),
755 .format_trace = format_esp_no_crypto_trace,
756 .n_errors = ARRAY_LEN(esp_no_crypto_error_strings),
757 .error_strings = esp_no_crypto_error_strings,
758 .n_next_nodes = ESP_NO_CRYPTO_N_NEXT,
759 .next_nodes = {
760 [ESP_NO_CRYPTO_NEXT_DROP] = "ip4-drop",
761 },
762};
763
764VNET_FEATURE_INIT (esp4_no_crypto_tun_feat_node, static) =
765{
766 .arc_name = "ip4-output",
767 .node_name = "esp4-no-crypto",
768 .runs_before = VNET_FEATURES ("adj-midchain-tx"),
769};
770
771VLIB_NODE_FN (esp6_no_crypto_tun_node) (vlib_main_t * vm,
772 vlib_node_runtime_t * node,
773 vlib_frame_t * from_frame)
774{
775 return esp_no_crypto_inline (vm, node, from_frame);
776}
777
778/* *INDENT-OFF* */
779VLIB_REGISTER_NODE (esp6_no_crypto_tun_node) =
780{
781 .name = "esp6-no-crypto",
782 .vector_size = sizeof (u32),
783 .format_trace = format_esp_no_crypto_trace,
784 .n_errors = ARRAY_LEN(esp_no_crypto_error_strings),
785 .error_strings = esp_no_crypto_error_strings,
786 .n_next_nodes = ESP_NO_CRYPTO_N_NEXT,
787 .next_nodes = {
788 [ESP_NO_CRYPTO_NEXT_DROP] = "ip6-drop",
789 },
790};
791
792VNET_FEATURE_INIT (esp6_no_crypto_tun_feat_node, static) =
793{
794 .arc_name = "ip6-output",
795 .node_name = "esp6-no-crypto",
796 .runs_before = VNET_FEATURES ("adj-midchain-tx"),
797};
798/* *INDENT-ON* */
799
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700800/*
801 * fd.io coding-style-patch-verification: ON
802 *
803 * Local Variables:
804 * eval: (c-set-style "gnu")
805 * End:
806 */