2 * ah_decrypt.c : IPSec AH decrypt node
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <vnet/vnet.h>
19 #include <vnet/api_errno.h>
20 #include <vnet/ip/ip.h>
22 #include <vnet/ipsec/ipsec.h>
23 #include <vnet/ipsec/esp.h>
24 #include <vnet/ipsec/ah.h>
25 #include <vnet/ipsec/ipsec_io.h>
27 #define foreach_ah_decrypt_next \
28 _(DROP, "error-drop") \
29 _(IP4_INPUT, "ip4-input") \
30 _(IP6_INPUT, "ip6-input") \
33 #define _(v, s) AH_DECRYPT_NEXT_##v,
36 foreach_ah_decrypt_next
41 #define foreach_ah_decrypt_error \
42 _ (RX_PKTS, "AH pkts received") \
43 _ (DECRYPTION_FAILED, "AH decryption failed") \
44 _ (INTEG_ERROR, "Integrity check failed") \
45 _ (NO_TAIL_SPACE, "not enough buffer tail space (dropped)") \
46 _ (DROP_FRAGMENTS, "IP fragments drop") \
47 _ (REPLAY, "SA replayed packet")
51 #define _(sym,str) AH_DECRYPT_ERROR_##sym,
52 foreach_ah_decrypt_error
57 static char *ah_decrypt_error_strings[] = {
58 #define _(sym,string) string,
59 foreach_ah_decrypt_error
65 ipsec_integ_alg_t integ_alg;
69 /* packet trace format function */
71 format_ah_decrypt_trace (u8 * s, va_list * args)
73 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
74 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
75 ah_decrypt_trace_t *t = va_arg (*args, ah_decrypt_trace_t *);
77 s = format (s, "ah: integrity %U seq-num %d",
78 format_ipsec_integ_alg, t->integ_alg, t->seq_num);
90 u32 ip_version_traffic_class_and_flow_label;
107 } ah_decrypt_packet_data_t;
109 static_always_inline void
110 ah_process_ops (vlib_main_t * vm, vlib_node_runtime_t * node,
111 vnet_crypto_op_t * ops, vlib_buffer_t * b[], u16 * nexts)
113 u32 n_fail, n_ops = vec_len (ops);
114 vnet_crypto_op_t *op = ops;
119 n_fail = n_ops - vnet_crypto_process_ops (vm, op, n_ops);
123 ASSERT (op - ops < n_ops);
125 if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
127 u32 bi = op->user_data;
128 b[bi]->error = node->errors[AH_DECRYPT_ERROR_INTEG_ERROR];
129 nexts[bi] = AH_DECRYPT_NEXT_DROP;
137 ah_decrypt_inline (vlib_main_t * vm,
138 vlib_node_runtime_t * node, vlib_frame_t * from_frame,
142 u32 thread_index = vm->thread_index;
143 u16 buffer_data_size = vlib_buffer_get_default_data_size (vm);
144 ah_decrypt_packet_data_t pkt_data[VLIB_FRAME_SIZE], *pd = pkt_data;
145 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
146 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
147 ipsec_main_t *im = &ipsec_main;
148 ipsec_per_thread_data_t *ptd = vec_elt_at_index (im->ptd, thread_index);
149 from = vlib_frame_vector_args (from_frame);
150 n_left = from_frame->n_vectors;
152 u32 current_sa_index = ~0, current_sa_bytes = 0, current_sa_pkts = 0;
154 clib_memset (pkt_data, 0, VLIB_FRAME_SIZE * sizeof (pkt_data[0]));
155 vlib_get_buffers (vm, from, b, n_left);
156 clib_memset_u16 (nexts, -1, n_left);
157 vec_reset_length (ptd->integ_ops);
165 if (vnet_buffer (b[0])->ipsec.sad_index != current_sa_index)
167 if (current_sa_index != ~0)
168 vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
172 current_sa_index = vnet_buffer (b[0])->ipsec.sad_index;
173 sa0 = ipsec_sa_get (current_sa_index);
175 current_sa_bytes = current_sa_pkts = 0;
176 vlib_prefetch_combined_counter (&ipsec_sa_counters,
177 thread_index, current_sa_index);
180 if (PREDICT_FALSE (~0 == sa0->thread_index))
182 /* this is the first packet to use this SA, claim the SA
183 * for this thread. this could happen simultaneously on
185 clib_atomic_cmp_and_swap (&sa0->thread_index, ~0,
186 ipsec_sa_assign_thread (thread_index));
189 if (PREDICT_TRUE (thread_index != sa0->thread_index))
191 vnet_buffer (b[0])->ipsec.thread_index = sa0->thread_index;
192 next[0] = AH_DECRYPT_NEXT_HANDOFF;
196 pd->sa_index = current_sa_index;
198 ih4 = vlib_buffer_get_current (b[0]);
199 ih6 = vlib_buffer_get_current (b[0]);
200 pd->current_data = b[0]->current_data;
204 ip6_ext_header_t *prev = NULL;
206 ip6_ext_header_find (vm, b[0], ih6, IP_PROTOCOL_IPSEC_AH, &prev);
207 pd->ip_hdr_size = sizeof (ip6_header_t);
208 ASSERT ((u8 *) ah0 - (u8 *) ih6 == pd->ip_hdr_size);
212 if (ip4_is_fragment (ih4))
214 b[0]->error = node->errors[AH_DECRYPT_ERROR_DROP_FRAGMENTS];
215 next[0] = AH_DECRYPT_NEXT_DROP;
218 pd->ip_hdr_size = ip4_header_bytes (ih4);
219 ah0 = (ah_header_t *) ((u8 *) ih4 + pd->ip_hdr_size);
222 pd->seq = clib_host_to_net_u32 (ah0->seq_no);
224 /* anti-replay check */
225 if (ipsec_sa_anti_replay_and_sn_advance (sa0, pd->seq, ~0, false,
228 b[0]->error = node->errors[AH_DECRYPT_ERROR_REPLAY];
229 next[0] = AH_DECRYPT_NEXT_DROP;
233 current_sa_bytes += b[0]->current_length;
234 current_sa_pkts += 1;
236 pd->icv_size = sa0->integ_icv_size;
237 pd->nexthdr_cached = ah0->nexthdr;
238 if (PREDICT_TRUE (sa0->integ_alg != IPSEC_INTEG_ALG_NONE))
240 if (PREDICT_FALSE (ipsec_sa_is_set_USE_ESN (sa0) &&
241 pd->current_data + b[0]->current_length
242 + sizeof (u32) > buffer_data_size))
244 b[0]->error = node->errors[AH_DECRYPT_ERROR_NO_TAIL_SPACE];
245 next[0] = AH_DECRYPT_NEXT_DROP;
249 vnet_crypto_op_t *op;
250 vec_add2_aligned (ptd->integ_ops, op, 1, CLIB_CACHE_LINE_BYTES);
251 vnet_crypto_op_init (op, sa0->integ_op_id);
253 op->src = (u8 *) ih4;
254 op->len = b[0]->current_length;
255 op->digest = (u8 *) ih4 - pd->icv_size;
256 op->flags = VNET_CRYPTO_OP_FLAG_HMAC_CHECK;
257 op->digest_len = pd->icv_size;
258 op->key_index = sa0->integ_key_index;
259 op->user_data = b - bufs;
260 if (ipsec_sa_is_set_USE_ESN (sa0))
262 u32 seq_hi = clib_host_to_net_u32 (pd->seq_hi);
264 op->len += sizeof (seq_hi);
265 clib_memcpy (op->src + b[0]->current_length, &seq_hi,
268 clib_memcpy (op->digest, ah0->auth_data, pd->icv_size);
269 clib_memset (ah0->auth_data, 0, pd->icv_size);
273 pd->ip_version_traffic_class_and_flow_label =
274 ih6->ip_version_traffic_class_and_flow_label;
275 pd->hop_limit = ih6->hop_limit;
276 ih6->ip_version_traffic_class_and_flow_label = 0x60;
278 pd->nexthdr = ah0->nexthdr;
279 pd->icv_padding_len =
280 ah_calc_icv_padding_len (pd->icv_size, 1 /* is_ipv6 */ );
289 pd->icv_padding_len =
290 ah_calc_icv_padding_len (pd->icv_size, 0 /* is_ipv6 */ );
301 n_left = from_frame->n_vectors;
306 vlib_node_increment_counter (vm, node->node_index, AH_DECRYPT_ERROR_RX_PKTS,
308 vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
309 current_sa_index, current_sa_pkts,
312 ah_process_ops (vm, node, ptd->integ_ops, bufs, nexts);
320 if (next[0] < AH_DECRYPT_N_NEXT)
323 sa0 = ipsec_sa_get (pd->sa_index);
325 if (PREDICT_TRUE (sa0->integ_alg != IPSEC_INTEG_ALG_NONE))
327 /* redo the anti-reply check. see esp_decrypt for details */
328 if (ipsec_sa_anti_replay_and_sn_advance (sa0, pd->seq, pd->seq_hi,
331 b[0]->error = node->errors[AH_DECRYPT_ERROR_REPLAY];
332 next[0] = AH_DECRYPT_NEXT_DROP;
335 n_lost = ipsec_sa_anti_replay_advance (sa0, thread_index, pd->seq,
337 vlib_prefetch_simple_counter (&ipsec_sa_lost_counters, thread_index,
341 u16 ah_hdr_len = sizeof (ah_header_t) + pd->icv_size
342 + pd->icv_padding_len;
343 vlib_buffer_advance (b[0], pd->ip_hdr_size + ah_hdr_len);
344 b[0]->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
346 if (PREDICT_TRUE (ipsec_sa_is_set_IS_TUNNEL (sa0)))
348 if (PREDICT_TRUE (pd->nexthdr_cached == IP_PROTOCOL_IP_IN_IP))
349 next[0] = AH_DECRYPT_NEXT_IP4_INPUT;
350 else if (pd->nexthdr_cached == IP_PROTOCOL_IPV6)
351 next[0] = AH_DECRYPT_NEXT_IP6_INPUT;
354 b[0]->error = node->errors[AH_DECRYPT_ERROR_DECRYPTION_FAILED];
355 next[0] = AH_DECRYPT_NEXT_DROP;
360 { /* transport mode */
363 vlib_buffer_advance (b[0], -sizeof (ip6_header_t));
364 oh6 = vlib_buffer_get_current (b[0]);
365 if (ah_hdr_len >= sizeof (ip6_header_t))
366 clib_memcpy (oh6, b[0]->data + pd->current_data,
367 sizeof (ip6_header_t));
369 memmove (oh6, b[0]->data + pd->current_data,
370 sizeof (ip6_header_t));
372 next[0] = AH_DECRYPT_NEXT_IP6_INPUT;
373 oh6->protocol = pd->nexthdr;
374 oh6->hop_limit = pd->hop_limit;
375 oh6->ip_version_traffic_class_and_flow_label =
376 pd->ip_version_traffic_class_and_flow_label;
377 oh6->payload_length =
378 clib_host_to_net_u16 (vlib_buffer_length_in_chain
379 (vm, b[0]) - sizeof (ip6_header_t));
383 vlib_buffer_advance (b[0], -sizeof (ip4_header_t));
384 oh4 = vlib_buffer_get_current (b[0]);
385 if (ah_hdr_len >= sizeof (ip4_header_t))
386 clib_memcpy (oh4, b[0]->data + pd->current_data,
387 sizeof (ip4_header_t));
389 memmove (oh4, b[0]->data + pd->current_data,
390 sizeof (ip4_header_t));
392 next[0] = AH_DECRYPT_NEXT_IP4_INPUT;
393 oh4->ip_version_and_header_length = 0x45;
394 oh4->fragment_id = 0;
395 oh4->flags_and_fragment_offset = 0;
396 oh4->protocol = pd->nexthdr_cached;
398 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b[0]));
401 oh4->checksum = ip4_header_checksum (oh4);
405 if (PREDICT_FALSE (n_lost))
406 vlib_increment_simple_counter (&ipsec_sa_lost_counters, thread_index,
407 pd->sa_index, n_lost);
409 vnet_buffer (b[0])->sw_if_index[VLIB_TX] = (u32) ~ 0;
411 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
413 sa0 = ipsec_sa_get (vnet_buffer (b[0])->ipsec.sad_index);
414 ah_decrypt_trace_t *tr =
415 vlib_add_trace (vm, node, b[0], sizeof (*tr));
416 tr->integ_alg = sa0->integ_alg;
417 tr->seq_num = pd->seq;
426 n_left = from_frame->n_vectors;
427 vlib_buffer_enqueue_to_next (vm, node, from, nexts, n_left);
432 VLIB_NODE_FN (ah4_decrypt_node) (vlib_main_t * vm,
433 vlib_node_runtime_t * node,
434 vlib_frame_t * from_frame)
436 return ah_decrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ );
440 VLIB_REGISTER_NODE (ah4_decrypt_node) = {
441 .name = "ah4-decrypt",
442 .vector_size = sizeof (u32),
443 .format_trace = format_ah_decrypt_trace,
444 .type = VLIB_NODE_TYPE_INTERNAL,
446 .n_errors = ARRAY_LEN(ah_decrypt_error_strings),
447 .error_strings = ah_decrypt_error_strings,
449 .n_next_nodes = AH_DECRYPT_N_NEXT,
451 [AH_DECRYPT_NEXT_DROP] = "ip4-drop",
452 [AH_DECRYPT_NEXT_IP4_INPUT] = "ip4-input-no-checksum",
453 [AH_DECRYPT_NEXT_IP6_INPUT] = "ip6-input",
454 [AH_DECRYPT_NEXT_HANDOFF] = "ah4-decrypt-handoff",
459 VLIB_NODE_FN (ah6_decrypt_node) (vlib_main_t * vm,
460 vlib_node_runtime_t * node,
461 vlib_frame_t * from_frame)
463 return ah_decrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ );
467 VLIB_REGISTER_NODE (ah6_decrypt_node) = {
468 .name = "ah6-decrypt",
469 .vector_size = sizeof (u32),
470 .format_trace = format_ah_decrypt_trace,
471 .type = VLIB_NODE_TYPE_INTERNAL,
473 .n_errors = ARRAY_LEN(ah_decrypt_error_strings),
474 .error_strings = ah_decrypt_error_strings,
476 .n_next_nodes = AH_DECRYPT_N_NEXT,
478 [AH_DECRYPT_NEXT_DROP] = "ip6-drop",
479 [AH_DECRYPT_NEXT_IP4_INPUT] = "ip4-input-no-checksum",
480 [AH_DECRYPT_NEXT_IP6_INPUT] = "ip6-input",
481 [AH_DECRYPT_NEXT_HANDOFF] = "ah6-decrypt-handoff",
486 #ifndef CLIB_MARCH_VARIANT
488 static clib_error_t *
489 ah_decrypt_init (vlib_main_t *vm)
491 ipsec_main_t *im = &ipsec_main;
493 im->ah4_dec_fq_index =
494 vlib_frame_queue_main_init (ah4_decrypt_node.index, 0);
495 im->ah6_dec_fq_index =
496 vlib_frame_queue_main_init (ah6_decrypt_node.index, 0);
501 VLIB_INIT_FUNCTION (ah_decrypt_init);
506 * fd.io coding-style-patch-verification: ON
509 * eval: (c-set-style "gnu")