1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
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>
22 #include <net/dst_metadata.h>
24 #include <net/rtnetlink.h>
25 #include <net/ip_tunnels.h>
26 #include <net/addrconf.h>
28 static LIST_HEAD(device_list);
30 static int wg_open(struct net_device *dev)
32 struct in_device *dev_v4 = __in_dev_get_rtnl(dev);
33 struct inet6_dev *dev_v6 = __in6_dev_get(dev);
34 struct wg_device *wg = netdev_priv(dev);
39 /* At some point we might put this check near the ip_rt_send_
40 * redirect call of ip_forward in net/ipv4/ip_forward.c, similar
41 * to the current secpath check.
43 IN_DEV_CONF_SET(dev_v4, SEND_REDIRECTS, false);
44 IPV4_DEVCONF_ALL(dev_net(dev), SEND_REDIRECTS) = false;
47 dev_v6->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_NONE;
49 mutex_lock(&wg->device_update_lock);
50 ret = wg_socket_init(wg, wg->incoming_port);
53 list_for_each_entry(peer, &wg->peer_list, peer_list) {
54 wg_packet_send_staged_packets(peer);
55 if (peer->persistent_keepalive_interval)
56 wg_packet_send_keepalive(peer);
59 mutex_unlock(&wg->device_update_lock);
63 static int wg_pm_notification(struct notifier_block *nb, unsigned long action, void *data)
68 /* If the machine is constantly suspending and resuming, as part of
69 * its normal operation rather than as a somewhat rare event, then we
70 * don't actually want to clear keys.
72 if (IS_ENABLED(CONFIG_PM_AUTOSLEEP) || IS_ENABLED(CONFIG_ANDROID))
75 if (action != PM_HIBERNATION_PREPARE && action != PM_SUSPEND_PREPARE)
79 list_for_each_entry(wg, &device_list, device_list) {
80 mutex_lock(&wg->device_update_lock);
81 list_for_each_entry(peer, &wg->peer_list, peer_list) {
82 del_timer(&peer->timer_zero_key_material);
83 wg_noise_handshake_clear(&peer->handshake);
84 wg_noise_keypairs_clear(&peer->keypairs);
86 mutex_unlock(&wg->device_update_lock);
93 static struct notifier_block pm_notifier = { .notifier_call = wg_pm_notification };
95 static int wg_vm_notification(struct notifier_block *nb, unsigned long action, void *data)
101 list_for_each_entry(wg, &device_list, device_list) {
102 mutex_lock(&wg->device_update_lock);
103 list_for_each_entry(peer, &wg->peer_list, peer_list)
104 wg_noise_expire_current_peer_keypairs(peer);
105 mutex_unlock(&wg->device_update_lock);
111 static struct notifier_block vm_notifier = { .notifier_call = wg_vm_notification };
113 static int wg_stop(struct net_device *dev)
115 struct wg_device *wg = netdev_priv(dev);
116 struct wg_peer *peer;
119 mutex_lock(&wg->device_update_lock);
120 list_for_each_entry(peer, &wg->peer_list, peer_list) {
121 wg_packet_purge_staged_packets(peer);
122 wg_timers_stop(peer);
123 wg_noise_handshake_clear(&peer->handshake);
124 wg_noise_keypairs_clear(&peer->keypairs);
125 wg_noise_reset_last_sent_handshake(&peer->last_sent_handshake);
127 mutex_unlock(&wg->device_update_lock);
128 while ((skb = ptr_ring_consume(&wg->handshake_queue.ring)) != NULL)
130 atomic_set(&wg->handshake_queue_len, 0);
131 wg_socket_reinit(wg, NULL, NULL);
135 static netdev_tx_t wg_xmit(struct sk_buff *skb, struct net_device *dev)
137 struct wg_device *wg = netdev_priv(dev);
138 struct sk_buff_head packets;
139 struct wg_peer *peer;
140 struct sk_buff *next;
145 if (unlikely(!wg_check_packet_protocol(skb))) {
146 ret = -EPROTONOSUPPORT;
147 net_dbg_ratelimited("%s: Invalid IP packet\n", dev->name);
151 peer = wg_allowedips_lookup_dst(&wg->peer_allowedips, skb);
152 if (unlikely(!peer)) {
154 if (skb->protocol == htons(ETH_P_IP))
155 net_dbg_ratelimited("%s: No peer has allowed IPs matching %pI4\n",
156 dev->name, &ip_hdr(skb)->daddr);
157 else if (skb->protocol == htons(ETH_P_IPV6))
158 net_dbg_ratelimited("%s: No peer has allowed IPs matching %pI6\n",
159 dev->name, &ipv6_hdr(skb)->daddr);
163 family = READ_ONCE(peer->endpoint.addr.sa_family);
164 if (unlikely(family != AF_INET && family != AF_INET6)) {
166 net_dbg_ratelimited("%s: No valid endpoint has been configured or discovered for peer %llu\n",
167 dev->name, peer->internal_id);
171 mtu = skb_valid_dst(skb) ? dst_mtu(skb_dst(skb)) : dev->mtu;
173 __skb_queue_head_init(&packets);
174 if (!skb_is_gso(skb)) {
175 skb_mark_not_on_list(skb);
177 struct sk_buff *segs = skb_gso_segment(skb, 0);
187 skb_list_walk_safe(skb, skb, next) {
188 skb_mark_not_on_list(skb);
190 skb = skb_share_check(skb, GFP_ATOMIC);
194 /* We only need to keep the original dst around for icmp,
195 * so at this point we're in a position to drop it.
199 PACKET_CB(skb)->mtu = mtu;
201 __skb_queue_tail(&packets, skb);
204 spin_lock_bh(&peer->staged_packet_queue.lock);
205 /* If the queue is getting too big, we start removing the oldest packets
206 * until it's small again. We do this before adding the new packet, so
207 * we don't remove GSO segments that are in excess.
209 while (skb_queue_len(&peer->staged_packet_queue) > MAX_STAGED_PACKETS) {
210 dev_kfree_skb(__skb_dequeue(&peer->staged_packet_queue));
211 ++dev->stats.tx_dropped;
213 skb_queue_splice_tail(&packets, &peer->staged_packet_queue);
214 spin_unlock_bh(&peer->staged_packet_queue.lock);
216 wg_packet_send_staged_packets(peer);
224 if (skb->protocol == htons(ETH_P_IP))
225 icmp_ndo_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0);
226 else if (skb->protocol == htons(ETH_P_IPV6))
227 icmpv6_ndo_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
229 ++dev->stats.tx_errors;
234 static const struct net_device_ops netdev_ops = {
237 .ndo_start_xmit = wg_xmit,
238 .ndo_get_stats64 = dev_get_tstats64
241 static void wg_destruct(struct net_device *dev)
243 struct wg_device *wg = netdev_priv(dev);
246 list_del(&wg->device_list);
248 mutex_lock(&wg->device_update_lock);
249 rcu_assign_pointer(wg->creating_net, NULL);
250 wg->incoming_port = 0;
251 wg_socket_reinit(wg, NULL, NULL);
252 /* The final references are cleared in the below calls to destroy_workqueue. */
253 wg_peer_remove_all(wg);
254 destroy_workqueue(wg->handshake_receive_wq);
255 destroy_workqueue(wg->handshake_send_wq);
256 destroy_workqueue(wg->packet_crypt_wq);
257 wg_packet_queue_free(&wg->handshake_queue, true);
258 wg_packet_queue_free(&wg->decrypt_queue, false);
259 wg_packet_queue_free(&wg->encrypt_queue, false);
260 rcu_barrier(); /* Wait for all the peers to be actually freed. */
261 wg_ratelimiter_uninit();
262 memzero_explicit(&wg->static_identity, sizeof(wg->static_identity));
263 free_percpu(dev->tstats);
264 kvfree(wg->index_hashtable);
265 kvfree(wg->peer_hashtable);
266 mutex_unlock(&wg->device_update_lock);
268 pr_debug("%s: Interface destroyed\n", dev->name);
272 static const struct device_type device_type = { .name = KBUILD_MODNAME };
274 static void wg_setup(struct net_device *dev)
276 struct wg_device *wg = netdev_priv(dev);
277 enum { WG_NETDEV_FEATURES = NETIF_F_HW_CSUM | NETIF_F_RXCSUM |
278 NETIF_F_SG | NETIF_F_GSO |
279 NETIF_F_GSO_SOFTWARE | NETIF_F_HIGHDMA };
280 const int overhead = MESSAGE_MINIMUM_LENGTH + sizeof(struct udphdr) +
281 max(sizeof(struct ipv6hdr), sizeof(struct iphdr));
283 dev->netdev_ops = &netdev_ops;
284 dev->header_ops = &ip_tunnel_header_ops;
285 dev->hard_header_len = 0;
287 dev->needed_headroom = DATA_PACKET_HEAD_ROOM;
288 dev->needed_tailroom = noise_encrypted_len(MESSAGE_PADDING_MULTIPLE);
289 dev->type = ARPHRD_NONE;
290 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
291 dev->priv_flags |= IFF_NO_QUEUE;
292 dev->features |= NETIF_F_LLTX;
293 dev->features |= WG_NETDEV_FEATURES;
294 dev->hw_features |= WG_NETDEV_FEATURES;
295 dev->hw_enc_features |= WG_NETDEV_FEATURES;
296 dev->mtu = ETH_DATA_LEN - overhead;
297 dev->max_mtu = round_down(INT_MAX, MESSAGE_PADDING_MULTIPLE) - overhead;
299 SET_NETDEV_DEVTYPE(dev, &device_type);
301 /* We need to keep the dst around in case of icmp replies. */
304 memset(wg, 0, sizeof(*wg));
308 static int wg_newlink(struct net *src_net, struct net_device *dev,
309 struct nlattr *tb[], struct nlattr *data[],
310 struct netlink_ext_ack *extack)
312 struct wg_device *wg = netdev_priv(dev);
315 rcu_assign_pointer(wg->creating_net, src_net);
316 init_rwsem(&wg->static_identity.lock);
317 mutex_init(&wg->socket_update_lock);
318 mutex_init(&wg->device_update_lock);
319 wg_allowedips_init(&wg->peer_allowedips);
320 wg_cookie_checker_init(&wg->cookie_checker, wg);
321 INIT_LIST_HEAD(&wg->peer_list);
322 wg->device_update_gen = 1;
324 wg->peer_hashtable = wg_pubkey_hashtable_alloc();
325 if (!wg->peer_hashtable)
328 wg->index_hashtable = wg_index_hashtable_alloc();
329 if (!wg->index_hashtable)
330 goto err_free_peer_hashtable;
332 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
334 goto err_free_index_hashtable;
336 wg->handshake_receive_wq = alloc_workqueue("wg-kex-%s",
337 WQ_CPU_INTENSIVE | WQ_FREEZABLE, 0, dev->name);
338 if (!wg->handshake_receive_wq)
339 goto err_free_tstats;
341 wg->handshake_send_wq = alloc_workqueue("wg-kex-%s",
342 WQ_UNBOUND | WQ_FREEZABLE, 0, dev->name);
343 if (!wg->handshake_send_wq)
344 goto err_destroy_handshake_receive;
346 wg->packet_crypt_wq = alloc_workqueue("wg-crypt-%s",
347 WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, 0, dev->name);
348 if (!wg->packet_crypt_wq)
349 goto err_destroy_handshake_send;
351 ret = wg_packet_queue_init(&wg->encrypt_queue, wg_packet_encrypt_worker,
354 goto err_destroy_packet_crypt;
356 ret = wg_packet_queue_init(&wg->decrypt_queue, wg_packet_decrypt_worker,
359 goto err_free_encrypt_queue;
361 ret = wg_packet_queue_init(&wg->handshake_queue, wg_packet_handshake_receive_worker,
362 MAX_QUEUED_INCOMING_HANDSHAKES);
364 goto err_free_decrypt_queue;
366 ret = wg_ratelimiter_init();
368 goto err_free_handshake_queue;
370 ret = register_netdevice(dev);
372 goto err_uninit_ratelimiter;
374 list_add(&wg->device_list, &device_list);
376 /* We wait until the end to assign priv_destructor, so that
377 * register_netdevice doesn't call it for us if it fails.
379 dev->priv_destructor = wg_destruct;
381 pr_debug("%s: Interface created\n", dev->name);
384 err_uninit_ratelimiter:
385 wg_ratelimiter_uninit();
386 err_free_handshake_queue:
387 wg_packet_queue_free(&wg->handshake_queue, false);
388 err_free_decrypt_queue:
389 wg_packet_queue_free(&wg->decrypt_queue, false);
390 err_free_encrypt_queue:
391 wg_packet_queue_free(&wg->encrypt_queue, false);
392 err_destroy_packet_crypt:
393 destroy_workqueue(wg->packet_crypt_wq);
394 err_destroy_handshake_send:
395 destroy_workqueue(wg->handshake_send_wq);
396 err_destroy_handshake_receive:
397 destroy_workqueue(wg->handshake_receive_wq);
399 free_percpu(dev->tstats);
400 err_free_index_hashtable:
401 kvfree(wg->index_hashtable);
402 err_free_peer_hashtable:
403 kvfree(wg->peer_hashtable);
407 static struct rtnl_link_ops link_ops __read_mostly = {
408 .kind = KBUILD_MODNAME,
409 .priv_size = sizeof(struct wg_device),
411 .newlink = wg_newlink,
414 static void wg_netns_pre_exit(struct net *net)
416 struct wg_device *wg;
417 struct wg_peer *peer;
420 list_for_each_entry(wg, &device_list, device_list) {
421 if (rcu_access_pointer(wg->creating_net) == net) {
422 pr_debug("%s: Creating namespace exiting\n", wg->dev->name);
423 netif_carrier_off(wg->dev);
424 mutex_lock(&wg->device_update_lock);
425 rcu_assign_pointer(wg->creating_net, NULL);
426 wg_socket_reinit(wg, NULL, NULL);
427 list_for_each_entry(peer, &wg->peer_list, peer_list)
428 wg_socket_clear_peer_endpoint_src(peer);
429 mutex_unlock(&wg->device_update_lock);
435 static struct pernet_operations pernet_ops = {
436 .pre_exit = wg_netns_pre_exit
439 int __init wg_device_init(void)
443 ret = register_pm_notifier(&pm_notifier);
447 ret = register_random_vmfork_notifier(&vm_notifier);
451 ret = register_pernet_device(&pernet_ops);
455 ret = rtnl_link_register(&link_ops);
462 unregister_pernet_device(&pernet_ops);
464 unregister_random_vmfork_notifier(&vm_notifier);
466 unregister_pm_notifier(&pm_notifier);
470 void wg_device_uninit(void)
472 rtnl_link_unregister(&link_ops);
473 unregister_pernet_device(&pernet_ops);
474 unregister_random_vmfork_notifier(&vm_notifier);
475 unregister_pm_notifier(&pm_notifier);