ipsec: compress ipsec_sa_t so data used by dataplane code fits in cacheline
[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       u32 seq;
138       ip4_header_t *ih4 = 0, *oh4 = 0;
139       ip6_header_t *ih6 = 0, *oh6 = 0;
140       u8 tunnel_mode = 1;
141
142       next[0] = ESP_DECRYPT_NEXT_DROP;
143
144       esp0 = vlib_buffer_get_current (ib[0]);
145       sa_index0 = vnet_buffer (ib[0])->ipsec.sad_index;
146       sa0 = pool_elt_at_index (im->sad, sa_index0);
147       seq = clib_host_to_net_u32 (esp0->seq);
148
149       /* anti-replay check */
150       if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa0))
151         {
152           int rv = 0;
153
154           if (PREDICT_TRUE (ipsec_sa_is_set_USE_EXTENDED_SEQ_NUM (sa0)))
155             rv = esp_replay_check_esn (sa0, seq);
156           else
157             rv = esp_replay_check (sa0, seq);
158
159           if (PREDICT_FALSE (rv))
160             {
161               u32 tmp, off = n_alloc - n_left_from;
162               /* send original packet to drop node */
163               tmp = from[off];
164               from[off] = new_bufs[off];
165               new_bufs[off] = tmp;
166               ib[0]->error = node->errors[ESP_DECRYPT_ERROR_REPLAY];
167               next[0] = ESP_DECRYPT_NEXT_DROP;
168               goto trace;
169             }
170         }
171
172       vlib_increment_combined_counter
173         (&ipsec_sa_counters, thread_index, sa_index0,
174          1, ib[0]->current_length);
175
176       if (PREDICT_TRUE (sa0->integ_alg != IPSEC_INTEG_ALG_NONE))
177         {
178           u8 sig[64];
179           int icv_size = sa0->integ_trunc_size;
180           clib_memset (sig, 0, sizeof (sig));
181           u8 *icv = vlib_buffer_get_current (ib[0]) + ib[0]->current_length -
182             icv_size;
183           ib[0]->current_length -= icv_size;
184
185           hmac_calc (vm, sa0, (u8 *) esp0, ib[0]->current_length, sig);
186
187           if (PREDICT_FALSE (memcmp (icv, sig, icv_size)))
188             {
189               u32 tmp, off = n_alloc - n_left_from;
190               /* send original packet to drop node */
191               tmp = from[off];
192               from[off] = new_bufs[off];
193               new_bufs[off] = tmp;
194               ib[0]->error = node->errors[ESP_DECRYPT_ERROR_INTEG_ERROR];
195               next[0] = ESP_DECRYPT_NEXT_DROP;
196               goto trace;
197             }
198         }
199
200       if (PREDICT_TRUE (ipsec_sa_is_set_USE_ANTI_REPLAY (sa0)))
201         {
202           if (PREDICT_TRUE (ipsec_sa_is_set_USE_EXTENDED_SEQ_NUM (sa0)))
203             esp_replay_advance_esn (sa0, seq);
204           else
205             esp_replay_advance (sa0, seq);
206         }
207
208       if ((sa0->crypto_alg >= IPSEC_CRYPTO_ALG_AES_CBC_128 &&
209            sa0->crypto_alg <= IPSEC_CRYPTO_ALG_AES_CBC_256) ||
210           (sa0->crypto_alg >= IPSEC_CRYPTO_ALG_DES_CBC &&
211            sa0->crypto_alg <= IPSEC_CRYPTO_ALG_3DES_CBC))
212         {
213           const int BLOCK_SIZE = sa0->crypto_block_size;
214           const int IV_SIZE = sa0->crypto_block_size;
215           esp_footer_t *f0;
216           u8 ip_hdr_size = 0;
217
218           int blocks =
219             (ib[0]->current_length - sizeof (esp_header_t) -
220              IV_SIZE) / BLOCK_SIZE;
221
222           ob[0]->current_data = sizeof (ethernet_header_t);
223
224           /* transport mode */
225           if (PREDICT_FALSE (!ipsec_sa_is_set_IS_TUNNEL (sa0) &&
226                              !ipsec_sa_is_set_IS_TUNNEL_V6 (sa0)))
227             {
228               tunnel_mode = 0;
229
230               if (is_ip6)
231                 {
232                   ip_hdr_size = sizeof (ip6_header_t);
233                   ih6 = (ip6_header_t *) ((u8 *) esp0 - ip_hdr_size);
234                   oh6 = vlib_buffer_get_current (ob[0]);
235                 }
236               else
237                 {
238                   ip_hdr_size = sizeof (ip4_header_t);
239                   if (ipsec_sa_is_set_UDP_ENCAP (sa0))
240                     ih4 = (ip4_header_t *) ((u8 *) esp0 - ip_hdr_size -
241                                             sizeof (udp_header_t));
242                   else
243                     ih4 = (ip4_header_t *) ((u8 *) esp0 - ip_hdr_size);
244                   oh4 = vlib_buffer_get_current (ob[0]);
245                 }
246             }
247
248           esp_decrypt_cbc (vm, sa0, esp0->data + IV_SIZE,
249                            (u8 *) vlib_buffer_get_current (ob[0]) +
250                            ip_hdr_size, BLOCK_SIZE * blocks,
251                            sa0->crypto_key.data, esp0->data);
252
253           ob[0]->current_length = (blocks * BLOCK_SIZE) - 2 + ip_hdr_size;
254           ob[0]->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
255           f0 = (esp_footer_t *) ((u8 *) vlib_buffer_get_current (ob[0]) +
256                                  ob[0]->current_length);
257           ob[0]->current_length -= f0->pad_length;
258
259           /* tunnel mode */
260           if (PREDICT_TRUE (tunnel_mode))
261             {
262               if (PREDICT_TRUE (f0->next_header == IP_PROTOCOL_IP_IN_IP))
263                 {
264                   next[0] = ESP_DECRYPT_NEXT_IP4_INPUT;
265                   oh4 = vlib_buffer_get_current (ob[0]);
266                 }
267               else if (f0->next_header == IP_PROTOCOL_IPV6)
268                 next[0] = ESP_DECRYPT_NEXT_IP6_INPUT;
269               else
270                 {
271                   vlib_node_increment_counter (vm, node->node_index,
272                                                ESP_DECRYPT_ERROR_DECRYPTION_FAILED,
273                                                1);
274                   ob[0] = 0;
275                   goto trace;
276                 }
277             }
278           /* transport mode */
279           else
280             {
281               u32 len = vlib_buffer_length_in_chain (vm, ob[0]);
282               if (is_ip6)
283                 {
284                   next[0] = ESP_DECRYPT_NEXT_IP6_INPUT;
285                   oh6->ip_version_traffic_class_and_flow_label =
286                     ih6->ip_version_traffic_class_and_flow_label;
287                   oh6->protocol = f0->next_header;
288                   oh6->hop_limit = ih6->hop_limit;
289                   oh6->src_address.as_u64[0] = ih6->src_address.as_u64[0];
290                   oh6->src_address.as_u64[1] = ih6->src_address.as_u64[1];
291                   oh6->dst_address.as_u64[0] = ih6->dst_address.as_u64[0];
292                   oh6->dst_address.as_u64[1] = ih6->dst_address.as_u64[1];
293                   len -= sizeof (ip6_header_t);
294                   oh6->payload_length = clib_host_to_net_u16 (len);
295                 }
296               else
297                 {
298                   next[0] = ESP_DECRYPT_NEXT_IP4_INPUT;
299                   oh4->ip_version_and_header_length = 0x45;
300                   oh4->tos = ih4->tos;
301                   oh4->fragment_id = 0;
302                   oh4->flags_and_fragment_offset = 0;
303                   oh4->ttl = ih4->ttl;
304                   oh4->protocol = f0->next_header;
305                   oh4->src_address.as_u32 = ih4->src_address.as_u32;
306                   oh4->dst_address.as_u32 = ih4->dst_address.as_u32;
307                   oh4->length = clib_host_to_net_u16 (len);
308                   oh4->checksum = ip4_header_checksum (oh4);
309                 }
310             }
311
312           /* for IPSec-GRE tunnel next node is ipsec-gre-input */
313           if (PREDICT_FALSE
314               ((vnet_buffer (ib[0])->ipsec.flags) &
315                IPSEC_FLAG_IPSEC_GRE_TUNNEL))
316             next[0] = ESP_DECRYPT_NEXT_IPSEC_GRE_INPUT;
317
318           vnet_buffer (ob[0])->sw_if_index[VLIB_TX] = (u32) ~ 0;
319           vnet_buffer (ob[0])->sw_if_index[VLIB_RX] =
320             vnet_buffer (ib[0])->sw_if_index[VLIB_RX];
321         }
322
323     trace:
324       if (PREDICT_FALSE (ib[0]->flags & VLIB_BUFFER_IS_TRACED))
325         {
326           if (ob[0])
327             {
328               ob[0]->flags |= VLIB_BUFFER_IS_TRACED;
329               ob[0]->trace_index = ib[0]->trace_index;
330               esp_decrypt_trace_t *tr =
331                 vlib_add_trace (vm, node, ob[0], sizeof (*tr));
332               tr->crypto_alg = sa0->crypto_alg;
333               tr->integ_alg = sa0->integ_alg;
334             }
335         }
336
337       /* next */
338       n_left_from -= 1;
339       ib += 1;
340       ob += 1;
341       next += 1;
342     }
343
344   vlib_node_increment_counter (vm, node->node_index,
345                                ESP_DECRYPT_ERROR_RX_PKTS, n_alloc);
346
347   vlib_buffer_enqueue_to_next (vm, node, new_bufs, nexts, n_alloc);
348 done:
349   vlib_buffer_free (vm, from, from_frame->n_vectors);
350   return n_alloc;
351 }
352
353 VLIB_NODE_FN (esp4_decrypt_node) (vlib_main_t * vm,
354                                   vlib_node_runtime_t * node,
355                                   vlib_frame_t * from_frame)
356 {
357   return esp_decrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ );
358 }
359
360 /* *INDENT-OFF* */
361 VLIB_REGISTER_NODE (esp4_decrypt_node) = {
362   .name = "esp4-decrypt",
363   .vector_size = sizeof (u32),
364   .format_trace = format_esp_decrypt_trace,
365   .type = VLIB_NODE_TYPE_INTERNAL,
366
367   .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
368   .error_strings = esp_decrypt_error_strings,
369
370   .n_next_nodes = ESP_DECRYPT_N_NEXT,
371   .next_nodes = {
372 #define _(s,n) [ESP_DECRYPT_NEXT_##s] = n,
373     foreach_esp_decrypt_next
374 #undef _
375   },
376 };
377 /* *INDENT-ON* */
378
379 VLIB_NODE_FN (esp6_decrypt_node) (vlib_main_t * vm,
380                                   vlib_node_runtime_t * node,
381                                   vlib_frame_t * from_frame)
382 {
383   return esp_decrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ );
384 }
385
386 /* *INDENT-OFF* */
387 VLIB_REGISTER_NODE (esp6_decrypt_node) = {
388   .name = "esp6-decrypt",
389   .vector_size = sizeof (u32),
390   .format_trace = format_esp_decrypt_trace,
391   .type = VLIB_NODE_TYPE_INTERNAL,
392
393   .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
394   .error_strings = esp_decrypt_error_strings,
395
396   .n_next_nodes = ESP_DECRYPT_N_NEXT,
397   .next_nodes = {
398 #define _(s,n) [ESP_DECRYPT_NEXT_##s] = n,
399     foreach_esp_decrypt_next
400 #undef _
401   },
402 };
403 /* *INDENT-ON* */
404
405 /*
406  * fd.io coding-style-patch-verification: ON
407  *
408  * Local Variables:
409  * eval: (c-set-style "gnu")
410  * End:
411  */