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
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") \
46 _(NOT_IP, "Not IP packet (dropped)")
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;
68 /* packet trace format function */
70 format_ah_decrypt_trace (u8 * s, va_list * args)
72 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
73 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
74 ah_decrypt_trace_t *t = va_arg (*args, ah_decrypt_trace_t *);
76 s = format (s, "ah: integrity %U", format_ipsec_integ_alg, t->integ_alg);
81 ah_decrypt_node_fn (vlib_main_t * vm,
82 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
84 u32 n_left_from, *from, next_index, *to_next;
85 ipsec_main_t *im = &ipsec_main;
86 ipsec_proto_main_t *em = &ipsec_proto_main;
87 from = vlib_frame_vector_args (from_frame);
88 n_left_from = from_frame->n_vectors;
91 next_index = node->cached_next_index;
93 while (n_left_from > 0)
97 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
99 while (n_left_from > 0 && n_left_to_next > 0)
108 ip4_header_t *ih4 = 0, *oh4 = 0;
109 ip6_header_t *ih6 = 0, *oh6 = 0;
111 u8 transport_ip6 = 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 ip_hdr_size = ip4_header_bytes (ih4);
129 ah0 = (ah_header_t *) ((u8 *) ih4 + ip_hdr_size);
131 sa_index0 = vnet_buffer (i_b0)->ipsec.sad_index;
132 sa0 = pool_elt_at_index (im->sad, sa_index0);
134 seq = clib_host_to_net_u32 (ah0->seq_no);
135 /* anti-replay check */
137 if (sa0->use_anti_replay)
141 if (PREDICT_TRUE (sa0->use_esn))
142 rv = esp_replay_check_esn (sa0, seq);
144 rv = esp_replay_check (sa0, seq);
146 if (PREDICT_FALSE (rv))
148 clib_warning ("anti-replay SPI %u seq %u", sa0->spi, seq);
149 vlib_node_increment_counter (vm, ah_decrypt_node.index,
150 AH_DECRYPT_ERROR_REPLAY, 1);
158 sa0->total_data_size += i_b0->current_length;
160 em->ipsec_proto_main_integ_algs[sa0->integ_alg].trunc_size;
161 if (PREDICT_TRUE (sa0->integ_alg != IPSEC_INTEG_ALG_NONE))
165 memset (sig, 0, sizeof (sig));
166 memset (digest, 0, sizeof (digest));
168 vlib_buffer_get_current (i_b0) + ip_hdr_size +
169 sizeof (ah_header_t);
170 memcpy (digest, icv, icv_size);
171 memset (icv, 0, icv_size);
173 if ((ih4->ip_version_and_header_length & 0xF0) == 0x40)
180 ih4->flags_and_fragment_offset = 0;
181 } //TODO else part for IPv6
182 hmac_calc (sa0->integ_alg, sa0->integ_key, sa0->integ_key_len,
183 (u8 *) ih4, i_b0->current_length, sig, sa0->use_esn,
186 if (PREDICT_FALSE (memcmp (digest, sig, icv_size)))
188 vlib_node_increment_counter (vm, ah_decrypt_node.index,
189 AH_DECRYPT_ERROR_INTEG_ERROR,
197 if (PREDICT_TRUE (sa0->use_anti_replay))
199 if (PREDICT_TRUE (sa0->use_esn))
200 esp_replay_advance_esn (sa0, seq);
202 esp_replay_advance (sa0, seq);
208 vlib_buffer_advance (i_b0,
209 ip_hdr_size + sizeof (ah_header_t) + icv_size);
210 i_b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
213 if (PREDICT_FALSE (!sa0->is_tunnel && !sa0->is_tunnel_ip6))
218 ((ih4->ip_version_and_header_length & 0xF0) != 0x40))
221 ((ih4->ip_version_and_header_length & 0xF0) == 0x60))
225 clib_warning ("next header: 0x%x", ah0->nexthdr);
226 vlib_node_increment_counter (vm, ah_decrypt_node.index,
227 AH_DECRYPT_ERROR_NOT_IP,
234 if (PREDICT_TRUE (tunnel_mode))
236 if (PREDICT_TRUE (ah0->nexthdr == IP_PROTOCOL_IP_IN_IP))
237 next0 = AH_DECRYPT_NEXT_IP4_INPUT;
238 else if (ah0->nexthdr == IP_PROTOCOL_IPV6)
239 next0 = AH_DECRYPT_NEXT_IP6_INPUT;
242 clib_warning ("next header: 0x%x", ah0->nexthdr);
243 vlib_node_increment_counter (vm, ah_decrypt_node.index,
244 AH_DECRYPT_ERROR_DECRYPTION_FAILED,
252 if (PREDICT_FALSE (transport_ip6))
255 (ip6_header_t *) (i_b0->data +
256 sizeof (ethernet_header_t));
257 vlib_buffer_advance (i_b0, -sizeof (ip6_header_t));
258 oh6 = vlib_buffer_get_current (i_b0);
259 memmove (oh6, ih6, sizeof (ip6_header_t));
261 next0 = AH_DECRYPT_NEXT_IP6_INPUT;
262 oh6->protocol = ah0->nexthdr;
263 oh6->payload_length =
264 clib_host_to_net_u16 (vlib_buffer_length_in_chain
265 (vm, i_b0) - sizeof (ip6_header_t));
269 vlib_buffer_advance (i_b0, -sizeof (ip4_header_t));
270 oh4 = vlib_buffer_get_current (i_b0);
271 memmove (oh4, ih4, sizeof (ip4_header_t));
273 next0 = AH_DECRYPT_NEXT_IP4_INPUT;
274 oh4->ip_version_and_header_length = 0x45;
275 oh4->fragment_id = 0;
276 oh4->flags_and_fragment_offset = 0;
277 oh4->protocol = ah0->nexthdr;
279 clib_host_to_net_u16 (vlib_buffer_length_in_chain
283 oh4->checksum = ip4_header_checksum (oh4);
287 /* for IPSec-GRE tunnel next node is ipsec-gre-input */
289 ((vnet_buffer (i_b0)->ipsec.flags) &
290 IPSEC_FLAG_IPSEC_GRE_TUNNEL))
291 next0 = AH_DECRYPT_NEXT_IPSEC_GRE_INPUT;
294 vnet_buffer (i_b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
296 if (PREDICT_FALSE (i_b0->flags & VLIB_BUFFER_IS_TRACED))
298 i_b0->flags |= VLIB_BUFFER_IS_TRACED;
299 ah_decrypt_trace_t *tr =
300 vlib_add_trace (vm, node, i_b0, sizeof (*tr));
301 tr->integ_alg = sa0->integ_alg;
303 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
304 n_left_to_next, i_bi0, next0);
306 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
308 vlib_node_increment_counter (vm, ah_decrypt_node.index,
309 AH_DECRYPT_ERROR_RX_PKTS,
310 from_frame->n_vectors);
312 return from_frame->n_vectors;
317 VLIB_REGISTER_NODE (ah_decrypt_node) = {
318 .function = ah_decrypt_node_fn,
319 .name = "ah-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_FUNCTION_MULTIARCH (ah_decrypt_node, ah_decrypt_node_fn)
338 * fd.io coding-style-patch-verification: ON
341 * eval: (c-set-style "gnu")