crypto: introduce crypto infra
[vpp.git] / src / vnet / ipsec / esp_encrypt.c
1 /*
2  * esp_encrypt.c : IPSec ESP encrypt 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 #include <vnet/udp/udp.h>
22
23 #include <vnet/crypto/crypto.h>
24
25 #include <vnet/ipsec/ipsec.h>
26 #include <vnet/ipsec/esp.h>
27
28 #define foreach_esp_encrypt_next                   \
29 _(DROP, "error-drop")                              \
30 _(IP4_LOOKUP, "ip4-lookup")                        \
31 _(IP6_LOOKUP, "ip6-lookup")                        \
32 _(INTERFACE_OUTPUT, "interface-output")
33
34 #define _(v, s) ESP_ENCRYPT_NEXT_##v,
35 typedef enum
36 {
37   foreach_esp_encrypt_next
38 #undef _
39     ESP_ENCRYPT_N_NEXT,
40 } esp_encrypt_next_t;
41
42 #define foreach_esp_encrypt_error                   \
43  _(RX_PKTS, "ESP pkts received")                    \
44  _(NO_BUFFER, "No buffer (packet dropped)")         \
45  _(DECRYPTION_FAILED, "ESP encryption failed")      \
46  _(SEQ_CYCLED, "sequence number cycled")
47
48
49 typedef enum
50 {
51 #define _(sym,str) ESP_ENCRYPT_ERROR_##sym,
52   foreach_esp_encrypt_error
53 #undef _
54     ESP_ENCRYPT_N_ERROR,
55 } esp_encrypt_error_t;
56
57 static char *esp_encrypt_error_strings[] = {
58 #define _(sym,string) string,
59   foreach_esp_encrypt_error
60 #undef _
61 };
62
63 typedef struct
64 {
65   u32 sa_index;
66   u32 spi;
67   u32 seq;
68   u8 udp_encap;
69   ipsec_crypto_alg_t crypto_alg;
70   ipsec_integ_alg_t integ_alg;
71 } esp_encrypt_trace_t;
72
73 /* packet trace format function */
74 static u8 *
75 format_esp_encrypt_trace (u8 * s, va_list * args)
76 {
77   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
78   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
79   esp_encrypt_trace_t *t = va_arg (*args, esp_encrypt_trace_t *);
80
81   s = format (s, "esp: sa-index %d spi %u seq %u crypto %U integrity %U%s",
82               t->sa_index, t->spi, t->seq,
83               format_ipsec_crypto_alg, t->crypto_alg,
84               format_ipsec_integ_alg, t->integ_alg,
85               t->udp_encap ? " udp-encap-enabled" : "");
86   return s;
87 }
88
89 always_inline void
90 esp_encrypt_cbc (vlib_main_t * vm, ipsec_crypto_alg_t alg,
91                  u8 * in, u8 * out, size_t in_len, u8 * key, u8 * iv)
92 {
93   ipsec_main_t *im = &ipsec_main;
94   ipsec_main_crypto_alg_t *a;
95   vnet_crypto_op_t _op, *op = &_op;
96
97   ASSERT (alg < IPSEC_CRYPTO_N_ALG);
98
99   a = &im->crypto_algs[alg];
100
101   if (PREDICT_FALSE (a->enc_op_type == VNET_CRYPTO_OP_NONE))
102     return;
103
104   op->op = a->enc_op_type;
105   op->flags = VNET_CRYPTO_OP_FLAG_INIT_IV;
106   op->iv = iv;
107   op->src = in;
108   op->dst = out;
109   op->len = in_len;
110   op->key = key;
111
112   vnet_crypto_process_ops (vm, op, 1);
113 }
114
115 always_inline uword
116 esp_encrypt_inline (vlib_main_t * vm,
117                     vlib_node_runtime_t * node, vlib_frame_t * from_frame,
118                     int is_ip6)
119 {
120   u32 *from = vlib_frame_vector_args (from_frame);
121   u32 n_left_from = from_frame->n_vectors;
122   ipsec_main_t *im = &ipsec_main;
123   u32 new_bufs[VLIB_FRAME_SIZE];
124   vlib_buffer_t *i_bufs[VLIB_FRAME_SIZE], **ib = i_bufs;
125   vlib_buffer_t *o_bufs[VLIB_FRAME_SIZE], **ob = o_bufs;
126   u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
127   u32 n_alloc, thread_index = vm->thread_index;
128
129   n_alloc = vlib_buffer_alloc (vm, new_bufs, n_left_from);
130   if (n_alloc != n_left_from)
131     {
132       vlib_node_increment_counter (vm, node->node_index,
133                                    ESP_ENCRYPT_ERROR_NO_BUFFER,
134                                    n_left_from - n_alloc);
135       if (n_alloc == 0)
136         goto done;
137       n_left_from = n_alloc;
138     }
139
140   vlib_get_buffers (vm, from, ib, n_left_from);
141   vlib_get_buffers (vm, new_bufs, ob, n_left_from);
142
143   while (n_left_from > 0)
144     {
145       u32 sa_index0;
146       ipsec_sa_t *sa0;
147       ip4_and_esp_header_t *oh0 = 0;
148       ip6_and_esp_header_t *ih6_0, *oh6_0 = 0;
149       ip4_and_udp_and_esp_header_t *iuh0, *ouh0 = 0;
150       esp_header_t *o_esp0;
151       esp_footer_t *f0;
152       u8 ip_udp_hdr_size;
153       u8 next_hdr_type;
154       u32 ip_proto = 0;
155       u8 transport_mode = 0;
156       u32 esp_seq_err;
157
158       next[0] = ESP_ENCRYPT_NEXT_DROP;
159
160       sa_index0 = vnet_buffer (ib[0])->ipsec.sad_index;
161       sa0 = pool_elt_at_index (im->sad, sa_index0);
162
163       vlib_prefetch_combined_counter (&ipsec_sa_counters, thread_index,
164                                       sa_index0);
165
166       esp_seq_err = esp_seq_advance (sa0);
167
168       /* grab free buffer */
169       ob[0]->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
170       ob[0]->current_data = sizeof (ethernet_header_t);
171       iuh0 = vlib_buffer_get_current (ib[0]);
172
173       if (is_ip6)
174         {
175           ih6_0 = vlib_buffer_get_current (ib[0]);
176           next_hdr_type = IP_PROTOCOL_IPV6;
177           oh6_0 = vlib_buffer_get_current (ob[0]);
178
179           oh6_0->ip6.ip_version_traffic_class_and_flow_label =
180             ih6_0->ip6.ip_version_traffic_class_and_flow_label;
181           oh6_0->ip6.protocol = IP_PROTOCOL_IPSEC_ESP;
182           ip_udp_hdr_size = sizeof (ip6_header_t);
183           o_esp0 = vlib_buffer_get_current (ob[0]) + ip_udp_hdr_size;
184           oh6_0->ip6.hop_limit = 254;
185           oh6_0->ip6.src_address.as_u64[0] = ih6_0->ip6.src_address.as_u64[0];
186           oh6_0->ip6.src_address.as_u64[1] = ih6_0->ip6.src_address.as_u64[1];
187           oh6_0->ip6.dst_address.as_u64[0] = ih6_0->ip6.dst_address.as_u64[0];
188           oh6_0->ip6.dst_address.as_u64[1] = ih6_0->ip6.dst_address.as_u64[1];
189           o_esp0->spi = clib_net_to_host_u32 (sa0->spi);
190           o_esp0->seq = clib_net_to_host_u32 (sa0->seq);
191           ip_proto = ih6_0->ip6.protocol;
192
193           next[0] = ESP_ENCRYPT_NEXT_IP6_LOOKUP;
194         }
195       else
196         {
197           next_hdr_type = IP_PROTOCOL_IP_IN_IP;
198           oh0 = vlib_buffer_get_current (ob[0]);
199           ouh0 = vlib_buffer_get_current (ob[0]);
200
201           oh0->ip4.ip_version_and_header_length = 0x45;
202           oh0->ip4.tos = iuh0->ip4.tos;
203           oh0->ip4.fragment_id = 0;
204           oh0->ip4.flags_and_fragment_offset = 0;
205           oh0->ip4.ttl = 254;
206           if (sa0->udp_encap)
207             {
208               ouh0->udp.src_port = clib_host_to_net_u16 (UDP_DST_PORT_ipsec);
209               ouh0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_ipsec);
210               ouh0->udp.checksum = 0;
211               ouh0->ip4.protocol = IP_PROTOCOL_UDP;
212               ip_udp_hdr_size = sizeof (udp_header_t) + sizeof (ip4_header_t);
213             }
214           else
215             {
216               oh0->ip4.protocol = IP_PROTOCOL_IPSEC_ESP;
217               ip_udp_hdr_size = sizeof (ip4_header_t);
218             }
219           o_esp0 = vlib_buffer_get_current (ob[0]) + ip_udp_hdr_size;
220           oh0->ip4.src_address.as_u32 = iuh0->ip4.src_address.as_u32;
221           oh0->ip4.dst_address.as_u32 = iuh0->ip4.dst_address.as_u32;
222           o_esp0->spi = clib_net_to_host_u32 (sa0->spi);
223           o_esp0->seq = clib_net_to_host_u32 (sa0->seq);
224           ip_proto = iuh0->ip4.protocol;
225
226           next[0] = ESP_ENCRYPT_NEXT_IP4_LOOKUP;
227         }
228
229       if (PREDICT_TRUE (!is_ip6 && sa0->is_tunnel && !sa0->is_tunnel_ip6))
230         {
231           oh0->ip4.src_address.as_u32 = sa0->tunnel_src_addr.ip4.as_u32;
232           oh0->ip4.dst_address.as_u32 = sa0->tunnel_dst_addr.ip4.as_u32;
233
234           next[0] = sa0->dpo[IPSEC_PROTOCOL_ESP].dpoi_next_node;
235           vnet_buffer (ob[0])->ip.adj_index[VLIB_TX] =
236             sa0->dpo[IPSEC_PROTOCOL_ESP].dpoi_index;
237         }
238       else if (is_ip6 && sa0->is_tunnel && sa0->is_tunnel_ip6)
239         {
240           oh6_0->ip6.src_address.as_u64[0] =
241             sa0->tunnel_src_addr.ip6.as_u64[0];
242           oh6_0->ip6.src_address.as_u64[1] =
243             sa0->tunnel_src_addr.ip6.as_u64[1];
244           oh6_0->ip6.dst_address.as_u64[0] =
245             sa0->tunnel_dst_addr.ip6.as_u64[0];
246           oh6_0->ip6.dst_address.as_u64[1] =
247             sa0->tunnel_dst_addr.ip6.as_u64[1];
248
249           next[0] = sa0->dpo[IPSEC_PROTOCOL_ESP].dpoi_next_node;
250           vnet_buffer (ob[0])->ip.adj_index[VLIB_TX] =
251             sa0->dpo[IPSEC_PROTOCOL_ESP].dpoi_index;
252         }
253       else
254         {
255           next_hdr_type = ip_proto;
256           if (vnet_buffer (ib[0])->sw_if_index[VLIB_TX] != ~0)
257             {
258               transport_mode = 1;
259               ethernet_header_t *ieh0, *oeh0;
260               ieh0 =
261                 (ethernet_header_t *) ((u8 *)
262                                        vlib_buffer_get_current (ib[0]) -
263                                        sizeof (ethernet_header_t));
264               oeh0 = (ethernet_header_t *) ob[0]->data;
265               clib_memcpy_fast (oeh0, ieh0, sizeof (ethernet_header_t));
266               next[0] = ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT;
267               vnet_buffer (ob[0])->sw_if_index[VLIB_TX] =
268                 vnet_buffer (ib[0])->sw_if_index[VLIB_TX];
269             }
270
271           if (is_ip6)
272             vlib_buffer_advance (ib[0], sizeof (ip6_header_t));
273           else
274             vlib_buffer_advance (ib[0], sizeof (ip4_header_t));
275         }
276
277       ASSERT (sa0->crypto_alg < IPSEC_CRYPTO_N_ALG);
278       vlib_increment_combined_counter
279         (&ipsec_sa_counters, thread_index, sa_index0,
280          1, ib[0]->current_length);
281
282       if (PREDICT_TRUE (sa0->crypto_alg != IPSEC_CRYPTO_ALG_NONE))
283         {
284
285           const int BLOCK_SIZE = im->crypto_algs[sa0->crypto_alg].block_size;
286           const int IV_SIZE = im->crypto_algs[sa0->crypto_alg].iv_size;
287           int blocks = 1 + (ib[0]->current_length + 1) / BLOCK_SIZE;
288
289           /* pad packet in input buffer */
290           u8 pad_bytes = BLOCK_SIZE * blocks - 2 - ib[0]->current_length;
291           u8 i;
292           u8 *padding =
293             vlib_buffer_get_current (ib[0]) + ib[0]->current_length;
294           ib[0]->current_length = BLOCK_SIZE * blocks;
295           for (i = 0; i < pad_bytes; ++i)
296             {
297               padding[i] = i + 1;
298             }
299           f0 = vlib_buffer_get_current (ib[0]) + ib[0]->current_length - 2;
300           f0->pad_length = pad_bytes;
301           f0->next_header = next_hdr_type;
302
303           ob[0]->current_length = ip_udp_hdr_size + sizeof (esp_header_t) +
304             BLOCK_SIZE * blocks + IV_SIZE;
305
306           vnet_buffer (ob[0])->sw_if_index[VLIB_RX] =
307             vnet_buffer (ib[0])->sw_if_index[VLIB_RX];
308
309           u8 *iv = vlib_buffer_get_current (ob[0]) + ip_udp_hdr_size +
310             sizeof (esp_header_t);
311
312           clib_memcpy_fast ((u8 *) vlib_buffer_get_current (ob[0]) +
313                             ip_udp_hdr_size + sizeof (esp_header_t), iv,
314                             im->crypto_algs[sa0->crypto_alg].iv_size);
315
316           esp_encrypt_cbc (vm, sa0->crypto_alg,
317                            (u8 *) vlib_buffer_get_current (ib[0]),
318                            (u8 *) vlib_buffer_get_current (ob[0]) +
319                            ip_udp_hdr_size + sizeof (esp_header_t) +
320                            IV_SIZE, BLOCK_SIZE * blocks,
321                            sa0->crypto_key.data, iv);
322         }
323
324       ob[0]->current_length +=
325         hmac_calc (vm, sa0->integ_alg, sa0->integ_key.data,
326                    sa0->integ_key.len, (u8 *) o_esp0,
327                    ob[0]->current_length - ip_udp_hdr_size,
328                    vlib_buffer_get_current (ob[0]) + ob[0]->current_length,
329                    sa0->use_esn, sa0->seq_hi);
330
331
332       if (is_ip6)
333         {
334           oh6_0->ip6.payload_length =
335             clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, ob[0]) -
336                                   sizeof (ip6_header_t));
337         }
338       else
339         {
340           oh0->ip4.length =
341             clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, ob[0]));
342           oh0->ip4.checksum = ip4_header_checksum (&oh0->ip4);
343           if (sa0->udp_encap)
344             {
345               ouh0->udp.length =
346                 clib_host_to_net_u16 (clib_net_to_host_u16
347                                       (oh0->ip4.length) -
348                                       ip4_header_bytes (&oh0->ip4));
349             }
350         }
351
352       if (transport_mode)
353         vlib_buffer_reset (ob[0]);
354
355       if (PREDICT_FALSE (esp_seq_err))
356         {
357           ob[0]->error = node->errors[ESP_ENCRYPT_ERROR_SEQ_CYCLED];
358           next[0] = ESP_ENCRYPT_NEXT_DROP;
359         }
360
361       if (PREDICT_FALSE (ib[0]->flags & VLIB_BUFFER_IS_TRACED))
362         {
363           if (ob[0])
364             {
365               ob[0]->flags |= VLIB_BUFFER_IS_TRACED;
366               ob[0]->trace_index = ib[0]->trace_index;
367               esp_encrypt_trace_t *tr =
368                 vlib_add_trace (vm, node, ob[0], sizeof (*tr));
369               tr->sa_index = sa_index0;
370               tr->spi = sa0->spi;
371               tr->seq = sa0->seq - 1;
372               tr->udp_encap = sa0->udp_encap;
373               tr->crypto_alg = sa0->crypto_alg;
374               tr->integ_alg = sa0->integ_alg;
375             }
376         }
377
378       /* next */
379       n_left_from -= 1;
380       ib += 1;
381       ob += 1;
382       next += 1;
383     }
384
385   vlib_node_increment_counter (vm, node->node_index,
386                                ESP_ENCRYPT_ERROR_RX_PKTS, n_alloc);
387
388   vlib_buffer_enqueue_to_next (vm, node, new_bufs, nexts, n_alloc);
389 done:
390   vlib_buffer_free (vm, from, from_frame->n_vectors);
391   return n_alloc;
392 }
393
394 VLIB_NODE_FN (esp4_encrypt_node) (vlib_main_t * vm,
395                                   vlib_node_runtime_t * node,
396                                   vlib_frame_t * from_frame)
397 {
398   return esp_encrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ );
399 }
400
401 /* *INDENT-OFF* */
402 VLIB_REGISTER_NODE (esp4_encrypt_node) = {
403   .name = "esp4-encrypt",
404   .vector_size = sizeof (u32),
405   .format_trace = format_esp_encrypt_trace,
406   .type = VLIB_NODE_TYPE_INTERNAL,
407
408   .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
409   .error_strings = esp_encrypt_error_strings,
410
411   .n_next_nodes = ESP_ENCRYPT_N_NEXT,
412   .next_nodes = {
413 #define _(s,n) [ESP_ENCRYPT_NEXT_##s] = n,
414     foreach_esp_encrypt_next
415 #undef _
416   },
417 };
418 /* *INDENT-ON* */
419
420 VLIB_NODE_FN (esp6_encrypt_node) (vlib_main_t * vm,
421                                   vlib_node_runtime_t * node,
422                                   vlib_frame_t * from_frame)
423 {
424   return esp_encrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ );
425 }
426
427 /* *INDENT-OFF* */
428 VLIB_REGISTER_NODE (esp6_encrypt_node) = {
429   .name = "esp6-encrypt",
430   .vector_size = sizeof (u32),
431   .format_trace = format_esp_encrypt_trace,
432   .type = VLIB_NODE_TYPE_INTERNAL,
433
434   .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
435   .error_strings = esp_encrypt_error_strings,
436
437   .n_next_nodes = ESP_ENCRYPT_N_NEXT,
438   .next_nodes = {
439 #define _(s,n) [ESP_ENCRYPT_NEXT_##s] = n,
440     foreach_esp_encrypt_next
441 #undef _
442   },
443 };
444 /* *INDENT-ON* */
445
446 /*
447  * fd.io coding-style-patch-verification: ON
448  *
449  * Local Variables:
450  * eval: (c-set-style "gnu")
451  * End:
452  */