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>
26 #define foreach_ah_decrypt_next \
27 _ (DROP, "error-drop") \
28 _ (IP4_INPUT, "ip4-input") \
29 _ (IP6_INPUT, "ip6-input") \
30 _ (IPSEC_GRE_INPUT, "ipsec-gre-input")
32 #define _(v, s) AH_DECRYPT_NEXT_##v,
35 foreach_ah_decrypt_next
40 #define foreach_ah_decrypt_error \
41 _ (RX_PKTS, "AH pkts received") \
42 _ (DECRYPTION_FAILED, "AH decryption failed") \
43 _ (INTEG_ERROR, "Integrity check failed") \
44 _ (REPLAY, "SA replayed packet")
48 #define _(sym,str) AH_DECRYPT_ERROR_##sym,
49 foreach_ah_decrypt_error
54 static char *ah_decrypt_error_strings[] = {
55 #define _(sym,string) string,
56 foreach_ah_decrypt_error
62 ipsec_integ_alg_t integ_alg;
65 /* packet trace format function */
67 format_ah_decrypt_trace (u8 * s, va_list * args)
69 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
70 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
71 ah_decrypt_trace_t *t = va_arg (*args, ah_decrypt_trace_t *);
73 s = format (s, "ah: integrity %U", format_ipsec_integ_alg, t->integ_alg);
78 ah_decrypt_inline (vlib_main_t * vm,
79 vlib_node_runtime_t * node, vlib_frame_t * from_frame,
82 u32 n_left_from, *from, next_index, *to_next;
83 ipsec_main_t *im = &ipsec_main;
84 ipsec_proto_main_t *em = &ipsec_proto_main;
85 from = vlib_frame_vector_args (from_frame);
86 n_left_from = from_frame->n_vectors;
89 next_index = node->cached_next_index;
91 while (n_left_from > 0)
95 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
97 while (n_left_from > 0 && n_left_to_next > 0)
106 ip4_header_t *ih4 = 0, *oh4 = 0;
107 ip6_header_t *ih6 = 0, *oh6 = 0;
111 u32 ip_version_traffic_class_and_flow_label = 0;
114 u8 icv_padding_len = 0;
122 next0 = AH_DECRYPT_NEXT_DROP;
124 i_b0 = vlib_get_buffer (vm, i_bi0);
127 ih4 = vlib_buffer_get_current (i_b0);
128 ih6 = vlib_buffer_get_current (i_b0);
129 sa_index0 = vnet_buffer (i_b0)->ipsec.sad_index;
130 sa0 = pool_elt_at_index (im->sad, sa_index0);
134 ip6_ext_header_t *prev = NULL;
135 ip6_ext_header_find_t (ih6, prev, ah0, IP_PROTOCOL_IPSEC_AH);
136 ip_hdr_size = sizeof (ip6_header_t);
137 ASSERT ((u8 *) ah0 - (u8 *) ih6 == ip_hdr_size);
141 ip_hdr_size = ip4_header_bytes (ih4);
142 ah0 = (ah_header_t *) ((u8 *) ih4 + ip_hdr_size);
145 seq = clib_host_to_net_u32 (ah0->seq_no);
146 /* anti-replay check */
148 if (sa0->use_anti_replay)
152 if (PREDICT_TRUE (sa0->use_esn))
153 rv = esp_replay_check_esn (sa0, seq);
155 rv = esp_replay_check (sa0, seq);
157 if (PREDICT_FALSE (rv))
160 vlib_node_increment_counter (vm,
161 ah6_decrypt_node.index,
162 AH_DECRYPT_ERROR_REPLAY, 1);
164 vlib_node_increment_counter (vm,
165 ah4_decrypt_node.index,
166 AH_DECRYPT_ERROR_REPLAY, 1);
172 sa0->total_data_size += i_b0->current_length;
174 em->ipsec_proto_main_integ_algs[sa0->integ_alg].trunc_size;
175 if (PREDICT_TRUE (sa0->integ_alg != IPSEC_INTEG_ALG_NONE))
179 clib_memset (sig, 0, sizeof (sig));
180 clib_memset (digest, 0, sizeof (digest));
181 u8 *icv = ah0->auth_data;
182 memcpy (digest, icv, icv_size);
183 clib_memset (icv, 0, icv_size);
187 ip_version_traffic_class_and_flow_label =
188 ih6->ip_version_traffic_class_and_flow_label;
189 hop_limit = ih6->hop_limit;
190 ih6->ip_version_traffic_class_and_flow_label = 0x60;
192 nexthdr = ah0->nexthdr;
194 ah_calc_icv_padding_len (icv_size, 1 /* is_ipv6 */ );
203 ih4->flags_and_fragment_offset = 0;
205 ah_calc_icv_padding_len (icv_size, 0 /* is_ipv6 */ );
207 hmac_calc (sa0->integ_alg, sa0->integ_key, sa0->integ_key_len,
208 (u8 *) ih4, i_b0->current_length, sig, sa0->use_esn,
211 if (PREDICT_FALSE (memcmp (digest, sig, icv_size)))
214 vlib_node_increment_counter (vm,
215 ah6_decrypt_node.index,
216 AH_DECRYPT_ERROR_INTEG_ERROR,
219 vlib_node_increment_counter (vm,
220 ah4_decrypt_node.index,
221 AH_DECRYPT_ERROR_INTEG_ERROR,
227 if (PREDICT_TRUE (sa0->use_anti_replay))
229 if (PREDICT_TRUE (sa0->use_esn))
230 esp_replay_advance_esn (sa0, seq);
232 esp_replay_advance (sa0, seq);
237 vlib_buffer_advance (i_b0,
238 ip_hdr_size + sizeof (ah_header_t) + icv_size +
240 i_b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
242 if (PREDICT_TRUE (sa0->is_tunnel))
244 if (PREDICT_TRUE (ah0->nexthdr == IP_PROTOCOL_IP_IN_IP))
245 next0 = AH_DECRYPT_NEXT_IP4_INPUT;
246 else if (ah0->nexthdr == IP_PROTOCOL_IPV6)
247 next0 = AH_DECRYPT_NEXT_IP6_INPUT;
250 clib_warning ("next header: 0x%x", ah0->nexthdr);
252 vlib_node_increment_counter (vm,
253 ah6_decrypt_node.index,
254 AH_DECRYPT_ERROR_DECRYPTION_FAILED,
257 vlib_node_increment_counter (vm,
258 ah4_decrypt_node.index,
259 AH_DECRYPT_ERROR_DECRYPTION_FAILED,
265 { /* transport mode */
268 vlib_buffer_advance (i_b0, -sizeof (ip6_header_t));
269 oh6 = vlib_buffer_get_current (i_b0);
270 memmove (oh6, ih6, sizeof (ip6_header_t));
272 next0 = AH_DECRYPT_NEXT_IP6_INPUT;
273 oh6->protocol = nexthdr;
274 oh6->hop_limit = hop_limit;
275 oh6->ip_version_traffic_class_and_flow_label =
276 ip_version_traffic_class_and_flow_label;
277 oh6->payload_length =
278 clib_host_to_net_u16 (vlib_buffer_length_in_chain
279 (vm, i_b0) - sizeof (ip6_header_t));
283 vlib_buffer_advance (i_b0, -sizeof (ip4_header_t));
284 oh4 = vlib_buffer_get_current (i_b0);
285 memmove (oh4, ih4, sizeof (ip4_header_t));
287 next0 = AH_DECRYPT_NEXT_IP4_INPUT;
288 oh4->ip_version_and_header_length = 0x45;
289 oh4->fragment_id = 0;
290 oh4->flags_and_fragment_offset = 0;
291 oh4->protocol = ah0->nexthdr;
293 clib_host_to_net_u16 (vlib_buffer_length_in_chain
297 oh4->checksum = ip4_header_checksum (oh4);
301 /* for IPSec-GRE tunnel next node is ipsec-gre-input */
303 ((vnet_buffer (i_b0)->ipsec.flags) &
304 IPSEC_FLAG_IPSEC_GRE_TUNNEL))
305 next0 = AH_DECRYPT_NEXT_IPSEC_GRE_INPUT;
308 vnet_buffer (i_b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
310 if (PREDICT_FALSE (i_b0->flags & VLIB_BUFFER_IS_TRACED))
312 i_b0->flags |= VLIB_BUFFER_IS_TRACED;
313 ah_decrypt_trace_t *tr =
314 vlib_add_trace (vm, node, i_b0, sizeof (*tr));
315 tr->integ_alg = sa0->integ_alg;
317 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
318 n_left_to_next, i_bi0, next0);
320 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
323 vlib_node_increment_counter (vm, ah6_decrypt_node.index,
324 AH_DECRYPT_ERROR_RX_PKTS,
325 from_frame->n_vectors);
327 vlib_node_increment_counter (vm, ah4_decrypt_node.index,
328 AH_DECRYPT_ERROR_RX_PKTS,
329 from_frame->n_vectors);
331 return from_frame->n_vectors;
334 VLIB_NODE_FN (ah4_decrypt_node) (vlib_main_t * vm,
335 vlib_node_runtime_t * node,
336 vlib_frame_t * from_frame)
338 return ah_decrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ );
342 VLIB_REGISTER_NODE (ah4_decrypt_node) = {
343 .name = "ah4-decrypt",
344 .vector_size = sizeof (u32),
345 .format_trace = format_ah_decrypt_trace,
346 .type = VLIB_NODE_TYPE_INTERNAL,
348 .n_errors = ARRAY_LEN(ah_decrypt_error_strings),
349 .error_strings = ah_decrypt_error_strings,
351 .n_next_nodes = AH_DECRYPT_N_NEXT,
353 #define _(s,n) [AH_DECRYPT_NEXT_##s] = n,
354 foreach_ah_decrypt_next
360 VLIB_NODE_FN (ah6_decrypt_node) (vlib_main_t * vm,
361 vlib_node_runtime_t * node,
362 vlib_frame_t * from_frame)
364 return ah_decrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ );
368 VLIB_REGISTER_NODE (ah6_decrypt_node) = {
369 .name = "ah6-decrypt",
370 .vector_size = sizeof (u32),
371 .format_trace = format_ah_decrypt_trace,
372 .type = VLIB_NODE_TYPE_INTERNAL,
374 .n_errors = ARRAY_LEN(ah_decrypt_error_strings),
375 .error_strings = ah_decrypt_error_strings,
377 .n_next_nodes = AH_DECRYPT_N_NEXT,
379 #define _(s,n) [AH_DECRYPT_NEXT_##s] = n,
380 foreach_ah_decrypt_next
387 * fd.io coding-style-patch-verification: ON
390 * eval: (c-set-style "gnu")