blob: 75294a23102a9d693deabca2fb2a0d63e5b77c5f [file] [log] [blame]
“mukeshyadav1984”430ac932017-11-23 02:39:33 -08001/*
2 * ah_encrypt.c : IPSec AH 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>
21
22#include <vnet/ipsec/ipsec.h>
23#include <vnet/ipsec/esp.h>
24#include <vnet/ipsec/ah.h>
25
Klement Sekerabe5a5dd2018-10-09 16:05:48 +020026#define foreach_ah_encrypt_next \
27 _ (DROP, "error-drop") \
28 _ (IP4_LOOKUP, "ip4-lookup") \
29 _ (IP6_LOOKUP, "ip6-lookup") \
30 _ (INTERFACE_OUTPUT, "interface-output")
31
“mukeshyadav1984”430ac932017-11-23 02:39:33 -080032
33#define _(v, s) AH_ENCRYPT_NEXT_##v,
34typedef enum
35{
36 foreach_ah_encrypt_next
37#undef _
38 AH_ENCRYPT_N_NEXT,
39} ah_encrypt_next_t;
40
Filip Tehlar11974492019-04-17 07:16:39 +000041#define foreach_ah_encrypt_error \
42 _(RX_PKTS, "AH pkts received") \
43 _(CRYPTO_ENGINE_ERROR, "crypto engine error (packet dropped)") \
“mukeshyadav1984”430ac932017-11-23 02:39:33 -080044 _(SEQ_CYCLED, "sequence number cycled")
45
46
47typedef enum
48{
49#define _(sym,str) AH_ENCRYPT_ERROR_##sym,
50 foreach_ah_encrypt_error
51#undef _
52 AH_ENCRYPT_N_ERROR,
53} ah_encrypt_error_t;
54
55static char *ah_encrypt_error_strings[] = {
56#define _(sym,string) string,
57 foreach_ah_encrypt_error
58#undef _
59};
60
“mukeshyadav1984”430ac932017-11-23 02:39:33 -080061typedef struct
62{
Neale Ranns8d7c5022019-02-06 01:41:05 -080063 u32 sa_index;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -080064 u32 spi;
Neale Ranns3833ffd2019-03-21 14:34:09 +000065 u32 seq_lo;
66 u32 seq_hi;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -080067 ipsec_integ_alg_t integ_alg;
68} ah_encrypt_trace_t;
69
70/* packet trace format function */
71static u8 *
72format_ah_encrypt_trace (u8 * s, va_list * args)
73{
74 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
75 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
76 ah_encrypt_trace_t *t = va_arg (*args, ah_encrypt_trace_t *);
77
Guillaume Solignac8f818cc2019-05-15 12:02:33 +020078 s = format (s, "ah: sa-index %d spi %u (0x%08x) seq %u:%u integrity %U",
79 t->sa_index, t->spi, t->spi, t->seq_hi, t->seq_lo,
Neale Ranns8d7c5022019-02-06 01:41:05 -080080 format_ipsec_integ_alg, t->integ_alg);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -080081 return s;
82}
83
Filip Tehlar11974492019-04-17 07:16:39 +000084static_always_inline void
85ah_process_ops (vlib_main_t * vm, vlib_node_runtime_t * node,
86 vnet_crypto_op_t * ops, vlib_buffer_t * b[], u16 * nexts)
87{
88 u32 n_fail, n_ops = vec_len (ops);
89 vnet_crypto_op_t *op = ops;
90
91 if (n_ops == 0)
92 return;
93
94 n_fail = n_ops - vnet_crypto_process_ops (vm, op, n_ops);
95
96 while (n_fail)
97 {
98 ASSERT (op - ops < n_ops);
99
100 if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
101 {
102 u32 bi = op->user_data;
103 b[bi]->error = node->errors[AH_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR];
104 nexts[bi] = AH_ENCRYPT_NEXT_DROP;
105 n_fail--;
106 }
107 op++;
108 }
109}
110
111typedef struct
112{
113 union
114 {
115 struct
116 {
117 u8 hop_limit;
118 u32 ip_version_traffic_class_and_flow_label;
119 };
120
121 struct
122 {
123 u8 ttl;
124 u8 tos;
125 };
126 };
127 i16 current_data;
128 u8 skip;
129 u32 sa_index;
130} ah_encrypt_packet_data_t;
131
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200132always_inline uword
133ah_encrypt_inline (vlib_main_t * vm,
Filip Tehlar11974492019-04-17 07:16:39 +0000134 vlib_node_runtime_t * node, vlib_frame_t * frame,
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200135 int is_ip6)
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800136{
Filip Tehlar11974492019-04-17 07:16:39 +0000137 u32 n_left, *from, thread_index;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800138 int icv_size = 0;
Filip Tehlar11974492019-04-17 07:16:39 +0000139 from = vlib_frame_vector_args (frame);
140 n_left = frame->n_vectors;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800141 ipsec_main_t *im = &ipsec_main;
Filip Tehlar11974492019-04-17 07:16:39 +0000142 ah_encrypt_packet_data_t pkt_data[VLIB_FRAME_SIZE], *pd = pkt_data;
Neale Rannseba31ec2019-02-17 18:04:27 +0000143 thread_index = vm->thread_index;
Filip Tehlar11974492019-04-17 07:16:39 +0000144 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
145 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
146 ipsec_per_thread_data_t *ptd = vec_elt_at_index (im->ptd, thread_index);
147 ipsec_sa_t *sa0 = 0;
148 ip4_and_ah_header_t *ih0, *oh0 = 0;
149 ip6_and_ah_header_t *ih6_0, *oh6_0 = 0;
150 u32 current_sa_index = ~0, current_sa_bytes = 0, current_sa_pkts = 0;
151 const static ip4_header_t ip4_hdr_template = {
152 .ip_version_and_header_length = 0x45,
153 .protocol = IP_PROTOCOL_IPSEC_AH,
154 };
155 const static ip6_header_t ip6_hdr_template = {
156 .ip_version_traffic_class_and_flow_label = 0x60,
157 .protocol = IP_PROTOCOL_IPSEC_AH,
158 };
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800159
Filip Tehlar11974492019-04-17 07:16:39 +0000160 clib_memset (pkt_data, 0, VLIB_FRAME_SIZE * sizeof (pkt_data[0]));
161 vlib_get_buffers (vm, from, b, n_left);
162 vec_reset_length (ptd->crypto_ops);
163 vec_reset_length (ptd->integ_ops);
164
165 while (n_left > 0)
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800166 {
Filip Tehlar11974492019-04-17 07:16:39 +0000167 u8 ip_hdr_size;
168 u8 next_hdr_type;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800169
Filip Tehlar11974492019-04-17 07:16:39 +0000170 if (vnet_buffer (b[0])->ipsec.sad_index != current_sa_index)
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800171 {
Filip Tehlar98d6ee72019-06-03 23:36:10 +0000172 if (current_sa_index != ~0)
173 vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
174 current_sa_index,
175 current_sa_pkts,
176 current_sa_bytes);
Filip Tehlar11974492019-04-17 07:16:39 +0000177 current_sa_index = vnet_buffer (b[0])->ipsec.sad_index;
178 sa0 = pool_elt_at_index (im->sad, current_sa_index);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800179
Filip Tehlar11974492019-04-17 07:16:39 +0000180 current_sa_bytes = current_sa_pkts = 0;
181 }
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800182
Filip Tehlar11974492019-04-17 07:16:39 +0000183 pd->sa_index = current_sa_index;
184 next[0] = AH_ENCRYPT_NEXT_DROP;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800185
Filip Tehlar11974492019-04-17 07:16:39 +0000186 if (PREDICT_FALSE (esp_seq_advance (sa0)))
187 {
188 b[0]->error = node->errors[AH_ENCRYPT_ERROR_SEQ_CYCLED];
189 pd->skip = 1;
190 goto next;
191 }
192
193 current_sa_pkts += 1;
194 current_sa_bytes += b[0]->current_length;
195
196 ssize_t adv;
197 ih0 = vlib_buffer_get_current (b[0]);
198 pd->ttl = ih0->ip4.ttl;
199 pd->tos = ih0->ip4.tos;
200
201 if (PREDICT_TRUE (ipsec_sa_is_set_IS_TUNNEL (sa0)))
202 {
203 if (is_ip6)
204 adv = -sizeof (ip6_and_ah_header_t);
205 else
206 adv = -sizeof (ip4_and_ah_header_t);
207 }
208 else
209 {
210 adv = -sizeof (ah_header_t);
211 }
212
213 icv_size = sa0->integ_icv_size;
214 const u8 padding_len = ah_calc_icv_padding_len (icv_size, is_ip6);
215 adv -= padding_len;
216 /* transport mode save the eth header before it is overwritten */
217 if (PREDICT_FALSE (!ipsec_sa_is_set_IS_TUNNEL (sa0)))
218 {
Klement Sekera45155452019-06-19 11:26:34 +0000219 const u32 l2_len = vnet_buffer (b[0])->ip.save_rewrite_length;
220 u8 *l2_hdr_in = (u8 *) vlib_buffer_get_current (b[0]) - l2_len;
221
222 u8 *l2_hdr_out = l2_hdr_in + adv - icv_size;
223
224 clib_memcpy_le32 (l2_hdr_out, l2_hdr_in, l2_len);
Filip Tehlar11974492019-04-17 07:16:39 +0000225 }
226
227 vlib_buffer_advance (b[0], adv - icv_size);
228
229 if (is_ip6)
230 {
231 ih6_0 = (ip6_and_ah_header_t *) ih0;
232 ip_hdr_size = sizeof (ip6_header_t);
233 oh6_0 = vlib_buffer_get_current (b[0]);
234 pd->current_data = b[0]->current_data;
235
236 pd->hop_limit = ih6_0->ip6.hop_limit;
237 pd->ip_version_traffic_class_and_flow_label =
238 ih6_0->ip6.ip_version_traffic_class_and_flow_label;
239 if (PREDICT_TRUE (ipsec_sa_is_set_IS_TUNNEL (sa0)))
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800240 {
Filip Tehlar11974492019-04-17 07:16:39 +0000241 next_hdr_type = IP_PROTOCOL_IPV6;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800242 }
Filip Tehlar11974492019-04-17 07:16:39 +0000243 else
244 {
245 next_hdr_type = ih6_0->ip6.protocol;
246 memmove (oh6_0, ih6_0, sizeof (ip6_header_t));
247 }
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800248
Filip Tehlar11974492019-04-17 07:16:39 +0000249 clib_memcpy_fast (&oh6_0->ip6, &ip6_hdr_template, 8);
250 oh6_0->ah.reserved = 0;
251 oh6_0->ah.nexthdr = next_hdr_type;
252 oh6_0->ah.spi = clib_net_to_host_u32 (sa0->spi);
253 oh6_0->ah.seq_no = clib_net_to_host_u32 (sa0->seq);
254 oh6_0->ip6.payload_length =
255 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b[0]) -
256 sizeof (ip6_header_t));
257 oh6_0->ah.hdrlen =
258 (sizeof (ah_header_t) + icv_size + padding_len) / 4 - 2;
259 }
260 else
261 {
262 ip_hdr_size = sizeof (ip4_header_t);
263 oh0 = vlib_buffer_get_current (b[0]);
264 clib_memset (oh0, 0, sizeof (ip4_and_ah_header_t));
265 pd->current_data = b[0]->current_data;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800266
Damjan Mariond709cbc2019-03-26 13:16:42 +0100267 if (PREDICT_TRUE (ipsec_sa_is_set_IS_TUNNEL (sa0)))
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800268 {
Filip Tehlar11974492019-04-17 07:16:39 +0000269 next_hdr_type = IP_PROTOCOL_IP_IN_IP;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800270 }
271 else
272 {
Filip Tehlar11974492019-04-17 07:16:39 +0000273 next_hdr_type = ih0->ip4.protocol;
274 memmove (oh0, ih0, sizeof (ip4_header_t));
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800275 }
276
Filip Tehlar11974492019-04-17 07:16:39 +0000277 clib_memcpy_fast (&oh0->ip4, &ip4_hdr_template,
278 sizeof (ip4_header_t) -
279 sizeof (ip4_address_pair_t));
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800280
Filip Tehlar11974492019-04-17 07:16:39 +0000281 oh0->ip4.length =
282 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b[0]));
283 oh0->ah.spi = clib_net_to_host_u32 (sa0->spi);
284 oh0->ah.seq_no = clib_net_to_host_u32 (sa0->seq);
285 oh0->ah.nexthdr = next_hdr_type;
286 oh0->ah.hdrlen =
287 (sizeof (ah_header_t) + icv_size + padding_len) / 4 - 2;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800288 }
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800289
Filip Tehlar11974492019-04-17 07:16:39 +0000290 if (PREDICT_TRUE (!is_ip6 && ipsec_sa_is_set_IS_TUNNEL (sa0) &&
291 !ipsec_sa_is_set_IS_TUNNEL_V6 (sa0)))
292 {
293 clib_memcpy_fast (&oh0->ip4.address_pair,
294 &sa0->ip4_hdr.address_pair,
295 sizeof (ip4_address_t));
296
Neale Ranns72f2a3a2019-06-17 15:43:38 +0000297 next[0] = sa0->dpo.dpoi_next_node;
298 vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = sa0->dpo.dpoi_index;
Filip Tehlar11974492019-04-17 07:16:39 +0000299 }
300 else if (is_ip6 && ipsec_sa_is_set_IS_TUNNEL (sa0) &&
301 ipsec_sa_is_set_IS_TUNNEL_V6 (sa0))
302 {
303 clib_memcpy_fast (&oh6_0->ip6.src_address,
304 &sa0->ip6_hdr.src_address,
305 sizeof (ip6_address_t) * 2);
Neale Ranns72f2a3a2019-06-17 15:43:38 +0000306 next[0] = sa0->dpo.dpoi_next_node;
307 vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = sa0->dpo.dpoi_index;
Filip Tehlar11974492019-04-17 07:16:39 +0000308 }
309
310 if (PREDICT_TRUE (sa0->integ_op_id))
311 {
312 vnet_crypto_op_t *op;
313 vec_add2_aligned (ptd->integ_ops, op, 1, CLIB_CACHE_LINE_BYTES);
314 vnet_crypto_op_init (op, sa0->integ_op_id);
315 op->src = vlib_buffer_get_current (b[0]);
316 op->len = b[0]->current_length;
317 op->digest = vlib_buffer_get_current (b[0]) + ip_hdr_size +
318 sizeof (ah_header_t);
319 clib_memset (op->digest, 0, icv_size);
320 op->digest_len = icv_size;
321 op->key_index = sa0->integ_key_index;
322 op->user_data = b - bufs;
323 if (ipsec_sa_is_set_USE_ESN (sa0))
324 {
325 u32 seq_hi = clib_host_to_net_u32 (sa0->seq_hi);
326
327 op->len += sizeof (seq_hi);
328 clib_memcpy (op->src + b[0]->current_length, &seq_hi,
329 sizeof (seq_hi));
330 }
331 }
332
333 if (!ipsec_sa_is_set_IS_TUNNEL (sa0))
334 {
335 next[0] = AH_ENCRYPT_NEXT_INTERFACE_OUTPUT;
336 vlib_buffer_advance (b[0], -sizeof (ethernet_header_t));
337 }
338
339 next:
340 n_left -= 1;
341 next += 1;
342 pd += 1;
343 b += 1;
344 }
345
346 n_left = frame->n_vectors;
347 next = nexts;
348 pd = pkt_data;
349 b = bufs;
350
351 vlib_node_increment_counter (vm, node->node_index,
352 AH_ENCRYPT_ERROR_RX_PKTS, n_left);
353 vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
354 current_sa_index, current_sa_pkts,
355 current_sa_bytes);
356
357 ah_process_ops (vm, node, ptd->integ_ops, bufs, nexts);
358
359 while (n_left)
360 {
361 if (pd->skip)
362 goto trace;
363
364 if (is_ip6)
365 {
366 oh6_0 = (ip6_and_ah_header_t *) (b[0]->data + pd->current_data);
367 oh6_0->ip6.hop_limit = pd->hop_limit;
368 oh6_0->ip6.ip_version_traffic_class_and_flow_label =
369 pd->ip_version_traffic_class_and_flow_label;
370 }
371 else
372 {
373 oh0 = (ip4_and_ah_header_t *) (b[0]->data + pd->current_data);
374 oh0->ip4.ttl = pd->ttl;
375 oh0->ip4.tos = pd->tos;
376 oh0->ip4.checksum = ip4_header_checksum (&oh0->ip4);
377 }
378
379 trace:
380 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
381 {
382 sa0 = vec_elt_at_index (im->sad, pd->sa_index);
383 ah_encrypt_trace_t *tr =
384 vlib_add_trace (vm, node, b[0], sizeof (*tr));
385 tr->spi = sa0->spi;
386 tr->seq_lo = sa0->seq;
387 tr->seq_hi = sa0->seq_hi;
388 tr->integ_alg = sa0->integ_alg;
389 tr->sa_index = pd->sa_index;
390 }
391
392 n_left -= 1;
393 next += 1;
394 pd += 1;
395 b += 1;
396 }
397
398 n_left = frame->n_vectors;
399 vlib_buffer_enqueue_to_next (vm, node, from, nexts, n_left);
400
401 return n_left;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800402}
403
Klement Sekerab8f35442018-10-29 13:38:19 +0100404VLIB_NODE_FN (ah4_encrypt_node) (vlib_main_t * vm,
405 vlib_node_runtime_t * node,
406 vlib_frame_t * from_frame)
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200407{
408 return ah_encrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ );
409}
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800410
411/* *INDENT-OFF* */
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200412VLIB_REGISTER_NODE (ah4_encrypt_node) = {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200413 .name = "ah4-encrypt",
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800414 .vector_size = sizeof (u32),
415 .format_trace = format_ah_encrypt_trace,
416 .type = VLIB_NODE_TYPE_INTERNAL,
417
418 .n_errors = ARRAY_LEN(ah_encrypt_error_strings),
419 .error_strings = ah_encrypt_error_strings,
420
421 .n_next_nodes = AH_ENCRYPT_N_NEXT,
422 .next_nodes = {
423#define _(s,n) [AH_ENCRYPT_NEXT_##s] = n,
424 foreach_ah_encrypt_next
425#undef _
426 },
427};
428/* *INDENT-ON* */
429
Klement Sekerab8f35442018-10-29 13:38:19 +0100430VLIB_NODE_FN (ah6_encrypt_node) (vlib_main_t * vm,
431 vlib_node_runtime_t * node,
432 vlib_frame_t * from_frame)
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200433{
434 return ah_encrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ );
435}
436
437/* *INDENT-OFF* */
438VLIB_REGISTER_NODE (ah6_encrypt_node) = {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200439 .name = "ah6-encrypt",
440 .vector_size = sizeof (u32),
441 .format_trace = format_ah_encrypt_trace,
442 .type = VLIB_NODE_TYPE_INTERNAL,
443
444 .n_errors = ARRAY_LEN(ah_encrypt_error_strings),
445 .error_strings = ah_encrypt_error_strings,
446
447 .n_next_nodes = AH_ENCRYPT_N_NEXT,
448 .next_nodes = {
449#define _(s,n) [AH_ENCRYPT_NEXT_##s] = n,
450 foreach_ah_encrypt_next
451#undef _
452 },
453};
454/* *INDENT-ON* */
455
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800456/*
457 * fd.io coding-style-patch-verification: ON
458 *
459 * Local Variables:
460 * eval: (c-set-style "gnu")
461 * End:
462 */