netlink.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2015-2019 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
  4. */
  5. #include "netlink.h"
  6. #include "device.h"
  7. #include "peer.h"
  8. #include "socket.h"
  9. #include "queueing.h"
  10. #include "messages.h"
  11. #include <uapi/linux/wireguard.h>
  12. #include <linux/if.h>
  13. #include <net/genetlink.h>
  14. #include <net/sock.h>
  15. #include <crypto/algapi.h>
  16. static struct genl_family genl_family;
  17. static const struct nla_policy device_policy[WGDEVICE_A_MAX + 1] = {
  18. [WGDEVICE_A_IFINDEX] = { .type = NLA_U32 },
  19. [WGDEVICE_A_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  20. [WGDEVICE_A_PRIVATE_KEY] = NLA_POLICY_EXACT_LEN(NOISE_PUBLIC_KEY_LEN),
  21. [WGDEVICE_A_PUBLIC_KEY] = NLA_POLICY_EXACT_LEN(NOISE_PUBLIC_KEY_LEN),
  22. [WGDEVICE_A_FLAGS] = { .type = NLA_U32 },
  23. [WGDEVICE_A_LISTEN_PORT] = { .type = NLA_U16 },
  24. [WGDEVICE_A_FWMARK] = { .type = NLA_U32 },
  25. [WGDEVICE_A_PEERS] = { .type = NLA_NESTED }
  26. };
  27. static const struct nla_policy peer_policy[WGPEER_A_MAX + 1] = {
  28. [WGPEER_A_PUBLIC_KEY] = NLA_POLICY_EXACT_LEN(NOISE_PUBLIC_KEY_LEN),
  29. [WGPEER_A_PRESHARED_KEY] = NLA_POLICY_EXACT_LEN(NOISE_SYMMETRIC_KEY_LEN),
  30. [WGPEER_A_FLAGS] = { .type = NLA_U32 },
  31. [WGPEER_A_ENDPOINT] = NLA_POLICY_MIN_LEN(sizeof(struct sockaddr)),
  32. [WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL] = { .type = NLA_U16 },
  33. [WGPEER_A_LAST_HANDSHAKE_TIME] = NLA_POLICY_EXACT_LEN(sizeof(struct __kernel_timespec)),
  34. [WGPEER_A_RX_BYTES] = { .type = NLA_U64 },
  35. [WGPEER_A_TX_BYTES] = { .type = NLA_U64 },
  36. [WGPEER_A_ALLOWEDIPS] = { .type = NLA_NESTED },
  37. [WGPEER_A_PROTOCOL_VERSION] = { .type = NLA_U32 }
  38. };
  39. static const struct nla_policy allowedip_policy[WGALLOWEDIP_A_MAX + 1] = {
  40. [WGALLOWEDIP_A_FAMILY] = { .type = NLA_U16 },
  41. [WGALLOWEDIP_A_IPADDR] = NLA_POLICY_MIN_LEN(sizeof(struct in_addr)),
  42. [WGALLOWEDIP_A_CIDR_MASK] = { .type = NLA_U8 }
  43. };
  44. static struct wg_device *lookup_interface(struct nlattr **attrs,
  45. struct sk_buff *skb)
  46. {
  47. struct net_device *dev = NULL;
  48. if (!attrs[WGDEVICE_A_IFINDEX] == !attrs[WGDEVICE_A_IFNAME])
  49. return ERR_PTR(-EBADR);
  50. if (attrs[WGDEVICE_A_IFINDEX])
  51. dev = dev_get_by_index(sock_net(skb->sk),
  52. nla_get_u32(attrs[WGDEVICE_A_IFINDEX]));
  53. else if (attrs[WGDEVICE_A_IFNAME])
  54. dev = dev_get_by_name(sock_net(skb->sk),
  55. nla_data(attrs[WGDEVICE_A_IFNAME]));
  56. if (!dev)
  57. return ERR_PTR(-ENODEV);
  58. if (!dev->rtnl_link_ops || !dev->rtnl_link_ops->kind ||
  59. strcmp(dev->rtnl_link_ops->kind, KBUILD_MODNAME)) {
  60. dev_put(dev);
  61. return ERR_PTR(-EOPNOTSUPP);
  62. }
  63. return netdev_priv(dev);
  64. }
  65. static int get_allowedips(struct sk_buff *skb, const u8 *ip, u8 cidr,
  66. int family)
  67. {
  68. struct nlattr *allowedip_nest;
  69. allowedip_nest = nla_nest_start(skb, 0);
  70. if (!allowedip_nest)
  71. return -EMSGSIZE;
  72. if (nla_put_u8(skb, WGALLOWEDIP_A_CIDR_MASK, cidr) ||
  73. nla_put_u16(skb, WGALLOWEDIP_A_FAMILY, family) ||
  74. nla_put(skb, WGALLOWEDIP_A_IPADDR, family == AF_INET6 ?
  75. sizeof(struct in6_addr) : sizeof(struct in_addr), ip)) {
  76. nla_nest_cancel(skb, allowedip_nest);
  77. return -EMSGSIZE;
  78. }
  79. nla_nest_end(skb, allowedip_nest);
  80. return 0;
  81. }
  82. struct dump_ctx {
  83. struct wg_device *wg;
  84. struct wg_peer *next_peer;
  85. u64 allowedips_seq;
  86. struct allowedips_node *next_allowedip;
  87. };
  88. #define DUMP_CTX(cb) ((struct dump_ctx *)(cb)->args)
  89. static int
  90. get_peer(struct wg_peer *peer, struct sk_buff *skb, struct dump_ctx *ctx)
  91. {
  92. struct nlattr *allowedips_nest, *peer_nest = nla_nest_start(skb, 0);
  93. struct allowedips_node *allowedips_node = ctx->next_allowedip;
  94. bool fail;
  95. if (!peer_nest)
  96. return -EMSGSIZE;
  97. down_read(&peer->handshake.lock);
  98. fail = nla_put(skb, WGPEER_A_PUBLIC_KEY, NOISE_PUBLIC_KEY_LEN,
  99. peer->handshake.remote_static);
  100. up_read(&peer->handshake.lock);
  101. if (fail)
  102. goto err;
  103. if (!allowedips_node) {
  104. const struct __kernel_timespec last_handshake = {
  105. .tv_sec = peer->walltime_last_handshake.tv_sec,
  106. .tv_nsec = peer->walltime_last_handshake.tv_nsec
  107. };
  108. down_read(&peer->handshake.lock);
  109. fail = nla_put(skb, WGPEER_A_PRESHARED_KEY,
  110. NOISE_SYMMETRIC_KEY_LEN,
  111. peer->handshake.preshared_key);
  112. up_read(&peer->handshake.lock);
  113. if (fail)
  114. goto err;
  115. if (nla_put(skb, WGPEER_A_LAST_HANDSHAKE_TIME,
  116. sizeof(last_handshake), &last_handshake) ||
  117. nla_put_u16(skb, WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL,
  118. peer->persistent_keepalive_interval) ||
  119. nla_put_u64_64bit(skb, WGPEER_A_TX_BYTES, peer->tx_bytes,
  120. WGPEER_A_UNSPEC) ||
  121. nla_put_u64_64bit(skb, WGPEER_A_RX_BYTES, peer->rx_bytes,
  122. WGPEER_A_UNSPEC) ||
  123. nla_put_u32(skb, WGPEER_A_PROTOCOL_VERSION, 1))
  124. goto err;
  125. read_lock_bh(&peer->endpoint_lock);
  126. if (peer->endpoint.addr.sa_family == AF_INET)
  127. fail = nla_put(skb, WGPEER_A_ENDPOINT,
  128. sizeof(peer->endpoint.addr4),
  129. &peer->endpoint.addr4);
  130. else if (peer->endpoint.addr.sa_family == AF_INET6)
  131. fail = nla_put(skb, WGPEER_A_ENDPOINT,
  132. sizeof(peer->endpoint.addr6),
  133. &peer->endpoint.addr6);
  134. read_unlock_bh(&peer->endpoint_lock);
  135. if (fail)
  136. goto err;
  137. allowedips_node =
  138. list_first_entry_or_null(&peer->allowedips_list,
  139. struct allowedips_node, peer_list);
  140. }
  141. if (!allowedips_node)
  142. goto no_allowedips;
  143. if (!ctx->allowedips_seq)
  144. ctx->allowedips_seq = peer->device->peer_allowedips.seq;
  145. else if (ctx->allowedips_seq != peer->device->peer_allowedips.seq)
  146. goto no_allowedips;
  147. allowedips_nest = nla_nest_start(skb, WGPEER_A_ALLOWEDIPS);
  148. if (!allowedips_nest)
  149. goto err;
  150. list_for_each_entry_from(allowedips_node, &peer->allowedips_list,
  151. peer_list) {
  152. u8 cidr, ip[16] __aligned(__alignof(u64));
  153. int family;
  154. family = wg_allowedips_read_node(allowedips_node, ip, &cidr);
  155. if (get_allowedips(skb, ip, cidr, family)) {
  156. nla_nest_end(skb, allowedips_nest);
  157. nla_nest_end(skb, peer_nest);
  158. ctx->next_allowedip = allowedips_node;
  159. return -EMSGSIZE;
  160. }
  161. }
  162. nla_nest_end(skb, allowedips_nest);
  163. no_allowedips:
  164. nla_nest_end(skb, peer_nest);
  165. ctx->next_allowedip = NULL;
  166. ctx->allowedips_seq = 0;
  167. return 0;
  168. err:
  169. nla_nest_cancel(skb, peer_nest);
  170. return -EMSGSIZE;
  171. }
  172. static int wg_get_device_start(struct netlink_callback *cb)
  173. {
  174. struct wg_device *wg;
  175. wg = lookup_interface(genl_dumpit_info(cb)->attrs, cb->skb);
  176. if (IS_ERR(wg))
  177. return PTR_ERR(wg);
  178. DUMP_CTX(cb)->wg = wg;
  179. return 0;
  180. }
  181. static int wg_get_device_dump(struct sk_buff *skb, struct netlink_callback *cb)
  182. {
  183. struct wg_peer *peer, *next_peer_cursor;
  184. struct dump_ctx *ctx = DUMP_CTX(cb);
  185. struct wg_device *wg = ctx->wg;
  186. struct nlattr *peers_nest;
  187. int ret = -EMSGSIZE;
  188. bool done = true;
  189. void *hdr;
  190. rtnl_lock();
  191. mutex_lock(&wg->device_update_lock);
  192. cb->seq = wg->device_update_gen;
  193. next_peer_cursor = ctx->next_peer;
  194. hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  195. &genl_family, NLM_F_MULTI, WG_CMD_GET_DEVICE);
  196. if (!hdr)
  197. goto out;
  198. genl_dump_check_consistent(cb, hdr);
  199. if (!ctx->next_peer) {
  200. if (nla_put_u16(skb, WGDEVICE_A_LISTEN_PORT,
  201. wg->incoming_port) ||
  202. nla_put_u32(skb, WGDEVICE_A_FWMARK, wg->fwmark) ||
  203. nla_put_u32(skb, WGDEVICE_A_IFINDEX, wg->dev->ifindex) ||
  204. nla_put_string(skb, WGDEVICE_A_IFNAME, wg->dev->name))
  205. goto out;
  206. down_read(&wg->static_identity.lock);
  207. if (wg->static_identity.has_identity) {
  208. if (nla_put(skb, WGDEVICE_A_PRIVATE_KEY,
  209. NOISE_PUBLIC_KEY_LEN,
  210. wg->static_identity.static_private) ||
  211. nla_put(skb, WGDEVICE_A_PUBLIC_KEY,
  212. NOISE_PUBLIC_KEY_LEN,
  213. wg->static_identity.static_public)) {
  214. up_read(&wg->static_identity.lock);
  215. goto out;
  216. }
  217. }
  218. up_read(&wg->static_identity.lock);
  219. }
  220. peers_nest = nla_nest_start(skb, WGDEVICE_A_PEERS);
  221. if (!peers_nest)
  222. goto out;
  223. ret = 0;
  224. /* If the last cursor was removed via list_del_init in peer_remove, then
  225. * we just treat this the same as there being no more peers left. The
  226. * reason is that seq_nr should indicate to userspace that this isn't a
  227. * coherent dump anyway, so they'll try again.
  228. */
  229. if (list_empty(&wg->peer_list) ||
  230. (ctx->next_peer && list_empty(&ctx->next_peer->peer_list))) {
  231. nla_nest_cancel(skb, peers_nest);
  232. goto out;
  233. }
  234. lockdep_assert_held(&wg->device_update_lock);
  235. peer = list_prepare_entry(ctx->next_peer, &wg->peer_list, peer_list);
  236. list_for_each_entry_continue(peer, &wg->peer_list, peer_list) {
  237. if (get_peer(peer, skb, ctx)) {
  238. done = false;
  239. break;
  240. }
  241. next_peer_cursor = peer;
  242. }
  243. nla_nest_end(skb, peers_nest);
  244. out:
  245. if (!ret && !done && next_peer_cursor)
  246. wg_peer_get(next_peer_cursor);
  247. wg_peer_put(ctx->next_peer);
  248. mutex_unlock(&wg->device_update_lock);
  249. rtnl_unlock();
  250. if (ret) {
  251. genlmsg_cancel(skb, hdr);
  252. return ret;
  253. }
  254. genlmsg_end(skb, hdr);
  255. if (done) {
  256. ctx->next_peer = NULL;
  257. return 0;
  258. }
  259. ctx->next_peer = next_peer_cursor;
  260. return skb->len;
  261. /* At this point, we can't really deal ourselves with safely zeroing out
  262. * the private key material after usage. This will need an additional API
  263. * in the kernel for marking skbs as zero_on_free.
  264. */
  265. }
  266. static int wg_get_device_done(struct netlink_callback *cb)
  267. {
  268. struct dump_ctx *ctx = DUMP_CTX(cb);
  269. if (ctx->wg)
  270. dev_put(ctx->wg->dev);
  271. wg_peer_put(ctx->next_peer);
  272. return 0;
  273. }
  274. static int set_port(struct wg_device *wg, u16 port)
  275. {
  276. struct wg_peer *peer;
  277. if (wg->incoming_port == port)
  278. return 0;
  279. list_for_each_entry(peer, &wg->peer_list, peer_list)
  280. wg_socket_clear_peer_endpoint_src(peer);
  281. if (!netif_running(wg->dev)) {
  282. wg->incoming_port = port;
  283. return 0;
  284. }
  285. return wg_socket_init(wg, port);
  286. }
  287. static int set_allowedip(struct wg_peer *peer, struct nlattr **attrs)
  288. {
  289. int ret = -EINVAL;
  290. u16 family;
  291. u8 cidr;
  292. if (!attrs[WGALLOWEDIP_A_FAMILY] || !attrs[WGALLOWEDIP_A_IPADDR] ||
  293. !attrs[WGALLOWEDIP_A_CIDR_MASK])
  294. return ret;
  295. family = nla_get_u16(attrs[WGALLOWEDIP_A_FAMILY]);
  296. cidr = nla_get_u8(attrs[WGALLOWEDIP_A_CIDR_MASK]);
  297. if (family == AF_INET && cidr <= 32 &&
  298. nla_len(attrs[WGALLOWEDIP_A_IPADDR]) == sizeof(struct in_addr))
  299. ret = wg_allowedips_insert_v4(
  300. &peer->device->peer_allowedips,
  301. nla_data(attrs[WGALLOWEDIP_A_IPADDR]), cidr, peer,
  302. &peer->device->device_update_lock);
  303. else if (family == AF_INET6 && cidr <= 128 &&
  304. nla_len(attrs[WGALLOWEDIP_A_IPADDR]) == sizeof(struct in6_addr))
  305. ret = wg_allowedips_insert_v6(
  306. &peer->device->peer_allowedips,
  307. nla_data(attrs[WGALLOWEDIP_A_IPADDR]), cidr, peer,
  308. &peer->device->device_update_lock);
  309. return ret;
  310. }
  311. static int set_peer(struct wg_device *wg, struct nlattr **attrs)
  312. {
  313. u8 *public_key = NULL, *preshared_key = NULL;
  314. struct wg_peer *peer = NULL;
  315. u32 flags = 0;
  316. int ret;
  317. ret = -EINVAL;
  318. if (attrs[WGPEER_A_PUBLIC_KEY] &&
  319. nla_len(attrs[WGPEER_A_PUBLIC_KEY]) == NOISE_PUBLIC_KEY_LEN)
  320. public_key = nla_data(attrs[WGPEER_A_PUBLIC_KEY]);
  321. else
  322. goto out;
  323. if (attrs[WGPEER_A_PRESHARED_KEY] &&
  324. nla_len(attrs[WGPEER_A_PRESHARED_KEY]) == NOISE_SYMMETRIC_KEY_LEN)
  325. preshared_key = nla_data(attrs[WGPEER_A_PRESHARED_KEY]);
  326. if (attrs[WGPEER_A_FLAGS])
  327. flags = nla_get_u32(attrs[WGPEER_A_FLAGS]);
  328. ret = -EOPNOTSUPP;
  329. if (flags & ~__WGPEER_F_ALL)
  330. goto out;
  331. ret = -EPFNOSUPPORT;
  332. if (attrs[WGPEER_A_PROTOCOL_VERSION]) {
  333. if (nla_get_u32(attrs[WGPEER_A_PROTOCOL_VERSION]) != 1)
  334. goto out;
  335. }
  336. peer = wg_pubkey_hashtable_lookup(wg->peer_hashtable,
  337. nla_data(attrs[WGPEER_A_PUBLIC_KEY]));
  338. ret = 0;
  339. if (!peer) { /* Peer doesn't exist yet. Add a new one. */
  340. if (flags & (WGPEER_F_REMOVE_ME | WGPEER_F_UPDATE_ONLY))
  341. goto out;
  342. /* The peer is new, so there aren't allowed IPs to remove. */
  343. flags &= ~WGPEER_F_REPLACE_ALLOWEDIPS;
  344. down_read(&wg->static_identity.lock);
  345. if (wg->static_identity.has_identity &&
  346. !memcmp(nla_data(attrs[WGPEER_A_PUBLIC_KEY]),
  347. wg->static_identity.static_public,
  348. NOISE_PUBLIC_KEY_LEN)) {
  349. /* We silently ignore peers that have the same public
  350. * key as the device. The reason we do it silently is
  351. * that we'd like for people to be able to reuse the
  352. * same set of API calls across peers.
  353. */
  354. up_read(&wg->static_identity.lock);
  355. ret = 0;
  356. goto out;
  357. }
  358. up_read(&wg->static_identity.lock);
  359. peer = wg_peer_create(wg, public_key, preshared_key);
  360. if (IS_ERR(peer)) {
  361. ret = PTR_ERR(peer);
  362. peer = NULL;
  363. goto out;
  364. }
  365. /* Take additional reference, as though we've just been
  366. * looked up.
  367. */
  368. wg_peer_get(peer);
  369. }
  370. if (flags & WGPEER_F_REMOVE_ME) {
  371. wg_peer_remove(peer);
  372. goto out;
  373. }
  374. if (preshared_key) {
  375. down_write(&peer->handshake.lock);
  376. memcpy(&peer->handshake.preshared_key, preshared_key,
  377. NOISE_SYMMETRIC_KEY_LEN);
  378. up_write(&peer->handshake.lock);
  379. }
  380. if (attrs[WGPEER_A_ENDPOINT]) {
  381. struct sockaddr *addr = nla_data(attrs[WGPEER_A_ENDPOINT]);
  382. size_t len = nla_len(attrs[WGPEER_A_ENDPOINT]);
  383. struct endpoint endpoint = { { { 0 } } };
  384. if (len == sizeof(struct sockaddr_in) && addr->sa_family == AF_INET) {
  385. endpoint.addr4 = *(struct sockaddr_in *)addr;
  386. wg_socket_set_peer_endpoint(peer, &endpoint);
  387. } else if (len == sizeof(struct sockaddr_in6) && addr->sa_family == AF_INET6) {
  388. endpoint.addr6 = *(struct sockaddr_in6 *)addr;
  389. wg_socket_set_peer_endpoint(peer, &endpoint);
  390. }
  391. }
  392. if (flags & WGPEER_F_REPLACE_ALLOWEDIPS)
  393. wg_allowedips_remove_by_peer(&wg->peer_allowedips, peer,
  394. &wg->device_update_lock);
  395. if (attrs[WGPEER_A_ALLOWEDIPS]) {
  396. struct nlattr *attr, *allowedip[WGALLOWEDIP_A_MAX + 1];
  397. int rem;
  398. nla_for_each_nested(attr, attrs[WGPEER_A_ALLOWEDIPS], rem) {
  399. ret = nla_parse_nested(allowedip, WGALLOWEDIP_A_MAX,
  400. attr, allowedip_policy, NULL);
  401. if (ret < 0)
  402. goto out;
  403. ret = set_allowedip(peer, allowedip);
  404. if (ret < 0)
  405. goto out;
  406. }
  407. }
  408. if (attrs[WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL]) {
  409. const u16 persistent_keepalive_interval = nla_get_u16(
  410. attrs[WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL]);
  411. const bool send_keepalive =
  412. !peer->persistent_keepalive_interval &&
  413. persistent_keepalive_interval &&
  414. netif_running(wg->dev);
  415. peer->persistent_keepalive_interval = persistent_keepalive_interval;
  416. if (send_keepalive)
  417. wg_packet_send_keepalive(peer);
  418. }
  419. if (netif_running(wg->dev))
  420. wg_packet_send_staged_packets(peer);
  421. out:
  422. wg_peer_put(peer);
  423. if (attrs[WGPEER_A_PRESHARED_KEY])
  424. memzero_explicit(nla_data(attrs[WGPEER_A_PRESHARED_KEY]),
  425. nla_len(attrs[WGPEER_A_PRESHARED_KEY]));
  426. return ret;
  427. }
  428. static int wg_set_device(struct sk_buff *skb, struct genl_info *info)
  429. {
  430. struct wg_device *wg = lookup_interface(info->attrs, skb);
  431. u32 flags = 0;
  432. int ret;
  433. if (IS_ERR(wg)) {
  434. ret = PTR_ERR(wg);
  435. goto out_nodev;
  436. }
  437. rtnl_lock();
  438. mutex_lock(&wg->device_update_lock);
  439. if (info->attrs[WGDEVICE_A_FLAGS])
  440. flags = nla_get_u32(info->attrs[WGDEVICE_A_FLAGS]);
  441. ret = -EOPNOTSUPP;
  442. if (flags & ~__WGDEVICE_F_ALL)
  443. goto out;
  444. if (info->attrs[WGDEVICE_A_LISTEN_PORT] || info->attrs[WGDEVICE_A_FWMARK]) {
  445. struct net *net;
  446. rcu_read_lock();
  447. net = rcu_dereference(wg->creating_net);
  448. ret = !net || !ns_capable(net->user_ns, CAP_NET_ADMIN) ? -EPERM : 0;
  449. rcu_read_unlock();
  450. if (ret)
  451. goto out;
  452. }
  453. ++wg->device_update_gen;
  454. if (info->attrs[WGDEVICE_A_FWMARK]) {
  455. struct wg_peer *peer;
  456. wg->fwmark = nla_get_u32(info->attrs[WGDEVICE_A_FWMARK]);
  457. list_for_each_entry(peer, &wg->peer_list, peer_list)
  458. wg_socket_clear_peer_endpoint_src(peer);
  459. }
  460. if (info->attrs[WGDEVICE_A_LISTEN_PORT]) {
  461. ret = set_port(wg,
  462. nla_get_u16(info->attrs[WGDEVICE_A_LISTEN_PORT]));
  463. if (ret)
  464. goto out;
  465. }
  466. if (flags & WGDEVICE_F_REPLACE_PEERS)
  467. wg_peer_remove_all(wg);
  468. if (info->attrs[WGDEVICE_A_PRIVATE_KEY] &&
  469. nla_len(info->attrs[WGDEVICE_A_PRIVATE_KEY]) ==
  470. NOISE_PUBLIC_KEY_LEN) {
  471. u8 *private_key = nla_data(info->attrs[WGDEVICE_A_PRIVATE_KEY]);
  472. u8 public_key[NOISE_PUBLIC_KEY_LEN];
  473. struct wg_peer *peer, *temp;
  474. bool send_staged_packets;
  475. if (!crypto_memneq(wg->static_identity.static_private,
  476. private_key, NOISE_PUBLIC_KEY_LEN))
  477. goto skip_set_private_key;
  478. /* We remove before setting, to prevent race, which means doing
  479. * two 25519-genpub ops.
  480. */
  481. if (curve25519_generate_public(public_key, private_key)) {
  482. peer = wg_pubkey_hashtable_lookup(wg->peer_hashtable,
  483. public_key);
  484. if (peer) {
  485. wg_peer_put(peer);
  486. wg_peer_remove(peer);
  487. }
  488. }
  489. down_write(&wg->static_identity.lock);
  490. send_staged_packets = !wg->static_identity.has_identity && netif_running(wg->dev);
  491. wg_noise_set_static_identity_private_key(&wg->static_identity, private_key);
  492. send_staged_packets = send_staged_packets && wg->static_identity.has_identity;
  493. wg_cookie_checker_precompute_device_keys(&wg->cookie_checker);
  494. list_for_each_entry_safe(peer, temp, &wg->peer_list, peer_list) {
  495. wg_noise_precompute_static_static(peer);
  496. wg_noise_expire_current_peer_keypairs(peer);
  497. if (send_staged_packets)
  498. wg_packet_send_staged_packets(peer);
  499. }
  500. up_write(&wg->static_identity.lock);
  501. }
  502. skip_set_private_key:
  503. if (info->attrs[WGDEVICE_A_PEERS]) {
  504. struct nlattr *attr, *peer[WGPEER_A_MAX + 1];
  505. int rem;
  506. nla_for_each_nested(attr, info->attrs[WGDEVICE_A_PEERS], rem) {
  507. ret = nla_parse_nested(peer, WGPEER_A_MAX, attr,
  508. peer_policy, NULL);
  509. if (ret < 0)
  510. goto out;
  511. ret = set_peer(wg, peer);
  512. if (ret < 0)
  513. goto out;
  514. }
  515. }
  516. ret = 0;
  517. out:
  518. mutex_unlock(&wg->device_update_lock);
  519. rtnl_unlock();
  520. dev_put(wg->dev);
  521. out_nodev:
  522. if (info->attrs[WGDEVICE_A_PRIVATE_KEY])
  523. memzero_explicit(nla_data(info->attrs[WGDEVICE_A_PRIVATE_KEY]),
  524. nla_len(info->attrs[WGDEVICE_A_PRIVATE_KEY]));
  525. return ret;
  526. }
  527. static const struct genl_ops genl_ops[] = {
  528. {
  529. .cmd = WG_CMD_GET_DEVICE,
  530. .start = wg_get_device_start,
  531. .dumpit = wg_get_device_dump,
  532. .done = wg_get_device_done,
  533. .flags = GENL_UNS_ADMIN_PERM
  534. }, {
  535. .cmd = WG_CMD_SET_DEVICE,
  536. .doit = wg_set_device,
  537. .flags = GENL_UNS_ADMIN_PERM
  538. }
  539. };
  540. static struct genl_family genl_family __ro_after_init = {
  541. .ops = genl_ops,
  542. .n_ops = ARRAY_SIZE(genl_ops),
  543. .resv_start_op = WG_CMD_SET_DEVICE + 1,
  544. .name = WG_GENL_NAME,
  545. .version = WG_GENL_VERSION,
  546. .maxattr = WGDEVICE_A_MAX,
  547. .module = THIS_MODULE,
  548. .policy = device_policy,
  549. .netnsok = true
  550. };
  551. int __init wg_genetlink_init(void)
  552. {
  553. return genl_register_family(&genl_family);
  554. }
  555. void __exit wg_genetlink_uninit(void)
  556. {
  557. genl_unregister_family(&genl_family);
  558. }