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") \
31 _ (IPSEC_GRE_INPUT, "ipsec-gre-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 _ (REPLAY, "SA replayed packet")
49 #define _(sym,str) AH_DECRYPT_ERROR_##sym,
50 foreach_ah_decrypt_error
55 static char *ah_decrypt_error_strings[] = {
56 #define _(sym,string) string,
57 foreach_ah_decrypt_error
63 ipsec_integ_alg_t integ_alg;
67 /* packet trace format function */
69 format_ah_decrypt_trace (u8 * s, va_list * args)
71 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
72 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
73 ah_decrypt_trace_t *t = va_arg (*args, ah_decrypt_trace_t *);
75 s = format (s, "ah: integrity %U seq-num %d",
76 format_ipsec_integ_alg, t->integ_alg, t->seq_num);
81 ah_decrypt_inline (vlib_main_t * vm,
82 vlib_node_runtime_t * node, vlib_frame_t * from_frame,
85 u32 n_left_from, *from, next_index, *to_next, thread_index;
86 ipsec_main_t *im = &ipsec_main;
87 from = vlib_frame_vector_args (from_frame);
88 n_left_from = from_frame->n_vectors;
91 next_index = node->cached_next_index;
92 thread_index = vm->thread_index;
94 while (n_left_from > 0)
98 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
100 while (n_left_from > 0 && n_left_to_next > 0)
109 ip4_header_t *ih4 = 0, *oh4 = 0;
110 ip6_header_t *ih6 = 0, *oh6 = 0;
114 u32 ip_version_traffic_class_and_flow_label = 0;
117 u8 icv_padding_len = 0;
125 next0 = AH_DECRYPT_NEXT_DROP;
127 i_b0 = vlib_get_buffer (vm, i_bi0);
130 ih4 = vlib_buffer_get_current (i_b0);
131 ih6 = vlib_buffer_get_current (i_b0);
132 sa_index0 = vnet_buffer (i_b0)->ipsec.sad_index;
133 sa0 = pool_elt_at_index (im->sad, sa_index0);
135 vlib_prefetch_combined_counter (&ipsec_sa_counters,
136 thread_index, sa_index0);
140 ip6_ext_header_t *prev = NULL;
141 ip6_ext_header_find_t (ih6, prev, ah0, IP_PROTOCOL_IPSEC_AH);
142 ip_hdr_size = sizeof (ip6_header_t);
143 ASSERT ((u8 *) ah0 - (u8 *) ih6 == ip_hdr_size);
147 ip_hdr_size = ip4_header_bytes (ih4);
148 ah0 = (ah_header_t *) ((u8 *) ih4 + ip_hdr_size);
151 seq = clib_host_to_net_u32 (ah0->seq_no);
153 /* anti-replay check */
154 if (sa0->use_anti_replay)
158 if (PREDICT_TRUE (sa0->use_esn))
159 rv = esp_replay_check_esn (sa0, seq);
161 rv = esp_replay_check (sa0, seq);
163 if (PREDICT_FALSE (rv))
165 vlib_node_increment_counter (vm, node->node_index,
166 AH_DECRYPT_ERROR_REPLAY, 1);
171 vlib_increment_combined_counter
172 (&ipsec_sa_counters, thread_index, sa_index0,
173 1, i_b0->current_length);
175 icv_size = sa0->integ_trunc_size;
176 if (PREDICT_TRUE (sa0->integ_alg != IPSEC_INTEG_ALG_NONE))
180 u8 *icv = ah0->auth_data;
181 memcpy (digest, icv, icv_size);
182 clib_memset (icv, 0, icv_size);
186 ip_version_traffic_class_and_flow_label =
187 ih6->ip_version_traffic_class_and_flow_label;
188 hop_limit = ih6->hop_limit;
189 ih6->ip_version_traffic_class_and_flow_label = 0x60;
191 nexthdr = ah0->nexthdr;
193 ah_calc_icv_padding_len (icv_size, 1 /* is_ipv6 */ );
202 ih4->flags_and_fragment_offset = 0;
204 ah_calc_icv_padding_len (icv_size, 0 /* is_ipv6 */ );
206 hmac_calc (vm, sa0, (u8 *) ih4, i_b0->current_length, sig);
208 if (PREDICT_FALSE (memcmp (digest, sig, icv_size)))
210 vlib_node_increment_counter (vm, node->node_index,
211 AH_DECRYPT_ERROR_INTEG_ERROR,
216 if (PREDICT_TRUE (sa0->use_anti_replay))
218 if (PREDICT_TRUE (sa0->use_esn))
219 esp_replay_advance_esn (sa0, seq);
221 esp_replay_advance (sa0, seq);
226 vlib_buffer_advance (i_b0,
227 ip_hdr_size + sizeof (ah_header_t) + icv_size +
229 i_b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
231 if (PREDICT_TRUE (sa0->is_tunnel))
233 if (PREDICT_TRUE (ah0->nexthdr == IP_PROTOCOL_IP_IN_IP))
234 next0 = AH_DECRYPT_NEXT_IP4_INPUT;
235 else if (ah0->nexthdr == IP_PROTOCOL_IPV6)
236 next0 = AH_DECRYPT_NEXT_IP6_INPUT;
239 vlib_node_increment_counter (vm, node->node_index,
240 AH_DECRYPT_ERROR_DECRYPTION_FAILED,
246 { /* transport mode */
249 vlib_buffer_advance (i_b0, -sizeof (ip6_header_t));
250 oh6 = vlib_buffer_get_current (i_b0);
251 memmove (oh6, ih6, sizeof (ip6_header_t));
253 next0 = AH_DECRYPT_NEXT_IP6_INPUT;
254 oh6->protocol = nexthdr;
255 oh6->hop_limit = hop_limit;
256 oh6->ip_version_traffic_class_and_flow_label =
257 ip_version_traffic_class_and_flow_label;
258 oh6->payload_length =
259 clib_host_to_net_u16 (vlib_buffer_length_in_chain
260 (vm, i_b0) - sizeof (ip6_header_t));
264 vlib_buffer_advance (i_b0, -sizeof (ip4_header_t));
265 oh4 = vlib_buffer_get_current (i_b0);
266 memmove (oh4, ih4, sizeof (ip4_header_t));
268 next0 = AH_DECRYPT_NEXT_IP4_INPUT;
269 oh4->ip_version_and_header_length = 0x45;
270 oh4->fragment_id = 0;
271 oh4->flags_and_fragment_offset = 0;
272 oh4->protocol = ah0->nexthdr;
274 clib_host_to_net_u16 (vlib_buffer_length_in_chain
278 oh4->checksum = ip4_header_checksum (oh4);
282 /* for IPSec-GRE tunnel next node is ipsec-gre-input */
284 ((vnet_buffer (i_b0)->ipsec.flags) &
285 IPSEC_FLAG_IPSEC_GRE_TUNNEL))
286 next0 = AH_DECRYPT_NEXT_IPSEC_GRE_INPUT;
289 vnet_buffer (i_b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
291 if (PREDICT_FALSE (i_b0->flags & VLIB_BUFFER_IS_TRACED))
293 i_b0->flags |= VLIB_BUFFER_IS_TRACED;
294 ah_decrypt_trace_t *tr =
295 vlib_add_trace (vm, node, i_b0, sizeof (*tr));
296 tr->integ_alg = sa0->integ_alg;
299 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
300 n_left_to_next, i_bi0, next0);
302 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
304 vlib_node_increment_counter (vm, node->node_index, AH_DECRYPT_ERROR_RX_PKTS,
305 from_frame->n_vectors);
307 return from_frame->n_vectors;
310 VLIB_NODE_FN (ah4_decrypt_node) (vlib_main_t * vm,
311 vlib_node_runtime_t * node,
312 vlib_frame_t * from_frame)
314 return ah_decrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ );
318 VLIB_REGISTER_NODE (ah4_decrypt_node) = {
319 .name = "ah4-decrypt",
320 .vector_size = sizeof (u32),
321 .format_trace = format_ah_decrypt_trace,
322 .type = VLIB_NODE_TYPE_INTERNAL,
324 .n_errors = ARRAY_LEN(ah_decrypt_error_strings),
325 .error_strings = ah_decrypt_error_strings,
327 .n_next_nodes = AH_DECRYPT_N_NEXT,
329 #define _(s,n) [AH_DECRYPT_NEXT_##s] = n,
330 foreach_ah_decrypt_next
336 VLIB_NODE_FN (ah6_decrypt_node) (vlib_main_t * vm,
337 vlib_node_runtime_t * node,
338 vlib_frame_t * from_frame)
340 return ah_decrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ );
344 VLIB_REGISTER_NODE (ah6_decrypt_node) = {
345 .name = "ah6-decrypt",
346 .vector_size = sizeof (u32),
347 .format_trace = format_ah_decrypt_trace,
348 .type = VLIB_NODE_TYPE_INTERNAL,
350 .n_errors = ARRAY_LEN(ah_decrypt_error_strings),
351 .error_strings = ah_decrypt_error_strings,
353 .n_next_nodes = AH_DECRYPT_N_NEXT,
355 #define _(s,n) [AH_DECRYPT_NEXT_##s] = n,
356 foreach_ah_decrypt_next
363 * fd.io coding-style-patch-verification: ON
366 * eval: (c-set-style "gnu")