1 // SPDX-License-Identifier: GPL-2.0
10 #include "ratelimiter.h"
14 #include <linux/module.h>
15 #include <linux/rtnetlink.h>
16 #include <linux/inet.h>
17 #include <linux/netdevice.h>
18 #include <linux/inetdevice.h>
19 #include <linux/if_arp.h>
20 #include <linux/icmp.h>
21 #include <linux/suspend.h>
23 #include <net/rtnetlink.h>
24 #include <net/ip_tunnels.h>
25 #include <net/addrconf.h>
27 static LIST_HEAD(device_list);
29 static int wg_open(struct net_device *dev)
31 struct in_device *dev_v4 = __in_dev_get_rtnl(dev);
32 struct inet6_dev *dev_v6 = __in6_dev_get(dev);
33 struct wg_device *wg = netdev_priv(dev);
38 /* At some point we might put this check near the ip_rt_send_
39 * redirect call of ip_forward in net/ipv4/ip_forward.c, similar
40 * to the current secpath check.
42 IN_DEV_CONF_SET(dev_v4, SEND_REDIRECTS, false);
43 IPV4_DEVCONF_ALL(dev_net(dev), SEND_REDIRECTS) = false;
46 dev_v6->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_NONE;
48 mutex_lock(&wg->device_update_lock);
49 ret = wg_socket_init(wg, wg->incoming_port);
52 list_for_each_entry(peer, &wg->peer_list, peer_list) {
53 wg_packet_send_staged_packets(peer);
54 if (peer->persistent_keepalive_interval)
55 wg_packet_send_keepalive(peer);
58 mutex_unlock(&wg->device_update_lock);
62 static int wg_pm_notification(struct notifier_block *nb, unsigned long action, void *data)
67 /* If the machine is constantly suspending and resuming, as part of
68 * its normal operation rather than as a somewhat rare event, then we
69 * don't actually want to clear keys.
71 if (IS_ENABLED(CONFIG_PM_AUTOSLEEP) || IS_ENABLED(CONFIG_ANDROID))
74 if (action != PM_HIBERNATION_PREPARE && action != PM_SUSPEND_PREPARE)
78 list_for_each_entry(wg, &device_list, device_list) {
79 mutex_lock(&wg->device_update_lock);
80 list_for_each_entry(peer, &wg->peer_list, peer_list) {
81 del_timer(&peer->timer_zero_key_material);
82 wg_noise_handshake_clear(&peer->handshake);
83 wg_noise_keypairs_clear(&peer->keypairs);
85 mutex_unlock(&wg->device_update_lock);
92 static struct notifier_block pm_notifier = { .notifier_call = wg_pm_notification };
94 static int wg_vm_notification(struct notifier_block *nb, unsigned long action, void *data)
100 list_for_each_entry(wg, &device_list, device_list) {
101 mutex_lock(&wg->device_update_lock);
102 list_for_each_entry(peer, &wg->peer_list, peer_list)
103 wg_noise_expire_current_peer_keypairs(peer);
104 mutex_unlock(&wg->device_update_lock);
110 static struct notifier_block vm_notifier = { .notifier_call = wg_vm_notification };
112 static int wg_stop(struct net_device *dev)
114 struct wg_device *wg = netdev_priv(dev);
115 struct wg_peer *peer;
118 mutex_lock(&wg->device_update_lock);
119 list_for_each_entry(peer, &wg->peer_list, peer_list) {
120 wg_packet_purge_staged_packets(peer);
121 wg_timers_stop(peer);
122 wg_noise_handshake_clear(&peer->handshake);
123 wg_noise_keypairs_clear(&peer->keypairs);
124 wg_noise_reset_last_sent_handshake(&peer->last_sent_handshake);
126 mutex_unlock(&wg->device_update_lock);
127 while ((skb = ptr_ring_consume(&wg->handshake_queue.ring)) != NULL)
129 atomic_set(&wg->handshake_queue_len, 0);
130 wg_socket_reinit(wg, NULL, NULL);
134 static netdev_tx_t wg_xmit(struct sk_buff *skb, struct net_device *dev)
136 struct wg_device *wg = netdev_priv(dev);
137 struct sk_buff_head packets;
138 struct wg_peer *peer;
139 struct sk_buff *next;
144 if (unlikely(!wg_check_packet_protocol(skb))) {
145 ret = -EPROTONOSUPPORT;
146 net_dbg_ratelimited("%s: Invalid IP packet\n", dev->name);
150 peer = wg_allowedips_lookup_dst(&wg->peer_allowedips, skb);
151 if (unlikely(!peer)) {
153 if (skb->protocol == htons(ETH_P_IP))
154 net_dbg_ratelimited("%s: No peer has allowed IPs matching %pI4\n",
155 dev->name, &ip_hdr(skb)->daddr);
156 else if (skb->protocol == htons(ETH_P_IPV6))
157 net_dbg_ratelimited("%s: No peer has allowed IPs matching %pI6\n",
158 dev->name, &ipv6_hdr(skb)->daddr);
162 family = READ_ONCE(peer->endpoint.addr.sa_family);
163 if (unlikely(family != AF_INET && family != AF_INET6)) {
165 net_dbg_ratelimited("%s: No valid endpoint has been configured or discovered for peer %llu\n",
166 dev->name, peer->internal_id);
170 mtu = skb_dst(skb) ? dst_mtu(skb_dst(skb)) : dev->mtu;
172 __skb_queue_head_init(&packets);
173 if (!skb_is_gso(skb)) {
174 skb_mark_not_on_list(skb);
176 struct sk_buff *segs = skb_gso_segment(skb, 0);
186 skb_list_walk_safe(skb, skb, next) {
187 skb_mark_not_on_list(skb);
189 skb = skb_share_check(skb, GFP_ATOMIC);
193 /* We only need to keep the original dst around for icmp,
194 * so at this point we're in a position to drop it.
198 PACKET_CB(skb)->mtu = mtu;
200 __skb_queue_tail(&packets, skb);
203 spin_lock_bh(&peer->staged_packet_queue.lock);
204 /* If the queue is getting too big, we start removing the oldest packets
205 * until it's small again. We do this before adding the new packet, so
206 * we don't remove GSO segments that are in excess.
208 while (skb_queue_len(&peer->staged_packet_queue) > MAX_STAGED_PACKETS) {
209 dev_kfree_skb(__skb_dequeue(&peer->staged_packet_queue));
210 ++dev->stats.tx_dropped;
212 skb_queue_splice_tail(&packets, &peer->staged_packet_queue);
213 spin_unlock_bh(&peer->staged_packet_queue.lock);
215 wg_packet_send_staged_packets(peer);
223 if (skb->protocol == htons(ETH_P_IP))
224 icmp_ndo_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0);
225 else if (skb->protocol == htons(ETH_P_IPV6))
226 icmpv6_ndo_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
228 ++dev->stats.tx_errors;
233 static const struct net_device_ops netdev_ops = {
236 .ndo_start_xmit = wg_xmit,
237 .ndo_get_stats64 = dev_get_tstats64
240 static void wg_destruct(struct net_device *dev)
242 struct wg_device *wg = netdev_priv(dev);
245 list_del(&wg->device_list);
247 mutex_lock(&wg->device_update_lock);
248 rcu_assign_pointer(wg->creating_net, NULL);
249 wg->incoming_port = 0;
250 wg_socket_reinit(wg, NULL, NULL);
251 /* The final references are cleared in the below calls to destroy_workqueue. */
252 wg_peer_remove_all(wg);
253 destroy_workqueue(wg->handshake_receive_wq);
254 destroy_workqueue(wg->handshake_send_wq);
255 destroy_workqueue(wg->packet_crypt_wq);
256 wg_packet_queue_free(&wg->handshake_queue, true);
257 wg_packet_queue_free(&wg->decrypt_queue, false);
258 wg_packet_queue_free(&wg->encrypt_queue, false);
259 rcu_barrier(); /* Wait for all the peers to be actually freed. */
260 wg_ratelimiter_uninit();
261 memzero_explicit(&wg->static_identity, sizeof(wg->static_identity));
262 free_percpu(dev->tstats);
263 kvfree(wg->index_hashtable);
264 kvfree(wg->peer_hashtable);
265 mutex_unlock(&wg->device_update_lock);
267 pr_debug("%s: Interface destroyed\n", dev->name);
271 static const struct device_type device_type = { .name = KBUILD_MODNAME };
273 static void wg_setup(struct net_device *dev)
275 struct wg_device *wg = netdev_priv(dev);
276 enum { WG_NETDEV_FEATURES = NETIF_F_HW_CSUM | NETIF_F_RXCSUM |
277 NETIF_F_SG | NETIF_F_GSO |
278 NETIF_F_GSO_SOFTWARE | NETIF_F_HIGHDMA };
279 const int overhead = MESSAGE_MINIMUM_LENGTH + sizeof(struct udphdr) +
280 max(sizeof(struct ipv6hdr), sizeof(struct iphdr));
282 dev->netdev_ops = &netdev_ops;
283 dev->header_ops = &ip_tunnel_header_ops;
284 dev->hard_header_len = 0;
286 dev->needed_headroom = DATA_PACKET_HEAD_ROOM;
287 dev->needed_tailroom = noise_encrypted_len(MESSAGE_PADDING_MULTIPLE);
288 dev->type = ARPHRD_NONE;
289 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
290 dev->priv_flags |= IFF_NO_QUEUE;
291 dev->features |= NETIF_F_LLTX;
292 dev->features |= WG_NETDEV_FEATURES;
293 dev->hw_features |= WG_NETDEV_FEATURES;
294 dev->hw_enc_features |= WG_NETDEV_FEATURES;
295 dev->mtu = ETH_DATA_LEN - overhead;
296 dev->max_mtu = round_down(INT_MAX, MESSAGE_PADDING_MULTIPLE) - overhead;
298 SET_NETDEV_DEVTYPE(dev, &device_type);
300 /* We need to keep the dst around in case of icmp replies. */
303 memset(wg, 0, sizeof(*wg));
307 static int wg_newlink(struct net *src_net, struct net_device *dev,
308 struct nlattr *tb[], struct nlattr *data[],
309 struct netlink_ext_ack *extack)
311 struct wg_device *wg = netdev_priv(dev);
314 rcu_assign_pointer(wg->creating_net, src_net);
315 init_rwsem(&wg->static_identity.lock);
316 mutex_init(&wg->socket_update_lock);
317 mutex_init(&wg->device_update_lock);
318 wg_allowedips_init(&wg->peer_allowedips);
319 wg_cookie_checker_init(&wg->cookie_checker, wg);
320 INIT_LIST_HEAD(&wg->peer_list);
321 wg->device_update_gen = 1;
323 wg->peer_hashtable = wg_pubkey_hashtable_alloc();
324 if (!wg->peer_hashtable)
327 wg->index_hashtable = wg_index_hashtable_alloc();
328 if (!wg->index_hashtable)
329 goto err_free_peer_hashtable;
331 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
333 goto err_free_index_hashtable;
335 wg->handshake_receive_wq = alloc_workqueue("wg-kex-%s",
336 WQ_CPU_INTENSIVE | WQ_FREEZABLE, 0, dev->name);
337 if (!wg->handshake_receive_wq)
338 goto err_free_tstats;
340 wg->handshake_send_wq = alloc_workqueue("wg-kex-%s",
341 WQ_UNBOUND | WQ_FREEZABLE, 0, dev->name);
342 if (!wg->handshake_send_wq)
343 goto err_destroy_handshake_receive;
345 wg->packet_crypt_wq = alloc_workqueue("wg-crypt-%s",
346 WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, 0, dev->name);
347 if (!wg->packet_crypt_wq)
348 goto err_destroy_handshake_send;
350 ret = wg_packet_queue_init(&wg->encrypt_queue, wg_packet_encrypt_worker,
353 goto err_destroy_packet_crypt;
355 ret = wg_packet_queue_init(&wg->decrypt_queue, wg_packet_decrypt_worker,
358 goto err_free_encrypt_queue;
360 ret = wg_packet_queue_init(&wg->handshake_queue, wg_packet_handshake_receive_worker,
361 MAX_QUEUED_INCOMING_HANDSHAKES);
363 goto err_free_decrypt_queue;
365 ret = wg_ratelimiter_init();
367 goto err_free_handshake_queue;
369 ret = register_netdevice(dev);
371 goto err_uninit_ratelimiter;
373 list_add(&wg->device_list, &device_list);
375 /* We wait until the end to assign priv_destructor, so that
376 * register_netdevice doesn't call it for us if it fails.
378 dev->priv_destructor = wg_destruct;
380 pr_debug("%s: Interface created\n", dev->name);
383 err_uninit_ratelimiter:
384 wg_ratelimiter_uninit();
385 err_free_handshake_queue:
386 wg_packet_queue_free(&wg->handshake_queue, false);
387 err_free_decrypt_queue:
388 wg_packet_queue_free(&wg->decrypt_queue, false);
389 err_free_encrypt_queue:
390 wg_packet_queue_free(&wg->encrypt_queue, false);
391 err_destroy_packet_crypt:
392 destroy_workqueue(wg->packet_crypt_wq);
393 err_destroy_handshake_send:
394 destroy_workqueue(wg->handshake_send_wq);
395 err_destroy_handshake_receive:
396 destroy_workqueue(wg->handshake_receive_wq);
398 free_percpu(dev->tstats);
399 err_free_index_hashtable:
400 kvfree(wg->index_hashtable);
401 err_free_peer_hashtable:
402 kvfree(wg->peer_hashtable);
406 static struct rtnl_link_ops link_ops __read_mostly = {
407 .kind = KBUILD_MODNAME,
408 .priv_size = sizeof(struct wg_device),
410 .newlink = wg_newlink,
413 static void wg_netns_pre_exit(struct net *net)
415 struct wg_device *wg;
416 struct wg_peer *peer;
419 list_for_each_entry(wg, &device_list, device_list) {
420 if (rcu_access_pointer(wg->creating_net) == net) {
421 pr_debug("%s: Creating namespace exiting\n", wg->dev->name);
422 netif_carrier_off(wg->dev);
423 mutex_lock(&wg->device_update_lock);
424 rcu_assign_pointer(wg->creating_net, NULL);
425 wg_socket_reinit(wg, NULL, NULL);
426 list_for_each_entry(peer, &wg->peer_list, peer_list)
427 wg_socket_clear_peer_endpoint_src(peer);
428 mutex_unlock(&wg->device_update_lock);
434 static struct pernet_operations pernet_ops = {
435 .pre_exit = wg_netns_pre_exit
438 int __init wg_device_init(void)
442 ret = register_pm_notifier(&pm_notifier);
446 ret = register_random_vmfork_notifier(&vm_notifier);
450 ret = register_pernet_device(&pernet_ops);
454 ret = rtnl_link_register(&link_ops);
461 unregister_pernet_device(&pernet_ops);
463 unregister_random_vmfork_notifier(&vm_notifier);
465 unregister_pm_notifier(&pm_notifier);
469 void wg_device_uninit(void)
471 rtnl_link_unregister(&link_ops);
472 unregister_pernet_device(&pernet_ops);
473 unregister_random_vmfork_notifier(&vm_notifier);
474 unregister_pm_notifier(&pm_notifier);