Revert "ipsec: Use the new tunnel API types to add flow label and TTL copy"
[vpp.git] / src / vnet / ipsec / ah_encrypt.c
1 /*
2  * ah_encrypt.c : IPSec AH 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
22 #include <vnet/ipsec/ipsec.h>
23 #include <vnet/ipsec/esp.h>
24 #include <vnet/ipsec/ah.h>
25 #include <vnet/tunnel/tunnel_dp.h>
26
27 #define foreach_ah_encrypt_next \
28   _ (DROP, "error-drop")                           \
29   _ (HANDOFF, "handoff")                           \
30   _ (INTERFACE_OUTPUT, "interface-output")
31
32
33 #define _(v, s) AH_ENCRYPT_NEXT_##v,
34 typedef enum
35 {
36   foreach_ah_encrypt_next
37 #undef _
38     AH_ENCRYPT_N_NEXT,
39 } ah_encrypt_next_t;
40
41 #define foreach_ah_encrypt_error                                \
42  _(RX_PKTS, "AH pkts received")                                 \
43  _(CRYPTO_ENGINE_ERROR, "crypto engine error (packet dropped)") \
44  _(SEQ_CYCLED, "sequence number cycled")
45
46
47 typedef enum
48 {
49 #define _(sym,str) AH_ENCRYPT_ERROR_##sym,
50   foreach_ah_encrypt_error
51 #undef _
52     AH_ENCRYPT_N_ERROR,
53 } ah_encrypt_error_t;
54
55 static char *ah_encrypt_error_strings[] = {
56 #define _(sym,string) string,
57   foreach_ah_encrypt_error
58 #undef _
59 };
60
61 typedef struct
62 {
63   u32 sa_index;
64   u32 spi;
65   u32 seq_lo;
66   u32 seq_hi;
67   ipsec_integ_alg_t integ_alg;
68 } ah_encrypt_trace_t;
69
70 /* packet trace format function */
71 static u8 *
72 format_ah_encrypt_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   ah_encrypt_trace_t *t = va_arg (*args, ah_encrypt_trace_t *);
77
78   s = format (s, "ah: sa-index %d spi %u (0x%08x) seq %u:%u integrity %U",
79               t->sa_index, t->spi, t->spi, t->seq_hi, t->seq_lo,
80               format_ipsec_integ_alg, t->integ_alg);
81   return s;
82 }
83
84 static_always_inline void
85 ah_process_ops (vlib_main_t * vm, vlib_node_runtime_t * node,
86                 vnet_crypto_op_t * ops, vlib_buffer_t * b[], u16 * nexts)
87 {
88   u32 n_fail, n_ops = vec_len (ops);
89   vnet_crypto_op_t *op = ops;
90
91   if (n_ops == 0)
92     return;
93
94   n_fail = n_ops - vnet_crypto_process_ops (vm, op, n_ops);
95
96   while (n_fail)
97     {
98       ASSERT (op - ops < n_ops);
99
100       if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
101         {
102           u32 bi = op->user_data;
103           b[bi]->error = node->errors[AH_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR];
104           nexts[bi] = AH_ENCRYPT_NEXT_DROP;
105           n_fail--;
106         }
107       op++;
108     }
109 }
110
111 typedef struct
112 {
113   union
114   {
115     /* Variable fields in the IP header not covered by the AH
116      * integrity check */
117     struct
118     {
119       u8 hop_limit;
120       u32 ip_version_traffic_class_and_flow_label;
121     };
122     struct
123     {
124       u8 ttl;
125       u8 tos;
126     };
127   };
128   i16 current_data;
129   u8 skip;
130   u32 sa_index;
131 } ah_encrypt_packet_data_t;
132
133 always_inline uword
134 ah_encrypt_inline (vlib_main_t * vm,
135                    vlib_node_runtime_t * node, vlib_frame_t * frame,
136                    int is_ip6)
137 {
138   u32 n_left, *from, thread_index;
139   int icv_size = 0;
140   from = vlib_frame_vector_args (frame);
141   n_left = frame->n_vectors;
142   ipsec_main_t *im = &ipsec_main;
143   ah_encrypt_packet_data_t pkt_data[VLIB_FRAME_SIZE], *pd = pkt_data;
144   thread_index = vm->thread_index;
145   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
146   u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
147   ipsec_per_thread_data_t *ptd = vec_elt_at_index (im->ptd, thread_index);
148   ipsec_sa_t *sa0 = 0;
149   ip4_and_ah_header_t *ih0, *oh0 = 0;
150   ip6_and_ah_header_t *ih6_0, *oh6_0 = 0;
151   u32 current_sa_index = ~0, current_sa_bytes = 0, current_sa_pkts = 0;
152   const static ip4_header_t ip4_hdr_template = {
153     .ip_version_and_header_length = 0x45,
154     .protocol = IP_PROTOCOL_IPSEC_AH,
155   };
156   const static ip6_header_t ip6_hdr_template = {
157     .ip_version_traffic_class_and_flow_label = 0x60,
158     .protocol = IP_PROTOCOL_IPSEC_AH,
159   };
160
161   clib_memset (pkt_data, 0, VLIB_FRAME_SIZE * sizeof (pkt_data[0]));
162   vlib_get_buffers (vm, from, b, n_left);
163   vec_reset_length (ptd->crypto_ops);
164   vec_reset_length (ptd->integ_ops);
165
166   while (n_left > 0)
167     {
168       u8 ip_hdr_size;
169       u8 next_hdr_type;
170
171       if (vnet_buffer (b[0])->ipsec.sad_index != current_sa_index)
172         {
173           if (current_sa_index != ~0)
174             vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
175                                              current_sa_index,
176                                              current_sa_pkts,
177                                              current_sa_bytes);
178           current_sa_index = vnet_buffer (b[0])->ipsec.sad_index;
179           sa0 = pool_elt_at_index (im->sad, current_sa_index);
180
181           current_sa_bytes = current_sa_pkts = 0;
182         }
183
184       pd->sa_index = current_sa_index;
185       next[0] = AH_ENCRYPT_NEXT_DROP;
186
187       if (PREDICT_FALSE (~0 == sa0->thread_index))
188         {
189           /* this is the first packet to use this SA, claim the SA
190            * for this thread. this could happen simultaneously on
191            * another thread */
192           clib_atomic_cmp_and_swap (&sa0->thread_index, ~0,
193                                     ipsec_sa_assign_thread (thread_index));
194         }
195
196       if (PREDICT_TRUE (thread_index != sa0->thread_index))
197         {
198           next[0] = AH_ENCRYPT_NEXT_HANDOFF;
199           goto next;
200         }
201
202       if (PREDICT_FALSE (esp_seq_advance (sa0)))
203         {
204           b[0]->error = node->errors[AH_ENCRYPT_ERROR_SEQ_CYCLED];
205           pd->skip = 1;
206           goto next;
207         }
208
209       current_sa_pkts += 1;
210       current_sa_bytes += b[0]->current_length;
211
212       ssize_t adv;
213       ih0 = vlib_buffer_get_current (b[0]);
214
215       if (PREDICT_TRUE (ipsec_sa_is_set_IS_TUNNEL (sa0)))
216         {
217           if (is_ip6)
218             adv = -sizeof (ip6_and_ah_header_t);
219           else
220             adv = -sizeof (ip4_and_ah_header_t);
221         }
222       else
223         {
224           adv = -sizeof (ah_header_t);
225         }
226
227       icv_size = sa0->integ_icv_size;
228       const u8 padding_len = ah_calc_icv_padding_len (icv_size, is_ip6);
229       adv -= padding_len;
230       /* transport mode save the eth header before it is overwritten */
231       if (PREDICT_FALSE (!ipsec_sa_is_set_IS_TUNNEL (sa0)))
232         {
233           const u32 l2_len = vnet_buffer (b[0])->ip.save_rewrite_length;
234           u8 *l2_hdr_in = (u8 *) vlib_buffer_get_current (b[0]) - l2_len;
235
236           u8 *l2_hdr_out = l2_hdr_in + adv - icv_size;
237
238           clib_memcpy_le32 (l2_hdr_out, l2_hdr_in, l2_len);
239         }
240
241       vlib_buffer_advance (b[0], adv - icv_size);
242
243       if (is_ip6)
244         {
245           ih6_0 = (ip6_and_ah_header_t *) ih0;
246           ip_hdr_size = sizeof (ip6_header_t);
247           oh6_0 = vlib_buffer_get_current (b[0]);
248           pd->current_data = b[0]->current_data;
249           pd->hop_limit = ih6_0->ip6.hop_limit;
250
251           oh6_0->ip6.ip_version_traffic_class_and_flow_label =
252             ih6_0->ip6.ip_version_traffic_class_and_flow_label;
253
254           ip6_set_dscp_network_order (&oh6_0->ip6, sa0->dscp);
255
256           tunnel_encap_fixup_6o6 (sa0->tunnel_flags,
257                                   &ih6_0->ip6, &oh6_0->ip6);
258
259           pd->ip_version_traffic_class_and_flow_label =
260             oh6_0->ip6.ip_version_traffic_class_and_flow_label;
261           oh6_0->ip6.ip_version_traffic_class_and_flow_label = 0;
262
263           if (PREDICT_TRUE (ipsec_sa_is_set_IS_TUNNEL (sa0)))
264             {
265               next_hdr_type = IP_PROTOCOL_IPV6;
266             }
267           else
268             {
269               next_hdr_type = ih6_0->ip6.protocol;
270               memmove (oh6_0, ih6_0, sizeof (ip6_header_t));
271             }
272
273           clib_memcpy_fast (&oh6_0->ip6, &ip6_hdr_template, 8);
274           oh6_0->ah.reserved = 0;
275           oh6_0->ah.nexthdr = next_hdr_type;
276           oh6_0->ah.spi = clib_net_to_host_u32 (sa0->spi);
277           oh6_0->ah.seq_no = clib_net_to_host_u32 (sa0->seq);
278           oh6_0->ip6.payload_length =
279             clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b[0]) -
280                                   sizeof (ip6_header_t));
281           oh6_0->ah.hdrlen =
282             (sizeof (ah_header_t) + icv_size + padding_len) / 4 - 2;
283         }
284       else
285         {
286           ip_hdr_size = sizeof (ip4_header_t);
287           oh0 = vlib_buffer_get_current (b[0]);
288           pd->ttl = ih0->ip4.ttl;
289
290           if (sa0->dscp)
291             pd->tos = sa0->dscp << 2;
292           else
293             {
294               pd->tos = ih0->ip4.tos;
295               if (!
296                   (sa0->tunnel_flags &
297                    TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP))
298                 pd->tos &= 0x3;
299               if (!
300                   (sa0->tunnel_flags &
301                    TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN))
302                 pd->tos &= 0xfc;
303             }
304           pd->current_data = b[0]->current_data;
305           clib_memset (oh0, 0, sizeof (ip4_and_ah_header_t));
306
307           if (PREDICT_TRUE (ipsec_sa_is_set_IS_TUNNEL (sa0)))
308             {
309               next_hdr_type = IP_PROTOCOL_IP_IN_IP;
310             }
311           else
312             {
313               next_hdr_type = ih0->ip4.protocol;
314               memmove (oh0, ih0, sizeof (ip4_header_t));
315             }
316
317           clib_memcpy_fast (&oh0->ip4, &ip4_hdr_template,
318                             sizeof (ip4_header_t) -
319                             sizeof (ip4_address_pair_t));
320
321           oh0->ip4.length =
322             clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b[0]));
323           oh0->ah.spi = clib_net_to_host_u32 (sa0->spi);
324           oh0->ah.seq_no = clib_net_to_host_u32 (sa0->seq);
325           oh0->ah.nexthdr = next_hdr_type;
326           oh0->ah.hdrlen =
327             (sizeof (ah_header_t) + icv_size + padding_len) / 4 - 2;
328         }
329
330       if (PREDICT_TRUE (!is_ip6 && ipsec_sa_is_set_IS_TUNNEL (sa0) &&
331                         !ipsec_sa_is_set_IS_TUNNEL_V6 (sa0)))
332         {
333           clib_memcpy_fast (&oh0->ip4.address_pair,
334                             &sa0->ip4_hdr.address_pair,
335                             sizeof (ip4_address_pair_t));
336
337           next[0] = sa0->dpo.dpoi_next_node;
338           vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = sa0->dpo.dpoi_index;
339         }
340       else if (is_ip6 && ipsec_sa_is_set_IS_TUNNEL (sa0) &&
341                ipsec_sa_is_set_IS_TUNNEL_V6 (sa0))
342         {
343           clib_memcpy_fast (&oh6_0->ip6.src_address,
344                             &sa0->ip6_hdr.src_address,
345                             sizeof (ip6_address_t) * 2);
346           next[0] = sa0->dpo.dpoi_next_node;
347           vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = sa0->dpo.dpoi_index;
348         }
349
350       if (PREDICT_TRUE (sa0->integ_op_id))
351         {
352           vnet_crypto_op_t *op;
353           vec_add2_aligned (ptd->integ_ops, op, 1, CLIB_CACHE_LINE_BYTES);
354           vnet_crypto_op_init (op, sa0->integ_op_id);
355           op->src = vlib_buffer_get_current (b[0]);
356           op->len = b[0]->current_length;
357           op->digest = vlib_buffer_get_current (b[0]) + ip_hdr_size +
358             sizeof (ah_header_t);
359           clib_memset (op->digest, 0, icv_size);
360           op->digest_len = icv_size;
361           op->key_index = sa0->integ_key_index;
362           op->user_data = b - bufs;
363           if (ipsec_sa_is_set_USE_ESN (sa0))
364             {
365               u32 seq_hi = clib_host_to_net_u32 (sa0->seq_hi);
366
367               op->len += sizeof (seq_hi);
368               clib_memcpy (op->src + b[0]->current_length, &seq_hi,
369                            sizeof (seq_hi));
370             }
371         }
372
373       if (!ipsec_sa_is_set_IS_TUNNEL (sa0))
374         {
375           next[0] = AH_ENCRYPT_NEXT_INTERFACE_OUTPUT;
376           vlib_buffer_advance (b[0], -sizeof (ethernet_header_t));
377         }
378
379     next:
380       n_left -= 1;
381       next += 1;
382       pd += 1;
383       b += 1;
384     }
385
386   n_left = frame->n_vectors;
387   next = nexts;
388   pd = pkt_data;
389   b = bufs;
390
391   vlib_node_increment_counter (vm, node->node_index,
392                                AH_ENCRYPT_ERROR_RX_PKTS, n_left);
393   vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
394                                    current_sa_index, current_sa_pkts,
395                                    current_sa_bytes);
396
397   ah_process_ops (vm, node, ptd->integ_ops, bufs, nexts);
398
399   while (n_left)
400     {
401       if (pd->skip)
402         goto trace;
403
404       if (is_ip6)
405         {
406           oh6_0 = (ip6_and_ah_header_t *) (b[0]->data + pd->current_data);
407           oh6_0->ip6.hop_limit = pd->hop_limit;
408           oh6_0->ip6.ip_version_traffic_class_and_flow_label =
409             pd->ip_version_traffic_class_and_flow_label;
410         }
411       else
412         {
413           oh0 = (ip4_and_ah_header_t *) (b[0]->data + pd->current_data);
414           oh0->ip4.ttl = pd->ttl;
415           oh0->ip4.tos = pd->tos;
416           oh0->ip4.checksum = ip4_header_checksum (&oh0->ip4);
417         }
418
419     trace:
420       if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
421         {
422           sa0 = vec_elt_at_index (im->sad, pd->sa_index);
423           ah_encrypt_trace_t *tr =
424             vlib_add_trace (vm, node, b[0], sizeof (*tr));
425           tr->spi = sa0->spi;
426           tr->seq_lo = sa0->seq;
427           tr->seq_hi = sa0->seq_hi;
428           tr->integ_alg = sa0->integ_alg;
429           tr->sa_index = pd->sa_index;
430         }
431
432       n_left -= 1;
433       next += 1;
434       pd += 1;
435       b += 1;
436     }
437
438   n_left = frame->n_vectors;
439   vlib_buffer_enqueue_to_next (vm, node, from, nexts, n_left);
440
441   return n_left;
442 }
443
444 VLIB_NODE_FN (ah4_encrypt_node) (vlib_main_t * vm,
445                                  vlib_node_runtime_t * node,
446                                  vlib_frame_t * from_frame)
447 {
448   return ah_encrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ );
449 }
450
451 /* *INDENT-OFF* */
452 VLIB_REGISTER_NODE (ah4_encrypt_node) = {
453   .name = "ah4-encrypt",
454   .vector_size = sizeof (u32),
455   .format_trace = format_ah_encrypt_trace,
456   .type = VLIB_NODE_TYPE_INTERNAL,
457
458   .n_errors = ARRAY_LEN(ah_encrypt_error_strings),
459   .error_strings = ah_encrypt_error_strings,
460
461   .n_next_nodes = AH_ENCRYPT_N_NEXT,
462   .next_nodes = {
463     [AH_ENCRYPT_NEXT_DROP] = "ip4-drop",
464     [AH_ENCRYPT_NEXT_HANDOFF] = "ah4-encrypt-handoff",
465     [AH_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "interface-output",
466   },
467 };
468 /* *INDENT-ON* */
469
470 VLIB_NODE_FN (ah6_encrypt_node) (vlib_main_t * vm,
471                                  vlib_node_runtime_t * node,
472                                  vlib_frame_t * from_frame)
473 {
474   return ah_encrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ );
475 }
476
477 /* *INDENT-OFF* */
478 VLIB_REGISTER_NODE (ah6_encrypt_node) = {
479   .name = "ah6-encrypt",
480   .vector_size = sizeof (u32),
481   .format_trace = format_ah_encrypt_trace,
482   .type = VLIB_NODE_TYPE_INTERNAL,
483
484   .n_errors = ARRAY_LEN(ah_encrypt_error_strings),
485   .error_strings = ah_encrypt_error_strings,
486
487   .n_next_nodes = AH_ENCRYPT_N_NEXT,
488   .next_nodes = {
489     [AH_ENCRYPT_NEXT_DROP] = "ip6-drop",
490     [AH_ENCRYPT_NEXT_HANDOFF] = "ah6-encrypt-handoff",
491     [AH_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "interface-output",
492   },
493 };
494 /* *INDENT-ON* */
495
496 /*
497  * fd.io coding-style-patch-verification: ON
498  *
499  * Local Variables:
500  * eval: (c-set-style "gnu")
501  * End:
502  */