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