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