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>
20 #include <wireguard/wireguard.h>
22 #include <wireguard/wireguard_send.h>
23 #include <wireguard/wireguard_if.h>
25 #define foreach_wg_input_error \
26 _ (NONE, "No error") \
27 _ (HANDSHAKE_MAC, "Invalid MAC handshake") \
28 _ (PEER, "Peer error") \
29 _ (INTERFACE, "Interface error") \
30 _ (DECRYPTION, "Failed during decryption") \
31 _ (KEEPALIVE_SEND, "Failed while sending Keepalive") \
32 _ (HANDSHAKE_SEND, "Failed while sending Handshake") \
33 _ (HANDSHAKE_RECEIVE, "Failed while receiving Handshake") \
34 _ (TOO_BIG, "Packet too big") \
35 _ (UNDEFINED, "Undefined error") \
36 _ (CRYPTO_ENGINE_ERROR, "crypto engine error (packet dropped)")
40 #define _(sym,str) WG_INPUT_ERROR_##sym,
41 foreach_wg_input_error
46 static char *wg_input_error_strings[] = {
47 #define _(sym,string) string,
48 foreach_wg_input_error
64 } wg_input_post_trace_t;
67 format_wg_message_type (u8 * s, va_list * args)
69 message_type_t type = va_arg (*args, message_type_t);
73 #define _(v,a) case MESSAGE_##v: return (format (s, "%s", a));
74 foreach_wg_message_type
77 return (format (s, "unknown"));
80 /* packet trace format function */
82 format_wg_input_trace (u8 * s, va_list * args)
84 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
85 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
87 wg_input_trace_t *t = va_arg (*args, wg_input_trace_t *);
89 s = format (s, "Wireguard input: \n");
90 s = format (s, " Type: %U\n", format_wg_message_type, t->type);
91 s = format (s, " Peer: %d\n", t->peer);
92 s = format (s, " Length: %d\n", t->current_length);
93 s = format (s, " Keepalive: %s", t->is_keepalive ? "true" : "false");
98 /* post-node packet trace format function */
100 format_wg_input_post_trace (u8 *s, va_list *args)
102 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
103 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
105 wg_input_post_trace_t *t = va_arg (*args, wg_input_post_trace_t *);
107 s = format (s, "WG input post: \n");
108 s = format (s, " peer: %u\n", t->peer);
109 s = format (s, " next: %u\n", t->next);
116 WG_INPUT_NEXT_HANDOFF_HANDSHAKE,
117 WG_INPUT_NEXT_HANDOFF_DATA,
118 WG_INPUT_NEXT_IP4_INPUT,
119 WG_INPUT_NEXT_IP6_INPUT,
126 /* set_peer_address (wg_peer_t * peer, ip4_address_t ip4, u16 udp_port) */
130 /* ip46_address_set_ip4 (&peer->dst.addr, &ip4); */
131 /* peer->dst.port = udp_port; */
136 is_ip4_header (u8 *data)
138 return (data[0] >> 4) == 0x4;
141 static wg_input_error_t
142 wg_handshake_process (vlib_main_t *vm, wg_main_t *wmp, vlib_buffer_t *b,
143 u32 node_idx, u8 is_ip4)
145 ASSERT (vm->thread_index == 0);
147 enum cookie_mac_state mac_state;
148 bool packet_needs_cookie;
152 wg_peer_t *peer = NULL;
154 void *current_b_data = vlib_buffer_get_current (b);
156 ip46_address_t src_ip;
160 current_b_data - sizeof (udp_header_t) - sizeof (ip4_header_t);
161 ip46_address_set_ip4 (&src_ip, &iph4->src_address);
166 current_b_data - sizeof (udp_header_t) - sizeof (ip6_header_t);
167 ip46_address_set_ip6 (&src_ip, &iph6->src_address);
170 udp_header_t *uhd = current_b_data - sizeof (udp_header_t);
171 u16 udp_src_port = clib_host_to_net_u16 (uhd->src_port);;
172 u16 udp_dst_port = clib_host_to_net_u16 (uhd->dst_port);;
174 message_header_t *header = current_b_data;
177 if (PREDICT_FALSE (header->type == MESSAGE_HANDSHAKE_COOKIE))
179 message_handshake_cookie_t *packet =
180 (message_handshake_cookie_t *) current_b_data;
182 wg_index_table_lookup (&wmp->index_table, packet->receiver_index);
184 peer = wg_peer_get (*entry);
186 return WG_INPUT_ERROR_PEER;
188 // TODO: Implement cookie_maker_consume_payload
190 return WG_INPUT_ERROR_NONE;
193 u32 len = (header->type == MESSAGE_HANDSHAKE_INITIATION ?
194 sizeof (message_handshake_initiation_t) :
195 sizeof (message_handshake_response_t));
197 message_macs_t *macs = (message_macs_t *)
198 ((u8 *) current_b_data + len - sizeof (*macs));
201 wg_ifs = wg_if_indexes_get_by_port (udp_dst_port);
203 return WG_INPUT_ERROR_INTERFACE;
205 vec_foreach (ii, wg_ifs)
207 wg_if = wg_if_get (*ii);
211 mac_state = cookie_checker_validate_macs (
212 vm, &wg_if->cookie_checker, macs, current_b_data, len, under_load,
213 &src_ip, udp_src_port);
214 if (mac_state == INVALID_MAC)
223 return WG_INPUT_ERROR_HANDSHAKE_MAC;
225 if ((under_load && mac_state == VALID_MAC_WITH_COOKIE)
226 || (!under_load && mac_state == VALID_MAC_BUT_NO_COOKIE))
227 packet_needs_cookie = false;
228 else if (under_load && mac_state == VALID_MAC_BUT_NO_COOKIE)
229 packet_needs_cookie = true;
231 return WG_INPUT_ERROR_HANDSHAKE_MAC;
233 switch (header->type)
235 case MESSAGE_HANDSHAKE_INITIATION:
237 message_handshake_initiation_t *message = current_b_data;
239 if (packet_needs_cookie)
241 // TODO: Add processing
244 if (noise_consume_initiation
245 (vm, noise_local_get (wg_if->local_idx), &rp,
246 message->sender_index, message->unencrypted_ephemeral,
247 message->encrypted_static, message->encrypted_timestamp))
249 peer = wg_peer_get (rp->r_peer_idx);
253 return WG_INPUT_ERROR_PEER;
256 // set_peer_address (peer, ip4_src, udp_src_port);
257 if (PREDICT_FALSE (!wg_send_handshake_response (vm, peer)))
259 vlib_node_increment_counter (vm, node_idx,
260 WG_INPUT_ERROR_HANDSHAKE_SEND, 1);
264 wg_peer_update_flags (rp->r_peer_idx, WG_PEER_ESTABLISHED, true);
268 case MESSAGE_HANDSHAKE_RESPONSE:
270 message_handshake_response_t *resp = current_b_data;
271 index_t peeri = INDEX_INVALID;
273 wg_index_table_lookup (&wmp->index_table, resp->receiver_index);
275 if (PREDICT_TRUE (entry != NULL))
278 peer = wg_peer_get (peeri);
279 if (wg_peer_is_dead (peer))
280 return WG_INPUT_ERROR_PEER;
283 return WG_INPUT_ERROR_PEER;
285 if (!noise_consume_response
286 (vm, &peer->remote, resp->sender_index,
287 resp->receiver_index, resp->unencrypted_ephemeral,
288 resp->encrypted_nothing))
290 return WG_INPUT_ERROR_PEER;
292 if (packet_needs_cookie)
294 // TODO: Add processing
297 // set_peer_address (peer, ip4_src, udp_src_port);
298 if (noise_remote_begin_session (vm, &peer->remote))
301 wg_timers_session_derived (peer);
302 wg_timers_handshake_complete (peer);
303 if (PREDICT_FALSE (!wg_send_keepalive (vm, peer)))
305 vlib_node_increment_counter (vm, node_idx,
306 WG_INPUT_ERROR_KEEPALIVE_SEND, 1);
310 wg_peer_update_flags (peeri, WG_PEER_ESTABLISHED, true);
316 return WG_INPUT_ERROR_HANDSHAKE_RECEIVE;
319 wg_timers_any_authenticated_packet_received (peer);
320 wg_timers_any_authenticated_packet_traversal (peer);
321 return WG_INPUT_ERROR_NONE;
324 static_always_inline int
325 wg_input_post_process (vlib_main_t *vm, vlib_buffer_t *b, u16 *next,
326 wg_peer_t *peer, message_data_t *data,
329 next[0] = WG_INPUT_NEXT_PUNT;
331 noise_keypair_t *kp =
332 wg_get_active_keypair (&peer->remote, data->receiver_index);
334 if (!noise_counter_recv (&kp->kp_ctr, data->counter))
339 u16 encr_len = b->current_length - sizeof (message_data_t);
340 u16 decr_len = encr_len - NOISE_AUTHTAG_LEN;
342 vlib_buffer_advance (b, sizeof (message_data_t));
343 b->current_length = decr_len;
344 vnet_buffer_offload_flags_clear (b, VNET_BUFFER_OFFLOAD_F_UDP_CKSUM);
346 /* Keepalive packet has zero length */
349 *is_keepalive = true;
353 wg_timers_data_received (peer);
355 ip46_address_t src_ip;
356 u8 is_ip4_inner = is_ip4_header (vlib_buffer_get_current (b));
359 ip46_address_set_ip4 (
360 &src_ip, &((ip4_header_t *) vlib_buffer_get_current (b))->src_address);
364 ip46_address_set_ip6 (
365 &src_ip, &((ip6_header_t *) vlib_buffer_get_current (b))->src_address);
368 const fib_prefix_t *allowed_ip;
369 bool allowed = false;
372 * we could make this into an ACL, but the expectation
373 * is that there aren't many allowed IPs and thus a linear
374 * walk is faster than an ACL
376 vec_foreach (allowed_ip, peer->allowed_ips)
378 if (fib_prefix_is_cover_addr_46 (allowed_ip, &src_ip))
386 vnet_buffer (b)->sw_if_index[VLIB_RX] = peer->wg_sw_if_index;
388 is_ip4_inner ? WG_INPUT_NEXT_IP4_INPUT : WG_INPUT_NEXT_IP6_INPUT;
394 static_always_inline void
395 wg_input_process_ops (vlib_main_t *vm, vlib_node_runtime_t *node,
396 vnet_crypto_op_t *ops, vlib_buffer_t *b[], u16 *nexts,
399 u32 n_fail, n_ops = vec_len (ops);
400 vnet_crypto_op_t *op = ops;
405 n_fail = n_ops - vnet_crypto_process_ops (vm, op, n_ops);
409 ASSERT (op - ops < n_ops);
411 if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
413 u32 bi = op->user_data;
414 b[bi]->error = node->errors[WG_INPUT_ERROR_DECRYPTION];
415 nexts[bi] = drop_next;
423 wg_prepare_sync_dec_op (vlib_main_t *vm, vnet_crypto_op_t **crypto_ops,
424 u8 *src, u32 src_len, u8 *dst, u8 *aad, u32 aad_len,
425 vnet_crypto_key_index_t key_index, u32 bi, u8 *iv)
427 vnet_crypto_op_t _op, *op = &_op;
430 vec_add2_aligned (crypto_ops[0], op, 1, CLIB_CACHE_LINE_BYTES);
431 vnet_crypto_op_init (op, VNET_CRYPTO_OP_CHACHA20_POLY1305_DEC);
433 op->tag_len = NOISE_AUTHTAG_LEN;
434 op->tag = src + src_len;
435 op->src = !src ? src_ : src;
438 op->key_index = key_index;
440 op->aad_len = aad_len;
443 op->flags |= VNET_CRYPTO_OP_FLAG_HMAC_CHECK;
446 static_always_inline void
447 wg_input_add_to_frame (vlib_main_t *vm, vnet_crypto_async_frame_t *f,
448 u32 key_index, u32 crypto_len, i16 crypto_start_offset,
449 u32 buffer_index, u16 next_node, u8 *iv, u8 *tag,
452 vnet_crypto_async_frame_elt_t *fe;
455 ASSERT (f->n_elts < VNET_CRYPTO_FRAME_SIZE);
458 fe = &f->elts[index];
460 fe->key_index = key_index;
461 fe->crypto_total_length = crypto_len;
462 fe->crypto_start_offset = crypto_start_offset;
466 f->buffer_indices[index] = buffer_index;
467 f->next_node_index[index] = next_node;
470 static_always_inline enum noise_state_crypt
471 wg_input_process (vlib_main_t *vm, wg_per_thread_data_t *ptd,
472 vnet_crypto_op_t **crypto_ops,
473 vnet_crypto_async_frame_t **async_frame, vlib_buffer_t *b,
474 u32 buf_idx, noise_remote_t *r, uint32_t r_idx,
475 uint64_t nonce, uint8_t *src, size_t srclen, uint8_t *dst,
476 u32 from_idx, u8 *iv, f64 time, u8 is_async,
480 enum noise_state_crypt ret = SC_FAILED;
482 if ((kp = wg_get_active_keypair (r, r_idx)) == NULL)
487 /* We confirm that our values are within our tolerances. These values
488 * are the same as the encrypt routine.
490 * kp_ctr isn't locked here, we're happy to accept a racy read. */
491 if (wg_birthdate_has_expired_opt (kp->kp_birthdate, REJECT_AFTER_TIME,
493 kp->kp_ctr.c_recv >= REJECT_AFTER_MESSAGES)
496 /* Decrypt, then validate the counter. We don't want to validate the
497 * counter before decrypting as we do not know the message is authentic
498 * prior to decryption. */
500 clib_memset (iv, 0, 4);
501 clib_memcpy (iv + 4, &nonce, sizeof (nonce));
505 if (NULL == *async_frame ||
506 vnet_crypto_async_frame_is_full (*async_frame))
508 *async_frame = vnet_crypto_async_get_frame (
509 vm, VNET_CRYPTO_OP_CHACHA20_POLY1305_TAG16_AAD0_DEC);
510 /* Save the frame to the list we'll submit at the end */
511 vec_add1 (ptd->async_frames, *async_frame);
514 wg_input_add_to_frame (vm, *async_frame, kp->kp_recv_index, srclen,
515 src - b->data, buf_idx, async_next_node, iv,
516 src + srclen, VNET_CRYPTO_OP_FLAG_HMAC_CHECK);
520 wg_prepare_sync_dec_op (vm, crypto_ops, src, srclen, dst, NULL, 0,
521 kp->kp_recv_index, from_idx, iv);
524 /* If we've received the handshake confirming data packet then move the
525 * next keypair into current. If we do slide the next keypair in, then
526 * we skip the REKEY_AFTER_TIME_RECV check. This is safe to do as a
527 * data packet can't confirm a session that we are an INITIATOR of. */
530 clib_rwlock_writer_lock (&r->r_keypair_lock);
531 if (kp == r->r_next && kp->kp_local_index == r_idx)
533 noise_remote_keypair_free (vm, r, &r->r_previous);
534 r->r_previous = r->r_current;
535 r->r_current = r->r_next;
539 clib_rwlock_writer_unlock (&r->r_keypair_lock);
542 clib_rwlock_writer_unlock (&r->r_keypair_lock);
545 /* Similar to when we encrypt, we want to notify the caller when we
546 * are approaching our tolerances. We notify if:
547 * - we're the initiator and the current keypair is older than
548 * REKEY_AFTER_TIME_RECV seconds. */
549 ret = SC_KEEP_KEY_FRESH;
551 if (kp != NULL && kp->kp_valid && kp->kp_is_initiator &&
552 wg_birthdate_has_expired_opt (kp->kp_birthdate, REKEY_AFTER_TIME_RECV,
562 wg_input_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
563 vlib_frame_t *frame, u8 is_ip4, u16 async_next_node)
565 wg_main_t *wmp = &wg_main;
566 wg_per_thread_data_t *ptd =
567 vec_elt_at_index (wmp->per_thread_data, vm->thread_index);
568 u32 *from = vlib_frame_vector_args (frame);
569 u32 n_left_from = frame->n_vectors;
571 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
572 u32 thread_index = vm->thread_index;
573 vnet_crypto_op_t **crypto_ops = &ptd->crypto_ops;
574 const u16 drop_next = WG_INPUT_NEXT_PUNT;
575 message_type_t header_type;
576 vlib_buffer_t *data_bufs[VLIB_FRAME_SIZE];
577 u32 data_bi[VLIB_FRAME_SIZE]; /* buffer index for data */
578 u32 other_bi[VLIB_FRAME_SIZE]; /* buffer index for drop or handoff */
579 u16 other_nexts[VLIB_FRAME_SIZE], *other_next = other_nexts, n_other = 0;
580 u16 data_nexts[VLIB_FRAME_SIZE], *data_next = data_nexts, n_data = 0;
582 const u8 is_async = wg_op_mode_is_set_ASYNC ();
583 vnet_crypto_async_frame_t *async_frame = NULL;
585 vlib_get_buffers (vm, from, bufs, n_left_from);
586 vec_reset_length (ptd->crypto_ops);
587 vec_reset_length (ptd->async_frames);
589 f64 time = clib_time_now (&vm->clib_time) + vm->time_offset;
591 wg_peer_t *peer = NULL;
592 u32 *last_peer_time_idx = NULL;
593 u32 last_rec_idx = ~0;
595 bool is_keepalive = false;
596 u32 *peer_idx = NULL;
598 while (n_left_from > 0)
603 vlib_prefetch_buffer_header (b[2], LOAD);
604 p = vlib_buffer_get_current (b[1]);
605 CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD);
606 CLIB_PREFETCH (vlib_buffer_get_tail (b[1]), CLIB_CACHE_LINE_BYTES,
610 other_next[n_other] = WG_INPUT_NEXT_PUNT;
611 data_nexts[n_data] = WG_INPUT_N_NEXT;
614 ((message_header_t *) vlib_buffer_get_current (b[0]))->type;
616 if (PREDICT_TRUE (header_type == MESSAGE_DATA))
618 message_data_t *data = vlib_buffer_get_current (b[0]);
619 u8 *iv_data = b[0]->pre_data;
620 u32 buf_idx = from[b - bufs];
621 peer_idx = wg_index_table_lookup (&wmp->index_table,
622 data->receiver_index);
624 if (data->receiver_index != last_rec_idx)
626 peer_idx = wg_index_table_lookup (&wmp->index_table,
627 data->receiver_index);
628 if (PREDICT_TRUE (peer_idx != NULL))
630 peer = wg_peer_get (*peer_idx);
632 last_rec_idx = data->receiver_index;
635 if (PREDICT_FALSE (!peer_idx))
637 other_next[n_other] = WG_INPUT_NEXT_ERROR;
638 b[0]->error = node->errors[WG_INPUT_ERROR_PEER];
639 other_bi[n_other] = buf_idx;
644 if (PREDICT_FALSE (~0 == peer->input_thread_index))
646 /* this is the first packet to use this peer, claim the peer
649 clib_atomic_cmp_and_swap (&peer->input_thread_index, ~0,
650 wg_peer_assign_thread (thread_index));
653 if (PREDICT_TRUE (thread_index != peer->input_thread_index))
655 other_next[n_other] = WG_INPUT_NEXT_HANDOFF_DATA;
656 other_bi[n_other] = buf_idx;
661 u16 encr_len = b[0]->current_length - sizeof (message_data_t);
662 u16 decr_len = encr_len - NOISE_AUTHTAG_LEN;
663 if (PREDICT_FALSE (decr_len >= WG_DEFAULT_DATA_SIZE))
665 b[0]->error = node->errors[WG_INPUT_ERROR_TOO_BIG];
666 other_bi[n_other] = buf_idx;
671 enum noise_state_crypt state_cr = wg_input_process (
672 vm, ptd, crypto_ops, &async_frame, b[0], buf_idx, &peer->remote,
673 data->receiver_index, data->counter, data->encrypted_data,
674 decr_len, data->encrypted_data, n_data, iv_data, time, is_async,
677 if (PREDICT_FALSE (state_cr == SC_FAILED))
679 wg_peer_update_flags (*peer_idx, WG_PEER_ESTABLISHED, false);
680 other_next[n_other] = WG_INPUT_NEXT_ERROR;
681 b[0]->error = node->errors[WG_INPUT_ERROR_DECRYPTION];
682 other_bi[n_other] = buf_idx;
688 data_bufs[n_data] = b[0];
689 data_bi[n_data] = buf_idx;
697 if (PREDICT_FALSE (state_cr == SC_CONN_RESET))
699 wg_timers_handshake_complete (peer);
702 else if (PREDICT_FALSE (state_cr == SC_KEEP_KEY_FRESH))
704 wg_send_handshake_from_mt (*peer_idx, false);
707 else if (PREDICT_TRUE (state_cr == SC_OK))
714 /* Handshake packets should be processed in main thread */
715 if (thread_index != 0)
717 other_next[n_other] = WG_INPUT_NEXT_HANDOFF_HANDSHAKE;
718 other_bi[n_other] = from[b - bufs];
723 wg_input_error_t ret =
724 wg_handshake_process (vm, wmp, b[0], node->node_index, is_ip4);
725 if (ret != WG_INPUT_ERROR_NONE)
727 other_next[n_other] = WG_INPUT_NEXT_ERROR;
728 b[0]->error = node->errors[ret];
729 other_bi[n_other] = from[b - bufs];
734 other_bi[n_other] = from[b - bufs];
740 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
741 (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
743 wg_input_trace_t *t = vlib_add_trace (vm, node, b[0], sizeof (*t));
744 t->type = header_type;
745 t->current_length = b[0]->current_length;
746 t->is_keepalive = is_keepalive;
747 t->peer = peer_idx ? *peer_idx : INDEX_INVALID;
755 /* decrypt packets */
756 wg_input_process_ops (vm, node, ptd->crypto_ops, data_bufs, data_nexts,
759 /* process after decryption */
761 n_left_from = n_data;
763 last_peer_time_idx = NULL;
765 while (n_left_from > 0)
767 bool is_keepalive = false;
768 u32 *peer_idx = NULL;
770 if (PREDICT_FALSE (data_next[0] == WG_INPUT_NEXT_PUNT))
777 vlib_prefetch_buffer_header (b[2], LOAD);
778 p = vlib_buffer_get_current (b[1]);
779 CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD);
780 CLIB_PREFETCH (vlib_buffer_get_tail (b[1]), CLIB_CACHE_LINE_BYTES,
784 message_data_t *data = vlib_buffer_get_current (b[0]);
786 if (data->receiver_index != last_rec_idx)
789 wg_index_table_lookup (&wmp->index_table, data->receiver_index);
790 peer = wg_peer_get (*peer_idx);
791 last_rec_idx = data->receiver_index;
794 if (PREDICT_FALSE (wg_input_post_process (vm, b[0], data_next, peer,
795 data, &is_keepalive) < 0))
798 if (PREDICT_FALSE (peer_idx && (last_peer_time_idx != peer_idx)))
800 wg_timers_any_authenticated_packet_received_opt (peer, time);
801 wg_timers_any_authenticated_packet_traversal (peer);
802 last_peer_time_idx = peer_idx;
806 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
807 (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
809 wg_input_trace_t *t = vlib_add_trace (vm, node, b[0], sizeof (*t));
810 t->type = header_type;
811 t->current_length = b[0]->current_length;
812 t->is_keepalive = is_keepalive;
813 t->peer = peer_idx ? *peer_idx : INDEX_INVALID;
823 /* submit all of the open frames */
824 vnet_crypto_async_frame_t **async_frame;
825 vec_foreach (async_frame, ptd->async_frames)
828 vnet_crypto_async_submit_open_frame (vm, *async_frame) < 0))
830 u32 n_drop = (*async_frame)->n_elts;
831 u32 *bi = (*async_frame)->buffer_indices;
835 other_bi[index] = bi[0];
836 vlib_buffer_t *b = vlib_get_buffer (vm, bi[0]);
837 other_nexts[index] = drop_next;
838 b->error = node->errors[WG_INPUT_ERROR_CRYPTO_ENGINE_ERROR];
842 n_other += (*async_frame)->n_elts;
844 vnet_crypto_async_reset_frame (*async_frame);
845 vnet_crypto_async_free_frame (vm, *async_frame);
850 /* enqueue other bufs */
852 vlib_buffer_enqueue_to_next (vm, node, other_bi, other_next, n_other);
854 /* enqueue data bufs */
856 vlib_buffer_enqueue_to_next (vm, node, data_bi, data_nexts, n_data);
858 return frame->n_vectors;
862 wg_input_post (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
864 wg_main_t *wmp = &wg_main;
865 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
866 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
867 u32 *from = vlib_frame_vector_args (frame);
868 u32 n_left = frame->n_vectors;
869 wg_peer_t *peer = NULL;
870 u32 *peer_idx = NULL;
871 u32 *last_peer_time_idx = NULL;
872 u32 last_rec_idx = ~0;
873 f64 time = clib_time_now (&vm->clib_time) + vm->time_offset;
875 vlib_get_buffers (vm, from, b, n_left);
879 vlib_prefetch_buffer_header (b[0], LOAD);
880 vlib_prefetch_buffer_header (b[1], LOAD);
888 vlib_prefetch_buffer_header (b[2], LOAD);
889 p = vlib_buffer_get_current (b[1]);
890 CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD);
893 bool is_keepalive = false;
894 message_data_t *data = vlib_buffer_get_current (b[0]);
896 if (data->receiver_index != last_rec_idx)
899 wg_index_table_lookup (&wmp->index_table, data->receiver_index);
901 peer = wg_peer_get (*peer_idx);
902 last_rec_idx = data->receiver_index;
905 if (PREDICT_TRUE (peer != NULL))
907 if (PREDICT_FALSE (wg_input_post_process (vm, b[0], next, peer, data,
913 next[0] = WG_INPUT_NEXT_PUNT;
917 if (PREDICT_FALSE (peer_idx && (last_peer_time_idx != peer_idx)))
919 wg_timers_any_authenticated_packet_received_opt (peer, time);
920 wg_timers_any_authenticated_packet_traversal (peer);
921 last_peer_time_idx = peer_idx;
924 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
925 (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
927 wg_input_post_trace_t *t =
928 vlib_add_trace (vm, node, b[0], sizeof (*t));
930 t->peer = peer_idx ? *peer_idx : INDEX_INVALID;
938 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
939 return frame->n_vectors;
942 VLIB_NODE_FN (wg4_input_node)
943 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
945 return wg_input_inline (vm, node, frame, /* is_ip4 */ 1,
946 wg_decrypt_async_next.wg4_post_next);
949 VLIB_NODE_FN (wg6_input_node)
950 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
952 return wg_input_inline (vm, node, frame, /* is_ip4 */ 0,
953 wg_decrypt_async_next.wg6_post_next);
956 VLIB_NODE_FN (wg4_input_post_node)
957 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
959 return wg_input_post (vm, node, from_frame);
962 VLIB_NODE_FN (wg6_input_post_node)
963 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
965 return wg_input_post (vm, node, from_frame);
969 VLIB_REGISTER_NODE (wg4_input_node) =
972 .vector_size = sizeof (u32),
973 .format_trace = format_wg_input_trace,
974 .type = VLIB_NODE_TYPE_INTERNAL,
975 .n_errors = ARRAY_LEN (wg_input_error_strings),
976 .error_strings = wg_input_error_strings,
977 .n_next_nodes = WG_INPUT_N_NEXT,
978 /* edit / add dispositions here */
980 [WG_INPUT_NEXT_HANDOFF_HANDSHAKE] = "wg4-handshake-handoff",
981 [WG_INPUT_NEXT_HANDOFF_DATA] = "wg4-input-data-handoff",
982 [WG_INPUT_NEXT_IP4_INPUT] = "ip4-input-no-checksum",
983 [WG_INPUT_NEXT_IP6_INPUT] = "ip6-input",
984 [WG_INPUT_NEXT_PUNT] = "error-punt",
985 [WG_INPUT_NEXT_ERROR] = "error-drop",
989 VLIB_REGISTER_NODE (wg6_input_node) =
992 .vector_size = sizeof (u32),
993 .format_trace = format_wg_input_trace,
994 .type = VLIB_NODE_TYPE_INTERNAL,
995 .n_errors = ARRAY_LEN (wg_input_error_strings),
996 .error_strings = wg_input_error_strings,
997 .n_next_nodes = WG_INPUT_N_NEXT,
998 /* edit / add dispositions here */
1000 [WG_INPUT_NEXT_HANDOFF_HANDSHAKE] = "wg6-handshake-handoff",
1001 [WG_INPUT_NEXT_HANDOFF_DATA] = "wg6-input-data-handoff",
1002 [WG_INPUT_NEXT_IP4_INPUT] = "ip4-input-no-checksum",
1003 [WG_INPUT_NEXT_IP6_INPUT] = "ip6-input",
1004 [WG_INPUT_NEXT_PUNT] = "error-punt",
1005 [WG_INPUT_NEXT_ERROR] = "error-drop",
1009 VLIB_REGISTER_NODE (wg4_input_post_node) = {
1010 .name = "wg4-input-post-node",
1011 .vector_size = sizeof (u32),
1012 .format_trace = format_wg_input_post_trace,
1013 .type = VLIB_NODE_TYPE_INTERNAL,
1014 .sibling_of = "wg4-input",
1016 .n_errors = ARRAY_LEN (wg_input_error_strings),
1017 .error_strings = wg_input_error_strings,
1020 VLIB_REGISTER_NODE (wg6_input_post_node) = {
1021 .name = "wg6-input-post-node",
1022 .vector_size = sizeof (u32),
1023 .format_trace = format_wg_input_post_trace,
1024 .type = VLIB_NODE_TYPE_INTERNAL,
1025 .sibling_of = "wg6-input",
1027 .n_errors = ARRAY_LEN (wg_input_error_strings),
1028 .error_strings = wg_input_error_strings,
1034 * fd.io coding-style-patch-verification: ON
1037 * eval: (c-set-style "gnu")