ipsec: anti-replay code cleanup
[vpp.git] / src / vnet / ipsec / esp_decrypt.c
1 /*
2  * esp_decrypt.c : IPSec ESP 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/ipsec_io.h>
25
26 #define foreach_esp_decrypt_next                \
27 _(DROP, "error-drop")                           \
28 _(IP4_INPUT, "ip4-input-no-checksum")           \
29 _(IP6_INPUT, "ip6-input")                       \
30 _(IPSEC_GRE_INPUT, "ipsec-gre-input")
31
32 #define _(v, s) ESP_DECRYPT_NEXT_##v,
33 typedef enum
34 {
35   foreach_esp_decrypt_next
36 #undef _
37     ESP_DECRYPT_N_NEXT,
38 } esp_decrypt_next_t;
39
40
41 #define foreach_esp_decrypt_error                   \
42  _(RX_PKTS, "ESP pkts received")                    \
43  _(NO_BUFFER, "No buffer (packed dropped)")         \
44  _(DECRYPTION_FAILED, "ESP decryption failed")      \
45  _(INTEG_ERROR, "Integrity check failed")           \
46  _(REPLAY, "SA replayed packet")                    \
47  _(NOT_IP, "Not IP packet (dropped)")
48
49
50 typedef enum
51 {
52 #define _(sym,str) ESP_DECRYPT_ERROR_##sym,
53   foreach_esp_decrypt_error
54 #undef _
55     ESP_DECRYPT_N_ERROR,
56 } esp_decrypt_error_t;
57
58 static char *esp_decrypt_error_strings[] = {
59 #define _(sym,string) string,
60   foreach_esp_decrypt_error
61 #undef _
62 };
63
64 typedef struct
65 {
66   ipsec_crypto_alg_t crypto_alg;
67   ipsec_integ_alg_t integ_alg;
68 } esp_decrypt_trace_t;
69
70 /* packet trace format function */
71 static u8 *
72 format_esp_decrypt_trace (u8 * s, va_list * args)
73 {
74   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
75   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
76   esp_decrypt_trace_t *t = va_arg (*args, esp_decrypt_trace_t *);
77
78   s = format (s, "esp: crypto %U integrity %U",
79               format_ipsec_crypto_alg, t->crypto_alg,
80               format_ipsec_integ_alg, t->integ_alg);
81   return s;
82 }
83
84 always_inline void
85 esp_decrypt_cbc (vlib_main_t * vm, ipsec_sa_t * sa,
86                  u8 * in, u8 * out, size_t in_len, u8 * key, u8 * iv)
87 {
88   vnet_crypto_op_t _op, *op = &_op;
89
90
91   if (PREDICT_FALSE (sa->crypto_dec_op_type == VNET_CRYPTO_OP_NONE))
92     return;
93
94   op->op = sa->crypto_dec_op_type;
95   op->iv = iv;
96   op->src = in;
97   op->dst = out;
98   op->len = in_len;
99   op->key = key;
100
101   vnet_crypto_process_ops (vm, op, 1);
102 }
103
104 always_inline uword
105 esp_decrypt_inline (vlib_main_t * vm,
106                     vlib_node_runtime_t * node, vlib_frame_t * from_frame,
107                     int is_ip6)
108 {
109   ipsec_main_t *im = &ipsec_main;
110   u32 *from = vlib_frame_vector_args (from_frame);
111   u32 n_left_from = from_frame->n_vectors;
112   u32 new_bufs[VLIB_FRAME_SIZE];
113   vlib_buffer_t *i_bufs[VLIB_FRAME_SIZE], **ib = i_bufs;
114   vlib_buffer_t *o_bufs[VLIB_FRAME_SIZE], **ob = o_bufs;
115   u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
116   u32 n_alloc, thread_index = vm->thread_index;
117
118   n_alloc = vlib_buffer_alloc (vm, new_bufs, n_left_from);
119   if (n_alloc != n_left_from)
120     {
121       vlib_node_increment_counter (vm, node->node_index,
122                                    ESP_DECRYPT_ERROR_NO_BUFFER,
123                                    n_left_from - n_alloc);
124       if (n_alloc == 0)
125         goto done;
126       n_left_from = n_alloc;
127     }
128
129   vlib_get_buffers (vm, from, ib, n_left_from);
130   vlib_get_buffers (vm, new_bufs, ob, n_left_from);
131
132   while (n_left_from > 0)
133     {
134       esp_header_t *esp0;
135       ipsec_sa_t *sa0;
136       u32 sa_index0 = ~0;
137       ip4_header_t *ih4 = 0, *oh4 = 0;
138       ip6_header_t *ih6 = 0, *oh6 = 0;
139       u8 tunnel_mode = 1;
140
141       next[0] = ESP_DECRYPT_NEXT_DROP;
142
143       esp0 = vlib_buffer_get_current (ib[0]);
144       sa_index0 = vnet_buffer (ib[0])->ipsec.sad_index;
145       sa0 = pool_elt_at_index (im->sad, sa_index0);
146
147       /* anti-replay check */
148       if (ipsec_sa_anti_replay_check (sa0, &esp0->seq))
149         {
150           u32 tmp, off = n_alloc - n_left_from;
151           /* send original packet to drop node */
152           tmp = from[off];
153           from[off] = new_bufs[off];
154           new_bufs[off] = tmp;
155           ib[0]->error = node->errors[ESP_DECRYPT_ERROR_REPLAY];
156           next[0] = ESP_DECRYPT_NEXT_DROP;
157           goto trace;
158         }
159
160       vlib_increment_combined_counter
161         (&ipsec_sa_counters, thread_index, sa_index0,
162          1, ib[0]->current_length);
163
164       if (PREDICT_TRUE (sa0->integ_alg != IPSEC_INTEG_ALG_NONE))
165         {
166           u8 sig[64];
167           int icv_size = sa0->integ_trunc_size;
168           clib_memset (sig, 0, sizeof (sig));
169           u8 *icv = vlib_buffer_get_current (ib[0]) + ib[0]->current_length -
170             icv_size;
171           ib[0]->current_length -= icv_size;
172
173           hmac_calc (vm, sa0, (u8 *) esp0, ib[0]->current_length, sig);
174
175           if (PREDICT_FALSE (memcmp (icv, sig, icv_size)))
176             {
177               u32 tmp, off = n_alloc - n_left_from;
178               /* send original packet to drop node */
179               tmp = from[off];
180               from[off] = new_bufs[off];
181               new_bufs[off] = tmp;
182               ib[0]->error = node->errors[ESP_DECRYPT_ERROR_INTEG_ERROR];
183               next[0] = ESP_DECRYPT_NEXT_DROP;
184               goto trace;
185             }
186         }
187
188       ipsec_sa_anti_replay_advance (sa0, &esp0->seq);
189
190       if ((sa0->crypto_alg >= IPSEC_CRYPTO_ALG_AES_CBC_128 &&
191            sa0->crypto_alg <= IPSEC_CRYPTO_ALG_AES_CBC_256) ||
192           (sa0->crypto_alg >= IPSEC_CRYPTO_ALG_DES_CBC &&
193            sa0->crypto_alg <= IPSEC_CRYPTO_ALG_3DES_CBC))
194         {
195           const int BLOCK_SIZE = sa0->crypto_block_size;
196           const int IV_SIZE = sa0->crypto_block_size;
197           esp_footer_t *f0;
198           u8 ip_hdr_size = 0;
199
200           int blocks =
201             (ib[0]->current_length - sizeof (esp_header_t) -
202              IV_SIZE) / BLOCK_SIZE;
203
204           ob[0]->current_data = sizeof (ethernet_header_t);
205
206           /* transport mode */
207           if (PREDICT_FALSE (!ipsec_sa_is_set_IS_TUNNEL (sa0) &&
208                              !ipsec_sa_is_set_IS_TUNNEL_V6 (sa0)))
209             {
210               tunnel_mode = 0;
211
212               if (is_ip6)
213                 {
214                   ip_hdr_size = sizeof (ip6_header_t);
215                   ih6 = (ip6_header_t *) ((u8 *) esp0 - ip_hdr_size);
216                   oh6 = vlib_buffer_get_current (ob[0]);
217                 }
218               else
219                 {
220                   ip_hdr_size = sizeof (ip4_header_t);
221                   if (ipsec_sa_is_set_UDP_ENCAP (sa0))
222                     ih4 = (ip4_header_t *) ((u8 *) esp0 - ip_hdr_size -
223                                             sizeof (udp_header_t));
224                   else
225                     ih4 = (ip4_header_t *) ((u8 *) esp0 - ip_hdr_size);
226                   oh4 = vlib_buffer_get_current (ob[0]);
227                 }
228             }
229
230           esp_decrypt_cbc (vm, sa0, esp0->data + IV_SIZE,
231                            (u8 *) vlib_buffer_get_current (ob[0]) +
232                            ip_hdr_size, BLOCK_SIZE * blocks,
233                            sa0->crypto_key.data, esp0->data);
234
235           ob[0]->current_length = (blocks * BLOCK_SIZE) - 2 + ip_hdr_size;
236           ob[0]->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
237           f0 = (esp_footer_t *) ((u8 *) vlib_buffer_get_current (ob[0]) +
238                                  ob[0]->current_length);
239           ob[0]->current_length -= f0->pad_length;
240
241           /* tunnel mode */
242           if (PREDICT_TRUE (tunnel_mode))
243             {
244               if (PREDICT_TRUE (f0->next_header == IP_PROTOCOL_IP_IN_IP))
245                 {
246                   next[0] = ESP_DECRYPT_NEXT_IP4_INPUT;
247                   oh4 = vlib_buffer_get_current (ob[0]);
248                 }
249               else if (f0->next_header == IP_PROTOCOL_IPV6)
250                 next[0] = ESP_DECRYPT_NEXT_IP6_INPUT;
251               else
252                 {
253                   vlib_node_increment_counter (vm, node->node_index,
254                                                ESP_DECRYPT_ERROR_DECRYPTION_FAILED,
255                                                1);
256                   ob[0] = 0;
257                   goto trace;
258                 }
259             }
260           /* transport mode */
261           else
262             {
263               u32 len = vlib_buffer_length_in_chain (vm, ob[0]);
264               if (is_ip6)
265                 {
266                   next[0] = ESP_DECRYPT_NEXT_IP6_INPUT;
267                   oh6->ip_version_traffic_class_and_flow_label =
268                     ih6->ip_version_traffic_class_and_flow_label;
269                   oh6->protocol = f0->next_header;
270                   oh6->hop_limit = ih6->hop_limit;
271                   oh6->src_address.as_u64[0] = ih6->src_address.as_u64[0];
272                   oh6->src_address.as_u64[1] = ih6->src_address.as_u64[1];
273                   oh6->dst_address.as_u64[0] = ih6->dst_address.as_u64[0];
274                   oh6->dst_address.as_u64[1] = ih6->dst_address.as_u64[1];
275                   len -= sizeof (ip6_header_t);
276                   oh6->payload_length = clib_host_to_net_u16 (len);
277                 }
278               else
279                 {
280                   next[0] = ESP_DECRYPT_NEXT_IP4_INPUT;
281                   oh4->ip_version_and_header_length = 0x45;
282                   oh4->tos = ih4->tos;
283                   oh4->fragment_id = 0;
284                   oh4->flags_and_fragment_offset = 0;
285                   oh4->ttl = ih4->ttl;
286                   oh4->protocol = f0->next_header;
287                   oh4->src_address.as_u32 = ih4->src_address.as_u32;
288                   oh4->dst_address.as_u32 = ih4->dst_address.as_u32;
289                   oh4->length = clib_host_to_net_u16 (len);
290                   oh4->checksum = ip4_header_checksum (oh4);
291                 }
292             }
293
294           /* for IPSec-GRE tunnel next node is ipsec-gre-input */
295           if (PREDICT_FALSE
296               ((vnet_buffer (ib[0])->ipsec.flags) &
297                IPSEC_FLAG_IPSEC_GRE_TUNNEL))
298             next[0] = ESP_DECRYPT_NEXT_IPSEC_GRE_INPUT;
299
300           vnet_buffer (ob[0])->sw_if_index[VLIB_TX] = (u32) ~ 0;
301           vnet_buffer (ob[0])->sw_if_index[VLIB_RX] =
302             vnet_buffer (ib[0])->sw_if_index[VLIB_RX];
303         }
304
305     trace:
306       if (PREDICT_FALSE (ib[0]->flags & VLIB_BUFFER_IS_TRACED))
307         {
308           if (ob[0])
309             {
310               ob[0]->flags |= VLIB_BUFFER_IS_TRACED;
311               ob[0]->trace_index = ib[0]->trace_index;
312               esp_decrypt_trace_t *tr =
313                 vlib_add_trace (vm, node, ob[0], sizeof (*tr));
314               tr->crypto_alg = sa0->crypto_alg;
315               tr->integ_alg = sa0->integ_alg;
316             }
317         }
318
319       /* next */
320       n_left_from -= 1;
321       ib += 1;
322       ob += 1;
323       next += 1;
324     }
325
326   vlib_node_increment_counter (vm, node->node_index,
327                                ESP_DECRYPT_ERROR_RX_PKTS, n_alloc);
328
329   vlib_buffer_enqueue_to_next (vm, node, new_bufs, nexts, n_alloc);
330 done:
331   vlib_buffer_free (vm, from, from_frame->n_vectors);
332   return n_alloc;
333 }
334
335 VLIB_NODE_FN (esp4_decrypt_node) (vlib_main_t * vm,
336                                   vlib_node_runtime_t * node,
337                                   vlib_frame_t * from_frame)
338 {
339   return esp_decrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ );
340 }
341
342 /* *INDENT-OFF* */
343 VLIB_REGISTER_NODE (esp4_decrypt_node) = {
344   .name = "esp4-decrypt",
345   .vector_size = sizeof (u32),
346   .format_trace = format_esp_decrypt_trace,
347   .type = VLIB_NODE_TYPE_INTERNAL,
348
349   .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
350   .error_strings = esp_decrypt_error_strings,
351
352   .n_next_nodes = ESP_DECRYPT_N_NEXT,
353   .next_nodes = {
354 #define _(s,n) [ESP_DECRYPT_NEXT_##s] = n,
355     foreach_esp_decrypt_next
356 #undef _
357   },
358 };
359 /* *INDENT-ON* */
360
361 VLIB_NODE_FN (esp6_decrypt_node) (vlib_main_t * vm,
362                                   vlib_node_runtime_t * node,
363                                   vlib_frame_t * from_frame)
364 {
365   return esp_decrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ );
366 }
367
368 /* *INDENT-OFF* */
369 VLIB_REGISTER_NODE (esp6_decrypt_node) = {
370   .name = "esp6-decrypt",
371   .vector_size = sizeof (u32),
372   .format_trace = format_esp_decrypt_trace,
373   .type = VLIB_NODE_TYPE_INTERNAL,
374
375   .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
376   .error_strings = esp_decrypt_error_strings,
377
378   .n_next_nodes = ESP_DECRYPT_N_NEXT,
379   .next_nodes = {
380 #define _(s,n) [ESP_DECRYPT_NEXT_##s] = n,
381     foreach_esp_decrypt_next
382 #undef _
383   },
384 };
385 /* *INDENT-ON* */
386
387 /*
388  * fd.io coding-style-patch-verification: ON
389  *
390  * Local Variables:
391  * eval: (c-set-style "gnu")
392  * End:
393  */