Skip to content

Commit c94683e

Browse files
committed
Merge branch 'wireguard-fixes'
Jason A. Donenfeld says: ==================== wireguard fixes for 6.4.2/6.5-rc1 Sorry to send these patches during the merge window, but they're net fixes, not netdev enhancements, and while I'd ordinarily wait anyway, I just got a first bug report for one of these fixes, which I originally had thought was mostly unlikely. So please apply the following three patches to net: 1) Make proper use of nr_cpu_ids with cpumask_next(), rather than awkwardly using modulo, to handle dynamic CPU topology changes. Linus noticed this a while ago and pointed it out, and today a user actually got hit by it. 2) Respect persistent keepalive and other staged packets when setting the private key after the interface is already up. 3) Use timer_delete_sync() instead of del_timer_sync(), per the documentation. ==================== Signed-off-by: David S. Miller <[email protected]>
2 parents a27ac53 + 326534e commit c94683e

File tree

7 files changed

+54
-30
lines changed

7 files changed

+54
-30
lines changed

drivers/net/wireguard/netlink.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ static int wg_set_device(struct sk_buff *skb, struct genl_info *info)
546546
u8 *private_key = nla_data(info->attrs[WGDEVICE_A_PRIVATE_KEY]);
547547
u8 public_key[NOISE_PUBLIC_KEY_LEN];
548548
struct wg_peer *peer, *temp;
549+
bool send_staged_packets;
549550

550551
if (!crypto_memneq(wg->static_identity.static_private,
551552
private_key, NOISE_PUBLIC_KEY_LEN))
@@ -564,14 +565,17 @@ static int wg_set_device(struct sk_buff *skb, struct genl_info *info)
564565
}
565566

566567
down_write(&wg->static_identity.lock);
567-
wg_noise_set_static_identity_private_key(&wg->static_identity,
568-
private_key);
569-
list_for_each_entry_safe(peer, temp, &wg->peer_list,
570-
peer_list) {
568+
send_staged_packets = !wg->static_identity.has_identity && netif_running(wg->dev);
569+
wg_noise_set_static_identity_private_key(&wg->static_identity, private_key);
570+
send_staged_packets = send_staged_packets && wg->static_identity.has_identity;
571+
572+
wg_cookie_checker_precompute_device_keys(&wg->cookie_checker);
573+
list_for_each_entry_safe(peer, temp, &wg->peer_list, peer_list) {
571574
wg_noise_precompute_static_static(peer);
572575
wg_noise_expire_current_peer_keypairs(peer);
576+
if (send_staged_packets)
577+
wg_packet_send_staged_packets(peer);
573578
}
574-
wg_cookie_checker_precompute_device_keys(&wg->cookie_checker);
575579
up_write(&wg->static_identity.lock);
576580
}
577581
skip_set_private_key:

drivers/net/wireguard/queueing.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ int wg_packet_queue_init(struct crypt_queue *queue, work_func_t function,
2828
int ret;
2929

3030
memset(queue, 0, sizeof(*queue));
31+
queue->last_cpu = -1;
3132
ret = ptr_ring_init(&queue->ring, len, GFP_KERNEL);
3233
if (ret)
3334
return ret;

drivers/net/wireguard/queueing.h

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -117,20 +117,17 @@ static inline int wg_cpumask_choose_online(int *stored_cpu, unsigned int id)
117117
return cpu;
118118
}
119119

120-
/* This function is racy, in the sense that next is unlocked, so it could return
121-
* the same CPU twice. A race-free version of this would be to instead store an
122-
* atomic sequence number, do an increment-and-return, and then iterate through
123-
* every possible CPU until we get to that index -- choose_cpu. However that's
124-
* a bit slower, and it doesn't seem like this potential race actually
125-
* introduces any performance loss, so we live with it.
120+
/* This function is racy, in the sense that it's called while last_cpu is
121+
* unlocked, so it could return the same CPU twice. Adding locking or using
122+
* atomic sequence numbers is slower though, and the consequences of racing are
123+
* harmless, so live with it.
126124
*/
127-
static inline int wg_cpumask_next_online(int *next)
125+
static inline int wg_cpumask_next_online(int *last_cpu)
128126
{
129-
int cpu = *next;
130-
131-
while (unlikely(!cpumask_test_cpu(cpu, cpu_online_mask)))
132-
cpu = cpumask_next(cpu, cpu_online_mask) % nr_cpumask_bits;
133-
*next = cpumask_next(cpu, cpu_online_mask) % nr_cpumask_bits;
127+
int cpu = cpumask_next(*last_cpu, cpu_online_mask);
128+
if (cpu >= nr_cpu_ids)
129+
cpu = cpumask_first(cpu_online_mask);
130+
*last_cpu = cpu;
134131
return cpu;
135132
}
136133

@@ -159,7 +156,7 @@ static inline void wg_prev_queue_drop_peeked(struct prev_queue *queue)
159156

160157
static inline int wg_queue_enqueue_per_device_and_peer(
161158
struct crypt_queue *device_queue, struct prev_queue *peer_queue,
162-
struct sk_buff *skb, struct workqueue_struct *wq, int *next_cpu)
159+
struct sk_buff *skb, struct workqueue_struct *wq)
163160
{
164161
int cpu;
165162

@@ -173,7 +170,7 @@ static inline int wg_queue_enqueue_per_device_and_peer(
173170
/* Then we queue it up in the device queue, which consumes the
174171
* packet as soon as it can.
175172
*/
176-
cpu = wg_cpumask_next_online(next_cpu);
173+
cpu = wg_cpumask_next_online(&device_queue->last_cpu);
177174
if (unlikely(ptr_ring_produce_bh(&device_queue->ring, skb)))
178175
return -EPIPE;
179176
queue_work_on(cpu, wq, &per_cpu_ptr(device_queue->worker, cpu)->work);

drivers/net/wireguard/receive.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ static void wg_packet_consume_data(struct wg_device *wg, struct sk_buff *skb)
524524
goto err;
525525

526526
ret = wg_queue_enqueue_per_device_and_peer(&wg->decrypt_queue, &peer->rx_queue, skb,
527-
wg->packet_crypt_wq, &wg->decrypt_queue.last_cpu);
527+
wg->packet_crypt_wq);
528528
if (unlikely(ret == -EPIPE))
529529
wg_queue_enqueue_per_peer_rx(skb, PACKET_STATE_DEAD);
530530
if (likely(!ret || ret == -EPIPE)) {

drivers/net/wireguard/send.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ static void wg_packet_create_data(struct wg_peer *peer, struct sk_buff *first)
318318
goto err;
319319

320320
ret = wg_queue_enqueue_per_device_and_peer(&wg->encrypt_queue, &peer->tx_queue, first,
321-
wg->packet_crypt_wq, &wg->encrypt_queue.last_cpu);
321+
wg->packet_crypt_wq);
322322
if (unlikely(ret == -EPIPE))
323323
wg_queue_enqueue_per_peer_tx(first, PACKET_STATE_DEAD);
324324
err:

drivers/net/wireguard/timers.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,10 @@ void wg_timers_init(struct wg_peer *peer)
234234

235235
void wg_timers_stop(struct wg_peer *peer)
236236
{
237-
del_timer_sync(&peer->timer_retransmit_handshake);
238-
del_timer_sync(&peer->timer_send_keepalive);
239-
del_timer_sync(&peer->timer_new_handshake);
240-
del_timer_sync(&peer->timer_zero_key_material);
241-
del_timer_sync(&peer->timer_persistent_keepalive);
237+
timer_delete_sync(&peer->timer_retransmit_handshake);
238+
timer_delete_sync(&peer->timer_send_keepalive);
239+
timer_delete_sync(&peer->timer_new_handshake);
240+
timer_delete_sync(&peer->timer_zero_key_material);
241+
timer_delete_sync(&peer->timer_persistent_keepalive);
242242
flush_work(&peer->clear_peer_work);
243243
}

tools/testing/selftests/wireguard/netns.sh

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -514,10 +514,32 @@ n2 bash -c 'printf 0 > /proc/sys/net/ipv4/conf/all/rp_filter'
514514
n1 ping -W 1 -c 1 192.168.241.2
515515
[[ $(n2 wg show wg0 endpoints) == "$pub1 10.0.0.3:1" ]]
516516

517-
ip1 link del veth1
518-
ip1 link del veth3
519-
ip1 link del wg0
520-
ip2 link del wg0
517+
ip1 link del dev veth3
518+
ip1 link del dev wg0
519+
ip2 link del dev wg0
520+
521+
# Make sure persistent keep alives are sent when an adapter comes up
522+
ip1 link add dev wg0 type wireguard
523+
n1 wg set wg0 private-key <(echo "$key1") peer "$pub2" endpoint 10.0.0.1:1 persistent-keepalive 1
524+
read _ _ tx_bytes < <(n1 wg show wg0 transfer)
525+
[[ $tx_bytes -eq 0 ]]
526+
ip1 link set dev wg0 up
527+
read _ _ tx_bytes < <(n1 wg show wg0 transfer)
528+
[[ $tx_bytes -gt 0 ]]
529+
ip1 link del dev wg0
530+
# This should also happen even if the private key is set later
531+
ip1 link add dev wg0 type wireguard
532+
n1 wg set wg0 peer "$pub2" endpoint 10.0.0.1:1 persistent-keepalive 1
533+
read _ _ tx_bytes < <(n1 wg show wg0 transfer)
534+
[[ $tx_bytes -eq 0 ]]
535+
ip1 link set dev wg0 up
536+
read _ _ tx_bytes < <(n1 wg show wg0 transfer)
537+
[[ $tx_bytes -eq 0 ]]
538+
n1 wg set wg0 private-key <(echo "$key1")
539+
read _ _ tx_bytes < <(n1 wg show wg0 transfer)
540+
[[ $tx_bytes -gt 0 ]]
541+
ip1 link del dev veth1
542+
ip1 link del dev wg0
521543

522544
# We test that Netlink/IPC is working properly by doing things that usually cause split responses
523545
ip0 link add dev wg0 type wireguard

0 commit comments

Comments
 (0)