Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
  4 */
  5
  6#include "queueing.h"
  7#include "socket.h"
  8#include "timers.h"
  9#include "device.h"
 10#include "ratelimiter.h"
 11#include "peer.h"
 12#include "messages.h"
 13
 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>
 23#include <net/gso.h>
 24#include <net/icmp.h>
 25#include <net/rtnetlink.h>
 26#include <net/ip_tunnels.h>
 27#include <net/addrconf.h>
 28
 29static LIST_HEAD(device_list);
 30
 31static int wg_open(struct net_device *dev)
 32{
 33	struct in_device *dev_v4 = __in_dev_get_rtnl(dev);
 34	struct inet6_dev *dev_v6 = __in6_dev_get(dev);
 35	struct wg_device *wg = netdev_priv(dev);
 36	struct wg_peer *peer;
 37	int ret;
 38
 39	if (dev_v4) {
 40		/* At some point we might put this check near the ip_rt_send_
 41		 * redirect call of ip_forward in net/ipv4/ip_forward.c, similar
 42		 * to the current secpath check.
 43		 */
 44		IN_DEV_CONF_SET(dev_v4, SEND_REDIRECTS, false);
 45		IPV4_DEVCONF_ALL(dev_net(dev), SEND_REDIRECTS) = false;
 46	}
 47	if (dev_v6)
 48		dev_v6->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_NONE;
 49
 50	mutex_lock(&wg->device_update_lock);
 51	ret = wg_socket_init(wg, wg->incoming_port);
 52	if (ret < 0)
 53		goto out;
 54	list_for_each_entry(peer, &wg->peer_list, peer_list) {
 55		wg_packet_send_staged_packets(peer);
 56		if (peer->persistent_keepalive_interval)
 57			wg_packet_send_keepalive(peer);
 58	}
 59out:
 60	mutex_unlock(&wg->device_update_lock);
 61	return ret;
 62}
 63
 64static int wg_pm_notification(struct notifier_block *nb, unsigned long action, void *data)
 
 
 65{
 66	struct wg_device *wg;
 67	struct wg_peer *peer;
 68
 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.
 72	 */
 73	if (IS_ENABLED(CONFIG_PM_AUTOSLEEP) ||
 74	    IS_ENABLED(CONFIG_PM_USERSPACE_AUTOSLEEP))
 75		return 0;
 76
 77	if (action != PM_HIBERNATION_PREPARE && action != PM_SUSPEND_PREPARE)
 78		return 0;
 79
 80	rtnl_lock();
 81	list_for_each_entry(wg, &device_list, device_list) {
 82		mutex_lock(&wg->device_update_lock);
 83		list_for_each_entry(peer, &wg->peer_list, peer_list) {
 84			del_timer(&peer->timer_zero_key_material);
 85			wg_noise_handshake_clear(&peer->handshake);
 86			wg_noise_keypairs_clear(&peer->keypairs);
 87		}
 88		mutex_unlock(&wg->device_update_lock);
 89	}
 90	rtnl_unlock();
 91	rcu_barrier();
 92	return 0;
 93}
 94
 95static struct notifier_block pm_notifier = { .notifier_call = wg_pm_notification };
 96
 97static int wg_vm_notification(struct notifier_block *nb, unsigned long action, void *data)
 98{
 99	struct wg_device *wg;
100	struct wg_peer *peer;
101
102	rtnl_lock();
103	list_for_each_entry(wg, &device_list, device_list) {
104		mutex_lock(&wg->device_update_lock);
105		list_for_each_entry(peer, &wg->peer_list, peer_list)
106			wg_noise_expire_current_peer_keypairs(peer);
107		mutex_unlock(&wg->device_update_lock);
108	}
109	rtnl_unlock();
110	return 0;
111}
112
113static struct notifier_block vm_notifier = { .notifier_call = wg_vm_notification };
114
115static int wg_stop(struct net_device *dev)
116{
117	struct wg_device *wg = netdev_priv(dev);
118	struct wg_peer *peer;
119	struct sk_buff *skb;
120
121	mutex_lock(&wg->device_update_lock);
122	list_for_each_entry(peer, &wg->peer_list, peer_list) {
123		wg_packet_purge_staged_packets(peer);
124		wg_timers_stop(peer);
125		wg_noise_handshake_clear(&peer->handshake);
126		wg_noise_keypairs_clear(&peer->keypairs);
127		wg_noise_reset_last_sent_handshake(&peer->last_sent_handshake);
128	}
129	mutex_unlock(&wg->device_update_lock);
130	while ((skb = ptr_ring_consume(&wg->handshake_queue.ring)) != NULL)
131		kfree_skb(skb);
132	atomic_set(&wg->handshake_queue_len, 0);
133	wg_socket_reinit(wg, NULL, NULL);
134	return 0;
135}
136
137static netdev_tx_t wg_xmit(struct sk_buff *skb, struct net_device *dev)
138{
139	struct wg_device *wg = netdev_priv(dev);
140	struct sk_buff_head packets;
141	struct wg_peer *peer;
142	struct sk_buff *next;
143	sa_family_t family;
144	u32 mtu;
145	int ret;
146
147	if (unlikely(!wg_check_packet_protocol(skb))) {
148		ret = -EPROTONOSUPPORT;
149		net_dbg_ratelimited("%s: Invalid IP packet\n", dev->name);
150		goto err;
151	}
152
153	peer = wg_allowedips_lookup_dst(&wg->peer_allowedips, skb);
154	if (unlikely(!peer)) {
155		ret = -ENOKEY;
156		if (skb->protocol == htons(ETH_P_IP))
157			net_dbg_ratelimited("%s: No peer has allowed IPs matching %pI4\n",
158					    dev->name, &ip_hdr(skb)->daddr);
159		else if (skb->protocol == htons(ETH_P_IPV6))
160			net_dbg_ratelimited("%s: No peer has allowed IPs matching %pI6\n",
161					    dev->name, &ipv6_hdr(skb)->daddr);
162		goto err_icmp;
163	}
164
165	family = READ_ONCE(peer->endpoint.addr.sa_family);
166	if (unlikely(family != AF_INET && family != AF_INET6)) {
167		ret = -EDESTADDRREQ;
168		net_dbg_ratelimited("%s: No valid endpoint has been configured or discovered for peer %llu\n",
169				    dev->name, peer->internal_id);
170		goto err_peer;
171	}
172
173	mtu = skb_valid_dst(skb) ? dst_mtu(skb_dst(skb)) : dev->mtu;
174
175	__skb_queue_head_init(&packets);
176	if (!skb_is_gso(skb)) {
177		skb_mark_not_on_list(skb);
178	} else {
179		struct sk_buff *segs = skb_gso_segment(skb, 0);
180
181		if (IS_ERR(segs)) {
182			ret = PTR_ERR(segs);
183			goto err_peer;
184		}
185		dev_kfree_skb(skb);
186		skb = segs;
187	}
188
189	skb_list_walk_safe(skb, skb, next) {
190		skb_mark_not_on_list(skb);
191
192		skb = skb_share_check(skb, GFP_ATOMIC);
193		if (unlikely(!skb))
194			continue;
195
196		/* We only need to keep the original dst around for icmp,
197		 * so at this point we're in a position to drop it.
198		 */
199		skb_dst_drop(skb);
200
201		PACKET_CB(skb)->mtu = mtu;
202
203		__skb_queue_tail(&packets, skb);
204	}
205
206	spin_lock_bh(&peer->staged_packet_queue.lock);
207	/* If the queue is getting too big, we start removing the oldest packets
208	 * until it's small again. We do this before adding the new packet, so
209	 * we don't remove GSO segments that are in excess.
210	 */
211	while (skb_queue_len(&peer->staged_packet_queue) > MAX_STAGED_PACKETS) {
212		dev_kfree_skb(__skb_dequeue(&peer->staged_packet_queue));
213		DEV_STATS_INC(dev, tx_dropped);
214	}
215	skb_queue_splice_tail(&packets, &peer->staged_packet_queue);
216	spin_unlock_bh(&peer->staged_packet_queue.lock);
217
218	wg_packet_send_staged_packets(peer);
219
220	wg_peer_put(peer);
221	return NETDEV_TX_OK;
222
223err_peer:
224	wg_peer_put(peer);
225err_icmp:
226	if (skb->protocol == htons(ETH_P_IP))
227		icmp_ndo_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0);
228	else if (skb->protocol == htons(ETH_P_IPV6))
229		icmpv6_ndo_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
230err:
231	DEV_STATS_INC(dev, tx_errors);
232	kfree_skb(skb);
233	return ret;
234}
235
236static const struct net_device_ops netdev_ops = {
237	.ndo_open		= wg_open,
238	.ndo_stop		= wg_stop,
239	.ndo_start_xmit		= wg_xmit,
240	.ndo_get_stats64	= dev_get_tstats64
241};
242
243static void wg_destruct(struct net_device *dev)
244{
245	struct wg_device *wg = netdev_priv(dev);
246
247	rtnl_lock();
248	list_del(&wg->device_list);
249	rtnl_unlock();
250	mutex_lock(&wg->device_update_lock);
251	rcu_assign_pointer(wg->creating_net, NULL);
252	wg->incoming_port = 0;
253	wg_socket_reinit(wg, NULL, NULL);
254	/* The final references are cleared in the below calls to destroy_workqueue. */
255	wg_peer_remove_all(wg);
256	destroy_workqueue(wg->handshake_receive_wq);
257	destroy_workqueue(wg->handshake_send_wq);
258	destroy_workqueue(wg->packet_crypt_wq);
259	wg_packet_queue_free(&wg->handshake_queue, true);
260	wg_packet_queue_free(&wg->decrypt_queue, false);
261	wg_packet_queue_free(&wg->encrypt_queue, false);
262	rcu_barrier(); /* Wait for all the peers to be actually freed. */
263	wg_ratelimiter_uninit();
264	memzero_explicit(&wg->static_identity, sizeof(wg->static_identity));
 
265	free_percpu(dev->tstats);
 
266	kvfree(wg->index_hashtable);
267	kvfree(wg->peer_hashtable);
268	mutex_unlock(&wg->device_update_lock);
269
270	pr_debug("%s: Interface destroyed\n", dev->name);
271	free_netdev(dev);
272}
273
274static const struct device_type device_type = { .name = KBUILD_MODNAME };
275
276static void wg_setup(struct net_device *dev)
277{
278	struct wg_device *wg = netdev_priv(dev);
279	enum { WG_NETDEV_FEATURES = NETIF_F_HW_CSUM | NETIF_F_RXCSUM |
280				    NETIF_F_SG | NETIF_F_GSO |
281				    NETIF_F_GSO_SOFTWARE | NETIF_F_HIGHDMA };
282	const int overhead = MESSAGE_MINIMUM_LENGTH + sizeof(struct udphdr) +
283			     max(sizeof(struct ipv6hdr), sizeof(struct iphdr));
284
285	dev->netdev_ops = &netdev_ops;
286	dev->header_ops = &ip_tunnel_header_ops;
287	dev->hard_header_len = 0;
288	dev->addr_len = 0;
289	dev->needed_headroom = DATA_PACKET_HEAD_ROOM;
290	dev->needed_tailroom = noise_encrypted_len(MESSAGE_PADDING_MULTIPLE);
291	dev->type = ARPHRD_NONE;
292	dev->flags = IFF_POINTOPOINT | IFF_NOARP;
293	dev->priv_flags |= IFF_NO_QUEUE;
294	dev->features |= NETIF_F_LLTX;
295	dev->features |= WG_NETDEV_FEATURES;
296	dev->hw_features |= WG_NETDEV_FEATURES;
297	dev->hw_enc_features |= WG_NETDEV_FEATURES;
298	dev->mtu = ETH_DATA_LEN - overhead;
299	dev->max_mtu = round_down(INT_MAX, MESSAGE_PADDING_MULTIPLE) - overhead;
300
301	SET_NETDEV_DEVTYPE(dev, &device_type);
302
303	/* We need to keep the dst around in case of icmp replies. */
304	netif_keep_dst(dev);
305
306	memset(wg, 0, sizeof(*wg));
307	wg->dev = dev;
308}
309
310static int wg_newlink(struct net *src_net, struct net_device *dev,
311		      struct nlattr *tb[], struct nlattr *data[],
312		      struct netlink_ext_ack *extack)
313{
314	struct wg_device *wg = netdev_priv(dev);
315	int ret = -ENOMEM;
316
317	rcu_assign_pointer(wg->creating_net, src_net);
318	init_rwsem(&wg->static_identity.lock);
319	mutex_init(&wg->socket_update_lock);
320	mutex_init(&wg->device_update_lock);
 
321	wg_allowedips_init(&wg->peer_allowedips);
322	wg_cookie_checker_init(&wg->cookie_checker, wg);
323	INIT_LIST_HEAD(&wg->peer_list);
324	wg->device_update_gen = 1;
325
326	wg->peer_hashtable = wg_pubkey_hashtable_alloc();
327	if (!wg->peer_hashtable)
328		return ret;
329
330	wg->index_hashtable = wg_index_hashtable_alloc();
331	if (!wg->index_hashtable)
332		goto err_free_peer_hashtable;
333
334	dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
335	if (!dev->tstats)
336		goto err_free_index_hashtable;
337
 
 
 
 
 
 
338	wg->handshake_receive_wq = alloc_workqueue("wg-kex-%s",
339			WQ_CPU_INTENSIVE | WQ_FREEZABLE, 0, dev->name);
340	if (!wg->handshake_receive_wq)
341		goto err_free_tstats;
342
343	wg->handshake_send_wq = alloc_workqueue("wg-kex-%s",
344			WQ_UNBOUND | WQ_FREEZABLE, 0, dev->name);
345	if (!wg->handshake_send_wq)
346		goto err_destroy_handshake_receive;
347
348	wg->packet_crypt_wq = alloc_workqueue("wg-crypt-%s",
349			WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, 0, dev->name);
350	if (!wg->packet_crypt_wq)
351		goto err_destroy_handshake_send;
352
353	ret = wg_packet_queue_init(&wg->encrypt_queue, wg_packet_encrypt_worker,
354				   MAX_QUEUED_PACKETS);
355	if (ret < 0)
356		goto err_destroy_packet_crypt;
357
358	ret = wg_packet_queue_init(&wg->decrypt_queue, wg_packet_decrypt_worker,
359				   MAX_QUEUED_PACKETS);
360	if (ret < 0)
361		goto err_free_encrypt_queue;
362
363	ret = wg_packet_queue_init(&wg->handshake_queue, wg_packet_handshake_receive_worker,
364				   MAX_QUEUED_INCOMING_HANDSHAKES);
365	if (ret < 0)
366		goto err_free_decrypt_queue;
367
368	ret = wg_ratelimiter_init();
369	if (ret < 0)
370		goto err_free_handshake_queue;
371
372	ret = register_netdevice(dev);
373	if (ret < 0)
374		goto err_uninit_ratelimiter;
375
376	list_add(&wg->device_list, &device_list);
377
378	/* We wait until the end to assign priv_destructor, so that
379	 * register_netdevice doesn't call it for us if it fails.
380	 */
381	dev->priv_destructor = wg_destruct;
382
383	pr_debug("%s: Interface created\n", dev->name);
384	return ret;
385
386err_uninit_ratelimiter:
387	wg_ratelimiter_uninit();
388err_free_handshake_queue:
389	wg_packet_queue_free(&wg->handshake_queue, false);
390err_free_decrypt_queue:
391	wg_packet_queue_free(&wg->decrypt_queue, false);
392err_free_encrypt_queue:
393	wg_packet_queue_free(&wg->encrypt_queue, false);
394err_destroy_packet_crypt:
395	destroy_workqueue(wg->packet_crypt_wq);
396err_destroy_handshake_send:
397	destroy_workqueue(wg->handshake_send_wq);
398err_destroy_handshake_receive:
399	destroy_workqueue(wg->handshake_receive_wq);
 
 
400err_free_tstats:
401	free_percpu(dev->tstats);
402err_free_index_hashtable:
403	kvfree(wg->index_hashtable);
404err_free_peer_hashtable:
405	kvfree(wg->peer_hashtable);
406	return ret;
407}
408
409static struct rtnl_link_ops link_ops __read_mostly = {
410	.kind			= KBUILD_MODNAME,
411	.priv_size		= sizeof(struct wg_device),
412	.setup			= wg_setup,
413	.newlink		= wg_newlink,
414};
415
416static void wg_netns_pre_exit(struct net *net)
417{
418	struct wg_device *wg;
419	struct wg_peer *peer;
420
421	rtnl_lock();
422	list_for_each_entry(wg, &device_list, device_list) {
423		if (rcu_access_pointer(wg->creating_net) == net) {
424			pr_debug("%s: Creating namespace exiting\n", wg->dev->name);
425			netif_carrier_off(wg->dev);
426			mutex_lock(&wg->device_update_lock);
427			rcu_assign_pointer(wg->creating_net, NULL);
428			wg_socket_reinit(wg, NULL, NULL);
429			list_for_each_entry(peer, &wg->peer_list, peer_list)
430				wg_socket_clear_peer_endpoint_src(peer);
431			mutex_unlock(&wg->device_update_lock);
432		}
433	}
434	rtnl_unlock();
435}
436
437static struct pernet_operations pernet_ops = {
438	.pre_exit = wg_netns_pre_exit
439};
440
441int __init wg_device_init(void)
442{
443	int ret;
444
 
445	ret = register_pm_notifier(&pm_notifier);
446	if (ret)
447		return ret;
448
449	ret = register_random_vmfork_notifier(&vm_notifier);
450	if (ret)
451		goto error_pm;
452
453	ret = register_pernet_device(&pernet_ops);
454	if (ret)
455		goto error_vm;
456
457	ret = rtnl_link_register(&link_ops);
458	if (ret)
459		goto error_pernet;
460
461	return 0;
462
463error_pernet:
464	unregister_pernet_device(&pernet_ops);
465error_vm:
466	unregister_random_vmfork_notifier(&vm_notifier);
467error_pm:
 
468	unregister_pm_notifier(&pm_notifier);
 
469	return ret;
470}
471
472void wg_device_uninit(void)
473{
474	rtnl_link_unregister(&link_ops);
475	unregister_pernet_device(&pernet_ops);
476	unregister_random_vmfork_notifier(&vm_notifier);
477	unregister_pm_notifier(&pm_notifier);
 
478	rcu_barrier();
479}
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
  4 */
  5
  6#include "queueing.h"
  7#include "socket.h"
  8#include "timers.h"
  9#include "device.h"
 10#include "ratelimiter.h"
 11#include "peer.h"
 12#include "messages.h"
 13
 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/icmp.h>
 23#include <net/rtnetlink.h>
 24#include <net/ip_tunnels.h>
 25#include <net/addrconf.h>
 26
 27static LIST_HEAD(device_list);
 28
 29static int wg_open(struct net_device *dev)
 30{
 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);
 34	struct wg_peer *peer;
 35	int ret;
 36
 37	if (dev_v4) {
 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.
 41		 */
 42		IN_DEV_CONF_SET(dev_v4, SEND_REDIRECTS, false);
 43		IPV4_DEVCONF_ALL(dev_net(dev), SEND_REDIRECTS) = false;
 44	}
 45	if (dev_v6)
 46		dev_v6->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_NONE;
 47
 48	mutex_lock(&wg->device_update_lock);
 49	ret = wg_socket_init(wg, wg->incoming_port);
 50	if (ret < 0)
 51		goto out;
 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);
 56	}
 57out:
 58	mutex_unlock(&wg->device_update_lock);
 59	return ret;
 60}
 61
 62#ifdef CONFIG_PM_SLEEP
 63static int wg_pm_notification(struct notifier_block *nb, unsigned long action,
 64			      void *data)
 65{
 66	struct wg_device *wg;
 67	struct wg_peer *peer;
 68
 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.
 72	 */
 73	if (IS_ENABLED(CONFIG_PM_AUTOSLEEP) || IS_ENABLED(CONFIG_ANDROID))
 
 74		return 0;
 75
 76	if (action != PM_HIBERNATION_PREPARE && action != PM_SUSPEND_PREPARE)
 77		return 0;
 78
 79	rtnl_lock();
 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);
 86		}
 87		mutex_unlock(&wg->device_update_lock);
 88	}
 89	rtnl_unlock();
 90	rcu_barrier();
 91	return 0;
 92}
 93
 94static struct notifier_block pm_notifier = { .notifier_call = wg_pm_notification };
 95#endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 96
 97static int wg_stop(struct net_device *dev)
 98{
 99	struct wg_device *wg = netdev_priv(dev);
100	struct wg_peer *peer;
 
101
102	mutex_lock(&wg->device_update_lock);
103	list_for_each_entry(peer, &wg->peer_list, peer_list) {
104		wg_packet_purge_staged_packets(peer);
105		wg_timers_stop(peer);
106		wg_noise_handshake_clear(&peer->handshake);
107		wg_noise_keypairs_clear(&peer->keypairs);
108		wg_noise_reset_last_sent_handshake(&peer->last_sent_handshake);
109	}
110	mutex_unlock(&wg->device_update_lock);
111	skb_queue_purge(&wg->incoming_handshakes);
 
 
112	wg_socket_reinit(wg, NULL, NULL);
113	return 0;
114}
115
116static netdev_tx_t wg_xmit(struct sk_buff *skb, struct net_device *dev)
117{
118	struct wg_device *wg = netdev_priv(dev);
119	struct sk_buff_head packets;
120	struct wg_peer *peer;
121	struct sk_buff *next;
122	sa_family_t family;
123	u32 mtu;
124	int ret;
125
126	if (unlikely(!wg_check_packet_protocol(skb))) {
127		ret = -EPROTONOSUPPORT;
128		net_dbg_ratelimited("%s: Invalid IP packet\n", dev->name);
129		goto err;
130	}
131
132	peer = wg_allowedips_lookup_dst(&wg->peer_allowedips, skb);
133	if (unlikely(!peer)) {
134		ret = -ENOKEY;
135		if (skb->protocol == htons(ETH_P_IP))
136			net_dbg_ratelimited("%s: No peer has allowed IPs matching %pI4\n",
137					    dev->name, &ip_hdr(skb)->daddr);
138		else if (skb->protocol == htons(ETH_P_IPV6))
139			net_dbg_ratelimited("%s: No peer has allowed IPs matching %pI6\n",
140					    dev->name, &ipv6_hdr(skb)->daddr);
141		goto err_icmp;
142	}
143
144	family = READ_ONCE(peer->endpoint.addr.sa_family);
145	if (unlikely(family != AF_INET && family != AF_INET6)) {
146		ret = -EDESTADDRREQ;
147		net_dbg_ratelimited("%s: No valid endpoint has been configured or discovered for peer %llu\n",
148				    dev->name, peer->internal_id);
149		goto err_peer;
150	}
151
152	mtu = skb_dst(skb) ? dst_mtu(skb_dst(skb)) : dev->mtu;
153
154	__skb_queue_head_init(&packets);
155	if (!skb_is_gso(skb)) {
156		skb_mark_not_on_list(skb);
157	} else {
158		struct sk_buff *segs = skb_gso_segment(skb, 0);
159
160		if (IS_ERR(segs)) {
161			ret = PTR_ERR(segs);
162			goto err_peer;
163		}
164		dev_kfree_skb(skb);
165		skb = segs;
166	}
167
168	skb_list_walk_safe(skb, skb, next) {
169		skb_mark_not_on_list(skb);
170
171		skb = skb_share_check(skb, GFP_ATOMIC);
172		if (unlikely(!skb))
173			continue;
174
175		/* We only need to keep the original dst around for icmp,
176		 * so at this point we're in a position to drop it.
177		 */
178		skb_dst_drop(skb);
179
180		PACKET_CB(skb)->mtu = mtu;
181
182		__skb_queue_tail(&packets, skb);
183	}
184
185	spin_lock_bh(&peer->staged_packet_queue.lock);
186	/* If the queue is getting too big, we start removing the oldest packets
187	 * until it's small again. We do this before adding the new packet, so
188	 * we don't remove GSO segments that are in excess.
189	 */
190	while (skb_queue_len(&peer->staged_packet_queue) > MAX_STAGED_PACKETS) {
191		dev_kfree_skb(__skb_dequeue(&peer->staged_packet_queue));
192		++dev->stats.tx_dropped;
193	}
194	skb_queue_splice_tail(&packets, &peer->staged_packet_queue);
195	spin_unlock_bh(&peer->staged_packet_queue.lock);
196
197	wg_packet_send_staged_packets(peer);
198
199	wg_peer_put(peer);
200	return NETDEV_TX_OK;
201
202err_peer:
203	wg_peer_put(peer);
204err_icmp:
205	if (skb->protocol == htons(ETH_P_IP))
206		icmp_ndo_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0);
207	else if (skb->protocol == htons(ETH_P_IPV6))
208		icmpv6_ndo_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
209err:
210	++dev->stats.tx_errors;
211	kfree_skb(skb);
212	return ret;
213}
214
215static const struct net_device_ops netdev_ops = {
216	.ndo_open		= wg_open,
217	.ndo_stop		= wg_stop,
218	.ndo_start_xmit		= wg_xmit,
219	.ndo_get_stats64	= dev_get_tstats64
220};
221
222static void wg_destruct(struct net_device *dev)
223{
224	struct wg_device *wg = netdev_priv(dev);
225
226	rtnl_lock();
227	list_del(&wg->device_list);
228	rtnl_unlock();
229	mutex_lock(&wg->device_update_lock);
230	rcu_assign_pointer(wg->creating_net, NULL);
231	wg->incoming_port = 0;
232	wg_socket_reinit(wg, NULL, NULL);
233	/* The final references are cleared in the below calls to destroy_workqueue. */
234	wg_peer_remove_all(wg);
235	destroy_workqueue(wg->handshake_receive_wq);
236	destroy_workqueue(wg->handshake_send_wq);
237	destroy_workqueue(wg->packet_crypt_wq);
238	wg_packet_queue_free(&wg->decrypt_queue);
239	wg_packet_queue_free(&wg->encrypt_queue);
 
240	rcu_barrier(); /* Wait for all the peers to be actually freed. */
241	wg_ratelimiter_uninit();
242	memzero_explicit(&wg->static_identity, sizeof(wg->static_identity));
243	skb_queue_purge(&wg->incoming_handshakes);
244	free_percpu(dev->tstats);
245	free_percpu(wg->incoming_handshakes_worker);
246	kvfree(wg->index_hashtable);
247	kvfree(wg->peer_hashtable);
248	mutex_unlock(&wg->device_update_lock);
249
250	pr_debug("%s: Interface destroyed\n", dev->name);
251	free_netdev(dev);
252}
253
254static const struct device_type device_type = { .name = KBUILD_MODNAME };
255
256static void wg_setup(struct net_device *dev)
257{
258	struct wg_device *wg = netdev_priv(dev);
259	enum { WG_NETDEV_FEATURES = NETIF_F_HW_CSUM | NETIF_F_RXCSUM |
260				    NETIF_F_SG | NETIF_F_GSO |
261				    NETIF_F_GSO_SOFTWARE | NETIF_F_HIGHDMA };
262	const int overhead = MESSAGE_MINIMUM_LENGTH + sizeof(struct udphdr) +
263			     max(sizeof(struct ipv6hdr), sizeof(struct iphdr));
264
265	dev->netdev_ops = &netdev_ops;
266	dev->header_ops = &ip_tunnel_header_ops;
267	dev->hard_header_len = 0;
268	dev->addr_len = 0;
269	dev->needed_headroom = DATA_PACKET_HEAD_ROOM;
270	dev->needed_tailroom = noise_encrypted_len(MESSAGE_PADDING_MULTIPLE);
271	dev->type = ARPHRD_NONE;
272	dev->flags = IFF_POINTOPOINT | IFF_NOARP;
273	dev->priv_flags |= IFF_NO_QUEUE;
274	dev->features |= NETIF_F_LLTX;
275	dev->features |= WG_NETDEV_FEATURES;
276	dev->hw_features |= WG_NETDEV_FEATURES;
277	dev->hw_enc_features |= WG_NETDEV_FEATURES;
278	dev->mtu = ETH_DATA_LEN - overhead;
279	dev->max_mtu = round_down(INT_MAX, MESSAGE_PADDING_MULTIPLE) - overhead;
280
281	SET_NETDEV_DEVTYPE(dev, &device_type);
282
283	/* We need to keep the dst around in case of icmp replies. */
284	netif_keep_dst(dev);
285
286	memset(wg, 0, sizeof(*wg));
287	wg->dev = dev;
288}
289
290static int wg_newlink(struct net *src_net, struct net_device *dev,
291		      struct nlattr *tb[], struct nlattr *data[],
292		      struct netlink_ext_ack *extack)
293{
294	struct wg_device *wg = netdev_priv(dev);
295	int ret = -ENOMEM;
296
297	rcu_assign_pointer(wg->creating_net, src_net);
298	init_rwsem(&wg->static_identity.lock);
299	mutex_init(&wg->socket_update_lock);
300	mutex_init(&wg->device_update_lock);
301	skb_queue_head_init(&wg->incoming_handshakes);
302	wg_allowedips_init(&wg->peer_allowedips);
303	wg_cookie_checker_init(&wg->cookie_checker, wg);
304	INIT_LIST_HEAD(&wg->peer_list);
305	wg->device_update_gen = 1;
306
307	wg->peer_hashtable = wg_pubkey_hashtable_alloc();
308	if (!wg->peer_hashtable)
309		return ret;
310
311	wg->index_hashtable = wg_index_hashtable_alloc();
312	if (!wg->index_hashtable)
313		goto err_free_peer_hashtable;
314
315	dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
316	if (!dev->tstats)
317		goto err_free_index_hashtable;
318
319	wg->incoming_handshakes_worker =
320		wg_packet_percpu_multicore_worker_alloc(
321				wg_packet_handshake_receive_worker, wg);
322	if (!wg->incoming_handshakes_worker)
323		goto err_free_tstats;
324
325	wg->handshake_receive_wq = alloc_workqueue("wg-kex-%s",
326			WQ_CPU_INTENSIVE | WQ_FREEZABLE, 0, dev->name);
327	if (!wg->handshake_receive_wq)
328		goto err_free_incoming_handshakes;
329
330	wg->handshake_send_wq = alloc_workqueue("wg-kex-%s",
331			WQ_UNBOUND | WQ_FREEZABLE, 0, dev->name);
332	if (!wg->handshake_send_wq)
333		goto err_destroy_handshake_receive;
334
335	wg->packet_crypt_wq = alloc_workqueue("wg-crypt-%s",
336			WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, 0, dev->name);
337	if (!wg->packet_crypt_wq)
338		goto err_destroy_handshake_send;
339
340	ret = wg_packet_queue_init(&wg->encrypt_queue, wg_packet_encrypt_worker,
341				   MAX_QUEUED_PACKETS);
342	if (ret < 0)
343		goto err_destroy_packet_crypt;
344
345	ret = wg_packet_queue_init(&wg->decrypt_queue, wg_packet_decrypt_worker,
346				   MAX_QUEUED_PACKETS);
347	if (ret < 0)
348		goto err_free_encrypt_queue;
349
 
 
 
 
 
350	ret = wg_ratelimiter_init();
351	if (ret < 0)
352		goto err_free_decrypt_queue;
353
354	ret = register_netdevice(dev);
355	if (ret < 0)
356		goto err_uninit_ratelimiter;
357
358	list_add(&wg->device_list, &device_list);
359
360	/* We wait until the end to assign priv_destructor, so that
361	 * register_netdevice doesn't call it for us if it fails.
362	 */
363	dev->priv_destructor = wg_destruct;
364
365	pr_debug("%s: Interface created\n", dev->name);
366	return ret;
367
368err_uninit_ratelimiter:
369	wg_ratelimiter_uninit();
 
 
370err_free_decrypt_queue:
371	wg_packet_queue_free(&wg->decrypt_queue);
372err_free_encrypt_queue:
373	wg_packet_queue_free(&wg->encrypt_queue);
374err_destroy_packet_crypt:
375	destroy_workqueue(wg->packet_crypt_wq);
376err_destroy_handshake_send:
377	destroy_workqueue(wg->handshake_send_wq);
378err_destroy_handshake_receive:
379	destroy_workqueue(wg->handshake_receive_wq);
380err_free_incoming_handshakes:
381	free_percpu(wg->incoming_handshakes_worker);
382err_free_tstats:
383	free_percpu(dev->tstats);
384err_free_index_hashtable:
385	kvfree(wg->index_hashtable);
386err_free_peer_hashtable:
387	kvfree(wg->peer_hashtable);
388	return ret;
389}
390
391static struct rtnl_link_ops link_ops __read_mostly = {
392	.kind			= KBUILD_MODNAME,
393	.priv_size		= sizeof(struct wg_device),
394	.setup			= wg_setup,
395	.newlink		= wg_newlink,
396};
397
398static void wg_netns_pre_exit(struct net *net)
399{
400	struct wg_device *wg;
 
401
402	rtnl_lock();
403	list_for_each_entry(wg, &device_list, device_list) {
404		if (rcu_access_pointer(wg->creating_net) == net) {
405			pr_debug("%s: Creating namespace exiting\n", wg->dev->name);
406			netif_carrier_off(wg->dev);
407			mutex_lock(&wg->device_update_lock);
408			rcu_assign_pointer(wg->creating_net, NULL);
409			wg_socket_reinit(wg, NULL, NULL);
 
 
410			mutex_unlock(&wg->device_update_lock);
411		}
412	}
413	rtnl_unlock();
414}
415
416static struct pernet_operations pernet_ops = {
417	.pre_exit = wg_netns_pre_exit
418};
419
420int __init wg_device_init(void)
421{
422	int ret;
423
424#ifdef CONFIG_PM_SLEEP
425	ret = register_pm_notifier(&pm_notifier);
426	if (ret)
427		return ret;
428#endif
 
 
 
429
430	ret = register_pernet_device(&pernet_ops);
431	if (ret)
432		goto error_pm;
433
434	ret = rtnl_link_register(&link_ops);
435	if (ret)
436		goto error_pernet;
437
438	return 0;
439
440error_pernet:
441	unregister_pernet_device(&pernet_ops);
 
 
442error_pm:
443#ifdef CONFIG_PM_SLEEP
444	unregister_pm_notifier(&pm_notifier);
445#endif
446	return ret;
447}
448
449void wg_device_uninit(void)
450{
451	rtnl_link_unregister(&link_ops);
452	unregister_pernet_device(&pernet_ops);
453#ifdef CONFIG_PM_SLEEP
454	unregister_pm_notifier(&pm_notifier);
455#endif
456	rcu_barrier();
457}