blob: abe2e6f5f80f4ca6be9e344b7f368b3b097b1e88 [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
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")
31
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
40
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)")
47
48
49typedef enum
50{
51#define _(sym,str) AH_DECRYPT_ERROR_##sym,
52 foreach_ah_decrypt_error
53#undef _
54 AH_DECRYPT_N_ERROR,
55} ah_decrypt_error_t;
56
57static char *ah_decrypt_error_strings[] = {
58#define _(sym,string) string,
59 foreach_ah_decrypt_error
60#undef _
61};
62
63typedef struct
64{
65 ipsec_integ_alg_t integ_alg;
66} ah_decrypt_trace_t;
67
68/* packet trace format function */
69static u8 *
70format_ah_decrypt_trace (u8 * s, va_list * args)
71{
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 *);
75
76 s = format (s, "ah: integrity %U", format_ipsec_integ_alg, t->integ_alg);
77 return s;
78}
79
80static uword
81ah_decrypt_node_fn (vlib_main_t * vm,
82 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
83{
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;
110 u8 tunnel_mode = 1;
111 u8 transport_ip6 = 0;
112 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
Klement Sekera611864f2018-09-26 11:19:00 +0200136 if ((ih4->ip_version_and_header_length & 0xF0) == 0x40)
137 {
138 ip_hdr_size = ip4_header_bytes (ih4);
139 ah0 = (ah_header_t *) ((u8 *) ih4 + ip_hdr_size);
140 }
141 else if ((ih4->ip_version_and_header_length & 0xF0) == 0x60)
142 {
143 ip6_ext_header_t *prev = NULL;
144 ip6_ext_header_find_t (ih6, prev, ah0, IP_PROTOCOL_IPSEC_AH);
145 ip_hdr_size = sizeof (ip6_header_t);
146 ASSERT ((u8 *) ah0 - (u8 *) ih6 == ip_hdr_size);
147 }
148 else
149 {
150 vlib_node_increment_counter (vm, ah_decrypt_node.index,
151 AH_DECRYPT_ERROR_NOT_IP, 1);
152 goto trace;
153 }
154
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800155 seq = clib_host_to_net_u32 (ah0->seq_no);
156 /* anti-replay check */
157 //TODO UT remaining
158 if (sa0->use_anti_replay)
159 {
160 int rv = 0;
161
162 if (PREDICT_TRUE (sa0->use_esn))
163 rv = esp_replay_check_esn (sa0, seq);
164 else
165 rv = esp_replay_check (sa0, seq);
166
167 if (PREDICT_FALSE (rv))
168 {
169 clib_warning ("anti-replay SPI %u seq %u", sa0->spi, seq);
170 vlib_node_increment_counter (vm, ah_decrypt_node.index,
171 AH_DECRYPT_ERROR_REPLAY, 1);
172 to_next[0] = i_bi0;
173 to_next += 1;
174 goto trace;
175 }
176 }
177
178
179 sa0->total_data_size += i_b0->current_length;
180 icv_size =
181 em->ipsec_proto_main_integ_algs[sa0->integ_alg].trunc_size;
182 if (PREDICT_TRUE (sa0->integ_alg != IPSEC_INTEG_ALG_NONE))
183 {
184 u8 sig[64];
185 u8 digest[64];
186 memset (sig, 0, sizeof (sig));
187 memset (digest, 0, sizeof (digest));
Klement Sekera611864f2018-09-26 11:19:00 +0200188 u8 *icv = ah0->auth_data;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800189 memcpy (digest, icv, icv_size);
190 memset (icv, 0, icv_size);
191
192 if ((ih4->ip_version_and_header_length & 0xF0) == 0x40)
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;
Klement Sekera611864f2018-09-26 11:19:00 +0200200 icv_padding_len =
201 ah_calc_icv_padding_len (icv_size, 0 /* is_ipv6 */ );
202 }
203 else
204 {
205 ip_version_traffic_class_and_flow_label =
206 ih6->ip_version_traffic_class_and_flow_label;
207 hop_limit = ih6->hop_limit;
208 ih6->ip_version_traffic_class_and_flow_label = 0x60;
209 ih6->hop_limit = 0;
210 nexthdr = ah0->nexthdr;
211 icv_padding_len =
212 ah_calc_icv_padding_len (icv_size, 1 /* is_ipv6 */ );
213 }
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800214 hmac_calc (sa0->integ_alg, sa0->integ_key, sa0->integ_key_len,
215 (u8 *) ih4, i_b0->current_length, sig, sa0->use_esn,
216 sa0->seq_hi);
217
218 if (PREDICT_FALSE (memcmp (digest, sig, icv_size)))
219 {
220 vlib_node_increment_counter (vm, ah_decrypt_node.index,
221 AH_DECRYPT_ERROR_INTEG_ERROR,
222 1);
223 to_next[0] = i_bi0;
224 to_next += 1;
225 goto trace;
226 }
227
228 //TODO UT remaining
229 if (PREDICT_TRUE (sa0->use_anti_replay))
230 {
231 if (PREDICT_TRUE (sa0->use_esn))
232 esp_replay_advance_esn (sa0, seq);
233 else
234 esp_replay_advance (sa0, seq);
235 }
236
237 }
238
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800239 vlib_buffer_advance (i_b0,
Klement Sekera611864f2018-09-26 11:19:00 +0200240 ip_hdr_size + sizeof (ah_header_t) + icv_size +
241 icv_padding_len);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800242 i_b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
243
244 /* transport mode */
245 if (PREDICT_FALSE (!sa0->is_tunnel && !sa0->is_tunnel_ip6))
246 {
247 tunnel_mode = 0;
248
249 if (PREDICT_TRUE
250 ((ih4->ip_version_and_header_length & 0xF0) != 0x40))
251 {
252 if (PREDICT_TRUE
253 ((ih4->ip_version_and_header_length & 0xF0) == 0x60))
254 transport_ip6 = 1;
255 else
256 {
257 clib_warning ("next header: 0x%x", ah0->nexthdr);
258 vlib_node_increment_counter (vm, ah_decrypt_node.index,
259 AH_DECRYPT_ERROR_NOT_IP,
260 1);
261 goto trace;
262 }
263 }
264 }
265
266 if (PREDICT_TRUE (tunnel_mode))
267 {
268 if (PREDICT_TRUE (ah0->nexthdr == IP_PROTOCOL_IP_IN_IP))
269 next0 = AH_DECRYPT_NEXT_IP4_INPUT;
270 else if (ah0->nexthdr == IP_PROTOCOL_IPV6)
271 next0 = AH_DECRYPT_NEXT_IP6_INPUT;
272 else
273 {
274 clib_warning ("next header: 0x%x", ah0->nexthdr);
275 vlib_node_increment_counter (vm, ah_decrypt_node.index,
276 AH_DECRYPT_ERROR_DECRYPTION_FAILED,
277 1);
278 goto trace;
279 }
280 }
281 /* transport mode */
282 else
283 {
284 if (PREDICT_FALSE (transport_ip6))
285 {
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800286 vlib_buffer_advance (i_b0, -sizeof (ip6_header_t));
287 oh6 = vlib_buffer_get_current (i_b0);
288 memmove (oh6, ih6, sizeof (ip6_header_t));
289
290 next0 = AH_DECRYPT_NEXT_IP6_INPUT;
Klement Sekera611864f2018-09-26 11:19:00 +0200291 oh6->protocol = nexthdr;
292 oh6->hop_limit = hop_limit;
293 oh6->ip_version_traffic_class_and_flow_label =
294 ip_version_traffic_class_and_flow_label;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800295 oh6->payload_length =
296 clib_host_to_net_u16 (vlib_buffer_length_in_chain
297 (vm, i_b0) - sizeof (ip6_header_t));
298 }
299 else
300 {
301 vlib_buffer_advance (i_b0, -sizeof (ip4_header_t));
302 oh4 = vlib_buffer_get_current (i_b0);
303 memmove (oh4, ih4, sizeof (ip4_header_t));
304
305 next0 = AH_DECRYPT_NEXT_IP4_INPUT;
306 oh4->ip_version_and_header_length = 0x45;
307 oh4->fragment_id = 0;
308 oh4->flags_and_fragment_offset = 0;
309 oh4->protocol = ah0->nexthdr;
310 oh4->length =
311 clib_host_to_net_u16 (vlib_buffer_length_in_chain
312 (vm, i_b0));
313 oh4->ttl = ttl;
314 oh4->tos = tos;
315 oh4->checksum = ip4_header_checksum (oh4);
316 }
317 }
318
319 /* for IPSec-GRE tunnel next node is ipsec-gre-input */
320 if (PREDICT_FALSE
321 ((vnet_buffer (i_b0)->ipsec.flags) &
322 IPSEC_FLAG_IPSEC_GRE_TUNNEL))
323 next0 = AH_DECRYPT_NEXT_IPSEC_GRE_INPUT;
324
325
326 vnet_buffer (i_b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
327 trace:
328 if (PREDICT_FALSE (i_b0->flags & VLIB_BUFFER_IS_TRACED))
329 {
330 i_b0->flags |= VLIB_BUFFER_IS_TRACED;
331 ah_decrypt_trace_t *tr =
332 vlib_add_trace (vm, node, i_b0, sizeof (*tr));
333 tr->integ_alg = sa0->integ_alg;
334 }
335 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
336 n_left_to_next, i_bi0, next0);
337 }
338 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
339 }
340 vlib_node_increment_counter (vm, ah_decrypt_node.index,
341 AH_DECRYPT_ERROR_RX_PKTS,
342 from_frame->n_vectors);
343
344 return from_frame->n_vectors;
345}
346
347
348/* *INDENT-OFF* */
349VLIB_REGISTER_NODE (ah_decrypt_node) = {
350 .function = ah_decrypt_node_fn,
351 .name = "ah-decrypt",
352 .vector_size = sizeof (u32),
353 .format_trace = format_ah_decrypt_trace,
354 .type = VLIB_NODE_TYPE_INTERNAL,
355
356 .n_errors = ARRAY_LEN(ah_decrypt_error_strings),
357 .error_strings = ah_decrypt_error_strings,
358
359 .n_next_nodes = AH_DECRYPT_N_NEXT,
360 .next_nodes = {
361#define _(s,n) [AH_DECRYPT_NEXT_##s] = n,
362 foreach_ah_decrypt_next
363#undef _
364 },
365};
366/* *INDENT-ON* */
367
368VLIB_NODE_FUNCTION_MULTIARCH (ah_decrypt_node, ah_decrypt_node_fn)
369/*
370 * fd.io coding-style-patch-verification: ON
371 *
372 * Local Variables:
373 * eval: (c-set-style "gnu")
374 * End:
375 */