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