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