blob: c8c89028f9d540fb41cc64d0dbcbd4eb9d1eeb18 [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{
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;
89 int icv_size = 0;
90
91 next_index = node->cached_next_index;
92
93 while (n_left_from > 0)
94 {
95 u32 n_left_to_next;
96
97 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
98
99 while (n_left_from > 0 && n_left_to_next > 0)
100 {
101 u32 i_bi0;
102 u32 next0;
103 vlib_buffer_t *i_b0;
104 ah_header_t *ah0;
105 ipsec_sa_t *sa0;
106 u32 sa_index0 = ~0;
107 u32 seq;
108 ip4_header_t *ih4 = 0, *oh4 = 0;
109 ip6_header_t *ih6 = 0, *oh6 = 0;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800110 u8 ip_hdr_size = 0;
111 u8 tos = 0;
112 u8 ttl = 0;
Klement Sekera611864f2018-09-26 11:19:00 +0200113 u32 ip_version_traffic_class_and_flow_label = 0;
114 u8 hop_limit = 0;
115 u8 nexthdr = 0;
116 u8 icv_padding_len = 0;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800117
118
119 i_bi0 = from[0];
120 from += 1;
121 n_left_from -= 1;
122 n_left_to_next -= 1;
123
124 next0 = AH_DECRYPT_NEXT_DROP;
125
126 i_b0 = vlib_get_buffer (vm, i_bi0);
127 to_next[0] = i_bi0;
128 to_next += 1;
129 ih4 = vlib_buffer_get_current (i_b0);
Klement Sekera611864f2018-09-26 11:19:00 +0200130 ih6 = vlib_buffer_get_current (i_b0);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800131 sa_index0 = vnet_buffer (i_b0)->ipsec.sad_index;
132 sa0 = pool_elt_at_index (im->sad, sa_index0);
133
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200134 if (is_ip6)
Klement Sekera611864f2018-09-26 11:19:00 +0200135 {
136 ip6_ext_header_t *prev = NULL;
137 ip6_ext_header_find_t (ih6, prev, ah0, IP_PROTOCOL_IPSEC_AH);
138 ip_hdr_size = sizeof (ip6_header_t);
139 ASSERT ((u8 *) ah0 - (u8 *) ih6 == ip_hdr_size);
140 }
141 else
142 {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200143 ip_hdr_size = ip4_header_bytes (ih4);
144 ah0 = (ah_header_t *) ((u8 *) ih4 + ip_hdr_size);
Klement Sekera611864f2018-09-26 11:19:00 +0200145 }
146
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800147 seq = clib_host_to_net_u32 (ah0->seq_no);
Neale Rannsde847272018-11-28 01:38:34 -0800148
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800149 /* anti-replay check */
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800150 if (sa0->use_anti_replay)
151 {
152 int rv = 0;
153
154 if (PREDICT_TRUE (sa0->use_esn))
155 rv = esp_replay_check_esn (sa0, seq);
156 else
157 rv = esp_replay_check (sa0, seq);
158
159 if (PREDICT_FALSE (rv))
160 {
Klement Sekera2e02ba02018-11-30 14:37:03 +0100161 vlib_node_increment_counter (vm, node->node_index,
162 AH_DECRYPT_ERROR_REPLAY, 1);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800163 goto trace;
164 }
165 }
166
167
168 sa0->total_data_size += i_b0->current_length;
169 icv_size =
170 em->ipsec_proto_main_integ_algs[sa0->integ_alg].trunc_size;
171 if (PREDICT_TRUE (sa0->integ_alg != IPSEC_INTEG_ALG_NONE))
172 {
173 u8 sig[64];
174 u8 digest[64];
Dave Barachb7b92992018-10-17 10:38:51 -0400175 clib_memset (sig, 0, sizeof (sig));
176 clib_memset (digest, 0, sizeof (digest));
Klement Sekera611864f2018-09-26 11:19:00 +0200177 u8 *icv = ah0->auth_data;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800178 memcpy (digest, icv, icv_size);
Dave Barachb7b92992018-10-17 10:38:51 -0400179 clib_memset (icv, 0, icv_size);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800180
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200181 if (is_ip6)
Klement Sekera611864f2018-09-26 11:19:00 +0200182 {
183 ip_version_traffic_class_and_flow_label =
184 ih6->ip_version_traffic_class_and_flow_label;
185 hop_limit = ih6->hop_limit;
186 ih6->ip_version_traffic_class_and_flow_label = 0x60;
187 ih6->hop_limit = 0;
188 nexthdr = ah0->nexthdr;
189 icv_padding_len =
190 ah_calc_icv_padding_len (icv_size, 1 /* is_ipv6 */ );
191 }
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200192 else
193 {
194 tos = ih4->tos;
195 ttl = ih4->ttl;
196 ih4->tos = 0;
197 ih4->ttl = 0;
198 ih4->checksum = 0;
199 ih4->flags_and_fragment_offset = 0;
200 icv_padding_len =
201 ah_calc_icv_padding_len (icv_size, 0 /* is_ipv6 */ );
202 }
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800203 hmac_calc (sa0->integ_alg, sa0->integ_key, sa0->integ_key_len,
204 (u8 *) ih4, i_b0->current_length, sig, sa0->use_esn,
205 sa0->seq_hi);
206
207 if (PREDICT_FALSE (memcmp (digest, sig, icv_size)))
208 {
Klement Sekera2e02ba02018-11-30 14:37:03 +0100209 vlib_node_increment_counter (vm, node->node_index,
210 AH_DECRYPT_ERROR_INTEG_ERROR,
211 1);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800212 goto trace;
213 }
214
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800215 if (PREDICT_TRUE (sa0->use_anti_replay))
216 {
217 if (PREDICT_TRUE (sa0->use_esn))
218 esp_replay_advance_esn (sa0, seq);
219 else
220 esp_replay_advance (sa0, seq);
221 }
222
223 }
224
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800225 vlib_buffer_advance (i_b0,
Klement Sekera611864f2018-09-26 11:19:00 +0200226 ip_hdr_size + sizeof (ah_header_t) + icv_size +
227 icv_padding_len);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800228 i_b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
229
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200230 if (PREDICT_TRUE (sa0->is_tunnel))
231 { /* tunnel mode */
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800232 if (PREDICT_TRUE (ah0->nexthdr == IP_PROTOCOL_IP_IN_IP))
233 next0 = AH_DECRYPT_NEXT_IP4_INPUT;
234 else if (ah0->nexthdr == IP_PROTOCOL_IPV6)
235 next0 = AH_DECRYPT_NEXT_IP6_INPUT;
236 else
237 {
Klement Sekera2e02ba02018-11-30 14:37:03 +0100238 vlib_node_increment_counter (vm, node->node_index,
239 AH_DECRYPT_ERROR_DECRYPTION_FAILED,
240 1);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800241 goto trace;
242 }
243 }
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800244 else
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200245 { /* transport mode */
246 if (is_ip6)
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800247 {
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800248 vlib_buffer_advance (i_b0, -sizeof (ip6_header_t));
249 oh6 = vlib_buffer_get_current (i_b0);
250 memmove (oh6, ih6, sizeof (ip6_header_t));
251
252 next0 = AH_DECRYPT_NEXT_IP6_INPUT;
Klement Sekera611864f2018-09-26 11:19:00 +0200253 oh6->protocol = nexthdr;
254 oh6->hop_limit = hop_limit;
255 oh6->ip_version_traffic_class_and_flow_label =
256 ip_version_traffic_class_and_flow_label;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800257 oh6->payload_length =
258 clib_host_to_net_u16 (vlib_buffer_length_in_chain
259 (vm, i_b0) - sizeof (ip6_header_t));
260 }
261 else
262 {
263 vlib_buffer_advance (i_b0, -sizeof (ip4_header_t));
264 oh4 = vlib_buffer_get_current (i_b0);
265 memmove (oh4, ih4, sizeof (ip4_header_t));
266
267 next0 = AH_DECRYPT_NEXT_IP4_INPUT;
268 oh4->ip_version_and_header_length = 0x45;
269 oh4->fragment_id = 0;
270 oh4->flags_and_fragment_offset = 0;
271 oh4->protocol = ah0->nexthdr;
272 oh4->length =
273 clib_host_to_net_u16 (vlib_buffer_length_in_chain
274 (vm, i_b0));
275 oh4->ttl = ttl;
276 oh4->tos = tos;
277 oh4->checksum = ip4_header_checksum (oh4);
278 }
279 }
280
281 /* for IPSec-GRE tunnel next node is ipsec-gre-input */
282 if (PREDICT_FALSE
283 ((vnet_buffer (i_b0)->ipsec.flags) &
284 IPSEC_FLAG_IPSEC_GRE_TUNNEL))
285 next0 = AH_DECRYPT_NEXT_IPSEC_GRE_INPUT;
286
287
288 vnet_buffer (i_b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
289 trace:
290 if (PREDICT_FALSE (i_b0->flags & VLIB_BUFFER_IS_TRACED))
291 {
292 i_b0->flags |= VLIB_BUFFER_IS_TRACED;
293 ah_decrypt_trace_t *tr =
294 vlib_add_trace (vm, node, i_b0, sizeof (*tr));
295 tr->integ_alg = sa0->integ_alg;
Neale Rannsde847272018-11-28 01:38:34 -0800296 tr->seq_num = seq;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800297 }
298 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
299 n_left_to_next, i_bi0, next0);
300 }
301 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
302 }
Klement Sekera2e02ba02018-11-30 14:37:03 +0100303 vlib_node_increment_counter (vm, node->node_index, AH_DECRYPT_ERROR_RX_PKTS,
304 from_frame->n_vectors);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800305
306 return from_frame->n_vectors;
307}
308
Klement Sekerab8f35442018-10-29 13:38:19 +0100309VLIB_NODE_FN (ah4_decrypt_node) (vlib_main_t * vm,
310 vlib_node_runtime_t * node,
311 vlib_frame_t * from_frame)
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200312{
313 return ah_decrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ );
314}
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800315
316/* *INDENT-OFF* */
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200317VLIB_REGISTER_NODE (ah4_decrypt_node) = {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200318 .name = "ah4-decrypt",
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800319 .vector_size = sizeof (u32),
320 .format_trace = format_ah_decrypt_trace,
321 .type = VLIB_NODE_TYPE_INTERNAL,
322
323 .n_errors = ARRAY_LEN(ah_decrypt_error_strings),
324 .error_strings = ah_decrypt_error_strings,
325
326 .n_next_nodes = AH_DECRYPT_N_NEXT,
327 .next_nodes = {
328#define _(s,n) [AH_DECRYPT_NEXT_##s] = n,
329 foreach_ah_decrypt_next
330#undef _
331 },
332};
333/* *INDENT-ON* */
334
Klement Sekerab8f35442018-10-29 13:38:19 +0100335VLIB_NODE_FN (ah6_decrypt_node) (vlib_main_t * vm,
336 vlib_node_runtime_t * node,
337 vlib_frame_t * from_frame)
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200338{
339 return ah_decrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ );
340}
341
342/* *INDENT-OFF* */
343VLIB_REGISTER_NODE (ah6_decrypt_node) = {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200344 .name = "ah6-decrypt",
345 .vector_size = sizeof (u32),
346 .format_trace = format_ah_decrypt_trace,
347 .type = VLIB_NODE_TYPE_INTERNAL,
348
349 .n_errors = ARRAY_LEN(ah_decrypt_error_strings),
350 .error_strings = ah_decrypt_error_strings,
351
352 .n_next_nodes = AH_DECRYPT_N_NEXT,
353 .next_nodes = {
354#define _(s,n) [AH_DECRYPT_NEXT_##s] = n,
355 foreach_ah_decrypt_next
356#undef _
357 },
358};
359/* *INDENT-ON* */
360
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800361/*
362 * fd.io coding-style-patch-verification: ON
363 *
364 * Local Variables:
365 * eval: (c-set-style "gnu")
366 * End:
367 */