blob: 629e7f031c4f4fd514a78160b6a25b9a69df94fd [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>
25
Klement Sekerabe5a5dd2018-10-09 16:05:48 +020026#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")
“mukeshyadav1984”430ac932017-11-23 02:39:33 -080031
32#define _(v, s) AH_DECRYPT_NEXT_##v,
33typedef enum
34{
35 foreach_ah_decrypt_next
36#undef _
37 AH_DECRYPT_N_NEXT,
38} ah_decrypt_next_t;
39
Klement Sekerabe5a5dd2018-10-09 16:05:48 +020040#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")
“mukeshyadav1984”430ac932017-11-23 02:39:33 -080045
46typedef enum
47{
48#define _(sym,str) AH_DECRYPT_ERROR_##sym,
49 foreach_ah_decrypt_error
50#undef _
51 AH_DECRYPT_N_ERROR,
52} ah_decrypt_error_t;
53
54static char *ah_decrypt_error_strings[] = {
55#define _(sym,string) string,
56 foreach_ah_decrypt_error
57#undef _
58};
59
60typedef struct
61{
62 ipsec_integ_alg_t integ_alg;
Neale Rannsde847272018-11-28 01:38:34 -080063 u32 seq_num;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -080064} ah_decrypt_trace_t;
65
66/* packet trace format function */
67static u8 *
68format_ah_decrypt_trace (u8 * s, va_list * args)
69{
70 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
71 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
72 ah_decrypt_trace_t *t = va_arg (*args, ah_decrypt_trace_t *);
73
Neale Rannsde847272018-11-28 01:38:34 -080074 s = format (s, "ah: integrity %U seq-num %d",
75 format_ipsec_integ_alg, t->integ_alg, t->seq_num);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -080076 return s;
77}
78
Klement Sekerabe5a5dd2018-10-09 16:05:48 +020079always_inline uword
80ah_decrypt_inline (vlib_main_t * vm,
81 vlib_node_runtime_t * node, vlib_frame_t * from_frame,
82 int is_ip6)
“mukeshyadav1984”430ac932017-11-23 02:39:33 -080083{
Neale Rannseba31ec2019-02-17 18:04:27 +000084 u32 n_left_from, *from, next_index, *to_next, thread_index;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -080085 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;
89 int icv_size = 0;
90
91 next_index = node->cached_next_index;
Neale Rannseba31ec2019-02-17 18:04:27 +000092 thread_index = vm->thread_index;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -080093
94 while (n_left_from > 0)
95 {
96 u32 n_left_to_next;
97
98 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
99
100 while (n_left_from > 0 && n_left_to_next > 0)
101 {
102 u32 i_bi0;
103 u32 next0;
104 vlib_buffer_t *i_b0;
105 ah_header_t *ah0;
106 ipsec_sa_t *sa0;
107 u32 sa_index0 = ~0;
108 u32 seq;
109 ip4_header_t *ih4 = 0, *oh4 = 0;
110 ip6_header_t *ih6 = 0, *oh6 = 0;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800111 u8 ip_hdr_size = 0;
112 u8 tos = 0;
113 u8 ttl = 0;
Klement Sekera611864f2018-09-26 11:19:00 +0200114 u32 ip_version_traffic_class_and_flow_label = 0;
115 u8 hop_limit = 0;
116 u8 nexthdr = 0;
117 u8 icv_padding_len = 0;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800118
119
120 i_bi0 = from[0];
121 from += 1;
122 n_left_from -= 1;
123 n_left_to_next -= 1;
124
125 next0 = AH_DECRYPT_NEXT_DROP;
126
127 i_b0 = vlib_get_buffer (vm, i_bi0);
128 to_next[0] = i_bi0;
129 to_next += 1;
130 ih4 = vlib_buffer_get_current (i_b0);
Klement Sekera611864f2018-09-26 11:19:00 +0200131 ih6 = vlib_buffer_get_current (i_b0);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800132 sa_index0 = vnet_buffer (i_b0)->ipsec.sad_index;
133 sa0 = pool_elt_at_index (im->sad, sa_index0);
134
Neale Rannseba31ec2019-02-17 18:04:27 +0000135 vlib_prefetch_combined_counter (&ipsec_sa_counters,
136 thread_index, sa_index0);
137
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200138 if (is_ip6)
Klement Sekera611864f2018-09-26 11:19:00 +0200139 {
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);
144 }
145 else
146 {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200147 ip_hdr_size = ip4_header_bytes (ih4);
148 ah0 = (ah_header_t *) ((u8 *) ih4 + ip_hdr_size);
Klement Sekera611864f2018-09-26 11:19:00 +0200149 }
150
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800151 seq = clib_host_to_net_u32 (ah0->seq_no);
Neale Rannsde847272018-11-28 01:38:34 -0800152
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800153 /* anti-replay check */
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800154 if (sa0->use_anti_replay)
155 {
156 int rv = 0;
157
158 if (PREDICT_TRUE (sa0->use_esn))
159 rv = esp_replay_check_esn (sa0, seq);
160 else
161 rv = esp_replay_check (sa0, seq);
162
163 if (PREDICT_FALSE (rv))
164 {
Klement Sekera2e02ba02018-11-30 14:37:03 +0100165 vlib_node_increment_counter (vm, node->node_index,
166 AH_DECRYPT_ERROR_REPLAY, 1);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800167 goto trace;
168 }
169 }
170
Neale Rannseba31ec2019-02-17 18:04:27 +0000171 vlib_increment_combined_counter
172 (&ipsec_sa_counters, thread_index, sa_index0,
173 1, i_b0->current_length);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800174
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800175 icv_size =
176 em->ipsec_proto_main_integ_algs[sa0->integ_alg].trunc_size;
177 if (PREDICT_TRUE (sa0->integ_alg != IPSEC_INTEG_ALG_NONE))
178 {
179 u8 sig[64];
180 u8 digest[64];
Dave Barachb7b92992018-10-17 10:38:51 -0400181 clib_memset (sig, 0, sizeof (sig));
182 clib_memset (digest, 0, sizeof (digest));
Klement Sekera611864f2018-09-26 11:19:00 +0200183 u8 *icv = ah0->auth_data;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800184 memcpy (digest, icv, icv_size);
Dave Barachb7b92992018-10-17 10:38:51 -0400185 clib_memset (icv, 0, icv_size);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800186
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200187 if (is_ip6)
Klement Sekera611864f2018-09-26 11:19:00 +0200188 {
189 ip_version_traffic_class_and_flow_label =
190 ih6->ip_version_traffic_class_and_flow_label;
191 hop_limit = ih6->hop_limit;
192 ih6->ip_version_traffic_class_and_flow_label = 0x60;
193 ih6->hop_limit = 0;
194 nexthdr = ah0->nexthdr;
195 icv_padding_len =
196 ah_calc_icv_padding_len (icv_size, 1 /* is_ipv6 */ );
197 }
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200198 else
199 {
200 tos = ih4->tos;
201 ttl = ih4->ttl;
202 ih4->tos = 0;
203 ih4->ttl = 0;
204 ih4->checksum = 0;
205 ih4->flags_and_fragment_offset = 0;
206 icv_padding_len =
207 ah_calc_icv_padding_len (icv_size, 0 /* is_ipv6 */ );
208 }
Neale Ranns8d7c5022019-02-06 01:41:05 -0800209 hmac_calc (sa0->integ_alg, sa0->integ_key.data,
210 sa0->integ_key.len, (u8 *) ih4, i_b0->current_length,
211 sig, sa0->use_esn, sa0->seq_hi);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800212
213 if (PREDICT_FALSE (memcmp (digest, sig, icv_size)))
214 {
Klement Sekera2e02ba02018-11-30 14:37:03 +0100215 vlib_node_increment_counter (vm, node->node_index,
216 AH_DECRYPT_ERROR_INTEG_ERROR,
217 1);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800218 goto trace;
219 }
220
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800221 if (PREDICT_TRUE (sa0->use_anti_replay))
222 {
223 if (PREDICT_TRUE (sa0->use_esn))
224 esp_replay_advance_esn (sa0, seq);
225 else
226 esp_replay_advance (sa0, seq);
227 }
228
229 }
230
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800231 vlib_buffer_advance (i_b0,
Klement Sekera611864f2018-09-26 11:19:00 +0200232 ip_hdr_size + sizeof (ah_header_t) + icv_size +
233 icv_padding_len);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800234 i_b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
235
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200236 if (PREDICT_TRUE (sa0->is_tunnel))
237 { /* tunnel mode */
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800238 if (PREDICT_TRUE (ah0->nexthdr == IP_PROTOCOL_IP_IN_IP))
239 next0 = AH_DECRYPT_NEXT_IP4_INPUT;
240 else if (ah0->nexthdr == IP_PROTOCOL_IPV6)
241 next0 = AH_DECRYPT_NEXT_IP6_INPUT;
242 else
243 {
Klement Sekera2e02ba02018-11-30 14:37:03 +0100244 vlib_node_increment_counter (vm, node->node_index,
245 AH_DECRYPT_ERROR_DECRYPTION_FAILED,
246 1);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800247 goto trace;
248 }
249 }
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800250 else
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200251 { /* transport mode */
252 if (is_ip6)
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800253 {
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800254 vlib_buffer_advance (i_b0, -sizeof (ip6_header_t));
255 oh6 = vlib_buffer_get_current (i_b0);
256 memmove (oh6, ih6, sizeof (ip6_header_t));
257
258 next0 = AH_DECRYPT_NEXT_IP6_INPUT;
Klement Sekera611864f2018-09-26 11:19:00 +0200259 oh6->protocol = nexthdr;
260 oh6->hop_limit = hop_limit;
261 oh6->ip_version_traffic_class_and_flow_label =
262 ip_version_traffic_class_and_flow_label;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800263 oh6->payload_length =
264 clib_host_to_net_u16 (vlib_buffer_length_in_chain
265 (vm, i_b0) - sizeof (ip6_header_t));
266 }
267 else
268 {
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));
272
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;
278 oh4->length =
279 clib_host_to_net_u16 (vlib_buffer_length_in_chain
280 (vm, i_b0));
281 oh4->ttl = ttl;
282 oh4->tos = tos;
283 oh4->checksum = ip4_header_checksum (oh4);
284 }
285 }
286
287 /* for IPSec-GRE tunnel next node is ipsec-gre-input */
288 if (PREDICT_FALSE
289 ((vnet_buffer (i_b0)->ipsec.flags) &
290 IPSEC_FLAG_IPSEC_GRE_TUNNEL))
291 next0 = AH_DECRYPT_NEXT_IPSEC_GRE_INPUT;
292
293
294 vnet_buffer (i_b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
295 trace:
296 if (PREDICT_FALSE (i_b0->flags & VLIB_BUFFER_IS_TRACED))
297 {
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;
Neale Rannsde847272018-11-28 01:38:34 -0800302 tr->seq_num = seq;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800303 }
304 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
305 n_left_to_next, i_bi0, next0);
306 }
307 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
308 }
Klement Sekera2e02ba02018-11-30 14:37:03 +0100309 vlib_node_increment_counter (vm, node->node_index, AH_DECRYPT_ERROR_RX_PKTS,
310 from_frame->n_vectors);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800311
312 return from_frame->n_vectors;
313}
314
Klement Sekerab8f35442018-10-29 13:38:19 +0100315VLIB_NODE_FN (ah4_decrypt_node) (vlib_main_t * vm,
316 vlib_node_runtime_t * node,
317 vlib_frame_t * from_frame)
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200318{
319 return ah_decrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ );
320}
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800321
322/* *INDENT-OFF* */
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200323VLIB_REGISTER_NODE (ah4_decrypt_node) = {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200324 .name = "ah4-decrypt",
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800325 .vector_size = sizeof (u32),
326 .format_trace = format_ah_decrypt_trace,
327 .type = VLIB_NODE_TYPE_INTERNAL,
328
329 .n_errors = ARRAY_LEN(ah_decrypt_error_strings),
330 .error_strings = ah_decrypt_error_strings,
331
332 .n_next_nodes = AH_DECRYPT_N_NEXT,
333 .next_nodes = {
334#define _(s,n) [AH_DECRYPT_NEXT_##s] = n,
335 foreach_ah_decrypt_next
336#undef _
337 },
338};
339/* *INDENT-ON* */
340
Klement Sekerab8f35442018-10-29 13:38:19 +0100341VLIB_NODE_FN (ah6_decrypt_node) (vlib_main_t * vm,
342 vlib_node_runtime_t * node,
343 vlib_frame_t * from_frame)
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200344{
345 return ah_decrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ );
346}
347
348/* *INDENT-OFF* */
349VLIB_REGISTER_NODE (ah6_decrypt_node) = {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200350 .name = "ah6-decrypt",
351 .vector_size = sizeof (u32),
352 .format_trace = format_ah_decrypt_trace,
353 .type = VLIB_NODE_TYPE_INTERNAL,
354
355 .n_errors = ARRAY_LEN(ah_decrypt_error_strings),
356 .error_strings = ah_decrypt_error_strings,
357
358 .n_next_nodes = AH_DECRYPT_N_NEXT,
359 .next_nodes = {
360#define _(s,n) [AH_DECRYPT_NEXT_##s] = n,
361 foreach_ah_decrypt_next
362#undef _
363 },
364};
365/* *INDENT-ON* */
366
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800367/*
368 * fd.io coding-style-patch-verification: ON
369 *
370 * Local Variables:
371 * eval: (c-set-style "gnu")
372 * End:
373 */