device.c 13 KB

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