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 _ (HANDSHAKE_RATELIMITED, "Handshake ratelimited") \
29 _ (PEER, "Peer error") \
30 _ (INTERFACE, "Interface error") \
31 _ (DECRYPTION, "Failed during decryption") \
32 _ (KEEPALIVE_SEND, "Failed while sending Keepalive") \
33 _ (HANDSHAKE_SEND, "Failed while sending Handshake") \
34 _ (HANDSHAKE_RECEIVE, "Failed while receiving Handshake") \
35 _ (COOKIE_DECRYPTION, "Failed during Cookie decryption") \
36 _ (COOKIE_SEND, "Failed during sending Cookie") \
37 _ (NO_BUFFERS, "No buffers") \
38 _ (UNDEFINED, "Undefined error") \
39 _ (CRYPTO_ENGINE_ERROR, "crypto engine error (packet dropped)")
43 #define _(sym,str) WG_INPUT_ERROR_##sym,
44 foreach_wg_input_error
49 static char *wg_input_error_strings[] = {
50 #define _(sym,string) string,
51 foreach_wg_input_error
67 } wg_input_post_trace_t;
70 format_wg_message_type (u8 * s, va_list * args)
72 message_type_t type = va_arg (*args, message_type_t);
76 #define _(v,a) case MESSAGE_##v: return (format (s, "%s", a));
77 foreach_wg_message_type
80 return (format (s, "unknown"));
83 /* packet trace format function */
85 format_wg_input_trace (u8 * s, va_list * args)
87 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
88 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
90 wg_input_trace_t *t = va_arg (*args, wg_input_trace_t *);
92 s = format (s, "Wireguard input: \n");
93 s = format (s, " Type: %U\n", format_wg_message_type, t->type);
94 s = format (s, " Peer: %d\n", t->peer);
95 s = format (s, " Length: %d\n", t->current_length);
96 s = format (s, " Keepalive: %s", t->is_keepalive ? "true" : "false");
101 /* post-node packet trace format function */
103 format_wg_input_post_trace (u8 *s, va_list *args)
105 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
106 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
108 wg_input_post_trace_t *t = va_arg (*args, wg_input_post_trace_t *);
110 s = format (s, "WG input post: \n");
111 s = format (s, " peer: %u\n", t->peer);
112 s = format (s, " next: %u\n", t->next);
119 WG_INPUT_NEXT_HANDOFF_HANDSHAKE,
120 WG_INPUT_NEXT_HANDOFF_DATA,
121 WG_INPUT_NEXT_IP4_INPUT,
122 WG_INPUT_NEXT_IP6_INPUT,
129 is_ip4_header (u8 *data)
131 return (data[0] >> 4) == 0x4;
134 static wg_input_error_t
135 wg_handshake_process (vlib_main_t *vm, wg_main_t *wmp, vlib_buffer_t *b,
136 u32 node_idx, u8 is_ip4)
138 ASSERT (vm->thread_index == 0);
140 enum cookie_mac_state mac_state;
141 bool packet_needs_cookie;
145 wg_peer_t *peer = NULL;
147 void *current_b_data = vlib_buffer_get_current (b);
149 ip46_address_t src_ip;
153 current_b_data - sizeof (udp_header_t) - sizeof (ip4_header_t);
154 ip46_address_set_ip4 (&src_ip, &iph4->src_address);
159 current_b_data - sizeof (udp_header_t) - sizeof (ip6_header_t);
160 ip46_address_set_ip6 (&src_ip, &iph6->src_address);
163 udp_header_t *uhd = current_b_data - sizeof (udp_header_t);
164 u16 udp_src_port = clib_host_to_net_u16 (uhd->src_port);
165 u16 udp_dst_port = clib_host_to_net_u16 (uhd->dst_port);
167 message_header_t *header = current_b_data;
169 if (PREDICT_FALSE (header->type == MESSAGE_HANDSHAKE_COOKIE))
171 message_handshake_cookie_t *packet =
172 (message_handshake_cookie_t *) current_b_data;
174 wg_index_table_lookup (&wmp->index_table, packet->receiver_index);
176 peer = wg_peer_get (*entry);
178 return WG_INPUT_ERROR_PEER;
180 if (!cookie_maker_consume_payload (
181 vm, &peer->cookie_maker, packet->nonce, packet->encrypted_cookie))
182 return WG_INPUT_ERROR_COOKIE_DECRYPTION;
184 return WG_INPUT_ERROR_NONE;
187 u32 len = (header->type == MESSAGE_HANDSHAKE_INITIATION ?
188 sizeof (message_handshake_initiation_t) :
189 sizeof (message_handshake_response_t));
191 message_macs_t *macs = (message_macs_t *)
192 ((u8 *) current_b_data + len - sizeof (*macs));
195 wg_ifs = wg_if_indexes_get_by_port (udp_dst_port);
197 return WG_INPUT_ERROR_INTERFACE;
199 vec_foreach (ii, wg_ifs)
201 wg_if = wg_if_get (*ii);
205 under_load = wg_if_is_under_load (vm, wg_if);
206 mac_state = cookie_checker_validate_macs (
207 vm, &wg_if->cookie_checker, macs, current_b_data, len, under_load,
208 &src_ip, udp_src_port);
209 if (mac_state == INVALID_MAC)
211 wg_if_dec_handshake_num (wg_if);
219 return WG_INPUT_ERROR_HANDSHAKE_MAC;
221 if ((under_load && mac_state == VALID_MAC_WITH_COOKIE)
222 || (!under_load && mac_state == VALID_MAC_BUT_NO_COOKIE))
223 packet_needs_cookie = false;
224 else if (under_load && mac_state == VALID_MAC_BUT_NO_COOKIE)
225 packet_needs_cookie = true;
226 else if (mac_state == VALID_MAC_WITH_COOKIE_BUT_RATELIMITED)
227 return WG_INPUT_ERROR_HANDSHAKE_RATELIMITED;
229 return WG_INPUT_ERROR_HANDSHAKE_MAC;
231 switch (header->type)
233 case MESSAGE_HANDSHAKE_INITIATION:
235 message_handshake_initiation_t *message = current_b_data;
237 if (packet_needs_cookie)
240 if (!wg_send_handshake_cookie (vm, message->sender_index,
241 &wg_if->cookie_checker, macs,
242 &ip_addr_46 (&wg_if->src_ip),
243 wg_if->port, &src_ip, udp_src_port))
244 return WG_INPUT_ERROR_COOKIE_SEND;
246 return WG_INPUT_ERROR_NONE;
250 if (noise_consume_initiation
251 (vm, noise_local_get (wg_if->local_idx), &rp,
252 message->sender_index, message->unencrypted_ephemeral,
253 message->encrypted_static, message->encrypted_timestamp))
255 peer = wg_peer_get (rp->r_peer_idx);
259 return WG_INPUT_ERROR_PEER;
262 wg_peer_update_endpoint (rp->r_peer_idx, &src_ip, udp_src_port);
264 if (PREDICT_FALSE (!wg_send_handshake_response (vm, peer)))
266 vlib_node_increment_counter (vm, node_idx,
267 WG_INPUT_ERROR_HANDSHAKE_SEND, 1);
271 case MESSAGE_HANDSHAKE_RESPONSE:
273 message_handshake_response_t *resp = current_b_data;
275 if (packet_needs_cookie)
277 if (!wg_send_handshake_cookie (vm, resp->sender_index,
278 &wg_if->cookie_checker, macs,
279 &ip_addr_46 (&wg_if->src_ip),
280 wg_if->port, &src_ip, udp_src_port))
281 return WG_INPUT_ERROR_COOKIE_SEND;
283 return WG_INPUT_ERROR_NONE;
286 index_t peeri = INDEX_INVALID;
288 wg_index_table_lookup (&wmp->index_table, resp->receiver_index);
290 if (PREDICT_TRUE (entry != NULL))
293 peer = wg_peer_get (peeri);
294 if (wg_peer_is_dead (peer))
295 return WG_INPUT_ERROR_PEER;
298 return WG_INPUT_ERROR_PEER;
300 if (!noise_consume_response
301 (vm, &peer->remote, resp->sender_index,
302 resp->receiver_index, resp->unencrypted_ephemeral,
303 resp->encrypted_nothing))
305 return WG_INPUT_ERROR_PEER;
308 wg_peer_update_endpoint (peeri, &src_ip, udp_src_port);
310 if (noise_remote_begin_session (vm, &peer->remote))
313 wg_timers_session_derived (peer);
314 wg_timers_handshake_complete (peer);
315 if (PREDICT_FALSE (!wg_send_keepalive (vm, peer)))
317 vlib_node_increment_counter (vm, node_idx,
318 WG_INPUT_ERROR_KEEPALIVE_SEND, 1);
322 wg_peer_update_flags (peeri, WG_PEER_ESTABLISHED, true);
328 return WG_INPUT_ERROR_HANDSHAKE_RECEIVE;
331 wg_timers_any_authenticated_packet_received (peer);
332 wg_timers_any_authenticated_packet_traversal (peer);
333 return WG_INPUT_ERROR_NONE;
336 static_always_inline int
337 wg_input_post_process (vlib_main_t *vm, vlib_buffer_t *b, u16 *next,
338 wg_peer_t *peer, message_data_t *data,
341 next[0] = WG_INPUT_NEXT_PUNT;
345 if ((kp = wg_get_active_keypair (&peer->remote, data->receiver_index)) ==
349 if (!noise_counter_recv (&kp->kp_ctr, data->counter))
355 /* Find last buffer in the chain */
356 while (lb->flags & VLIB_BUFFER_NEXT_PRESENT)
357 lb = vlib_get_buffer (vm, lb->next_buffer);
359 u16 encr_len = vlib_buffer_length_in_chain (vm, b) - sizeof (message_data_t);
360 u16 decr_len = encr_len - NOISE_AUTHTAG_LEN;
362 vlib_buffer_advance (b, sizeof (message_data_t));
363 vlib_buffer_chain_increase_length (b, lb, -NOISE_AUTHTAG_LEN);
364 vnet_buffer_offload_flags_clear (b, VNET_BUFFER_OFFLOAD_F_UDP_CKSUM);
366 /* Keepalive packet has zero length */
369 *is_keepalive = true;
373 wg_timers_data_received (peer);
375 ip46_address_t src_ip;
376 u8 is_ip4_inner = is_ip4_header (vlib_buffer_get_current (b));
379 ip46_address_set_ip4 (
380 &src_ip, &((ip4_header_t *) vlib_buffer_get_current (b))->src_address);
384 ip46_address_set_ip6 (
385 &src_ip, &((ip6_header_t *) vlib_buffer_get_current (b))->src_address);
388 const fib_prefix_t *allowed_ip;
389 bool allowed = false;
392 * we could make this into an ACL, but the expectation
393 * is that there aren't many allowed IPs and thus a linear
394 * walk is faster than an ACL
396 vec_foreach (allowed_ip, peer->allowed_ips)
398 if (fib_prefix_is_cover_addr_46 (allowed_ip, &src_ip))
406 vnet_buffer (b)->sw_if_index[VLIB_RX] = peer->wg_sw_if_index;
408 is_ip4_inner ? WG_INPUT_NEXT_IP4_INPUT : WG_INPUT_NEXT_IP6_INPUT;
414 static_always_inline void
415 wg_input_process_ops (vlib_main_t *vm, vlib_node_runtime_t *node,
416 vnet_crypto_op_t *ops, vlib_buffer_t *b[], u16 *nexts,
419 u32 n_fail, n_ops = vec_len (ops);
420 vnet_crypto_op_t *op = ops;
425 n_fail = n_ops - vnet_crypto_process_ops (vm, op, n_ops);
429 ASSERT (op - ops < n_ops);
431 if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
433 u32 bi = op->user_data;
434 b[bi]->error = node->errors[WG_INPUT_ERROR_DECRYPTION];
435 nexts[bi] = drop_next;
442 static_always_inline void
443 wg_input_process_chained_ops (vlib_main_t *vm, vlib_node_runtime_t *node,
444 vnet_crypto_op_t *ops, vlib_buffer_t *b[],
445 u16 *nexts, vnet_crypto_op_chunk_t *chunks,
448 u32 n_fail, n_ops = vec_len (ops);
449 vnet_crypto_op_t *op = ops;
454 n_fail = n_ops - vnet_crypto_process_chained_ops (vm, op, chunks, n_ops);
458 ASSERT (op - ops < n_ops);
460 if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
462 u32 bi = op->user_data;
463 b[bi]->error = node->errors[WG_INPUT_ERROR_DECRYPTION];
464 nexts[bi] = drop_next;
471 static_always_inline void
472 wg_input_chain_crypto (vlib_main_t *vm, wg_per_thread_data_t *ptd,
473 vlib_buffer_t *b, vlib_buffer_t *lb, u8 *start,
474 u32 start_len, u16 *n_ch)
476 vnet_crypto_op_chunk_t *ch;
477 vlib_buffer_t *cb = b;
480 vec_add2 (ptd->chunks, ch, 1);
482 ch->src = ch->dst = start;
483 cb = vlib_get_buffer (vm, cb->next_buffer);
487 vec_add2 (ptd->chunks, ch, 1);
490 ch->len = cb->current_length - NOISE_AUTHTAG_LEN;
492 ch->len = cb->current_length;
494 ch->src = ch->dst = vlib_buffer_get_current (cb);
496 if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT))
499 cb = vlib_get_buffer (vm, cb->next_buffer);
507 wg_prepare_sync_dec_op (vlib_main_t *vm, wg_per_thread_data_t *ptd,
508 vlib_buffer_t *b, vlib_buffer_t *lb,
509 vnet_crypto_op_t **crypto_ops, u8 *src, u32 src_len,
510 u8 *dst, u8 *aad, u32 aad_len,
511 vnet_crypto_key_index_t key_index, u32 bi, u8 *iv)
513 vnet_crypto_op_t _op, *op = &_op;
516 vec_add2_aligned (crypto_ops[0], op, 1, CLIB_CACHE_LINE_BYTES);
517 vnet_crypto_op_init (op, VNET_CRYPTO_OP_CHACHA20_POLY1305_DEC);
519 op->tag_len = NOISE_AUTHTAG_LEN;
520 op->tag = vlib_buffer_get_tail (lb) - NOISE_AUTHTAG_LEN;
521 op->key_index = key_index;
523 op->aad_len = aad_len;
526 op->flags |= VNET_CRYPTO_OP_FLAG_HMAC_CHECK;
530 /* Chained buffers */
531 op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
532 op->chunk_index = vec_len (ptd->chunks);
533 wg_input_chain_crypto (vm, ptd, b, lb, src, src_len + NOISE_AUTHTAG_LEN,
538 op->src = !src ? src_ : src;
544 static_always_inline void
545 wg_input_add_to_frame (vlib_main_t *vm, vnet_crypto_async_frame_t *f,
546 u32 key_index, u32 crypto_len, i16 crypto_start_offset,
547 u32 buffer_index, u16 next_node, u8 *iv, u8 *tag,
550 vnet_crypto_async_frame_elt_t *fe;
553 ASSERT (f->n_elts < VNET_CRYPTO_FRAME_SIZE);
556 fe = &f->elts[index];
558 fe->key_index = key_index;
559 fe->crypto_total_length = crypto_len;
560 fe->crypto_start_offset = crypto_start_offset;
564 f->buffer_indices[index] = buffer_index;
565 f->next_node_index[index] = next_node;
568 static_always_inline enum noise_state_crypt
569 wg_input_process (vlib_main_t *vm, wg_per_thread_data_t *ptd,
570 vnet_crypto_op_t **crypto_ops,
571 vnet_crypto_async_frame_t **async_frame, vlib_buffer_t *b,
572 vlib_buffer_t *lb, u32 buf_idx, noise_remote_t *r,
573 uint32_t r_idx, uint64_t nonce, uint8_t *src, size_t srclen,
574 size_t srclen_total, uint8_t *dst, u32 from_idx, u8 *iv,
575 f64 time, u8 is_async, u16 async_next_node)
578 enum noise_state_crypt ret = SC_FAILED;
580 if ((kp = wg_get_active_keypair (r, r_idx)) == NULL)
585 /* We confirm that our values are within our tolerances. These values
586 * are the same as the encrypt routine.
588 * kp_ctr isn't locked here, we're happy to accept a racy read. */
589 if (wg_birthdate_has_expired_opt (kp->kp_birthdate, REJECT_AFTER_TIME,
591 kp->kp_ctr.c_recv >= REJECT_AFTER_MESSAGES)
594 /* Decrypt, then validate the counter. We don't want to validate the
595 * counter before decrypting as we do not know the message is authentic
596 * prior to decryption. */
598 clib_memset (iv, 0, 4);
599 clib_memcpy (iv + 4, &nonce, sizeof (nonce));
603 u8 flags = VNET_CRYPTO_OP_FLAG_HMAC_CHECK;
604 u8 *tag = vlib_buffer_get_tail (lb) - NOISE_AUTHTAG_LEN;
607 flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
609 if (NULL == *async_frame ||
610 vnet_crypto_async_frame_is_full (*async_frame))
612 *async_frame = vnet_crypto_async_get_frame (
613 vm, VNET_CRYPTO_OP_CHACHA20_POLY1305_TAG16_AAD0_DEC);
614 /* Save the frame to the list we'll submit at the end */
615 vec_add1 (ptd->async_frames, *async_frame);
618 wg_input_add_to_frame (vm, *async_frame, kp->kp_recv_index, srclen_total,
619 src - b->data, buf_idx, async_next_node, iv, tag,
624 wg_prepare_sync_dec_op (vm, ptd, b, lb, crypto_ops, src, srclen, dst,
625 NULL, 0, kp->kp_recv_index, from_idx, iv);
628 /* If we've received the handshake confirming data packet then move the
629 * next keypair into current. If we do slide the next keypair in, then
630 * we skip the REKEY_AFTER_TIME_RECV check. This is safe to do as a
631 * data packet can't confirm a session that we are an INITIATOR of. */
634 clib_rwlock_writer_lock (&r->r_keypair_lock);
635 if (kp == r->r_next && kp->kp_local_index == r_idx)
637 noise_remote_keypair_free (vm, r, &r->r_previous);
638 r->r_previous = r->r_current;
639 r->r_current = r->r_next;
643 clib_rwlock_writer_unlock (&r->r_keypair_lock);
646 clib_rwlock_writer_unlock (&r->r_keypair_lock);
649 /* Similar to when we encrypt, we want to notify the caller when we
650 * are approaching our tolerances. We notify if:
651 * - we're the initiator and the current keypair is older than
652 * REKEY_AFTER_TIME_RECV seconds. */
653 ret = SC_KEEP_KEY_FRESH;
655 if (kp != NULL && kp->kp_valid && kp->kp_is_initiator &&
656 wg_birthdate_has_expired_opt (kp->kp_birthdate, REKEY_AFTER_TIME_RECV,
665 static_always_inline void
666 wg_find_outer_addr_port (vlib_buffer_t *b, ip46_address_t *addr, u16 *port,
671 ip4_udp_header_t *ip4_udp_hdr =
672 vlib_buffer_get_current (b) - sizeof (ip4_udp_header_t);
673 ip46_address_set_ip4 (addr, &ip4_udp_hdr->ip4.src_address);
674 *port = clib_net_to_host_u16 (ip4_udp_hdr->udp.src_port);
678 ip6_udp_header_t *ip6_udp_hdr =
679 vlib_buffer_get_current (b) - sizeof (ip6_udp_header_t);
680 ip46_address_set_ip6 (addr, &ip6_udp_hdr->ip6.src_address);
681 *port = clib_net_to_host_u16 (ip6_udp_hdr->udp.src_port);
686 wg_input_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
687 vlib_frame_t *frame, u8 is_ip4, u16 async_next_node)
689 vnet_main_t *vnm = vnet_get_main ();
690 vnet_interface_main_t *im = &vnm->interface_main;
691 wg_main_t *wmp = &wg_main;
692 wg_per_thread_data_t *ptd =
693 vec_elt_at_index (wmp->per_thread_data, vm->thread_index);
694 u32 *from = vlib_frame_vector_args (frame);
695 u32 n_left_from = frame->n_vectors;
697 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
699 u32 thread_index = vm->thread_index;
700 vnet_crypto_op_t **crypto_ops;
701 const u16 drop_next = WG_INPUT_NEXT_PUNT;
702 message_type_t header_type;
703 vlib_buffer_t *data_bufs[VLIB_FRAME_SIZE];
704 u32 data_bi[VLIB_FRAME_SIZE]; /* buffer index for data */
705 u32 other_bi[VLIB_FRAME_SIZE]; /* buffer index for drop or handoff */
706 u16 other_nexts[VLIB_FRAME_SIZE], *other_next = other_nexts, n_other = 0;
707 u16 data_nexts[VLIB_FRAME_SIZE], *data_next = data_nexts, n_data = 0;
709 const u8 is_async = wg_op_mode_is_set_ASYNC ();
710 vnet_crypto_async_frame_t *async_frame = NULL;
712 vlib_get_buffers (vm, from, bufs, n_left_from);
713 vec_reset_length (ptd->crypto_ops);
714 vec_reset_length (ptd->chained_crypto_ops);
715 vec_reset_length (ptd->chunks);
716 vec_reset_length (ptd->async_frames);
718 f64 time = clib_time_now (&vm->clib_time) + vm->time_offset;
720 wg_peer_t *peer = NULL;
721 u32 *last_peer_time_idx = NULL;
722 u32 last_rec_idx = ~0;
724 bool is_keepalive = false;
725 u32 *peer_idx = NULL;
726 index_t peeri = INDEX_INVALID;
728 while (n_left_from > 0)
733 vlib_prefetch_buffer_header (b[2], LOAD);
734 p = vlib_buffer_get_current (b[1]);
735 CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD);
736 CLIB_PREFETCH (vlib_buffer_get_tail (b[1]), CLIB_CACHE_LINE_BYTES,
740 other_next[n_other] = WG_INPUT_NEXT_PUNT;
741 data_nexts[n_data] = WG_INPUT_N_NEXT;
744 ((message_header_t *) vlib_buffer_get_current (b[0]))->type;
746 if (PREDICT_TRUE (header_type == MESSAGE_DATA))
748 message_data_t *data = vlib_buffer_get_current (b[0]);
749 u8 *iv_data = b[0]->pre_data;
750 u32 buf_idx = from[b - bufs];
752 peer_idx = wg_index_table_lookup (&wmp->index_table,
753 data->receiver_index);
755 if (data->receiver_index != last_rec_idx)
757 peer_idx = wg_index_table_lookup (&wmp->index_table,
758 data->receiver_index);
759 if (PREDICT_TRUE (peer_idx != NULL))
762 peer = wg_peer_get (peeri);
763 last_rec_idx = data->receiver_index;
772 if (PREDICT_FALSE (!peer_idx))
774 other_next[n_other] = WG_INPUT_NEXT_ERROR;
775 b[0]->error = node->errors[WG_INPUT_ERROR_PEER];
776 other_bi[n_other] = buf_idx;
781 if (PREDICT_FALSE (~0 == peer->input_thread_index))
783 /* this is the first packet to use this peer, claim the peer
786 clib_atomic_cmp_and_swap (&peer->input_thread_index, ~0,
787 wg_peer_assign_thread (thread_index));
790 if (PREDICT_TRUE (thread_index != peer->input_thread_index))
792 other_next[n_other] = WG_INPUT_NEXT_HANDOFF_DATA;
793 other_bi[n_other] = buf_idx;
799 n_bufs = vlib_buffer_chain_linearize (vm, b[0]);
802 other_next[n_other] = WG_INPUT_NEXT_ERROR;
803 b[0]->error = node->errors[WG_INPUT_ERROR_NO_BUFFERS];
804 other_bi[n_other] = buf_idx;
811 vlib_buffer_t *before_last = b[0];
813 /* Find last and before last buffer in the chain */
814 while (lb->flags & VLIB_BUFFER_NEXT_PRESENT)
817 lb = vlib_get_buffer (vm, lb->next_buffer);
820 /* Ensure auth tag is contiguous and not splitted into two last
822 if (PREDICT_FALSE (lb->current_length < NOISE_AUTHTAG_LEN))
824 u32 len_diff = NOISE_AUTHTAG_LEN - lb->current_length;
826 before_last->current_length -= len_diff;
827 if (before_last == b[0])
828 before_last->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
830 vlib_buffer_advance (lb, (signed) -len_diff);
832 clib_memcpy_fast (vlib_buffer_get_current (lb),
833 vlib_buffer_get_tail (before_last),
838 u16 encr_len = b[0]->current_length - sizeof (message_data_t);
839 u16 decr_len = encr_len - NOISE_AUTHTAG_LEN;
841 vlib_buffer_length_in_chain (vm, b[0]) - sizeof (message_data_t);
842 u16 decr_len_total = encr_len_total - NOISE_AUTHTAG_LEN;
845 crypto_ops = &ptd->chained_crypto_ops;
847 crypto_ops = &ptd->crypto_ops;
849 enum noise_state_crypt state_cr =
850 wg_input_process (vm, ptd, crypto_ops, &async_frame, b[0], lb,
851 buf_idx, &peer->remote, data->receiver_index,
852 data->counter, data->encrypted_data, decr_len,
853 decr_len_total, data->encrypted_data, n_data,
854 iv_data, time, is_async, async_next_node);
856 if (PREDICT_FALSE (state_cr == SC_FAILED))
858 wg_peer_update_flags (*peer_idx, WG_PEER_ESTABLISHED, false);
859 other_next[n_other] = WG_INPUT_NEXT_ERROR;
860 b[0]->error = node->errors[WG_INPUT_ERROR_DECRYPTION];
861 other_bi[n_other] = buf_idx;
867 data_bufs[n_data] = b[0];
868 data_bi[n_data] = buf_idx;
876 if (PREDICT_FALSE (state_cr == SC_CONN_RESET))
878 wg_timers_handshake_complete (peer);
881 else if (PREDICT_FALSE (state_cr == SC_KEEP_KEY_FRESH))
883 wg_send_handshake_from_mt (peeri, false);
886 else if (PREDICT_TRUE (state_cr == SC_OK))
891 /* Handshake packets should be processed in main thread */
892 if (thread_index != 0)
894 other_next[n_other] = WG_INPUT_NEXT_HANDOFF_HANDSHAKE;
895 other_bi[n_other] = from[b - bufs];
900 wg_input_error_t ret =
901 wg_handshake_process (vm, wmp, b[0], node->node_index, is_ip4);
902 if (ret != WG_INPUT_ERROR_NONE)
904 other_next[n_other] = WG_INPUT_NEXT_ERROR;
905 b[0]->error = node->errors[ret];
906 other_bi[n_other] = from[b - bufs];
911 other_bi[n_other] = from[b - bufs];
917 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
918 (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
920 wg_input_trace_t *t = vlib_add_trace (vm, node, b[0], sizeof (*t));
921 t->type = header_type;
922 t->current_length = b[0]->current_length;
923 t->is_keepalive = is_keepalive;
924 t->peer = peer_idx ? peeri : INDEX_INVALID;
932 /* decrypt packets */
933 wg_input_process_ops (vm, node, ptd->crypto_ops, data_bufs, data_nexts,
935 wg_input_process_chained_ops (vm, node, ptd->chained_crypto_ops, data_bufs,
936 data_nexts, ptd->chunks, drop_next);
938 /* process after decryption */
940 n_left_from = n_data;
942 last_peer_time_idx = NULL;
944 while (n_left_from > 0)
946 bool is_keepalive = false;
947 u32 *peer_idx = NULL;
949 if (PREDICT_FALSE (data_next[0] == WG_INPUT_NEXT_PUNT))
956 vlib_prefetch_buffer_header (b[2], LOAD);
957 p = vlib_buffer_get_current (b[1]);
958 CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD);
959 CLIB_PREFETCH (vlib_buffer_get_tail (b[1]), CLIB_CACHE_LINE_BYTES,
963 message_data_t *data = vlib_buffer_get_current (b[0]);
964 ip46_address_t out_src_ip;
965 u16 out_udp_src_port;
967 wg_find_outer_addr_port (b[0], &out_src_ip, &out_udp_src_port, is_ip4);
969 if (data->receiver_index != last_rec_idx)
972 wg_index_table_lookup (&wmp->index_table, data->receiver_index);
973 if (PREDICT_TRUE (peer_idx != NULL))
976 peer = wg_peer_get (peeri);
977 last_rec_idx = data->receiver_index;
986 if (PREDICT_TRUE (peer != NULL))
988 if (PREDICT_FALSE (wg_input_post_process (vm, b[0], data_next, peer,
989 data, &is_keepalive) < 0))
994 data_next[0] = WG_INPUT_NEXT_PUNT;
998 if (PREDICT_FALSE (peer_idx && (last_peer_time_idx != peer_idx)))
1001 !ip46_address_is_equal (&peer->dst.addr, &out_src_ip) ||
1002 peer->dst.port != out_udp_src_port))
1003 wg_peer_update_endpoint_from_mt (peeri, &out_src_ip,
1005 wg_timers_any_authenticated_packet_received_opt (peer, time);
1006 wg_timers_any_authenticated_packet_traversal (peer);
1007 wg_peer_update_flags (*peer_idx, WG_PEER_ESTABLISHED, true);
1008 last_peer_time_idx = peer_idx;
1011 vlib_increment_combined_counter (im->combined_sw_if_counters +
1012 VNET_INTERFACE_COUNTER_RX,
1013 vm->thread_index, peer->wg_sw_if_index,
1014 1 /* packets */, b[0]->current_length);
1017 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
1018 (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
1020 wg_input_trace_t *t = vlib_add_trace (vm, node, b[0], sizeof (*t));
1021 t->type = header_type;
1022 t->current_length = b[0]->current_length;
1023 t->is_keepalive = is_keepalive;
1024 t->peer = peer_idx ? peeri : INDEX_INVALID;
1034 /* submit all of the open frames */
1035 vnet_crypto_async_frame_t **async_frame;
1036 vec_foreach (async_frame, ptd->async_frames)
1039 vnet_crypto_async_submit_open_frame (vm, *async_frame) < 0))
1041 u32 n_drop = (*async_frame)->n_elts;
1042 u32 *bi = (*async_frame)->buffer_indices;
1043 u16 index = n_other;
1046 other_bi[index] = bi[0];
1047 vlib_buffer_t *b = vlib_get_buffer (vm, bi[0]);
1048 other_nexts[index] = drop_next;
1049 b->error = node->errors[WG_INPUT_ERROR_CRYPTO_ENGINE_ERROR];
1053 n_other += (*async_frame)->n_elts;
1055 vnet_crypto_async_reset_frame (*async_frame);
1056 vnet_crypto_async_free_frame (vm, *async_frame);
1061 /* enqueue other bufs */
1063 vlib_buffer_enqueue_to_next (vm, node, other_bi, other_next, n_other);
1065 /* enqueue data bufs */
1067 vlib_buffer_enqueue_to_next (vm, node, data_bi, data_nexts, n_data);
1069 return frame->n_vectors;
1073 wg_input_post (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame,
1076 vnet_main_t *vnm = vnet_get_main ();
1077 vnet_interface_main_t *im = &vnm->interface_main;
1078 wg_main_t *wmp = &wg_main;
1079 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1080 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
1081 u32 *from = vlib_frame_vector_args (frame);
1082 u32 n_left = frame->n_vectors;
1083 wg_peer_t *peer = NULL;
1084 u32 *peer_idx = NULL;
1085 u32 *last_peer_time_idx = NULL;
1086 index_t peeri = INDEX_INVALID;
1087 u32 last_rec_idx = ~0;
1088 f64 time = clib_time_now (&vm->clib_time) + vm->time_offset;
1090 vlib_get_buffers (vm, from, b, n_left);
1094 vlib_prefetch_buffer_header (b[0], LOAD);
1095 vlib_prefetch_buffer_header (b[1], LOAD);
1103 vlib_prefetch_buffer_header (b[2], LOAD);
1104 p = vlib_buffer_get_current (b[1]);
1105 CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD);
1108 bool is_keepalive = false;
1109 message_data_t *data = vlib_buffer_get_current (b[0]);
1110 ip46_address_t out_src_ip;
1111 u16 out_udp_src_port;
1113 wg_find_outer_addr_port (b[0], &out_src_ip, &out_udp_src_port, is_ip4);
1115 if (data->receiver_index != last_rec_idx)
1118 wg_index_table_lookup (&wmp->index_table, data->receiver_index);
1120 if (PREDICT_TRUE (peer_idx != NULL))
1123 peer = wg_peer_get (peeri);
1124 last_rec_idx = data->receiver_index;
1133 if (PREDICT_TRUE (peer != NULL))
1135 if (PREDICT_FALSE (wg_input_post_process (vm, b[0], next, peer, data,
1136 &is_keepalive) < 0))
1141 next[0] = WG_INPUT_NEXT_PUNT;
1145 if (PREDICT_FALSE (peer_idx && (last_peer_time_idx != peer_idx)))
1148 !ip46_address_is_equal (&peer->dst.addr, &out_src_ip) ||
1149 peer->dst.port != out_udp_src_port))
1150 wg_peer_update_endpoint_from_mt (peeri, &out_src_ip,
1152 wg_timers_any_authenticated_packet_received_opt (peer, time);
1153 wg_timers_any_authenticated_packet_traversal (peer);
1154 wg_peer_update_flags (*peer_idx, WG_PEER_ESTABLISHED, true);
1155 last_peer_time_idx = peer_idx;
1158 vlib_increment_combined_counter (im->combined_sw_if_counters +
1159 VNET_INTERFACE_COUNTER_RX,
1160 vm->thread_index, peer->wg_sw_if_index,
1161 1 /* packets */, b[0]->current_length);
1164 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
1165 (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
1167 wg_input_post_trace_t *t =
1168 vlib_add_trace (vm, node, b[0], sizeof (*t));
1170 t->peer = peer_idx ? peeri : INDEX_INVALID;
1178 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
1179 return frame->n_vectors;
1182 VLIB_NODE_FN (wg4_input_node)
1183 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
1185 return wg_input_inline (vm, node, frame, /* is_ip4 */ 1,
1186 wg_decrypt_async_next.wg4_post_next);
1189 VLIB_NODE_FN (wg6_input_node)
1190 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
1192 return wg_input_inline (vm, node, frame, /* is_ip4 */ 0,
1193 wg_decrypt_async_next.wg6_post_next);
1196 VLIB_NODE_FN (wg4_input_post_node)
1197 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
1199 return wg_input_post (vm, node, from_frame, /* is_ip4 */ 1);
1202 VLIB_NODE_FN (wg6_input_post_node)
1203 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
1205 return wg_input_post (vm, node, from_frame, /* is_ip4 */ 0);
1209 VLIB_REGISTER_NODE (wg4_input_node) =
1211 .name = "wg4-input",
1212 .vector_size = sizeof (u32),
1213 .format_trace = format_wg_input_trace,
1214 .type = VLIB_NODE_TYPE_INTERNAL,
1215 .n_errors = ARRAY_LEN (wg_input_error_strings),
1216 .error_strings = wg_input_error_strings,
1217 .n_next_nodes = WG_INPUT_N_NEXT,
1218 /* edit / add dispositions here */
1220 [WG_INPUT_NEXT_HANDOFF_HANDSHAKE] = "wg4-handshake-handoff",
1221 [WG_INPUT_NEXT_HANDOFF_DATA] = "wg4-input-data-handoff",
1222 [WG_INPUT_NEXT_IP4_INPUT] = "ip4-input-no-checksum",
1223 [WG_INPUT_NEXT_IP6_INPUT] = "ip6-input",
1224 [WG_INPUT_NEXT_PUNT] = "error-punt",
1225 [WG_INPUT_NEXT_ERROR] = "error-drop",
1229 VLIB_REGISTER_NODE (wg6_input_node) =
1231 .name = "wg6-input",
1232 .vector_size = sizeof (u32),
1233 .format_trace = format_wg_input_trace,
1234 .type = VLIB_NODE_TYPE_INTERNAL,
1235 .n_errors = ARRAY_LEN (wg_input_error_strings),
1236 .error_strings = wg_input_error_strings,
1237 .n_next_nodes = WG_INPUT_N_NEXT,
1238 /* edit / add dispositions here */
1240 [WG_INPUT_NEXT_HANDOFF_HANDSHAKE] = "wg6-handshake-handoff",
1241 [WG_INPUT_NEXT_HANDOFF_DATA] = "wg6-input-data-handoff",
1242 [WG_INPUT_NEXT_IP4_INPUT] = "ip4-input-no-checksum",
1243 [WG_INPUT_NEXT_IP6_INPUT] = "ip6-input",
1244 [WG_INPUT_NEXT_PUNT] = "error-punt",
1245 [WG_INPUT_NEXT_ERROR] = "error-drop",
1249 VLIB_REGISTER_NODE (wg4_input_post_node) = {
1250 .name = "wg4-input-post-node",
1251 .vector_size = sizeof (u32),
1252 .format_trace = format_wg_input_post_trace,
1253 .type = VLIB_NODE_TYPE_INTERNAL,
1254 .sibling_of = "wg4-input",
1256 .n_errors = ARRAY_LEN (wg_input_error_strings),
1257 .error_strings = wg_input_error_strings,
1260 VLIB_REGISTER_NODE (wg6_input_post_node) = {
1261 .name = "wg6-input-post-node",
1262 .vector_size = sizeof (u32),
1263 .format_trace = format_wg_input_post_trace,
1264 .type = VLIB_NODE_TYPE_INTERNAL,
1265 .sibling_of = "wg6-input",
1267 .n_errors = ARRAY_LEN (wg_input_error_strings),
1268 .error_strings = wg_input_error_strings,
1274 * fd.io coding-style-patch-verification: ON
1277 * eval: (c-set-style "gnu")