2 * Copyright (c) 2020 Doc.ai and/or its affiliates.
3 * Copyright (c) 2020 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <vlib/vlib.h>
18 #include <vnet/vnet.h>
19 #include <vppinfra/error.h>
21 #include <wireguard/wireguard.h>
22 #include <wireguard/wireguard_send.h>
24 #define foreach_wg_output_error \
25 _ (NONE, "No error") \
26 _ (PEER, "Peer error") \
27 _ (KEYPAIR, "Keypair error") \
28 _ (NO_BUFFERS, "No buffers") \
29 _ (CRYPTO_ENGINE_ERROR, "crypto engine error (packet dropped)")
33 #define _(sym,str) WG_OUTPUT_ERROR_##sym,
34 foreach_wg_output_error
39 static char *wg_output_error_strings[] = {
40 #define _(sym,string) string,
41 foreach_wg_output_error
48 WG_OUTPUT_NEXT_HANDOFF,
49 WG_OUTPUT_NEXT_INTERFACE_OUTPUT,
56 u8 header[sizeof (ip6_udp_header_t)];
58 } wg_output_tun_trace_t;
64 } wg_output_tun_post_trace_t;
67 format_ip4_udp_header (u8 * s, va_list * args)
69 ip4_udp_header_t *hdr4 = va_arg (*args, ip4_udp_header_t *);
71 s = format (s, "%U:$U", format_ip4_header, &hdr4->ip4, format_udp_header,
77 format_ip6_udp_header (u8 *s, va_list *args)
79 ip6_udp_header_t *hdr6 = va_arg (*args, ip6_udp_header_t *);
81 s = format (s, "%U:$U", format_ip6_header, &hdr6->ip6, format_udp_header,
86 /* packet trace format function */
88 format_wg_output_tun_trace (u8 * s, va_list * args)
90 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
91 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
93 wg_output_tun_trace_t *t = va_arg (*args, wg_output_tun_trace_t *);
95 s = format (s, "peer: %d\n", t->peer);
96 s = format (s, " Encrypted packet: ");
98 s = t->is_ip4 ? format (s, "%U", format_ip4_udp_header, t->header) :
99 format (s, "%U", format_ip6_udp_header, t->header);
103 /* post node - packet trace format function */
105 format_wg_output_tun_post_trace (u8 *s, va_list *args)
107 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
108 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
110 wg_output_tun_post_trace_t *t = va_arg (*args, wg_output_tun_post_trace_t *);
112 s = format (s, "peer: %d\n", t->peer);
113 s = format (s, " wg-post: next node index %u", t->next_index);
117 static_always_inline void
118 wg_output_chain_crypto (vlib_main_t *vm, wg_per_thread_data_t *ptd,
119 vlib_buffer_t *b, vlib_buffer_t *lb, u8 *start,
120 u32 start_len, u16 *n_ch)
122 vnet_crypto_op_chunk_t *ch;
123 vlib_buffer_t *cb = b;
126 vec_add2 (ptd->chunks, ch, 1);
128 ch->src = ch->dst = start;
129 cb = vlib_get_buffer (vm, cb->next_buffer);
133 vec_add2 (ptd->chunks, ch, 1);
136 ch->len = cb->current_length - NOISE_AUTHTAG_LEN;
138 ch->len = cb->current_length;
140 ch->src = ch->dst = vlib_buffer_get_current (cb);
142 if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT))
145 cb = vlib_get_buffer (vm, cb->next_buffer);
152 static_always_inline void
153 wg_prepare_sync_enc_op (vlib_main_t *vm, wg_per_thread_data_t *ptd,
154 vlib_buffer_t *b, vlib_buffer_t *lb,
155 vnet_crypto_op_t **crypto_ops, u8 *src, u32 src_len,
156 u8 *dst, u8 *aad, u32 aad_len, u64 nonce,
157 vnet_crypto_key_index_t key_index, u32 bi, u8 *iv)
159 vnet_crypto_op_t _op, *op = &_op;
162 clib_memset (iv, 0, 4);
163 clib_memcpy (iv + 4, &nonce, sizeof (nonce));
165 vec_add2_aligned (crypto_ops[0], op, 1, CLIB_CACHE_LINE_BYTES);
166 vnet_crypto_op_init (op, VNET_CRYPTO_OP_CHACHA20_POLY1305_ENC);
168 op->tag_len = NOISE_AUTHTAG_LEN;
169 op->tag = vlib_buffer_get_tail (lb) - NOISE_AUTHTAG_LEN;
170 op->key_index = key_index;
172 op->aad_len = aad_len;
178 /* Chained buffers */
179 op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
180 op->chunk_index = vec_len (ptd->chunks);
181 wg_output_chain_crypto (vm, ptd, b, lb, src, src_len, &op->n_chunks);
185 op->src = !src ? src_ : src;
191 static_always_inline void
192 wg_output_process_chained_ops (vlib_main_t *vm, vlib_node_runtime_t *node,
193 vnet_crypto_op_t *ops, vlib_buffer_t *b[],
194 u16 *nexts, vnet_crypto_op_chunk_t *chunks,
197 u32 n_fail, n_ops = vec_len (ops);
198 vnet_crypto_op_t *op = ops;
203 n_fail = n_ops - vnet_crypto_process_chained_ops (vm, op, chunks, n_ops);
207 ASSERT (op - ops < n_ops);
209 if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
211 u32 bi = op->user_data;
212 b[bi]->error = node->errors[WG_OUTPUT_ERROR_CRYPTO_ENGINE_ERROR];
213 nexts[bi] = drop_next;
220 static_always_inline void
221 wg_output_process_ops (vlib_main_t *vm, vlib_node_runtime_t *node,
222 vnet_crypto_op_t *ops, vlib_buffer_t *b[], u16 *nexts,
225 u32 n_fail, n_ops = vec_len (ops);
226 vnet_crypto_op_t *op = ops;
231 n_fail = n_ops - vnet_crypto_process_ops (vm, op, n_ops);
235 ASSERT (op - ops < n_ops);
237 if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
239 u32 bi = op->user_data;
240 b[bi]->error = node->errors[WG_OUTPUT_ERROR_CRYPTO_ENGINE_ERROR];
241 nexts[bi] = drop_next;
248 static_always_inline void
249 wg_output_tun_add_to_frame (vlib_main_t *vm, vnet_crypto_async_frame_t *f,
250 u32 key_index, u32 crypto_len,
251 i16 crypto_start_offset, u32 buffer_index,
252 u16 next_node, u8 *iv, u8 *tag, u8 flags)
254 vnet_crypto_async_frame_elt_t *fe;
257 ASSERT (f->n_elts < VNET_CRYPTO_FRAME_SIZE);
260 fe = &f->elts[index];
262 fe->key_index = key_index;
263 fe->crypto_total_length = crypto_len;
264 fe->crypto_start_offset = crypto_start_offset;
268 f->buffer_indices[index] = buffer_index;
269 f->next_node_index[index] = next_node;
272 static_always_inline enum noise_state_crypt
273 wg_output_tun_process (vlib_main_t *vm, wg_per_thread_data_t *ptd,
274 vlib_buffer_t *b, vlib_buffer_t *lb,
275 vnet_crypto_op_t **crypto_ops, noise_remote_t *r,
276 uint32_t *r_idx, uint64_t *nonce, uint8_t *src,
277 size_t srclen, uint8_t *dst, u32 bi, u8 *iv, f64 time)
280 enum noise_state_crypt ret = SC_FAILED;
282 if ((kp = r->r_current) == NULL)
285 /* We confirm that our values are within our tolerances. We want:
287 * - our keypair to be less than REJECT_AFTER_TIME seconds old
288 * - our receive counter to be less than REJECT_AFTER_MESSAGES
289 * - our send counter to be less than REJECT_AFTER_MESSAGES
292 wg_birthdate_has_expired_opt (kp->kp_birthdate, REJECT_AFTER_TIME,
294 kp->kp_ctr.c_recv >= REJECT_AFTER_MESSAGES ||
295 ((*nonce = noise_counter_send (&kp->kp_ctr)) > REJECT_AFTER_MESSAGES))
298 /* We encrypt into the same buffer, so the caller must ensure that buf
299 * has NOISE_AUTHTAG_LEN bytes to store the MAC. The nonce and index
300 * are passed back out to the caller through the provided data pointer. */
301 *r_idx = kp->kp_remote_index;
303 wg_prepare_sync_enc_op (vm, ptd, b, lb, crypto_ops, src, srclen, dst, NULL,
304 0, *nonce, kp->kp_send_index, bi, iv);
306 /* If our values are still within tolerances, but we are approaching
307 * the tolerances, we notify the caller with ESTALE that they should
308 * establish a new keypair. The current keypair can continue to be used
309 * until the tolerances are hit. We notify if:
310 * - our send counter is valid and not less than REKEY_AFTER_MESSAGES
311 * - we're the initiator and our keypair is older than
312 * REKEY_AFTER_TIME seconds */
313 ret = SC_KEEP_KEY_FRESH;
314 if ((kp->kp_valid && *nonce >= REKEY_AFTER_MESSAGES) ||
315 (kp->kp_is_initiator && wg_birthdate_has_expired_opt (
316 kp->kp_birthdate, REKEY_AFTER_TIME, time)))
324 static_always_inline enum noise_state_crypt
325 wg_add_to_async_frame (vlib_main_t *vm, wg_per_thread_data_t *ptd,
326 vnet_crypto_async_frame_t **async_frame,
327 vlib_buffer_t *b, vlib_buffer_t *lb, u8 *payload,
328 u32 payload_len, u32 bi, u16 next, u16 async_next,
329 noise_remote_t *r, uint32_t *r_idx, uint64_t *nonce,
332 wg_post_data_t *post = wg_post_data (b);
337 post->next_index = next;
340 enum noise_state_crypt ret = SC_FAILED;
342 if ((kp = r->r_current) == NULL)
345 /* We confirm that our values are within our tolerances. We want:
347 * - our keypair to be less than REJECT_AFTER_TIME seconds old
348 * - our receive counter to be less than REJECT_AFTER_MESSAGES
349 * - our send counter to be less than REJECT_AFTER_MESSAGES
352 wg_birthdate_has_expired_opt (kp->kp_birthdate, REJECT_AFTER_TIME,
354 kp->kp_ctr.c_recv >= REJECT_AFTER_MESSAGES ||
355 ((*nonce = noise_counter_send (&kp->kp_ctr)) > REJECT_AFTER_MESSAGES))
358 /* We encrypt into the same buffer, so the caller must ensure that buf
359 * has NOISE_AUTHTAG_LEN bytes to store the MAC. The nonce and index
360 * are passed back out to the caller through the provided data pointer. */
361 *r_idx = kp->kp_remote_index;
363 clib_memset (iv, 0, 4);
364 clib_memcpy (iv + 4, nonce, sizeof (*nonce));
366 /* get a frame for this op if we don't yet have one or it's full */
367 if (NULL == *async_frame || vnet_crypto_async_frame_is_full (*async_frame))
369 *async_frame = vnet_crypto_async_get_frame (
370 vm, VNET_CRYPTO_OP_CHACHA20_POLY1305_TAG16_AAD0_ENC);
371 if (PREDICT_FALSE (NULL == *async_frame))
373 /* Save the frame to the list we'll submit at the end */
374 vec_add1 (ptd->async_frames, *async_frame);
378 flag |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
380 tag = vlib_buffer_get_tail (lb) - NOISE_AUTHTAG_LEN;
382 /* this always succeeds because we know the frame is not full */
383 wg_output_tun_add_to_frame (vm, *async_frame, kp->kp_send_index, payload_len,
384 payload - b->data, bi, async_next, iv, tag,
387 /* If our values are still within tolerances, but we are approaching
388 * the tolerances, we notify the caller with ESTALE that they should
389 * establish a new keypair. The current keypair can continue to be used
390 * until the tolerances are hit. We notify if:
391 * - our send counter is valid and not less than REKEY_AFTER_MESSAGES
392 * - we're the initiator and our keypair is older than
393 * REKEY_AFTER_TIME seconds */
394 ret = SC_KEEP_KEY_FRESH;
395 if ((kp->kp_valid && *nonce >= REKEY_AFTER_MESSAGES) ||
396 (kp->kp_is_initiator && wg_birthdate_has_expired_opt (
397 kp->kp_birthdate, REKEY_AFTER_TIME, time)))
405 static_always_inline void
406 wg_calc_checksum (vlib_main_t *vm, vlib_buffer_t *b)
409 u8 ip_ver_out = (*((u8 *) vlib_buffer_get_current (b)) >> 4);
411 /* IPv6 UDP checksum is mandatory */
415 (ip6_header_t *) ((u8 *) vlib_buffer_get_current (b));
416 udp_header_t *udp = ip6_next_header (ip6);
417 udp->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b, ip6, &bogus);
421 /* is_ip4 - inner header flag */
423 wg_output_tun_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
424 vlib_frame_t *frame, u8 is_ip4, u16 async_next_node)
426 wg_main_t *wmp = &wg_main;
427 wg_per_thread_data_t *ptd =
428 vec_elt_at_index (wmp->per_thread_data, vm->thread_index);
429 u32 *from = vlib_frame_vector_args (frame);
430 u32 n_left_from = frame->n_vectors;
431 ip4_udp_wg_header_t *hdr4_out = NULL;
432 ip6_udp_wg_header_t *hdr6_out = NULL;
433 message_data_t *message_data_wg = NULL;
434 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
436 vnet_crypto_op_t **crypto_ops;
437 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
438 vlib_buffer_t *sync_bufs[VLIB_FRAME_SIZE];
439 u32 thread_index = vm->thread_index;
441 const u16 drop_next = WG_OUTPUT_NEXT_ERROR;
442 const u8 is_async = wg_op_mode_is_set_ASYNC ();
443 vnet_crypto_async_frame_t *async_frame = NULL;
445 u16 noop_nexts[VLIB_FRAME_SIZE], *noop_next = noop_nexts, n_noop = 0;
447 u32 sync_bi[VLIB_FRAME_SIZE];
448 u32 noop_bi[VLIB_FRAME_SIZE];
450 vlib_get_buffers (vm, from, bufs, n_left_from);
451 vec_reset_length (ptd->crypto_ops);
452 vec_reset_length (ptd->chained_crypto_ops);
453 vec_reset_length (ptd->chunks);
454 vec_reset_length (ptd->async_frames);
456 wg_peer_t *peer = NULL;
458 u32 last_adj_index = ~0;
459 index_t peeri = INDEX_INVALID;
461 f64 time = clib_time_now (&vm->clib_time) + vm->time_offset;
463 while (n_left_from > 0)
469 u16 plain_data_len_total;
471 u16 b_space_left_at_beginning;
472 u32 bi = from[b - bufs];
477 vlib_prefetch_buffer_header (b[2], LOAD);
478 p = vlib_buffer_get_current (b[1]);
479 CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD);
480 CLIB_PREFETCH (vlib_buffer_get_tail (b[1]), CLIB_CACHE_LINE_BYTES,
484 noop_next[0] = WG_OUTPUT_NEXT_ERROR;
485 err = WG_OUTPUT_NEXT_ERROR;
487 adj_index = vnet_buffer (b[0])->ip.adj_index[VLIB_TX];
489 if (PREDICT_FALSE (last_adj_index != adj_index))
491 peeri = wg_peer_get_by_adj_index (adj_index);
492 if (peeri == INDEX_INVALID)
494 b[0]->error = node->errors[WG_OUTPUT_ERROR_PEER];
497 peer = wg_peer_get (peeri);
500 if (!peer || wg_peer_is_dead (peer))
502 b[0]->error = node->errors[WG_OUTPUT_ERROR_PEER];
505 if (PREDICT_FALSE (~0 == peer->output_thread_index))
507 /* this is the first packet to use this peer, claim the peer
510 clib_atomic_cmp_and_swap (&peer->output_thread_index, ~0,
511 wg_peer_assign_thread (thread_index));
514 if (PREDICT_FALSE (thread_index != peer->output_thread_index))
516 noop_next[0] = WG_OUTPUT_NEXT_HANDOFF;
517 err = WG_OUTPUT_NEXT_HANDOFF;
521 if (PREDICT_FALSE (!peer->remote.r_current))
523 wg_send_handshake_from_mt (peeri, false);
524 b[0]->error = node->errors[WG_OUTPUT_ERROR_KEYPAIR];
529 n_bufs = vlib_buffer_chain_linearize (vm, b[0]);
532 b[0]->error = node->errors[WG_OUTPUT_ERROR_NO_BUFFERS];
538 /* Find last buffer in the chain */
539 while (lb->flags & VLIB_BUFFER_NEXT_PRESENT)
540 lb = vlib_get_buffer (vm, lb->next_buffer);
543 /* Ensure there is enough free space at the beginning of the first buffer
544 * to write ethernet header (e.g. IPv6 VxLAN over IPv6 Wireguard will
547 ASSERT ((signed) b[0]->current_data >=
548 (signed) -VLIB_BUFFER_PRE_DATA_SIZE);
549 b_space_left_at_beginning =
550 b[0]->current_data + VLIB_BUFFER_PRE_DATA_SIZE;
551 if (PREDICT_FALSE (b_space_left_at_beginning <
552 sizeof (ethernet_header_t)))
555 sizeof (ethernet_header_t) - b_space_left_at_beginning;
557 /* Can only move buffer when it's single and has enough free space*/
559 vlib_buffer_space_left_at_end (vm, b[0]) >= size_diff)
561 vlib_buffer_move (vm, b[0],
562 b[0]->current_data + (signed) size_diff);
566 b[0]->error = node->errors[WG_OUTPUT_ERROR_NO_BUFFERS];
572 * Ensure there is enough free space at the end of the last buffer to
574 if (PREDICT_FALSE (vlib_buffer_space_left_at_end (vm, lb) <
578 if (vlib_buffer_alloc (vm, &tmp_bi, 1) != 1)
580 b[0]->error = node->errors[WG_OUTPUT_ERROR_NO_BUFFERS];
583 lb = vlib_buffer_chain_buffer (vm, lb, tmp_bi);
586 iph_offset = vnet_buffer (b[0])->ip.save_rewrite_length;
587 plain_data = vlib_buffer_get_current (b[0]) + iph_offset;
588 plain_data_len = b[0]->current_length - iph_offset;
589 plain_data_len_total =
590 vlib_buffer_length_in_chain (vm, b[0]) - iph_offset;
591 size_t encrypted_packet_len = message_data_len (plain_data_len_total);
592 vlib_buffer_chain_increase_length (b[0], lb, NOISE_AUTHTAG_LEN);
593 u8 *iv_data = b[0]->pre_data;
595 is_ip4_out = ip46_address_is_ip4 (&peer->src.addr);
598 hdr4_out = vlib_buffer_get_current (b[0]);
599 message_data_wg = &hdr4_out->wg;
603 hdr6_out = vlib_buffer_get_current (b[0]);
604 message_data_wg = &hdr6_out->wg;
607 if (PREDICT_FALSE (last_adj_index != adj_index))
609 wg_timers_any_authenticated_packet_sent_opt (peer, time);
610 wg_timers_data_sent_opt (peer, time);
611 wg_timers_any_authenticated_packet_traversal (peer);
612 last_adj_index = adj_index;
615 /* Here we are sure that can send packet to next node */
616 next[0] = WG_OUTPUT_NEXT_INTERFACE_OUTPUT;
619 crypto_ops = &ptd->chained_crypto_ops;
621 crypto_ops = &ptd->crypto_ops;
623 enum noise_state_crypt state;
627 state = wg_add_to_async_frame (
628 vm, ptd, &async_frame, b[0], lb, plain_data, plain_data_len_total,
629 bi, next[0], async_next_node, &peer->remote,
630 &message_data_wg->receiver_index, &message_data_wg->counter,
635 state = wg_output_tun_process (
636 vm, ptd, b[0], lb, crypto_ops, &peer->remote,
637 &message_data_wg->receiver_index, &message_data_wg->counter,
638 plain_data, plain_data_len, plain_data, n_sync, iv_data, time);
641 if (PREDICT_FALSE (state == SC_KEEP_KEY_FRESH))
643 wg_send_handshake_from_mt (peeri, false);
645 else if (PREDICT_FALSE (state == SC_FAILED))
648 wg_send_handshake_from_mt (peeri, false);
649 wg_peer_update_flags (peeri, WG_PEER_ESTABLISHED, false);
650 noop_next[0] = WG_OUTPUT_NEXT_ERROR;
654 err = WG_OUTPUT_NEXT_INTERFACE_OUTPUT;
658 hdr4_out->wg.header.type = MESSAGE_DATA;
659 hdr4_out->udp.length = clib_host_to_net_u16 (encrypted_packet_len +
660 sizeof (udp_header_t));
661 ip4_header_set_len_w_chksum (
662 &hdr4_out->ip4, clib_host_to_net_u16 (encrypted_packet_len +
663 sizeof (ip4_udp_header_t)));
667 hdr6_out->wg.header.type = MESSAGE_DATA;
668 hdr6_out->ip6.payload_length = hdr6_out->udp.length =
669 clib_host_to_net_u16 (encrypted_packet_len +
670 sizeof (udp_header_t));
674 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
675 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
677 wg_output_tun_trace_t *t =
678 vlib_add_trace (vm, node, b[0], sizeof (*t));
681 t->is_ip4 = is_ip4_out;
683 clib_memcpy (t->header, hdr4_out, sizeof (ip4_udp_header_t));
685 clib_memcpy (t->header, hdr6_out, sizeof (ip6_udp_header_t));
689 if (PREDICT_FALSE (err != WG_OUTPUT_NEXT_INTERFACE_OUTPUT))
691 noop_bi[n_noop] = bi;
698 sync_bi[n_sync] = bi;
699 sync_bufs[n_sync] = b[0];
714 /* wg-output-process-ops */
715 wg_output_process_ops (vm, node, ptd->crypto_ops, sync_bufs, nexts,
717 wg_output_process_chained_ops (vm, node, ptd->chained_crypto_ops,
718 sync_bufs, nexts, ptd->chunks, drop_next);
720 int n_left_from_sync_bufs = n_sync;
721 while (n_left_from_sync_bufs > 0)
723 n_left_from_sync_bufs--;
724 wg_calc_checksum (vm, sync_bufs[n_left_from_sync_bufs]);
727 vlib_buffer_enqueue_to_next (vm, node, sync_bi, nexts, n_sync);
731 /* submit all of the open frames */
732 vnet_crypto_async_frame_t **async_frame;
734 vec_foreach (async_frame, ptd->async_frames)
737 vnet_crypto_async_submit_open_frame (vm, *async_frame) < 0))
739 u32 n_drop = (*async_frame)->n_elts;
740 u32 *bi = (*async_frame)->buffer_indices;
744 noop_bi[index] = bi[0];
745 vlib_buffer_t *b = vlib_get_buffer (vm, bi[0]);
746 noop_nexts[index] = drop_next;
747 b->error = node->errors[WG_OUTPUT_ERROR_CRYPTO_ENGINE_ERROR];
751 n_noop += (*async_frame)->n_elts;
753 vnet_crypto_async_reset_frame (*async_frame);
754 vnet_crypto_async_free_frame (vm, *async_frame);
760 vlib_buffer_enqueue_to_next (vm, node, noop_bi, noop_nexts, n_noop);
763 return frame->n_vectors;
767 wg_output_tun_post (vlib_main_t *vm, vlib_node_runtime_t *node,
770 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
771 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
772 u32 *from = vlib_frame_vector_args (frame);
773 u32 n_left = frame->n_vectors;
777 vlib_get_buffers (vm, from, b, n_left);
781 vlib_prefetch_buffer_header (b[0], LOAD);
782 vlib_prefetch_buffer_header (b[1], LOAD);
783 vlib_prefetch_buffer_header (b[2], LOAD);
784 vlib_prefetch_buffer_header (b[3], LOAD);
789 vlib_prefetch_buffer_header (b[4], LOAD);
790 vlib_prefetch_buffer_header (b[5], LOAD);
791 vlib_prefetch_buffer_header (b[6], LOAD);
792 vlib_prefetch_buffer_header (b[7], LOAD);
794 next[0] = (wg_post_data (b[0]))->next_index;
795 next[1] = (wg_post_data (b[1]))->next_index;
796 next[2] = (wg_post_data (b[2]))->next_index;
797 next[3] = (wg_post_data (b[3]))->next_index;
799 wg_calc_checksum (vm, b[0]);
800 wg_calc_checksum (vm, b[1]);
801 wg_calc_checksum (vm, b[2]);
802 wg_calc_checksum (vm, b[3]);
804 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
806 if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
808 wg_output_tun_post_trace_t *tr =
809 vlib_add_trace (vm, node, b[0], sizeof (*tr));
810 peeri = wg_peer_get_by_adj_index (
811 vnet_buffer (b[0])->ip.adj_index[VLIB_TX]);
813 tr->next_index = next[0];
815 if (b[1]->flags & VLIB_BUFFER_IS_TRACED)
817 wg_output_tun_post_trace_t *tr =
818 vlib_add_trace (vm, node, b[1], sizeof (*tr));
819 peeri = wg_peer_get_by_adj_index (
820 vnet_buffer (b[1])->ip.adj_index[VLIB_TX]);
821 tr->next_index = next[1];
823 if (b[2]->flags & VLIB_BUFFER_IS_TRACED)
825 wg_output_tun_post_trace_t *tr =
826 vlib_add_trace (vm, node, b[2], sizeof (*tr));
827 peeri = wg_peer_get_by_adj_index (
828 vnet_buffer (b[2])->ip.adj_index[VLIB_TX]);
829 tr->next_index = next[2];
831 if (b[3]->flags & VLIB_BUFFER_IS_TRACED)
833 wg_output_tun_post_trace_t *tr =
834 vlib_add_trace (vm, node, b[3], sizeof (*tr));
835 peeri = wg_peer_get_by_adj_index (
836 vnet_buffer (b[3])->ip.adj_index[VLIB_TX]);
837 tr->next_index = next[3];
848 wg_calc_checksum (vm, b[0]);
850 next[0] = (wg_post_data (b[0]))->next_index;
851 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
852 (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
854 wg_output_tun_post_trace_t *tr =
855 vlib_add_trace (vm, node, b[0], sizeof (*tr));
856 peeri = wg_peer_get_by_adj_index (
857 vnet_buffer (b[0])->ip.adj_index[VLIB_TX]);
858 tr->next_index = next[0];
866 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
867 return frame->n_vectors;
870 VLIB_REGISTER_NODE (wg4_output_tun_post_node) = {
871 .name = "wg4-output-tun-post-node",
872 .vector_size = sizeof (u32),
873 .format_trace = format_wg_output_tun_post_trace,
874 .type = VLIB_NODE_TYPE_INTERNAL,
875 .sibling_of = "wg4-output-tun",
876 .n_errors = ARRAY_LEN (wg_output_error_strings),
877 .error_strings = wg_output_error_strings,
880 VLIB_REGISTER_NODE (wg6_output_tun_post_node) = {
881 .name = "wg6-output-tun-post-node",
882 .vector_size = sizeof (u32),
883 .format_trace = format_wg_output_tun_post_trace,
884 .type = VLIB_NODE_TYPE_INTERNAL,
885 .sibling_of = "wg6-output-tun",
886 .n_errors = ARRAY_LEN (wg_output_error_strings),
887 .error_strings = wg_output_error_strings,
890 VLIB_NODE_FN (wg4_output_tun_post_node)
891 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
893 return wg_output_tun_post (vm, node, from_frame);
896 VLIB_NODE_FN (wg6_output_tun_post_node)
897 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
899 return wg_output_tun_post (vm, node, from_frame);
902 VLIB_NODE_FN (wg4_output_tun_node)
903 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
905 return wg_output_tun_inline (vm, node, frame, /* is_ip4 */ 1,
906 wg_encrypt_async_next.wg4_post_next);
909 VLIB_NODE_FN (wg6_output_tun_node)
910 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
912 return wg_output_tun_inline (vm, node, frame, /* is_ip4 */ 0,
913 wg_encrypt_async_next.wg6_post_next);
917 VLIB_REGISTER_NODE (wg4_output_tun_node) =
919 .name = "wg4-output-tun",
920 .vector_size = sizeof (u32),
921 .format_trace = format_wg_output_tun_trace,
922 .type = VLIB_NODE_TYPE_INTERNAL,
923 .n_errors = ARRAY_LEN (wg_output_error_strings),
924 .error_strings = wg_output_error_strings,
925 .n_next_nodes = WG_OUTPUT_N_NEXT,
927 [WG_OUTPUT_NEXT_HANDOFF] = "wg4-output-tun-handoff",
928 [WG_OUTPUT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
929 [WG_OUTPUT_NEXT_ERROR] = "error-drop",
933 VLIB_REGISTER_NODE (wg6_output_tun_node) =
935 .name = "wg6-output-tun",
936 .vector_size = sizeof (u32),
937 .format_trace = format_wg_output_tun_trace,
938 .type = VLIB_NODE_TYPE_INTERNAL,
939 .n_errors = ARRAY_LEN (wg_output_error_strings),
940 .error_strings = wg_output_error_strings,
941 .n_next_nodes = WG_OUTPUT_N_NEXT,
943 [WG_OUTPUT_NEXT_HANDOFF] = "wg6-output-tun-handoff",
944 [WG_OUTPUT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
945 [WG_OUTPUT_NEXT_ERROR] = "error-drop",
951 * fd.io coding-style-patch-verification: ON
954 * eval: (c-set-style "gnu")