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 #ifdef CONFIG_PM_SLEEP
63 static int wg_pm_notification(struct notifier_block *nb, unsigned long action,
69 /* If the machine is constantly suspending and resuming, as part of
70 * its normal operation rather than as a somewhat rare event, then we
71 * don't actually want to clear keys.
73 if (IS_ENABLED(CONFIG_PM_AUTOSLEEP) || IS_ENABLED(CONFIG_ANDROID))
76 if (action != PM_HIBERNATION_PREPARE && action != PM_SUSPEND_PREPARE)
80 list_for_each_entry(wg, &device_list, device_list) {
81 mutex_lock(&wg->device_update_lock);
82 list_for_each_entry(peer, &wg->peer_list, peer_list) {
83 del_timer(&peer->timer_zero_key_material);
84 wg_noise_handshake_clear(&peer->handshake);
85 wg_noise_keypairs_clear(&peer->keypairs);
87 mutex_unlock(&wg->device_update_lock);
94 static struct notifier_block pm_notifier = { .notifier_call = wg_pm_notification };
97 static int wg_stop(struct net_device *dev)
99 struct wg_device *wg = netdev_priv(dev);
100 struct wg_peer *peer;
103 mutex_lock(&wg->device_update_lock);
104 list_for_each_entry(peer, &wg->peer_list, peer_list) {
105 wg_packet_purge_staged_packets(peer);
106 wg_timers_stop(peer);
107 wg_noise_handshake_clear(&peer->handshake);
108 wg_noise_keypairs_clear(&peer->keypairs);
109 wg_noise_reset_last_sent_handshake(&peer->last_sent_handshake);
111 mutex_unlock(&wg->device_update_lock);
112 while ((skb = ptr_ring_consume(&wg->handshake_queue.ring)) != NULL)
114 atomic_set(&wg->handshake_queue_len, 0);
115 wg_socket_reinit(wg, NULL, NULL);
119 static netdev_tx_t wg_xmit(struct sk_buff *skb, struct net_device *dev)
121 struct wg_device *wg = netdev_priv(dev);
122 struct sk_buff_head packets;
123 struct wg_peer *peer;
124 struct sk_buff *next;
129 if (unlikely(!wg_check_packet_protocol(skb))) {
130 ret = -EPROTONOSUPPORT;
131 net_dbg_ratelimited("%s: Invalid IP packet\n", dev->name);
135 peer = wg_allowedips_lookup_dst(&wg->peer_allowedips, skb);
136 if (unlikely(!peer)) {
138 if (skb->protocol == htons(ETH_P_IP))
139 net_dbg_ratelimited("%s: No peer has allowed IPs matching %pI4\n",
140 dev->name, &ip_hdr(skb)->daddr);
141 else if (skb->protocol == htons(ETH_P_IPV6))
142 net_dbg_ratelimited("%s: No peer has allowed IPs matching %pI6\n",
143 dev->name, &ipv6_hdr(skb)->daddr);
147 family = READ_ONCE(peer->endpoint.addr.sa_family);
148 if (unlikely(family != AF_INET && family != AF_INET6)) {
150 net_dbg_ratelimited("%s: No valid endpoint has been configured or discovered for peer %llu\n",
151 dev->name, peer->internal_id);
155 mtu = skb_dst(skb) ? dst_mtu(skb_dst(skb)) : dev->mtu;
157 __skb_queue_head_init(&packets);
158 if (!skb_is_gso(skb)) {
159 skb_mark_not_on_list(skb);
161 struct sk_buff *segs = skb_gso_segment(skb, 0);
171 skb_list_walk_safe(skb, skb, next) {
172 skb_mark_not_on_list(skb);
174 skb = skb_share_check(skb, GFP_ATOMIC);
178 /* We only need to keep the original dst around for icmp,
179 * so at this point we're in a position to drop it.
183 PACKET_CB(skb)->mtu = mtu;
185 __skb_queue_tail(&packets, skb);
188 spin_lock_bh(&peer->staged_packet_queue.lock);
189 /* If the queue is getting too big, we start removing the oldest packets
190 * until it's small again. We do this before adding the new packet, so
191 * we don't remove GSO segments that are in excess.
193 while (skb_queue_len(&peer->staged_packet_queue) > MAX_STAGED_PACKETS) {
194 dev_kfree_skb(__skb_dequeue(&peer->staged_packet_queue));
195 ++dev->stats.tx_dropped;
197 skb_queue_splice_tail(&packets, &peer->staged_packet_queue);
198 spin_unlock_bh(&peer->staged_packet_queue.lock);
200 wg_packet_send_staged_packets(peer);
208 if (skb->protocol == htons(ETH_P_IP))
209 icmp_ndo_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0);
210 else if (skb->protocol == htons(ETH_P_IPV6))
211 icmpv6_ndo_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
213 ++dev->stats.tx_errors;
218 static const struct net_device_ops netdev_ops = {
221 .ndo_start_xmit = wg_xmit,
222 .ndo_get_stats64 = dev_get_tstats64
225 static void wg_destruct(struct net_device *dev)
227 struct wg_device *wg = netdev_priv(dev);
230 list_del(&wg->device_list);
232 mutex_lock(&wg->device_update_lock);
233 rcu_assign_pointer(wg->creating_net, NULL);
234 wg->incoming_port = 0;
235 wg_socket_reinit(wg, NULL, NULL);
236 /* The final references are cleared in the below calls to destroy_workqueue. */
237 wg_peer_remove_all(wg);
238 destroy_workqueue(wg->handshake_receive_wq);
239 destroy_workqueue(wg->handshake_send_wq);
240 destroy_workqueue(wg->packet_crypt_wq);
241 wg_packet_queue_free(&wg->handshake_queue, true);
242 wg_packet_queue_free(&wg->decrypt_queue, false);
243 wg_packet_queue_free(&wg->encrypt_queue, false);
244 rcu_barrier(); /* Wait for all the peers to be actually freed. */
245 wg_ratelimiter_uninit();
246 memzero_explicit(&wg->static_identity, sizeof(wg->static_identity));
247 free_percpu(dev->tstats);
248 kvfree(wg->index_hashtable);
249 kvfree(wg->peer_hashtable);
250 mutex_unlock(&wg->device_update_lock);
252 pr_debug("%s: Interface destroyed\n", dev->name);
256 static const struct device_type device_type = { .name = KBUILD_MODNAME };
258 static void wg_setup(struct net_device *dev)
260 struct wg_device *wg = netdev_priv(dev);
261 enum { WG_NETDEV_FEATURES = NETIF_F_HW_CSUM | NETIF_F_RXCSUM |
262 NETIF_F_SG | NETIF_F_GSO |
263 NETIF_F_GSO_SOFTWARE | NETIF_F_HIGHDMA };
264 const int overhead = MESSAGE_MINIMUM_LENGTH + sizeof(struct udphdr) +
265 max(sizeof(struct ipv6hdr), sizeof(struct iphdr));
267 dev->netdev_ops = &netdev_ops;
268 dev->header_ops = &ip_tunnel_header_ops;
269 dev->hard_header_len = 0;
271 dev->needed_headroom = DATA_PACKET_HEAD_ROOM;
272 dev->needed_tailroom = noise_encrypted_len(MESSAGE_PADDING_MULTIPLE);
273 dev->type = ARPHRD_NONE;
274 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
275 dev->priv_flags |= IFF_NO_QUEUE;
276 dev->features |= NETIF_F_LLTX;
277 dev->features |= WG_NETDEV_FEATURES;
278 dev->hw_features |= WG_NETDEV_FEATURES;
279 dev->hw_enc_features |= WG_NETDEV_FEATURES;
280 dev->mtu = ETH_DATA_LEN - overhead;
281 dev->max_mtu = round_down(INT_MAX, MESSAGE_PADDING_MULTIPLE) - overhead;
283 SET_NETDEV_DEVTYPE(dev, &device_type);
285 /* We need to keep the dst around in case of icmp replies. */
288 memset(wg, 0, sizeof(*wg));
292 static int wg_newlink(struct net *src_net, struct net_device *dev,
293 struct nlattr *tb[], struct nlattr *data[],
294 struct netlink_ext_ack *extack)
296 struct wg_device *wg = netdev_priv(dev);
299 rcu_assign_pointer(wg->creating_net, src_net);
300 init_rwsem(&wg->static_identity.lock);
301 mutex_init(&wg->socket_update_lock);
302 mutex_init(&wg->device_update_lock);
303 wg_allowedips_init(&wg->peer_allowedips);
304 wg_cookie_checker_init(&wg->cookie_checker, wg);
305 INIT_LIST_HEAD(&wg->peer_list);
306 wg->device_update_gen = 1;
308 wg->peer_hashtable = wg_pubkey_hashtable_alloc();
309 if (!wg->peer_hashtable)
312 wg->index_hashtable = wg_index_hashtable_alloc();
313 if (!wg->index_hashtable)
314 goto err_free_peer_hashtable;
316 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
318 goto err_free_index_hashtable;
320 wg->handshake_receive_wq = alloc_workqueue("wg-kex-%s",
321 WQ_CPU_INTENSIVE | WQ_FREEZABLE, 0, dev->name);
322 if (!wg->handshake_receive_wq)
323 goto err_free_tstats;
325 wg->handshake_send_wq = alloc_workqueue("wg-kex-%s",
326 WQ_UNBOUND | WQ_FREEZABLE, 0, dev->name);
327 if (!wg->handshake_send_wq)
328 goto err_destroy_handshake_receive;
330 wg->packet_crypt_wq = alloc_workqueue("wg-crypt-%s",
331 WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, 0, dev->name);
332 if (!wg->packet_crypt_wq)
333 goto err_destroy_handshake_send;
335 ret = wg_packet_queue_init(&wg->encrypt_queue, wg_packet_encrypt_worker,
338 goto err_destroy_packet_crypt;
340 ret = wg_packet_queue_init(&wg->decrypt_queue, wg_packet_decrypt_worker,
343 goto err_free_encrypt_queue;
345 ret = wg_packet_queue_init(&wg->handshake_queue, wg_packet_handshake_receive_worker,
346 MAX_QUEUED_INCOMING_HANDSHAKES);
348 goto err_free_decrypt_queue;
350 ret = wg_ratelimiter_init();
352 goto err_free_handshake_queue;
354 ret = register_netdevice(dev);
356 goto err_uninit_ratelimiter;
358 list_add(&wg->device_list, &device_list);
360 /* We wait until the end to assign priv_destructor, so that
361 * register_netdevice doesn't call it for us if it fails.
363 dev->priv_destructor = wg_destruct;
365 pr_debug("%s: Interface created\n", dev->name);
368 err_uninit_ratelimiter:
369 wg_ratelimiter_uninit();
370 err_free_handshake_queue:
371 wg_packet_queue_free(&wg->handshake_queue, false);
372 err_free_decrypt_queue:
373 wg_packet_queue_free(&wg->decrypt_queue, false);
374 err_free_encrypt_queue:
375 wg_packet_queue_free(&wg->encrypt_queue, false);
376 err_destroy_packet_crypt:
377 destroy_workqueue(wg->packet_crypt_wq);
378 err_destroy_handshake_send:
379 destroy_workqueue(wg->handshake_send_wq);
380 err_destroy_handshake_receive:
381 destroy_workqueue(wg->handshake_receive_wq);
383 free_percpu(dev->tstats);
384 err_free_index_hashtable:
385 kvfree(wg->index_hashtable);
386 err_free_peer_hashtable:
387 kvfree(wg->peer_hashtable);
391 static struct rtnl_link_ops link_ops __read_mostly = {
392 .kind = KBUILD_MODNAME,
393 .priv_size = sizeof(struct wg_device),
395 .newlink = wg_newlink,
398 static void wg_netns_pre_exit(struct net *net)
400 struct wg_device *wg;
401 struct wg_peer *peer;
404 list_for_each_entry(wg, &device_list, device_list) {
405 if (rcu_access_pointer(wg->creating_net) == net) {
406 pr_debug("%s: Creating namespace exiting\n", wg->dev->name);
407 netif_carrier_off(wg->dev);
408 mutex_lock(&wg->device_update_lock);
409 rcu_assign_pointer(wg->creating_net, NULL);
410 wg_socket_reinit(wg, NULL, NULL);
411 list_for_each_entry(peer, &wg->peer_list, peer_list)
412 wg_socket_clear_peer_endpoint_src(peer);
413 mutex_unlock(&wg->device_update_lock);
419 static struct pernet_operations pernet_ops = {
420 .pre_exit = wg_netns_pre_exit
423 int __init wg_device_init(void)
427 #ifdef CONFIG_PM_SLEEP
428 ret = register_pm_notifier(&pm_notifier);
433 ret = register_pernet_device(&pernet_ops);
437 ret = rtnl_link_register(&link_ops);
444 unregister_pernet_device(&pernet_ops);
446 #ifdef CONFIG_PM_SLEEP
447 unregister_pm_notifier(&pm_notifier);
452 void wg_device_uninit(void)
454 rtnl_link_unregister(&link_ops);
455 unregister_pernet_device(&pernet_ops);
456 #ifdef CONFIG_PM_SLEEP
457 unregister_pm_notifier(&pm_notifier);