1e7096c13SJason A. Donenfeld // SPDX-License-Identifier: GPL-2.0 2e7096c13SJason A. Donenfeld /* 3e7096c13SJason A. Donenfeld * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. 4e7096c13SJason A. Donenfeld */ 5e7096c13SJason A. Donenfeld 6e7096c13SJason A. Donenfeld #include "queueing.h" 7e7096c13SJason A. Donenfeld #include "socket.h" 8e7096c13SJason A. Donenfeld #include "timers.h" 9e7096c13SJason A. Donenfeld #include "device.h" 10e7096c13SJason A. Donenfeld #include "ratelimiter.h" 11e7096c13SJason A. Donenfeld #include "peer.h" 12e7096c13SJason A. Donenfeld #include "messages.h" 13e7096c13SJason A. Donenfeld 14e7096c13SJason A. Donenfeld #include <linux/module.h> 15e7096c13SJason A. Donenfeld #include <linux/rtnetlink.h> 16e7096c13SJason A. Donenfeld #include <linux/inet.h> 17e7096c13SJason A. Donenfeld #include <linux/netdevice.h> 18e7096c13SJason A. Donenfeld #include <linux/inetdevice.h> 19e7096c13SJason A. Donenfeld #include <linux/if_arp.h> 20e7096c13SJason A. Donenfeld #include <linux/icmp.h> 21e7096c13SJason A. Donenfeld #include <linux/suspend.h> 2245ac774cSNikolay Aleksandrov #include <net/dst_metadata.h> 23e7096c13SJason A. Donenfeld #include <net/icmp.h> 24e7096c13SJason A. Donenfeld #include <net/rtnetlink.h> 25e7096c13SJason A. Donenfeld #include <net/ip_tunnels.h> 26e7096c13SJason A. Donenfeld #include <net/addrconf.h> 27e7096c13SJason A. Donenfeld 28e7096c13SJason A. Donenfeld static LIST_HEAD(device_list); 29e7096c13SJason A. Donenfeld 30e7096c13SJason A. Donenfeld static int wg_open(struct net_device *dev) 31e7096c13SJason A. Donenfeld { 32e7096c13SJason A. Donenfeld struct in_device *dev_v4 = __in_dev_get_rtnl(dev); 33e7096c13SJason A. Donenfeld struct inet6_dev *dev_v6 = __in6_dev_get(dev); 34e7096c13SJason A. Donenfeld struct wg_device *wg = netdev_priv(dev); 35e7096c13SJason A. Donenfeld struct wg_peer *peer; 36e7096c13SJason A. Donenfeld int ret; 37e7096c13SJason A. Donenfeld 38e7096c13SJason A. Donenfeld if (dev_v4) { 39e7096c13SJason A. Donenfeld /* At some point we might put this check near the ip_rt_send_ 40e7096c13SJason A. Donenfeld * redirect call of ip_forward in net/ipv4/ip_forward.c, similar 41e7096c13SJason A. Donenfeld * to the current secpath check. 42e7096c13SJason A. Donenfeld */ 43e7096c13SJason A. Donenfeld IN_DEV_CONF_SET(dev_v4, SEND_REDIRECTS, false); 44e7096c13SJason A. Donenfeld IPV4_DEVCONF_ALL(dev_net(dev), SEND_REDIRECTS) = false; 45e7096c13SJason A. Donenfeld } 46e7096c13SJason A. Donenfeld if (dev_v6) 47e7096c13SJason A. Donenfeld dev_v6->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_NONE; 48e7096c13SJason A. Donenfeld 49900575aaSJason A. Donenfeld mutex_lock(&wg->device_update_lock); 50e7096c13SJason A. Donenfeld ret = wg_socket_init(wg, wg->incoming_port); 51e7096c13SJason A. Donenfeld if (ret < 0) 52900575aaSJason A. Donenfeld goto out; 53e7096c13SJason A. Donenfeld list_for_each_entry(peer, &wg->peer_list, peer_list) { 54e7096c13SJason A. Donenfeld wg_packet_send_staged_packets(peer); 55e7096c13SJason A. Donenfeld if (peer->persistent_keepalive_interval) 56e7096c13SJason A. Donenfeld wg_packet_send_keepalive(peer); 57e7096c13SJason A. Donenfeld } 58900575aaSJason A. Donenfeld out: 59e7096c13SJason A. Donenfeld mutex_unlock(&wg->device_update_lock); 60900575aaSJason A. Donenfeld return ret; 61e7096c13SJason A. Donenfeld } 62e7096c13SJason A. Donenfeld 632d6919c3SJason A. Donenfeld static int wg_pm_notification(struct notifier_block *nb, unsigned long action, void *data) 64e7096c13SJason A. Donenfeld { 65e7096c13SJason A. Donenfeld struct wg_device *wg; 66e7096c13SJason A. Donenfeld struct wg_peer *peer; 67e7096c13SJason A. Donenfeld 68e7096c13SJason A. Donenfeld /* If the machine is constantly suspending and resuming, as part of 69e7096c13SJason A. Donenfeld * its normal operation rather than as a somewhat rare event, then we 70e7096c13SJason A. Donenfeld * don't actually want to clear keys. 71e7096c13SJason A. Donenfeld */ 72*261e224dSKalesh Singh if (IS_ENABLED(CONFIG_PM_AUTOSLEEP) || 73*261e224dSKalesh Singh IS_ENABLED(CONFIG_PM_USERSPACE_AUTOSLEEP)) 74e7096c13SJason A. Donenfeld return 0; 75e7096c13SJason A. Donenfeld 76e7096c13SJason A. Donenfeld if (action != PM_HIBERNATION_PREPARE && action != PM_SUSPEND_PREPARE) 77e7096c13SJason A. Donenfeld return 0; 78e7096c13SJason A. Donenfeld 79e7096c13SJason A. Donenfeld rtnl_lock(); 80e7096c13SJason A. Donenfeld list_for_each_entry(wg, &device_list, device_list) { 81e7096c13SJason A. Donenfeld mutex_lock(&wg->device_update_lock); 82e7096c13SJason A. Donenfeld list_for_each_entry(peer, &wg->peer_list, peer_list) { 83e7096c13SJason A. Donenfeld del_timer(&peer->timer_zero_key_material); 84e7096c13SJason A. Donenfeld wg_noise_handshake_clear(&peer->handshake); 85e7096c13SJason A. Donenfeld wg_noise_keypairs_clear(&peer->keypairs); 86e7096c13SJason A. Donenfeld } 87e7096c13SJason A. Donenfeld mutex_unlock(&wg->device_update_lock); 88e7096c13SJason A. Donenfeld } 89e7096c13SJason A. Donenfeld rtnl_unlock(); 90e7096c13SJason A. Donenfeld rcu_barrier(); 91e7096c13SJason A. Donenfeld return 0; 92e7096c13SJason A. Donenfeld } 93e7096c13SJason A. Donenfeld 94e7096c13SJason A. Donenfeld static struct notifier_block pm_notifier = { .notifier_call = wg_pm_notification }; 952d6919c3SJason A. Donenfeld 962d6919c3SJason A. Donenfeld static int wg_vm_notification(struct notifier_block *nb, unsigned long action, void *data) 972d6919c3SJason A. Donenfeld { 982d6919c3SJason A. Donenfeld struct wg_device *wg; 992d6919c3SJason A. Donenfeld struct wg_peer *peer; 1002d6919c3SJason A. Donenfeld 1012d6919c3SJason A. Donenfeld rtnl_lock(); 1022d6919c3SJason A. Donenfeld list_for_each_entry(wg, &device_list, device_list) { 1032d6919c3SJason A. Donenfeld mutex_lock(&wg->device_update_lock); 1042d6919c3SJason A. Donenfeld list_for_each_entry(peer, &wg->peer_list, peer_list) 1052d6919c3SJason A. Donenfeld wg_noise_expire_current_peer_keypairs(peer); 1062d6919c3SJason A. Donenfeld mutex_unlock(&wg->device_update_lock); 1072d6919c3SJason A. Donenfeld } 1082d6919c3SJason A. Donenfeld rtnl_unlock(); 1092d6919c3SJason A. Donenfeld return 0; 1102d6919c3SJason A. Donenfeld } 1112d6919c3SJason A. Donenfeld 1122d6919c3SJason A. Donenfeld static struct notifier_block vm_notifier = { .notifier_call = wg_vm_notification }; 113e7096c13SJason A. Donenfeld 114e7096c13SJason A. Donenfeld static int wg_stop(struct net_device *dev) 115e7096c13SJason A. Donenfeld { 116e7096c13SJason A. Donenfeld struct wg_device *wg = netdev_priv(dev); 117e7096c13SJason A. Donenfeld struct wg_peer *peer; 118886fcee9SJason A. Donenfeld struct sk_buff *skb; 119e7096c13SJason A. Donenfeld 120e7096c13SJason A. Donenfeld mutex_lock(&wg->device_update_lock); 121e7096c13SJason A. Donenfeld list_for_each_entry(peer, &wg->peer_list, peer_list) { 122e7096c13SJason A. Donenfeld wg_packet_purge_staged_packets(peer); 123e7096c13SJason A. Donenfeld wg_timers_stop(peer); 124e7096c13SJason A. Donenfeld wg_noise_handshake_clear(&peer->handshake); 125e7096c13SJason A. Donenfeld wg_noise_keypairs_clear(&peer->keypairs); 126e7096c13SJason A. Donenfeld wg_noise_reset_last_sent_handshake(&peer->last_sent_handshake); 127e7096c13SJason A. Donenfeld } 128e7096c13SJason A. Donenfeld mutex_unlock(&wg->device_update_lock); 129886fcee9SJason A. Donenfeld while ((skb = ptr_ring_consume(&wg->handshake_queue.ring)) != NULL) 130886fcee9SJason A. Donenfeld kfree_skb(skb); 131886fcee9SJason A. Donenfeld atomic_set(&wg->handshake_queue_len, 0); 132e7096c13SJason A. Donenfeld wg_socket_reinit(wg, NULL, NULL); 133e7096c13SJason A. Donenfeld return 0; 134e7096c13SJason A. Donenfeld } 135e7096c13SJason A. Donenfeld 136e7096c13SJason A. Donenfeld static netdev_tx_t wg_xmit(struct sk_buff *skb, struct net_device *dev) 137e7096c13SJason A. Donenfeld { 138e7096c13SJason A. Donenfeld struct wg_device *wg = netdev_priv(dev); 139e7096c13SJason A. Donenfeld struct sk_buff_head packets; 140e7096c13SJason A. Donenfeld struct wg_peer *peer; 141e7096c13SJason A. Donenfeld struct sk_buff *next; 142e7096c13SJason A. Donenfeld sa_family_t family; 143e7096c13SJason A. Donenfeld u32 mtu; 144e7096c13SJason A. Donenfeld int ret; 145e7096c13SJason A. Donenfeld 146a5588604SJason A. Donenfeld if (unlikely(!wg_check_packet_protocol(skb))) { 147e7096c13SJason A. Donenfeld ret = -EPROTONOSUPPORT; 148e7096c13SJason A. Donenfeld net_dbg_ratelimited("%s: Invalid IP packet\n", dev->name); 149e7096c13SJason A. Donenfeld goto err; 150e7096c13SJason A. Donenfeld } 151e7096c13SJason A. Donenfeld 152e7096c13SJason A. Donenfeld peer = wg_allowedips_lookup_dst(&wg->peer_allowedips, skb); 153e7096c13SJason A. Donenfeld if (unlikely(!peer)) { 154e7096c13SJason A. Donenfeld ret = -ENOKEY; 155e7096c13SJason A. Donenfeld if (skb->protocol == htons(ETH_P_IP)) 156e7096c13SJason A. Donenfeld net_dbg_ratelimited("%s: No peer has allowed IPs matching %pI4\n", 157e7096c13SJason A. Donenfeld dev->name, &ip_hdr(skb)->daddr); 158e7096c13SJason A. Donenfeld else if (skb->protocol == htons(ETH_P_IPV6)) 159e7096c13SJason A. Donenfeld net_dbg_ratelimited("%s: No peer has allowed IPs matching %pI6\n", 160e7096c13SJason A. Donenfeld dev->name, &ipv6_hdr(skb)->daddr); 16199fff526SJason A. Donenfeld goto err_icmp; 162e7096c13SJason A. Donenfeld } 163e7096c13SJason A. Donenfeld 164e7096c13SJason A. Donenfeld family = READ_ONCE(peer->endpoint.addr.sa_family); 165e7096c13SJason A. Donenfeld if (unlikely(family != AF_INET && family != AF_INET6)) { 166e7096c13SJason A. Donenfeld ret = -EDESTADDRREQ; 167e7096c13SJason A. Donenfeld net_dbg_ratelimited("%s: No valid endpoint has been configured or discovered for peer %llu\n", 168e7096c13SJason A. Donenfeld dev->name, peer->internal_id); 169e7096c13SJason A. Donenfeld goto err_peer; 170e7096c13SJason A. Donenfeld } 171e7096c13SJason A. Donenfeld 17245ac774cSNikolay Aleksandrov mtu = skb_valid_dst(skb) ? dst_mtu(skb_dst(skb)) : dev->mtu; 173e7096c13SJason A. Donenfeld 174e7096c13SJason A. Donenfeld __skb_queue_head_init(&packets); 175e7096c13SJason A. Donenfeld if (!skb_is_gso(skb)) { 176e7096c13SJason A. Donenfeld skb_mark_not_on_list(skb); 177e7096c13SJason A. Donenfeld } else { 178e7096c13SJason A. Donenfeld struct sk_buff *segs = skb_gso_segment(skb, 0); 179e7096c13SJason A. Donenfeld 18030ac4e2fSAntonio Quartulli if (IS_ERR(segs)) { 181e7096c13SJason A. Donenfeld ret = PTR_ERR(segs); 182e7096c13SJason A. Donenfeld goto err_peer; 183e7096c13SJason A. Donenfeld } 184e7096c13SJason A. Donenfeld dev_kfree_skb(skb); 185e7096c13SJason A. Donenfeld skb = segs; 186e7096c13SJason A. Donenfeld } 187e7096c13SJason A. Donenfeld 188e7096c13SJason A. Donenfeld skb_list_walk_safe(skb, skb, next) { 189e7096c13SJason A. Donenfeld skb_mark_not_on_list(skb); 190e7096c13SJason A. Donenfeld 191e7096c13SJason A. Donenfeld skb = skb_share_check(skb, GFP_ATOMIC); 192e7096c13SJason A. Donenfeld if (unlikely(!skb)) 193e7096c13SJason A. Donenfeld continue; 194e7096c13SJason A. Donenfeld 195e7096c13SJason A. Donenfeld /* We only need to keep the original dst around for icmp, 196e7096c13SJason A. Donenfeld * so at this point we're in a position to drop it. 197e7096c13SJason A. Donenfeld */ 198e7096c13SJason A. Donenfeld skb_dst_drop(skb); 199e7096c13SJason A. Donenfeld 200e7096c13SJason A. Donenfeld PACKET_CB(skb)->mtu = mtu; 201e7096c13SJason A. Donenfeld 202e7096c13SJason A. Donenfeld __skb_queue_tail(&packets, skb); 203e7096c13SJason A. Donenfeld } 204e7096c13SJason A. Donenfeld 205e7096c13SJason A. Donenfeld spin_lock_bh(&peer->staged_packet_queue.lock); 206e7096c13SJason A. Donenfeld /* If the queue is getting too big, we start removing the oldest packets 207e7096c13SJason A. Donenfeld * until it's small again. We do this before adding the new packet, so 208e7096c13SJason A. Donenfeld * we don't remove GSO segments that are in excess. 209e7096c13SJason A. Donenfeld */ 210e7096c13SJason A. Donenfeld while (skb_queue_len(&peer->staged_packet_queue) > MAX_STAGED_PACKETS) { 211e7096c13SJason A. Donenfeld dev_kfree_skb(__skb_dequeue(&peer->staged_packet_queue)); 212e7096c13SJason A. Donenfeld ++dev->stats.tx_dropped; 213e7096c13SJason A. Donenfeld } 214e7096c13SJason A. Donenfeld skb_queue_splice_tail(&packets, &peer->staged_packet_queue); 215e7096c13SJason A. Donenfeld spin_unlock_bh(&peer->staged_packet_queue.lock); 216e7096c13SJason A. Donenfeld 217e7096c13SJason A. Donenfeld wg_packet_send_staged_packets(peer); 218e7096c13SJason A. Donenfeld 219e7096c13SJason A. Donenfeld wg_peer_put(peer); 220e7096c13SJason A. Donenfeld return NETDEV_TX_OK; 221e7096c13SJason A. Donenfeld 222e7096c13SJason A. Donenfeld err_peer: 223e7096c13SJason A. Donenfeld wg_peer_put(peer); 22499fff526SJason A. Donenfeld err_icmp: 225e7096c13SJason A. Donenfeld if (skb->protocol == htons(ETH_P_IP)) 226a12d7f3cSJason A. Donenfeld icmp_ndo_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0); 227e7096c13SJason A. Donenfeld else if (skb->protocol == htons(ETH_P_IPV6)) 228a12d7f3cSJason A. Donenfeld icmpv6_ndo_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); 22999fff526SJason A. Donenfeld err: 23099fff526SJason A. Donenfeld ++dev->stats.tx_errors; 231e7096c13SJason A. Donenfeld kfree_skb(skb); 232e7096c13SJason A. Donenfeld return ret; 233e7096c13SJason A. Donenfeld } 234e7096c13SJason A. Donenfeld 235e7096c13SJason A. Donenfeld static const struct net_device_ops netdev_ops = { 236e7096c13SJason A. Donenfeld .ndo_open = wg_open, 237e7096c13SJason A. Donenfeld .ndo_stop = wg_stop, 238e7096c13SJason A. Donenfeld .ndo_start_xmit = wg_xmit, 23942f9e5f0SHeiner Kallweit .ndo_get_stats64 = dev_get_tstats64 240e7096c13SJason A. Donenfeld }; 241e7096c13SJason A. Donenfeld 242e7096c13SJason A. Donenfeld static void wg_destruct(struct net_device *dev) 243e7096c13SJason A. Donenfeld { 244e7096c13SJason A. Donenfeld struct wg_device *wg = netdev_priv(dev); 245e7096c13SJason A. Donenfeld 246e7096c13SJason A. Donenfeld rtnl_lock(); 247e7096c13SJason A. Donenfeld list_del(&wg->device_list); 248e7096c13SJason A. Donenfeld rtnl_unlock(); 249e7096c13SJason A. Donenfeld mutex_lock(&wg->device_update_lock); 250900575aaSJason A. Donenfeld rcu_assign_pointer(wg->creating_net, NULL); 251e7096c13SJason A. Donenfeld wg->incoming_port = 0; 252e7096c13SJason A. Donenfeld wg_socket_reinit(wg, NULL, NULL); 253e7096c13SJason A. Donenfeld /* The final references are cleared in the below calls to destroy_workqueue. */ 254e7096c13SJason A. Donenfeld wg_peer_remove_all(wg); 255e7096c13SJason A. Donenfeld destroy_workqueue(wg->handshake_receive_wq); 256e7096c13SJason A. Donenfeld destroy_workqueue(wg->handshake_send_wq); 257e7096c13SJason A. Donenfeld destroy_workqueue(wg->packet_crypt_wq); 258886fcee9SJason A. Donenfeld wg_packet_queue_free(&wg->handshake_queue, true); 259886fcee9SJason A. Donenfeld wg_packet_queue_free(&wg->decrypt_queue, false); 260886fcee9SJason A. Donenfeld wg_packet_queue_free(&wg->encrypt_queue, false); 261e7096c13SJason A. Donenfeld rcu_barrier(); /* Wait for all the peers to be actually freed. */ 262e7096c13SJason A. Donenfeld wg_ratelimiter_uninit(); 263e7096c13SJason A. Donenfeld memzero_explicit(&wg->static_identity, sizeof(wg->static_identity)); 264e7096c13SJason A. Donenfeld free_percpu(dev->tstats); 265e7096c13SJason A. Donenfeld kvfree(wg->index_hashtable); 266e7096c13SJason A. Donenfeld kvfree(wg->peer_hashtable); 267e7096c13SJason A. Donenfeld mutex_unlock(&wg->device_update_lock); 268e7096c13SJason A. Donenfeld 269900575aaSJason A. Donenfeld pr_debug("%s: Interface destroyed\n", dev->name); 270e7096c13SJason A. Donenfeld free_netdev(dev); 271e7096c13SJason A. Donenfeld } 272e7096c13SJason A. Donenfeld 273e7096c13SJason A. Donenfeld static const struct device_type device_type = { .name = KBUILD_MODNAME }; 274e7096c13SJason A. Donenfeld 275e7096c13SJason A. Donenfeld static void wg_setup(struct net_device *dev) 276e7096c13SJason A. Donenfeld { 277e7096c13SJason A. Donenfeld struct wg_device *wg = netdev_priv(dev); 278e7096c13SJason A. Donenfeld enum { WG_NETDEV_FEATURES = NETIF_F_HW_CSUM | NETIF_F_RXCSUM | 279e7096c13SJason A. Donenfeld NETIF_F_SG | NETIF_F_GSO | 280e7096c13SJason A. Donenfeld NETIF_F_GSO_SOFTWARE | NETIF_F_HIGHDMA }; 281175f1ca9SJason A. Donenfeld const int overhead = MESSAGE_MINIMUM_LENGTH + sizeof(struct udphdr) + 282175f1ca9SJason A. Donenfeld max(sizeof(struct ipv6hdr), sizeof(struct iphdr)); 283e7096c13SJason A. Donenfeld 284e7096c13SJason A. Donenfeld dev->netdev_ops = &netdev_ops; 28501a4967cSJason A. Donenfeld dev->header_ops = &ip_tunnel_header_ops; 286e7096c13SJason A. Donenfeld dev->hard_header_len = 0; 287e7096c13SJason A. Donenfeld dev->addr_len = 0; 288e7096c13SJason A. Donenfeld dev->needed_headroom = DATA_PACKET_HEAD_ROOM; 289e7096c13SJason A. Donenfeld dev->needed_tailroom = noise_encrypted_len(MESSAGE_PADDING_MULTIPLE); 290e7096c13SJason A. Donenfeld dev->type = ARPHRD_NONE; 291e7096c13SJason A. Donenfeld dev->flags = IFF_POINTOPOINT | IFF_NOARP; 292e7096c13SJason A. Donenfeld dev->priv_flags |= IFF_NO_QUEUE; 293e7096c13SJason A. Donenfeld dev->features |= NETIF_F_LLTX; 294e7096c13SJason A. Donenfeld dev->features |= WG_NETDEV_FEATURES; 295e7096c13SJason A. Donenfeld dev->hw_features |= WG_NETDEV_FEATURES; 296e7096c13SJason A. Donenfeld dev->hw_enc_features |= WG_NETDEV_FEATURES; 297175f1ca9SJason A. Donenfeld dev->mtu = ETH_DATA_LEN - overhead; 298175f1ca9SJason A. Donenfeld dev->max_mtu = round_down(INT_MAX, MESSAGE_PADDING_MULTIPLE) - overhead; 299e7096c13SJason A. Donenfeld 300e7096c13SJason A. Donenfeld SET_NETDEV_DEVTYPE(dev, &device_type); 301e7096c13SJason A. Donenfeld 302e7096c13SJason A. Donenfeld /* We need to keep the dst around in case of icmp replies. */ 303e7096c13SJason A. Donenfeld netif_keep_dst(dev); 304e7096c13SJason A. Donenfeld 305e7096c13SJason A. Donenfeld memset(wg, 0, sizeof(*wg)); 306e7096c13SJason A. Donenfeld wg->dev = dev; 307e7096c13SJason A. Donenfeld } 308e7096c13SJason A. Donenfeld 309e7096c13SJason A. Donenfeld static int wg_newlink(struct net *src_net, struct net_device *dev, 310e7096c13SJason A. Donenfeld struct nlattr *tb[], struct nlattr *data[], 311e7096c13SJason A. Donenfeld struct netlink_ext_ack *extack) 312e7096c13SJason A. Donenfeld { 313e7096c13SJason A. Donenfeld struct wg_device *wg = netdev_priv(dev); 314e7096c13SJason A. Donenfeld int ret = -ENOMEM; 315e7096c13SJason A. Donenfeld 316900575aaSJason A. Donenfeld rcu_assign_pointer(wg->creating_net, src_net); 317e7096c13SJason A. Donenfeld init_rwsem(&wg->static_identity.lock); 318e7096c13SJason A. Donenfeld mutex_init(&wg->socket_update_lock); 319e7096c13SJason A. Donenfeld mutex_init(&wg->device_update_lock); 320e7096c13SJason A. Donenfeld wg_allowedips_init(&wg->peer_allowedips); 321e7096c13SJason A. Donenfeld wg_cookie_checker_init(&wg->cookie_checker, wg); 322e7096c13SJason A. Donenfeld INIT_LIST_HEAD(&wg->peer_list); 323e7096c13SJason A. Donenfeld wg->device_update_gen = 1; 324e7096c13SJason A. Donenfeld 325e7096c13SJason A. Donenfeld wg->peer_hashtable = wg_pubkey_hashtable_alloc(); 326e7096c13SJason A. Donenfeld if (!wg->peer_hashtable) 327e7096c13SJason A. Donenfeld return ret; 328e7096c13SJason A. Donenfeld 329e7096c13SJason A. Donenfeld wg->index_hashtable = wg_index_hashtable_alloc(); 330e7096c13SJason A. Donenfeld if (!wg->index_hashtable) 331e7096c13SJason A. Donenfeld goto err_free_peer_hashtable; 332e7096c13SJason A. Donenfeld 333e7096c13SJason A. Donenfeld dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); 334e7096c13SJason A. Donenfeld if (!dev->tstats) 335e7096c13SJason A. Donenfeld goto err_free_index_hashtable; 336e7096c13SJason A. Donenfeld 337e7096c13SJason A. Donenfeld wg->handshake_receive_wq = alloc_workqueue("wg-kex-%s", 338e7096c13SJason A. Donenfeld WQ_CPU_INTENSIVE | WQ_FREEZABLE, 0, dev->name); 339e7096c13SJason A. Donenfeld if (!wg->handshake_receive_wq) 340886fcee9SJason A. Donenfeld goto err_free_tstats; 341e7096c13SJason A. Donenfeld 342e7096c13SJason A. Donenfeld wg->handshake_send_wq = alloc_workqueue("wg-kex-%s", 343e7096c13SJason A. Donenfeld WQ_UNBOUND | WQ_FREEZABLE, 0, dev->name); 344e7096c13SJason A. Donenfeld if (!wg->handshake_send_wq) 345e7096c13SJason A. Donenfeld goto err_destroy_handshake_receive; 346e7096c13SJason A. Donenfeld 347e7096c13SJason A. Donenfeld wg->packet_crypt_wq = alloc_workqueue("wg-crypt-%s", 348e7096c13SJason A. Donenfeld WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, 0, dev->name); 349e7096c13SJason A. Donenfeld if (!wg->packet_crypt_wq) 350e7096c13SJason A. Donenfeld goto err_destroy_handshake_send; 351e7096c13SJason A. Donenfeld 352e7096c13SJason A. Donenfeld ret = wg_packet_queue_init(&wg->encrypt_queue, wg_packet_encrypt_worker, 3538b5553acSJason A. Donenfeld MAX_QUEUED_PACKETS); 354e7096c13SJason A. Donenfeld if (ret < 0) 355e7096c13SJason A. Donenfeld goto err_destroy_packet_crypt; 356e7096c13SJason A. Donenfeld 357e7096c13SJason A. Donenfeld ret = wg_packet_queue_init(&wg->decrypt_queue, wg_packet_decrypt_worker, 3588b5553acSJason A. Donenfeld MAX_QUEUED_PACKETS); 359e7096c13SJason A. Donenfeld if (ret < 0) 360e7096c13SJason A. Donenfeld goto err_free_encrypt_queue; 361e7096c13SJason A. Donenfeld 362886fcee9SJason A. Donenfeld ret = wg_packet_queue_init(&wg->handshake_queue, wg_packet_handshake_receive_worker, 363886fcee9SJason A. Donenfeld MAX_QUEUED_INCOMING_HANDSHAKES); 364e7096c13SJason A. Donenfeld if (ret < 0) 365e7096c13SJason A. Donenfeld goto err_free_decrypt_queue; 366e7096c13SJason A. Donenfeld 367886fcee9SJason A. Donenfeld ret = wg_ratelimiter_init(); 368886fcee9SJason A. Donenfeld if (ret < 0) 369886fcee9SJason A. Donenfeld goto err_free_handshake_queue; 370886fcee9SJason A. Donenfeld 371e7096c13SJason A. Donenfeld ret = register_netdevice(dev); 372e7096c13SJason A. Donenfeld if (ret < 0) 373e7096c13SJason A. Donenfeld goto err_uninit_ratelimiter; 374e7096c13SJason A. Donenfeld 375e7096c13SJason A. Donenfeld list_add(&wg->device_list, &device_list); 376e7096c13SJason A. Donenfeld 377e7096c13SJason A. Donenfeld /* We wait until the end to assign priv_destructor, so that 378e7096c13SJason A. Donenfeld * register_netdevice doesn't call it for us if it fails. 379e7096c13SJason A. Donenfeld */ 380e7096c13SJason A. Donenfeld dev->priv_destructor = wg_destruct; 381e7096c13SJason A. Donenfeld 382e7096c13SJason A. Donenfeld pr_debug("%s: Interface created\n", dev->name); 383e7096c13SJason A. Donenfeld return ret; 384e7096c13SJason A. Donenfeld 385e7096c13SJason A. Donenfeld err_uninit_ratelimiter: 386e7096c13SJason A. Donenfeld wg_ratelimiter_uninit(); 387886fcee9SJason A. Donenfeld err_free_handshake_queue: 388886fcee9SJason A. Donenfeld wg_packet_queue_free(&wg->handshake_queue, false); 389e7096c13SJason A. Donenfeld err_free_decrypt_queue: 390886fcee9SJason A. Donenfeld wg_packet_queue_free(&wg->decrypt_queue, false); 391e7096c13SJason A. Donenfeld err_free_encrypt_queue: 392886fcee9SJason A. Donenfeld wg_packet_queue_free(&wg->encrypt_queue, false); 393e7096c13SJason A. Donenfeld err_destroy_packet_crypt: 394e7096c13SJason A. Donenfeld destroy_workqueue(wg->packet_crypt_wq); 395e7096c13SJason A. Donenfeld err_destroy_handshake_send: 396e7096c13SJason A. Donenfeld destroy_workqueue(wg->handshake_send_wq); 397e7096c13SJason A. Donenfeld err_destroy_handshake_receive: 398e7096c13SJason A. Donenfeld destroy_workqueue(wg->handshake_receive_wq); 399e7096c13SJason A. Donenfeld err_free_tstats: 400e7096c13SJason A. Donenfeld free_percpu(dev->tstats); 401e7096c13SJason A. Donenfeld err_free_index_hashtable: 402e7096c13SJason A. Donenfeld kvfree(wg->index_hashtable); 403e7096c13SJason A. Donenfeld err_free_peer_hashtable: 404e7096c13SJason A. Donenfeld kvfree(wg->peer_hashtable); 405e7096c13SJason A. Donenfeld return ret; 406e7096c13SJason A. Donenfeld } 407e7096c13SJason A. Donenfeld 408e7096c13SJason A. Donenfeld static struct rtnl_link_ops link_ops __read_mostly = { 409e7096c13SJason A. Donenfeld .kind = KBUILD_MODNAME, 410e7096c13SJason A. Donenfeld .priv_size = sizeof(struct wg_device), 411e7096c13SJason A. Donenfeld .setup = wg_setup, 412e7096c13SJason A. Donenfeld .newlink = wg_newlink, 413e7096c13SJason A. Donenfeld }; 414e7096c13SJason A. Donenfeld 415900575aaSJason A. Donenfeld static void wg_netns_pre_exit(struct net *net) 416e7096c13SJason A. Donenfeld { 417900575aaSJason A. Donenfeld struct wg_device *wg; 41820ae1d6aSJason A. Donenfeld struct wg_peer *peer; 419e7096c13SJason A. Donenfeld 420900575aaSJason A. Donenfeld rtnl_lock(); 421900575aaSJason A. Donenfeld list_for_each_entry(wg, &device_list, device_list) { 422900575aaSJason A. Donenfeld if (rcu_access_pointer(wg->creating_net) == net) { 423900575aaSJason A. Donenfeld pr_debug("%s: Creating namespace exiting\n", wg->dev->name); 424900575aaSJason A. Donenfeld netif_carrier_off(wg->dev); 425900575aaSJason A. Donenfeld mutex_lock(&wg->device_update_lock); 426900575aaSJason A. Donenfeld rcu_assign_pointer(wg->creating_net, NULL); 427900575aaSJason A. Donenfeld wg_socket_reinit(wg, NULL, NULL); 42820ae1d6aSJason A. Donenfeld list_for_each_entry(peer, &wg->peer_list, peer_list) 42920ae1d6aSJason A. Donenfeld wg_socket_clear_peer_endpoint_src(peer); 430900575aaSJason A. Donenfeld mutex_unlock(&wg->device_update_lock); 431e7096c13SJason A. Donenfeld } 432900575aaSJason A. Donenfeld } 433900575aaSJason A. Donenfeld rtnl_unlock(); 434e7096c13SJason A. Donenfeld } 435e7096c13SJason A. Donenfeld 436900575aaSJason A. Donenfeld static struct pernet_operations pernet_ops = { 437900575aaSJason A. Donenfeld .pre_exit = wg_netns_pre_exit 438e7096c13SJason A. Donenfeld }; 439e7096c13SJason A. Donenfeld 440e7096c13SJason A. Donenfeld int __init wg_device_init(void) 441e7096c13SJason A. Donenfeld { 442e7096c13SJason A. Donenfeld int ret; 443e7096c13SJason A. Donenfeld 444e7096c13SJason A. Donenfeld ret = register_pm_notifier(&pm_notifier); 445e7096c13SJason A. Donenfeld if (ret) 446e7096c13SJason A. Donenfeld return ret; 4472d6919c3SJason A. Donenfeld 4482d6919c3SJason A. Donenfeld ret = register_random_vmfork_notifier(&vm_notifier); 4492d6919c3SJason A. Donenfeld if (ret) 4502d6919c3SJason A. Donenfeld goto error_pm; 451e7096c13SJason A. Donenfeld 452900575aaSJason A. Donenfeld ret = register_pernet_device(&pernet_ops); 453e7096c13SJason A. Donenfeld if (ret) 4542d6919c3SJason A. Donenfeld goto error_vm; 455e7096c13SJason A. Donenfeld 456e7096c13SJason A. Donenfeld ret = rtnl_link_register(&link_ops); 457e7096c13SJason A. Donenfeld if (ret) 458900575aaSJason A. Donenfeld goto error_pernet; 459e7096c13SJason A. Donenfeld 460e7096c13SJason A. Donenfeld return 0; 461e7096c13SJason A. Donenfeld 462900575aaSJason A. Donenfeld error_pernet: 463900575aaSJason A. Donenfeld unregister_pernet_device(&pernet_ops); 4642d6919c3SJason A. Donenfeld error_vm: 4652d6919c3SJason A. Donenfeld unregister_random_vmfork_notifier(&vm_notifier); 466e7096c13SJason A. Donenfeld error_pm: 467e7096c13SJason A. Donenfeld unregister_pm_notifier(&pm_notifier); 468e7096c13SJason A. Donenfeld return ret; 469e7096c13SJason A. Donenfeld } 470e7096c13SJason A. Donenfeld 471e7096c13SJason A. Donenfeld void wg_device_uninit(void) 472e7096c13SJason A. Donenfeld { 473e7096c13SJason A. Donenfeld rtnl_link_unregister(&link_ops); 474900575aaSJason A. Donenfeld unregister_pernet_device(&pernet_ops); 4752d6919c3SJason A. Donenfeld unregister_random_vmfork_notifier(&vm_notifier); 476e7096c13SJason A. Donenfeld unregister_pm_notifier(&pm_notifier); 477e7096c13SJason A. Donenfeld rcu_barrier(); 478e7096c13SJason A. Donenfeld } 479