d27d42384d539152dd3eb92e9e4a96338e3407bc
[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 #include <vnet/ipsec/ipsec_io.h>
26
27 #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")
32
33 #define _(v, s) AH_DECRYPT_NEXT_##v,
34 typedef enum
35 {
36   foreach_ah_decrypt_next
37 #undef _
38     AH_DECRYPT_N_NEXT,
39 } ah_decrypt_next_t;
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   _ (NO_TAIL_SPACE, "not enough buffer tail space (dropped)")     \
46   _ (DROP_FRAGMENTS, "IP fragments drop")       \
47   _ (REPLAY, "SA replayed packet")
48
49 typedef 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
57 static char *ah_decrypt_error_strings[] = {
58 #define _(sym,string) string,
59   foreach_ah_decrypt_error
60 #undef _
61 };
62
63 typedef struct
64 {
65   ipsec_integ_alg_t integ_alg;
66   u32 seq_num;
67 } ah_decrypt_trace_t;
68
69 /* packet trace format function */
70 static u8 *
71 format_ah_decrypt_trace (u8 * s, va_list * args)
72 {
73   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
74   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
75   ah_decrypt_trace_t *t = va_arg (*args, ah_decrypt_trace_t *);
76
77   s = format (s, "ah: integrity %U seq-num %d",
78               format_ipsec_integ_alg, t->integ_alg, t->seq_num);
79   return s;
80 }
81
82 typedef struct
83 {
84   union
85   {
86     struct
87     {
88       u8 hop_limit;
89       u8 nexthdr;
90       u32 ip_version_traffic_class_and_flow_label;
91     };
92
93     struct
94     {
95       u8 ttl;
96       u8 tos;
97     };
98   };
99   u32 sa_index;
100   u32 seq;
101   u8 icv_padding_len;
102   u8 icv_size;
103   u8 ip_hdr_size;
104   i16 current_data;
105   u8 nexthdr_cached;
106 } ah_decrypt_packet_data_t;
107
108 static_always_inline void
109 ah_process_ops (vlib_main_t * vm, vlib_node_runtime_t * node,
110                 vnet_crypto_op_t * ops, vlib_buffer_t * b[], u16 * nexts)
111 {
112   u32 n_fail, n_ops = vec_len (ops);
113   vnet_crypto_op_t *op = ops;
114
115   if (n_ops == 0)
116     return;
117
118   n_fail = n_ops - vnet_crypto_process_ops (vm, op, n_ops);
119
120   while (n_fail)
121     {
122       ASSERT (op - ops < n_ops);
123
124       if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
125         {
126           u32 bi = op->user_data;
127           b[bi]->error = node->errors[AH_DECRYPT_ERROR_INTEG_ERROR];
128           nexts[bi] = AH_DECRYPT_NEXT_DROP;
129           n_fail--;
130         }
131       op++;
132     }
133 }
134
135 always_inline uword
136 ah_decrypt_inline (vlib_main_t * vm,
137                    vlib_node_runtime_t * node, vlib_frame_t * from_frame,
138                    int is_ip6)
139 {
140   u32 n_left, *from;
141   u32 thread_index = vm->thread_index;
142   u16 buffer_data_size = vlib_buffer_get_default_data_size (vm);
143   ah_decrypt_packet_data_t pkt_data[VLIB_FRAME_SIZE], *pd = pkt_data;
144   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
145   u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
146   ipsec_main_t *im = &ipsec_main;
147   ipsec_per_thread_data_t *ptd = vec_elt_at_index (im->ptd, thread_index);
148   from = vlib_frame_vector_args (from_frame);
149   n_left = from_frame->n_vectors;
150   ipsec_sa_t *sa0 = 0;
151   u32 current_sa_index = ~0, current_sa_bytes = 0, current_sa_pkts = 0;
152
153   clib_memset (pkt_data, 0, VLIB_FRAME_SIZE * sizeof (pkt_data[0]));
154   vlib_get_buffers (vm, from, b, n_left);
155   clib_memset_u16 (nexts, -1, n_left);
156   vec_reset_length (ptd->integ_ops);
157
158   while (n_left > 0)
159     {
160       ah_header_t *ah0;
161       ip4_header_t *ih4;
162       ip6_header_t *ih6;
163
164       if (vnet_buffer (b[0])->ipsec.sad_index != current_sa_index)
165         {
166           if (current_sa_index != ~0)
167             vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
168                                              current_sa_index,
169                                              current_sa_pkts,
170                                              current_sa_bytes);
171           current_sa_index = vnet_buffer (b[0])->ipsec.sad_index;
172           sa0 = pool_elt_at_index (im->sad, current_sa_index);
173
174           current_sa_bytes = current_sa_pkts = 0;
175           vlib_prefetch_combined_counter (&ipsec_sa_counters,
176                                           thread_index, current_sa_index);
177         }
178
179       pd->sa_index = current_sa_index;
180
181       ih4 = vlib_buffer_get_current (b[0]);
182       ih6 = vlib_buffer_get_current (b[0]);
183       pd->current_data = b[0]->current_data;
184
185       if (is_ip6)
186         {
187           ip6_ext_header_t *prev = NULL;
188           ip6_ext_header_find_t (ih6, prev, ah0, IP_PROTOCOL_IPSEC_AH);
189           pd->ip_hdr_size = sizeof (ip6_header_t);
190           ASSERT ((u8 *) ah0 - (u8 *) ih6 == pd->ip_hdr_size);
191         }
192       else
193         {
194           if (ip4_is_fragment (ih4))
195             {
196               b[0]->error = node->errors[AH_DECRYPT_ERROR_DROP_FRAGMENTS];
197               next[0] = AH_DECRYPT_NEXT_DROP;
198               goto next;
199             }
200           pd->ip_hdr_size = ip4_header_bytes (ih4);
201           ah0 = (ah_header_t *) ((u8 *) ih4 + pd->ip_hdr_size);
202         }
203
204       pd->seq = clib_host_to_net_u32 (ah0->seq_no);
205
206       /* anti-replay check */
207       if (ipsec_sa_anti_replay_check (sa0, &ah0->seq_no))
208         {
209           b[0]->error = node->errors[AH_DECRYPT_ERROR_REPLAY];
210           next[0] = AH_DECRYPT_NEXT_DROP;
211           goto next;
212         }
213
214       current_sa_bytes += b[0]->current_length;
215       current_sa_pkts += 1;
216
217       pd->icv_size = sa0->integ_icv_size;
218       pd->nexthdr_cached = ah0->nexthdr;
219       if (PREDICT_TRUE (sa0->integ_alg != IPSEC_INTEG_ALG_NONE))
220         {
221           if (PREDICT_FALSE (ipsec_sa_is_set_USE_ESN (sa0) &&
222                              pd->current_data + b[0]->current_length
223                              + sizeof (u32) > buffer_data_size))
224             {
225               b[0]->error = node->errors[AH_DECRYPT_ERROR_NO_TAIL_SPACE];
226               next[0] = AH_DECRYPT_NEXT_DROP;
227               goto next;
228             }
229
230           vnet_crypto_op_t *op;
231           vec_add2_aligned (ptd->integ_ops, op, 1, CLIB_CACHE_LINE_BYTES);
232           vnet_crypto_op_init (op, sa0->integ_op_id);
233
234           op->src = (u8 *) ih4;
235           op->len = b[0]->current_length;
236           op->digest = (u8 *) ih4 - pd->icv_size;
237           op->flags = VNET_CRYPTO_OP_FLAG_HMAC_CHECK;
238           op->digest_len = pd->icv_size;
239           op->key_index = sa0->integ_key_index;
240           op->user_data = b - bufs;
241           if (ipsec_sa_is_set_USE_ESN (sa0))
242             {
243               u32 seq_hi = clib_host_to_net_u32 (sa0->seq_hi);
244
245               op->len += sizeof (seq_hi);
246               clib_memcpy (op->src + b[0]->current_length, &seq_hi,
247                            sizeof (seq_hi));
248             }
249           clib_memcpy (op->digest, ah0->auth_data, pd->icv_size);
250           clib_memset (ah0->auth_data, 0, pd->icv_size);
251
252           if (is_ip6)
253             {
254               pd->ip_version_traffic_class_and_flow_label =
255                 ih6->ip_version_traffic_class_and_flow_label;
256               pd->hop_limit = ih6->hop_limit;
257               ih6->ip_version_traffic_class_and_flow_label = 0x60;
258               ih6->hop_limit = 0;
259               pd->nexthdr = ah0->nexthdr;
260               pd->icv_padding_len =
261                 ah_calc_icv_padding_len (pd->icv_size, 1 /* is_ipv6 */ );
262             }
263           else
264             {
265               pd->tos = ih4->tos;
266               pd->ttl = ih4->ttl;
267               ih4->tos = 0;
268               ih4->ttl = 0;
269               ih4->checksum = 0;
270               pd->icv_padding_len =
271                 ah_calc_icv_padding_len (pd->icv_size, 0 /* is_ipv6 */ );
272             }
273         }
274
275     next:
276       n_left -= 1;
277       pd += 1;
278       next += 1;
279       b += 1;
280     }
281
282   n_left = from_frame->n_vectors;
283   next = nexts;
284   pd = pkt_data;
285   b = bufs;
286
287   vlib_node_increment_counter (vm, node->node_index, AH_DECRYPT_ERROR_RX_PKTS,
288                                n_left);
289   vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
290                                    current_sa_index, current_sa_pkts,
291                                    current_sa_bytes);
292
293   ah_process_ops (vm, node, ptd->integ_ops, bufs, nexts);
294
295   while (n_left > 0)
296     {
297       ip4_header_t *oh4;
298       ip6_header_t *oh6;
299
300       if (next[0] < AH_DECRYPT_N_NEXT)
301         goto trace;
302
303       sa0 = vec_elt_at_index (im->sad, pd->sa_index);
304
305       if (PREDICT_TRUE (sa0->integ_alg != IPSEC_INTEG_ALG_NONE))
306         {
307           ipsec_sa_anti_replay_advance (sa0, clib_host_to_net_u32 (pd->seq));
308         }
309
310       u16 ah_hdr_len = sizeof (ah_header_t) + pd->icv_size
311         + pd->icv_padding_len;
312       vlib_buffer_advance (b[0], pd->ip_hdr_size + ah_hdr_len);
313       b[0]->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
314
315       if (PREDICT_TRUE (ipsec_sa_is_set_IS_TUNNEL (sa0)))
316         {                       /* tunnel mode */
317           if (PREDICT_TRUE (pd->nexthdr_cached == IP_PROTOCOL_IP_IN_IP))
318             next[0] = AH_DECRYPT_NEXT_IP4_INPUT;
319           else if (pd->nexthdr_cached == IP_PROTOCOL_IPV6)
320             next[0] = AH_DECRYPT_NEXT_IP6_INPUT;
321           else
322             {
323               b[0]->error = node->errors[AH_DECRYPT_ERROR_DECRYPTION_FAILED];
324               next[0] = AH_DECRYPT_NEXT_DROP;
325               goto trace;
326             }
327         }
328       else
329         {                       /* transport mode */
330           if (is_ip6)
331             {
332               vlib_buffer_advance (b[0], -sizeof (ip6_header_t));
333               oh6 = vlib_buffer_get_current (b[0]);
334               if (ah_hdr_len >= sizeof (ip6_header_t))
335                 clib_memcpy (oh6, b[0]->data + pd->current_data,
336                              sizeof (ip6_header_t));
337               else
338                 memmove (oh6, b[0]->data + pd->current_data,
339                          sizeof (ip6_header_t));
340
341               next[0] = AH_DECRYPT_NEXT_IP6_INPUT;
342               oh6->protocol = pd->nexthdr;
343               oh6->hop_limit = pd->hop_limit;
344               oh6->ip_version_traffic_class_and_flow_label =
345                 pd->ip_version_traffic_class_and_flow_label;
346               oh6->payload_length =
347                 clib_host_to_net_u16 (vlib_buffer_length_in_chain
348                                       (vm, b[0]) - sizeof (ip6_header_t));
349             }
350           else
351             {
352               vlib_buffer_advance (b[0], -sizeof (ip4_header_t));
353               oh4 = vlib_buffer_get_current (b[0]);
354               if (ah_hdr_len >= sizeof (ip4_header_t))
355                 clib_memcpy (oh4, b[0]->data + pd->current_data,
356                              sizeof (ip4_header_t));
357               else
358                 memmove (oh4, b[0]->data + pd->current_data,
359                          sizeof (ip4_header_t));
360
361               next[0] = AH_DECRYPT_NEXT_IP4_INPUT;
362               oh4->ip_version_and_header_length = 0x45;
363               oh4->fragment_id = 0;
364               oh4->flags_and_fragment_offset = 0;
365               oh4->protocol = pd->nexthdr_cached;
366               oh4->length =
367                 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b[0]));
368               oh4->ttl = pd->ttl;
369               oh4->tos = pd->tos;
370               oh4->checksum = ip4_header_checksum (oh4);
371             }
372         }
373
374       /* for IPSec-GRE tunnel next node is ipsec-gre-input */
375       if (PREDICT_FALSE (ipsec_sa_is_set_IS_GRE (sa0)))
376         next[0] = AH_DECRYPT_NEXT_IPSEC_GRE_INPUT;
377
378       vnet_buffer (b[0])->sw_if_index[VLIB_TX] = (u32) ~ 0;
379     trace:
380       if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
381         {
382           sa0 = pool_elt_at_index (im->sad,
383                                    vnet_buffer (b[0])->ipsec.sad_index);
384           ah_decrypt_trace_t *tr =
385             vlib_add_trace (vm, node, b[0], sizeof (*tr));
386           tr->integ_alg = sa0->integ_alg;
387           tr->seq_num = pd->seq;
388         }
389
390       n_left -= 1;
391       pd += 1;
392       next += 1;
393       b += 1;
394     }
395
396   n_left = from_frame->n_vectors;
397   vlib_buffer_enqueue_to_next (vm, node, from, nexts, n_left);
398
399   return n_left;
400 }
401
402 VLIB_NODE_FN (ah4_decrypt_node) (vlib_main_t * vm,
403                                  vlib_node_runtime_t * node,
404                                  vlib_frame_t * from_frame)
405 {
406   return ah_decrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ );
407 }
408
409 /* *INDENT-OFF* */
410 VLIB_REGISTER_NODE (ah4_decrypt_node) = {
411   .name = "ah4-decrypt",
412   .vector_size = sizeof (u32),
413   .format_trace = format_ah_decrypt_trace,
414   .type = VLIB_NODE_TYPE_INTERNAL,
415
416   .n_errors = ARRAY_LEN(ah_decrypt_error_strings),
417   .error_strings = ah_decrypt_error_strings,
418
419   .n_next_nodes = AH_DECRYPT_N_NEXT,
420   .next_nodes = {
421 #define _(s,n) [AH_DECRYPT_NEXT_##s] = n,
422     foreach_ah_decrypt_next
423 #undef _
424   },
425 };
426 /* *INDENT-ON* */
427
428 VLIB_NODE_FN (ah6_decrypt_node) (vlib_main_t * vm,
429                                  vlib_node_runtime_t * node,
430                                  vlib_frame_t * from_frame)
431 {
432   return ah_decrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ );
433 }
434
435 /* *INDENT-OFF* */
436 VLIB_REGISTER_NODE (ah6_decrypt_node) = {
437   .name = "ah6-decrypt",
438   .vector_size = sizeof (u32),
439   .format_trace = format_ah_decrypt_trace,
440   .type = VLIB_NODE_TYPE_INTERNAL,
441
442   .n_errors = ARRAY_LEN(ah_decrypt_error_strings),
443   .error_strings = ah_decrypt_error_strings,
444
445   .n_next_nodes = AH_DECRYPT_N_NEXT,
446   .next_nodes = {
447 #define _(s,n) [AH_DECRYPT_NEXT_##s] = n,
448     foreach_ah_decrypt_next
449 #undef _
450   },
451 };
452 /* *INDENT-ON* */
453
454 /*
455  * fd.io coding-style-patch-verification: ON
456  *
457  * Local Variables:
458  * eval: (c-set-style "gnu")
459  * End:
460  */