blob: 0fc4f48571d4f3b3f24eace6de96baf55da5b15c [file] [log] [blame]
“mukeshyadav1984”430ac932017-11-23 02:39:33 -08001/*
2 * ah_decrypt.c : IPSec AH decrypt node
3 *
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:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 */
17
18#include <vnet/vnet.h>
19#include <vnet/api_errno.h>
20#include <vnet/ip/ip.h>
21
22#include <vnet/ipsec/ipsec.h>
23#include <vnet/ipsec/esp.h>
24#include <vnet/ipsec/ah.h>
Neale Ranns918c1612019-02-21 23:34:59 -080025#include <vnet/ipsec/ipsec_io.h>
“mukeshyadav1984”430ac932017-11-23 02:39:33 -080026
Klement Sekerabe5a5dd2018-10-09 16:05:48 +020027#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")
“mukeshyadav1984”430ac932017-11-23 02:39:33 -080032
33#define _(v, s) AH_DECRYPT_NEXT_##v,
34typedef enum
35{
36 foreach_ah_decrypt_next
37#undef _
38 AH_DECRYPT_N_NEXT,
39} ah_decrypt_next_t;
40
Klement Sekerabe5a5dd2018-10-09 16:05:48 +020041#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")
“mukeshyadav1984”430ac932017-11-23 02:39:33 -080046
47typedef enum
48{
49#define _(sym,str) AH_DECRYPT_ERROR_##sym,
50 foreach_ah_decrypt_error
51#undef _
52 AH_DECRYPT_N_ERROR,
53} ah_decrypt_error_t;
54
55static char *ah_decrypt_error_strings[] = {
56#define _(sym,string) string,
57 foreach_ah_decrypt_error
58#undef _
59};
60
61typedef struct
62{
63 ipsec_integ_alg_t integ_alg;
Neale Rannsde847272018-11-28 01:38:34 -080064 u32 seq_num;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -080065} ah_decrypt_trace_t;
66
67/* packet trace format function */
68static u8 *
69format_ah_decrypt_trace (u8 * s, va_list * args)
70{
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 *);
74
Neale Rannsde847272018-11-28 01:38:34 -080075 s = format (s, "ah: integrity %U seq-num %d",
76 format_ipsec_integ_alg, t->integ_alg, t->seq_num);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -080077 return s;
78}
79
Klement Sekerabe5a5dd2018-10-09 16:05:48 +020080always_inline uword
81ah_decrypt_inline (vlib_main_t * vm,
82 vlib_node_runtime_t * node, vlib_frame_t * from_frame,
83 int is_ip6)
“mukeshyadav1984”430ac932017-11-23 02:39:33 -080084{
Neale Rannseba31ec2019-02-17 18:04:27 +000085 u32 n_left_from, *from, next_index, *to_next, thread_index;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -080086 ipsec_main_t *im = &ipsec_main;
87 ipsec_proto_main_t *em = &ipsec_proto_main;
88 from = vlib_frame_vector_args (from_frame);
89 n_left_from = from_frame->n_vectors;
Kingwel Xie561d1ca2019-03-05 22:56:17 -050090 int icv_size;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -080091
92 next_index = node->cached_next_index;
Neale Rannseba31ec2019-02-17 18:04:27 +000093 thread_index = vm->thread_index;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -080094
95 while (n_left_from > 0)
96 {
97 u32 n_left_to_next;
98
99 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
100
101 while (n_left_from > 0 && n_left_to_next > 0)
102 {
103 u32 i_bi0;
104 u32 next0;
105 vlib_buffer_t *i_b0;
106 ah_header_t *ah0;
107 ipsec_sa_t *sa0;
108 u32 sa_index0 = ~0;
109 u32 seq;
110 ip4_header_t *ih4 = 0, *oh4 = 0;
111 ip6_header_t *ih6 = 0, *oh6 = 0;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800112 u8 ip_hdr_size = 0;
113 u8 tos = 0;
114 u8 ttl = 0;
Klement Sekera611864f2018-09-26 11:19:00 +0200115 u32 ip_version_traffic_class_and_flow_label = 0;
116 u8 hop_limit = 0;
117 u8 nexthdr = 0;
118 u8 icv_padding_len = 0;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800119
120
121 i_bi0 = from[0];
122 from += 1;
123 n_left_from -= 1;
124 n_left_to_next -= 1;
125
126 next0 = AH_DECRYPT_NEXT_DROP;
127
128 i_b0 = vlib_get_buffer (vm, i_bi0);
129 to_next[0] = i_bi0;
130 to_next += 1;
131 ih4 = vlib_buffer_get_current (i_b0);
Klement Sekera611864f2018-09-26 11:19:00 +0200132 ih6 = vlib_buffer_get_current (i_b0);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800133 sa_index0 = vnet_buffer (i_b0)->ipsec.sad_index;
134 sa0 = pool_elt_at_index (im->sad, sa_index0);
135
Neale Rannseba31ec2019-02-17 18:04:27 +0000136 vlib_prefetch_combined_counter (&ipsec_sa_counters,
137 thread_index, sa_index0);
138
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200139 if (is_ip6)
Klement Sekera611864f2018-09-26 11:19:00 +0200140 {
141 ip6_ext_header_t *prev = NULL;
142 ip6_ext_header_find_t (ih6, prev, ah0, IP_PROTOCOL_IPSEC_AH);
143 ip_hdr_size = sizeof (ip6_header_t);
144 ASSERT ((u8 *) ah0 - (u8 *) ih6 == ip_hdr_size);
145 }
146 else
147 {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200148 ip_hdr_size = ip4_header_bytes (ih4);
149 ah0 = (ah_header_t *) ((u8 *) ih4 + ip_hdr_size);
Klement Sekera611864f2018-09-26 11:19:00 +0200150 }
151
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800152 seq = clib_host_to_net_u32 (ah0->seq_no);
Neale Rannsde847272018-11-28 01:38:34 -0800153
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800154 /* anti-replay check */
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800155 if (sa0->use_anti_replay)
156 {
157 int rv = 0;
158
159 if (PREDICT_TRUE (sa0->use_esn))
160 rv = esp_replay_check_esn (sa0, seq);
161 else
162 rv = esp_replay_check (sa0, seq);
163
164 if (PREDICT_FALSE (rv))
165 {
Klement Sekera2e02ba02018-11-30 14:37:03 +0100166 vlib_node_increment_counter (vm, node->node_index,
167 AH_DECRYPT_ERROR_REPLAY, 1);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800168 goto trace;
169 }
170 }
171
Neale Rannseba31ec2019-02-17 18:04:27 +0000172 vlib_increment_combined_counter
173 (&ipsec_sa_counters, thread_index, sa_index0,
174 1, i_b0->current_length);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800175
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800176 icv_size =
177 em->ipsec_proto_main_integ_algs[sa0->integ_alg].trunc_size;
178 if (PREDICT_TRUE (sa0->integ_alg != IPSEC_INTEG_ALG_NONE))
179 {
180 u8 sig[64];
Kingwel Xie561d1ca2019-03-05 22:56:17 -0500181 u8 digest[icv_size];
Klement Sekera611864f2018-09-26 11:19:00 +0200182 u8 *icv = ah0->auth_data;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800183 memcpy (digest, icv, icv_size);
Dave Barachb7b92992018-10-17 10:38:51 -0400184 clib_memset (icv, 0, icv_size);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800185
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200186 if (is_ip6)
Klement Sekera611864f2018-09-26 11:19:00 +0200187 {
188 ip_version_traffic_class_and_flow_label =
189 ih6->ip_version_traffic_class_and_flow_label;
190 hop_limit = ih6->hop_limit;
191 ih6->ip_version_traffic_class_and_flow_label = 0x60;
192 ih6->hop_limit = 0;
193 nexthdr = ah0->nexthdr;
194 icv_padding_len =
195 ah_calc_icv_padding_len (icv_size, 1 /* is_ipv6 */ );
196 }
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200197 else
198 {
199 tos = ih4->tos;
200 ttl = ih4->ttl;
201 ih4->tos = 0;
202 ih4->ttl = 0;
203 ih4->checksum = 0;
204 ih4->flags_and_fragment_offset = 0;
205 icv_padding_len =
206 ah_calc_icv_padding_len (icv_size, 0 /* is_ipv6 */ );
207 }
Neale Ranns8d7c5022019-02-06 01:41:05 -0800208 hmac_calc (sa0->integ_alg, sa0->integ_key.data,
209 sa0->integ_key.len, (u8 *) ih4, i_b0->current_length,
210 sig, sa0->use_esn, sa0->seq_hi);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800211
212 if (PREDICT_FALSE (memcmp (digest, sig, icv_size)))
213 {
Klement Sekera2e02ba02018-11-30 14:37:03 +0100214 vlib_node_increment_counter (vm, node->node_index,
215 AH_DECRYPT_ERROR_INTEG_ERROR,
216 1);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800217 goto trace;
218 }
219
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800220 if (PREDICT_TRUE (sa0->use_anti_replay))
221 {
222 if (PREDICT_TRUE (sa0->use_esn))
223 esp_replay_advance_esn (sa0, seq);
224 else
225 esp_replay_advance (sa0, seq);
226 }
227
228 }
229
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800230 vlib_buffer_advance (i_b0,
Klement Sekera611864f2018-09-26 11:19:00 +0200231 ip_hdr_size + sizeof (ah_header_t) + icv_size +
232 icv_padding_len);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800233 i_b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
234
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200235 if (PREDICT_TRUE (sa0->is_tunnel))
236 { /* tunnel mode */
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800237 if (PREDICT_TRUE (ah0->nexthdr == IP_PROTOCOL_IP_IN_IP))
238 next0 = AH_DECRYPT_NEXT_IP4_INPUT;
239 else if (ah0->nexthdr == IP_PROTOCOL_IPV6)
240 next0 = AH_DECRYPT_NEXT_IP6_INPUT;
241 else
242 {
Klement Sekera2e02ba02018-11-30 14:37:03 +0100243 vlib_node_increment_counter (vm, node->node_index,
244 AH_DECRYPT_ERROR_DECRYPTION_FAILED,
245 1);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800246 goto trace;
247 }
248 }
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800249 else
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200250 { /* transport mode */
251 if (is_ip6)
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800252 {
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800253 vlib_buffer_advance (i_b0, -sizeof (ip6_header_t));
254 oh6 = vlib_buffer_get_current (i_b0);
255 memmove (oh6, ih6, sizeof (ip6_header_t));
256
257 next0 = AH_DECRYPT_NEXT_IP6_INPUT;
Klement Sekera611864f2018-09-26 11:19:00 +0200258 oh6->protocol = nexthdr;
259 oh6->hop_limit = hop_limit;
260 oh6->ip_version_traffic_class_and_flow_label =
261 ip_version_traffic_class_and_flow_label;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800262 oh6->payload_length =
263 clib_host_to_net_u16 (vlib_buffer_length_in_chain
264 (vm, i_b0) - sizeof (ip6_header_t));
265 }
266 else
267 {
268 vlib_buffer_advance (i_b0, -sizeof (ip4_header_t));
269 oh4 = vlib_buffer_get_current (i_b0);
270 memmove (oh4, ih4, sizeof (ip4_header_t));
271
272 next0 = AH_DECRYPT_NEXT_IP4_INPUT;
273 oh4->ip_version_and_header_length = 0x45;
274 oh4->fragment_id = 0;
275 oh4->flags_and_fragment_offset = 0;
276 oh4->protocol = ah0->nexthdr;
277 oh4->length =
278 clib_host_to_net_u16 (vlib_buffer_length_in_chain
279 (vm, i_b0));
280 oh4->ttl = ttl;
281 oh4->tos = tos;
282 oh4->checksum = ip4_header_checksum (oh4);
283 }
284 }
285
286 /* for IPSec-GRE tunnel next node is ipsec-gre-input */
287 if (PREDICT_FALSE
288 ((vnet_buffer (i_b0)->ipsec.flags) &
289 IPSEC_FLAG_IPSEC_GRE_TUNNEL))
290 next0 = AH_DECRYPT_NEXT_IPSEC_GRE_INPUT;
291
292
293 vnet_buffer (i_b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
294 trace:
295 if (PREDICT_FALSE (i_b0->flags & VLIB_BUFFER_IS_TRACED))
296 {
297 i_b0->flags |= VLIB_BUFFER_IS_TRACED;
298 ah_decrypt_trace_t *tr =
299 vlib_add_trace (vm, node, i_b0, sizeof (*tr));
300 tr->integ_alg = sa0->integ_alg;
Neale Rannsde847272018-11-28 01:38:34 -0800301 tr->seq_num = seq;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800302 }
303 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
304 n_left_to_next, i_bi0, next0);
305 }
306 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
307 }
Klement Sekera2e02ba02018-11-30 14:37:03 +0100308 vlib_node_increment_counter (vm, node->node_index, AH_DECRYPT_ERROR_RX_PKTS,
309 from_frame->n_vectors);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800310
311 return from_frame->n_vectors;
312}
313
Klement Sekerab8f35442018-10-29 13:38:19 +0100314VLIB_NODE_FN (ah4_decrypt_node) (vlib_main_t * vm,
315 vlib_node_runtime_t * node,
316 vlib_frame_t * from_frame)
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200317{
318 return ah_decrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ );
319}
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800320
321/* *INDENT-OFF* */
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200322VLIB_REGISTER_NODE (ah4_decrypt_node) = {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200323 .name = "ah4-decrypt",
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800324 .vector_size = sizeof (u32),
325 .format_trace = format_ah_decrypt_trace,
326 .type = VLIB_NODE_TYPE_INTERNAL,
327
328 .n_errors = ARRAY_LEN(ah_decrypt_error_strings),
329 .error_strings = ah_decrypt_error_strings,
330
331 .n_next_nodes = AH_DECRYPT_N_NEXT,
332 .next_nodes = {
333#define _(s,n) [AH_DECRYPT_NEXT_##s] = n,
334 foreach_ah_decrypt_next
335#undef _
336 },
337};
338/* *INDENT-ON* */
339
Klement Sekerab8f35442018-10-29 13:38:19 +0100340VLIB_NODE_FN (ah6_decrypt_node) (vlib_main_t * vm,
341 vlib_node_runtime_t * node,
342 vlib_frame_t * from_frame)
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200343{
344 return ah_decrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ );
345}
346
347/* *INDENT-OFF* */
348VLIB_REGISTER_NODE (ah6_decrypt_node) = {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200349 .name = "ah6-decrypt",
350 .vector_size = sizeof (u32),
351 .format_trace = format_ah_decrypt_trace,
352 .type = VLIB_NODE_TYPE_INTERNAL,
353
354 .n_errors = ARRAY_LEN(ah_decrypt_error_strings),
355 .error_strings = ah_decrypt_error_strings,
356
357 .n_next_nodes = AH_DECRYPT_N_NEXT,
358 .next_nodes = {
359#define _(s,n) [AH_DECRYPT_NEXT_##s] = n,
360 foreach_ah_decrypt_next
361#undef _
362 },
363};
364/* *INDENT-ON* */
365
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800366/*
367 * fd.io coding-style-patch-verification: ON
368 *
369 * Local Variables:
370 * eval: (c-set-style "gnu")
371 * End:
372 */