device.c 13 KB

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