c11 safe string handling support
[vpp.git] / src / vnet / ipsec / ah_decrypt.c
1 /*
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,
33 typedef enum
34 {
35   foreach_ah_decrypt_next
36 #undef _
37     AH_DECRYPT_N_NEXT,
38 } ah_decrypt_next_t;
39
40 #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")
45
46 typedef 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
54 static char *ah_decrypt_error_strings[] = {
55 #define _(sym,string) string,
56   foreach_ah_decrypt_error
57 #undef _
58 };
59
60 typedef struct
61 {
62   ipsec_integ_alg_t integ_alg;
63 } ah_decrypt_trace_t;
64
65 /* packet trace format function */
66 static u8 *
67 format_ah_decrypt_trace (u8 * s, va_list * args)
68 {
69   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
70   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
71   ah_decrypt_trace_t *t = va_arg (*args, ah_decrypt_trace_t *);
72
73   s = format (s, "ah: integrity %U", format_ipsec_integ_alg, t->integ_alg);
74   return s;
75 }
76
77 always_inline uword
78 ah_decrypt_inline (vlib_main_t * vm,
79                    vlib_node_runtime_t * node, vlib_frame_t * from_frame,
80                    int is_ip6)
81 {
82   u32 n_left_from, *from, next_index, *to_next;
83   ipsec_main_t *im = &ipsec_main;
84   ipsec_proto_main_t *em = &ipsec_proto_main;
85   from = vlib_frame_vector_args (from_frame);
86   n_left_from = from_frame->n_vectors;
87   int icv_size = 0;
88
89   next_index = node->cached_next_index;
90
91   while (n_left_from > 0)
92     {
93       u32 n_left_to_next;
94
95       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
96
97       while (n_left_from > 0 && n_left_to_next > 0)
98         {
99           u32 i_bi0;
100           u32 next0;
101           vlib_buffer_t *i_b0;
102           ah_header_t *ah0;
103           ipsec_sa_t *sa0;
104           u32 sa_index0 = ~0;
105           u32 seq;
106           ip4_header_t *ih4 = 0, *oh4 = 0;
107           ip6_header_t *ih6 = 0, *oh6 = 0;
108           u8 ip_hdr_size = 0;
109           u8 tos = 0;
110           u8 ttl = 0;
111           u32 ip_version_traffic_class_and_flow_label = 0;
112           u8 hop_limit = 0;
113           u8 nexthdr = 0;
114           u8 icv_padding_len = 0;
115
116
117           i_bi0 = from[0];
118           from += 1;
119           n_left_from -= 1;
120           n_left_to_next -= 1;
121
122           next0 = AH_DECRYPT_NEXT_DROP;
123
124           i_b0 = vlib_get_buffer (vm, i_bi0);
125           to_next[0] = i_bi0;
126           to_next += 1;
127           ih4 = vlib_buffer_get_current (i_b0);
128           ih6 = vlib_buffer_get_current (i_b0);
129           sa_index0 = vnet_buffer (i_b0)->ipsec.sad_index;
130           sa0 = pool_elt_at_index (im->sad, sa_index0);
131
132           if (is_ip6)
133             {
134               ip6_ext_header_t *prev = NULL;
135               ip6_ext_header_find_t (ih6, prev, ah0, IP_PROTOCOL_IPSEC_AH);
136               ip_hdr_size = sizeof (ip6_header_t);
137               ASSERT ((u8 *) ah0 - (u8 *) ih6 == ip_hdr_size);
138             }
139           else
140             {
141               ip_hdr_size = ip4_header_bytes (ih4);
142               ah0 = (ah_header_t *) ((u8 *) ih4 + ip_hdr_size);
143             }
144
145           seq = clib_host_to_net_u32 (ah0->seq_no);
146           /* anti-replay check */
147           //TODO UT remaining
148           if (sa0->use_anti_replay)
149             {
150               int rv = 0;
151
152               if (PREDICT_TRUE (sa0->use_esn))
153                 rv = esp_replay_check_esn (sa0, seq);
154               else
155                 rv = esp_replay_check (sa0, seq);
156
157               if (PREDICT_FALSE (rv))
158                 {
159                   clib_warning ("anti-replay SPI %u seq %u", sa0->spi, seq);
160                   if (is_ip6)
161                     vlib_node_increment_counter (vm,
162                                                  ah6_decrypt_node.index,
163                                                  AH_DECRYPT_ERROR_REPLAY, 1);
164                   else
165                     vlib_node_increment_counter (vm,
166                                                  ah4_decrypt_node.index,
167                                                  AH_DECRYPT_ERROR_REPLAY, 1);
168                   to_next[0] = i_bi0;
169                   to_next += 1;
170                   goto trace;
171                 }
172             }
173
174
175           sa0->total_data_size += i_b0->current_length;
176           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];
181               u8 digest[64];
182               clib_memset (sig, 0, sizeof (sig));
183               clib_memset (digest, 0, sizeof (digest));
184               u8 *icv = ah0->auth_data;
185               memcpy (digest, icv, icv_size);
186               clib_memset (icv, 0, icv_size);
187
188               if (is_ip6)
189                 {
190                   ip_version_traffic_class_and_flow_label =
191                     ih6->ip_version_traffic_class_and_flow_label;
192                   hop_limit = ih6->hop_limit;
193                   ih6->ip_version_traffic_class_and_flow_label = 0x60;
194                   ih6->hop_limit = 0;
195                   nexthdr = ah0->nexthdr;
196                   icv_padding_len =
197                     ah_calc_icv_padding_len (icv_size, 1 /* is_ipv6 */ );
198                 }
199               else
200                 {
201                   tos = ih4->tos;
202                   ttl = ih4->ttl;
203                   ih4->tos = 0;
204                   ih4->ttl = 0;
205                   ih4->checksum = 0;
206                   ih4->flags_and_fragment_offset = 0;
207                   icv_padding_len =
208                     ah_calc_icv_padding_len (icv_size, 0 /* is_ipv6 */ );
209                 }
210               hmac_calc (sa0->integ_alg, sa0->integ_key, sa0->integ_key_len,
211                          (u8 *) ih4, i_b0->current_length, sig, sa0->use_esn,
212                          sa0->seq_hi);
213
214               if (PREDICT_FALSE (memcmp (digest, sig, icv_size)))
215                 {
216                   if (is_ip6)
217                     vlib_node_increment_counter (vm,
218                                                  ah6_decrypt_node.index,
219                                                  AH_DECRYPT_ERROR_INTEG_ERROR,
220                                                  1);
221                   else
222                     vlib_node_increment_counter (vm,
223                                                  ah4_decrypt_node.index,
224                                                  AH_DECRYPT_ERROR_INTEG_ERROR,
225                                                  1);
226                   to_next[0] = i_bi0;
227                   to_next += 1;
228                   goto trace;
229                 }
230
231               //TODO UT remaining
232               if (PREDICT_TRUE (sa0->use_anti_replay))
233                 {
234                   if (PREDICT_TRUE (sa0->use_esn))
235                     esp_replay_advance_esn (sa0, seq);
236                   else
237                     esp_replay_advance (sa0, seq);
238                 }
239
240             }
241
242           vlib_buffer_advance (i_b0,
243                                ip_hdr_size + sizeof (ah_header_t) + icv_size +
244                                icv_padding_len);
245           i_b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
246
247           if (PREDICT_TRUE (sa0->is_tunnel))
248             {                   /* tunnel mode */
249               if (PREDICT_TRUE (ah0->nexthdr == IP_PROTOCOL_IP_IN_IP))
250                 next0 = AH_DECRYPT_NEXT_IP4_INPUT;
251               else if (ah0->nexthdr == IP_PROTOCOL_IPV6)
252                 next0 = AH_DECRYPT_NEXT_IP6_INPUT;
253               else
254                 {
255                   clib_warning ("next header: 0x%x", ah0->nexthdr);
256                   if (is_ip6)
257                     vlib_node_increment_counter (vm,
258                                                  ah6_decrypt_node.index,
259                                                  AH_DECRYPT_ERROR_DECRYPTION_FAILED,
260                                                  1);
261                   else
262                     vlib_node_increment_counter (vm,
263                                                  ah4_decrypt_node.index,
264                                                  AH_DECRYPT_ERROR_DECRYPTION_FAILED,
265                                                  1);
266                   goto trace;
267                 }
268             }
269           else
270             {                   /* transport mode */
271               if (is_ip6)
272                 {
273                   vlib_buffer_advance (i_b0, -sizeof (ip6_header_t));
274                   oh6 = vlib_buffer_get_current (i_b0);
275                   memmove (oh6, ih6, sizeof (ip6_header_t));
276
277                   next0 = AH_DECRYPT_NEXT_IP6_INPUT;
278                   oh6->protocol = nexthdr;
279                   oh6->hop_limit = hop_limit;
280                   oh6->ip_version_traffic_class_and_flow_label =
281                     ip_version_traffic_class_and_flow_label;
282                   oh6->payload_length =
283                     clib_host_to_net_u16 (vlib_buffer_length_in_chain
284                                           (vm, i_b0) - sizeof (ip6_header_t));
285                 }
286               else
287                 {
288                   vlib_buffer_advance (i_b0, -sizeof (ip4_header_t));
289                   oh4 = vlib_buffer_get_current (i_b0);
290                   memmove (oh4, ih4, sizeof (ip4_header_t));
291
292                   next0 = AH_DECRYPT_NEXT_IP4_INPUT;
293                   oh4->ip_version_and_header_length = 0x45;
294                   oh4->fragment_id = 0;
295                   oh4->flags_and_fragment_offset = 0;
296                   oh4->protocol = ah0->nexthdr;
297                   oh4->length =
298                     clib_host_to_net_u16 (vlib_buffer_length_in_chain
299                                           (vm, i_b0));
300                   oh4->ttl = ttl;
301                   oh4->tos = tos;
302                   oh4->checksum = ip4_header_checksum (oh4);
303                 }
304             }
305
306           /* for IPSec-GRE tunnel next node is ipsec-gre-input */
307           if (PREDICT_FALSE
308               ((vnet_buffer (i_b0)->ipsec.flags) &
309                IPSEC_FLAG_IPSEC_GRE_TUNNEL))
310             next0 = AH_DECRYPT_NEXT_IPSEC_GRE_INPUT;
311
312
313           vnet_buffer (i_b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
314         trace:
315           if (PREDICT_FALSE (i_b0->flags & VLIB_BUFFER_IS_TRACED))
316             {
317               i_b0->flags |= VLIB_BUFFER_IS_TRACED;
318               ah_decrypt_trace_t *tr =
319                 vlib_add_trace (vm, node, i_b0, sizeof (*tr));
320               tr->integ_alg = sa0->integ_alg;
321             }
322           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
323                                            n_left_to_next, i_bi0, next0);
324         }
325       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
326     }
327   if (is_ip6)
328     vlib_node_increment_counter (vm, ah6_decrypt_node.index,
329                                  AH_DECRYPT_ERROR_RX_PKTS,
330                                  from_frame->n_vectors);
331   else
332     vlib_node_increment_counter (vm, ah4_decrypt_node.index,
333                                  AH_DECRYPT_ERROR_RX_PKTS,
334                                  from_frame->n_vectors);
335
336   return from_frame->n_vectors;
337 }
338
339 static uword
340 ah4_decrypt_node_fn (vlib_main_t * vm,
341                      vlib_node_runtime_t * node, vlib_frame_t * from_frame)
342 {
343   return ah_decrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ );
344 }
345
346 /* *INDENT-OFF* */
347 VLIB_REGISTER_NODE (ah4_decrypt_node) = {
348   .function = ah4_decrypt_node_fn,
349   .name = "ah4-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
366 VLIB_NODE_FUNCTION_MULTIARCH (ah4_decrypt_node, ah4_decrypt_node_fn);
367
368 static uword
369 ah6_decrypt_node_fn (vlib_main_t * vm,
370                      vlib_node_runtime_t * node, vlib_frame_t * from_frame)
371 {
372   return ah_decrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ );
373 }
374
375 /* *INDENT-OFF* */
376 VLIB_REGISTER_NODE (ah6_decrypt_node) = {
377   .function = ah6_decrypt_node_fn,
378   .name = "ah6-decrypt",
379   .vector_size = sizeof (u32),
380   .format_trace = format_ah_decrypt_trace,
381   .type = VLIB_NODE_TYPE_INTERNAL,
382
383   .n_errors = ARRAY_LEN(ah_decrypt_error_strings),
384   .error_strings = ah_decrypt_error_strings,
385
386   .n_next_nodes = AH_DECRYPT_N_NEXT,
387   .next_nodes = {
388 #define _(s,n) [AH_DECRYPT_NEXT_##s] = n,
389     foreach_ah_decrypt_next
390 #undef _
391   },
392 };
393 /* *INDENT-ON* */
394
395 VLIB_NODE_FUNCTION_MULTIARCH (ah6_decrypt_node, ah6_decrypt_node_fn);
396 /*
397  * fd.io coding-style-patch-verification: ON
398  *
399  * Local Variables:
400  * eval: (c-set-style "gnu")
401  * End:
402  */