2 * ah_encrypt.c : IPSec AH encrypt 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>
26 #define foreach_ah_encrypt_next \
27 _ (DROP, "error-drop") \
28 _ (HANDOFF, "handoff") \
29 _ (INTERFACE_OUTPUT, "interface-output")
32 #define _(v, s) AH_ENCRYPT_NEXT_##v,
35 foreach_ah_encrypt_next
40 #define foreach_ah_encrypt_error \
41 _(RX_PKTS, "AH pkts received") \
42 _(CRYPTO_ENGINE_ERROR, "crypto engine error (packet dropped)") \
43 _(SEQ_CYCLED, "sequence number cycled")
48 #define _(sym,str) AH_ENCRYPT_ERROR_##sym,
49 foreach_ah_encrypt_error
54 static char *ah_encrypt_error_strings[] = {
55 #define _(sym,string) string,
56 foreach_ah_encrypt_error
66 ipsec_integ_alg_t integ_alg;
69 /* packet trace format function */
71 format_ah_encrypt_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_encrypt_trace_t *t = va_arg (*args, ah_encrypt_trace_t *);
77 s = format (s, "ah: sa-index %d spi %u (0x%08x) seq %u:%u integrity %U",
78 t->sa_index, t->spi, t->spi, t->seq_hi, t->seq_lo,
79 format_ipsec_integ_alg, t->integ_alg);
83 static_always_inline void
84 ah_process_ops (vlib_main_t * vm, vlib_node_runtime_t * node,
85 vnet_crypto_op_t * ops, vlib_buffer_t * b[], u16 * nexts)
87 u32 n_fail, n_ops = vec_len (ops);
88 vnet_crypto_op_t *op = ops;
93 n_fail = n_ops - vnet_crypto_process_ops (vm, op, n_ops);
97 ASSERT (op - ops < n_ops);
99 if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
101 u32 bi = op->user_data;
102 b[bi]->error = node->errors[AH_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR];
103 nexts[bi] = AH_ENCRYPT_NEXT_DROP;
117 u32 ip_version_traffic_class_and_flow_label;
129 } ah_encrypt_packet_data_t;
132 ah_encrypt_inline (vlib_main_t * vm,
133 vlib_node_runtime_t * node, vlib_frame_t * frame,
136 u32 n_left, *from, thread_index;
138 from = vlib_frame_vector_args (frame);
139 n_left = frame->n_vectors;
140 ipsec_main_t *im = &ipsec_main;
141 ah_encrypt_packet_data_t pkt_data[VLIB_FRAME_SIZE], *pd = pkt_data;
142 thread_index = vm->thread_index;
143 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
144 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
145 ipsec_per_thread_data_t *ptd = vec_elt_at_index (im->ptd, thread_index);
147 ip4_and_ah_header_t *ih0, *oh0 = 0;
148 ip6_and_ah_header_t *ih6_0, *oh6_0 = 0;
149 u32 current_sa_index = ~0, current_sa_bytes = 0, current_sa_pkts = 0;
150 const static ip4_header_t ip4_hdr_template = {
151 .ip_version_and_header_length = 0x45,
152 .protocol = IP_PROTOCOL_IPSEC_AH,
154 const static ip6_header_t ip6_hdr_template = {
155 .ip_version_traffic_class_and_flow_label = 0x60,
156 .protocol = IP_PROTOCOL_IPSEC_AH,
159 clib_memset (pkt_data, 0, VLIB_FRAME_SIZE * sizeof (pkt_data[0]));
160 vlib_get_buffers (vm, from, b, n_left);
161 vec_reset_length (ptd->crypto_ops);
162 vec_reset_length (ptd->integ_ops);
169 if (vnet_buffer (b[0])->ipsec.sad_index != current_sa_index)
171 if (current_sa_index != ~0)
172 vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
176 current_sa_index = vnet_buffer (b[0])->ipsec.sad_index;
177 sa0 = pool_elt_at_index (im->sad, current_sa_index);
179 current_sa_bytes = current_sa_pkts = 0;
182 pd->sa_index = current_sa_index;
183 next[0] = AH_ENCRYPT_NEXT_DROP;
185 if (PREDICT_FALSE (~0 == sa0->encrypt_thread_index))
187 /* this is the first packet to use this SA, claim the SA
188 * for this thread. this could happen simultaneously on
190 clib_atomic_cmp_and_swap (&sa0->encrypt_thread_index, ~0,
191 ipsec_sa_assign_thread (thread_index));
194 if (PREDICT_TRUE (thread_index != sa0->encrypt_thread_index))
196 next[0] = AH_ENCRYPT_NEXT_HANDOFF;
200 if (PREDICT_FALSE (esp_seq_advance (sa0)))
202 b[0]->error = node->errors[AH_ENCRYPT_ERROR_SEQ_CYCLED];
207 current_sa_pkts += 1;
208 current_sa_bytes += b[0]->current_length;
211 ih0 = vlib_buffer_get_current (b[0]);
212 pd->ttl = ih0->ip4.ttl;
213 pd->tos = ih0->ip4.tos;
215 if (PREDICT_TRUE (ipsec_sa_is_set_IS_TUNNEL (sa0)))
218 adv = -sizeof (ip6_and_ah_header_t);
220 adv = -sizeof (ip4_and_ah_header_t);
224 adv = -sizeof (ah_header_t);
227 icv_size = sa0->integ_icv_size;
228 const u8 padding_len = ah_calc_icv_padding_len (icv_size, is_ip6);
230 /* transport mode save the eth header before it is overwritten */
231 if (PREDICT_FALSE (!ipsec_sa_is_set_IS_TUNNEL (sa0)))
233 const u32 l2_len = vnet_buffer (b[0])->ip.save_rewrite_length;
234 u8 *l2_hdr_in = (u8 *) vlib_buffer_get_current (b[0]) - l2_len;
236 u8 *l2_hdr_out = l2_hdr_in + adv - icv_size;
238 clib_memcpy_le32 (l2_hdr_out, l2_hdr_in, l2_len);
241 vlib_buffer_advance (b[0], adv - icv_size);
245 ih6_0 = (ip6_and_ah_header_t *) ih0;
246 ip_hdr_size = sizeof (ip6_header_t);
247 oh6_0 = vlib_buffer_get_current (b[0]);
248 pd->current_data = b[0]->current_data;
250 pd->hop_limit = ih6_0->ip6.hop_limit;
251 pd->ip_version_traffic_class_and_flow_label =
252 ih6_0->ip6.ip_version_traffic_class_and_flow_label;
253 if (PREDICT_TRUE (ipsec_sa_is_set_IS_TUNNEL (sa0)))
255 next_hdr_type = IP_PROTOCOL_IPV6;
259 next_hdr_type = ih6_0->ip6.protocol;
260 memmove (oh6_0, ih6_0, sizeof (ip6_header_t));
263 clib_memcpy_fast (&oh6_0->ip6, &ip6_hdr_template, 8);
264 oh6_0->ah.reserved = 0;
265 oh6_0->ah.nexthdr = next_hdr_type;
266 oh6_0->ah.spi = clib_net_to_host_u32 (sa0->spi);
267 oh6_0->ah.seq_no = clib_net_to_host_u32 (sa0->seq);
268 oh6_0->ip6.payload_length =
269 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b[0]) -
270 sizeof (ip6_header_t));
272 (sizeof (ah_header_t) + icv_size + padding_len) / 4 - 2;
276 ip_hdr_size = sizeof (ip4_header_t);
277 oh0 = vlib_buffer_get_current (b[0]);
278 clib_memset (oh0, 0, sizeof (ip4_and_ah_header_t));
279 pd->current_data = b[0]->current_data;
281 if (PREDICT_TRUE (ipsec_sa_is_set_IS_TUNNEL (sa0)))
283 next_hdr_type = IP_PROTOCOL_IP_IN_IP;
287 next_hdr_type = ih0->ip4.protocol;
288 memmove (oh0, ih0, sizeof (ip4_header_t));
291 clib_memcpy_fast (&oh0->ip4, &ip4_hdr_template,
292 sizeof (ip4_header_t) -
293 sizeof (ip4_address_pair_t));
296 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b[0]));
297 oh0->ah.spi = clib_net_to_host_u32 (sa0->spi);
298 oh0->ah.seq_no = clib_net_to_host_u32 (sa0->seq);
299 oh0->ah.nexthdr = next_hdr_type;
301 (sizeof (ah_header_t) + icv_size + padding_len) / 4 - 2;
304 if (PREDICT_TRUE (!is_ip6 && ipsec_sa_is_set_IS_TUNNEL (sa0) &&
305 !ipsec_sa_is_set_IS_TUNNEL_V6 (sa0)))
307 clib_memcpy_fast (&oh0->ip4.address_pair,
308 &sa0->ip4_hdr.address_pair,
309 sizeof (ip4_address_pair_t));
311 next[0] = sa0->dpo.dpoi_next_node;
312 vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = sa0->dpo.dpoi_index;
314 else if (is_ip6 && ipsec_sa_is_set_IS_TUNNEL (sa0) &&
315 ipsec_sa_is_set_IS_TUNNEL_V6 (sa0))
317 clib_memcpy_fast (&oh6_0->ip6.src_address,
318 &sa0->ip6_hdr.src_address,
319 sizeof (ip6_address_t) * 2);
320 next[0] = sa0->dpo.dpoi_next_node;
321 vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = sa0->dpo.dpoi_index;
324 if (PREDICT_TRUE (sa0->integ_op_id))
326 vnet_crypto_op_t *op;
327 vec_add2_aligned (ptd->integ_ops, op, 1, CLIB_CACHE_LINE_BYTES);
328 vnet_crypto_op_init (op, sa0->integ_op_id);
329 op->src = vlib_buffer_get_current (b[0]);
330 op->len = b[0]->current_length;
331 op->digest = vlib_buffer_get_current (b[0]) + ip_hdr_size +
332 sizeof (ah_header_t);
333 clib_memset (op->digest, 0, icv_size);
334 op->digest_len = icv_size;
335 op->key_index = sa0->integ_key_index;
336 op->user_data = b - bufs;
337 if (ipsec_sa_is_set_USE_ESN (sa0))
339 u32 seq_hi = clib_host_to_net_u32 (sa0->seq_hi);
341 op->len += sizeof (seq_hi);
342 clib_memcpy (op->src + b[0]->current_length, &seq_hi,
347 if (!ipsec_sa_is_set_IS_TUNNEL (sa0))
349 next[0] = AH_ENCRYPT_NEXT_INTERFACE_OUTPUT;
350 vlib_buffer_advance (b[0], -sizeof (ethernet_header_t));
360 n_left = frame->n_vectors;
365 vlib_node_increment_counter (vm, node->node_index,
366 AH_ENCRYPT_ERROR_RX_PKTS, n_left);
367 vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
368 current_sa_index, current_sa_pkts,
371 ah_process_ops (vm, node, ptd->integ_ops, bufs, nexts);
380 oh6_0 = (ip6_and_ah_header_t *) (b[0]->data + pd->current_data);
381 oh6_0->ip6.hop_limit = pd->hop_limit;
382 oh6_0->ip6.ip_version_traffic_class_and_flow_label =
383 pd->ip_version_traffic_class_and_flow_label;
387 oh0 = (ip4_and_ah_header_t *) (b[0]->data + pd->current_data);
388 oh0->ip4.ttl = pd->ttl;
389 oh0->ip4.tos = pd->tos;
390 oh0->ip4.checksum = ip4_header_checksum (&oh0->ip4);
394 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
396 sa0 = vec_elt_at_index (im->sad, pd->sa_index);
397 ah_encrypt_trace_t *tr =
398 vlib_add_trace (vm, node, b[0], sizeof (*tr));
400 tr->seq_lo = sa0->seq;
401 tr->seq_hi = sa0->seq_hi;
402 tr->integ_alg = sa0->integ_alg;
403 tr->sa_index = pd->sa_index;
412 n_left = frame->n_vectors;
413 vlib_buffer_enqueue_to_next (vm, node, from, nexts, n_left);
418 VLIB_NODE_FN (ah4_encrypt_node) (vlib_main_t * vm,
419 vlib_node_runtime_t * node,
420 vlib_frame_t * from_frame)
422 return ah_encrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ );
426 VLIB_REGISTER_NODE (ah4_encrypt_node) = {
427 .name = "ah4-encrypt",
428 .vector_size = sizeof (u32),
429 .format_trace = format_ah_encrypt_trace,
430 .type = VLIB_NODE_TYPE_INTERNAL,
432 .n_errors = ARRAY_LEN(ah_encrypt_error_strings),
433 .error_strings = ah_encrypt_error_strings,
435 .n_next_nodes = AH_ENCRYPT_N_NEXT,
437 [AH_ENCRYPT_NEXT_DROP] = "ip4-drop",
438 [AH_ENCRYPT_NEXT_HANDOFF] = "ah4-encrypt-handoff",
439 [AH_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "interface-output",
444 VLIB_NODE_FN (ah6_encrypt_node) (vlib_main_t * vm,
445 vlib_node_runtime_t * node,
446 vlib_frame_t * from_frame)
448 return ah_encrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ );
452 VLIB_REGISTER_NODE (ah6_encrypt_node) = {
453 .name = "ah6-encrypt",
454 .vector_size = sizeof (u32),
455 .format_trace = format_ah_encrypt_trace,
456 .type = VLIB_NODE_TYPE_INTERNAL,
458 .n_errors = ARRAY_LEN(ah_encrypt_error_strings),
459 .error_strings = ah_encrypt_error_strings,
461 .n_next_nodes = AH_ENCRYPT_N_NEXT,
463 [AH_ENCRYPT_NEXT_DROP] = "ip6-drop",
464 [AH_ENCRYPT_NEXT_HANDOFF] = "ah6-encrypt-handoff",
465 [AH_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "interface-output",
471 * fd.io coding-style-patch-verification: ON
474 * eval: (c-set-style "gnu")