2
0

wg-linux.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2022 Felix Fietkau <nbd@nbd.name>
  4. *
  5. * Based on wireguard-tools:
  6. * Copyright (C) 2015-2020 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
  7. */
  8. #define _GNU_SOURCE
  9. #include <stdbool.h>
  10. #include <stddef.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <dirent.h>
  15. #include <errno.h>
  16. #include <time.h>
  17. #include <sys/types.h>
  18. #include <sys/socket.h>
  19. #include <sys/stat.h>
  20. #include <sys/un.h>
  21. #include <arpa/inet.h>
  22. #include <linux/rtnetlink.h>
  23. #include <netlink/msg.h>
  24. #include <netlink/attr.h>
  25. #include <netlink/socket.h>
  26. #include <unl.h>
  27. #include "linux/wireguard.h"
  28. #include "unetd.h"
  29. struct timespec64 {
  30. int64_t tv_sec;
  31. int64_t tv_nsec;
  32. };
  33. struct wg_linux_peer_req {
  34. struct nl_msg *msg;
  35. struct nlattr *peers, *entry, *ips;
  36. };
  37. static struct unl unl;
  38. static int
  39. wg_nl_init(void)
  40. {
  41. int ret;
  42. if (unl.sock)
  43. return 0;
  44. ret = unl_genl_init(&unl, "wireguard");
  45. if (ret)
  46. return ret;
  47. nl_socket_set_buffer_size(unl.sock, 32768, 32768);
  48. nlmsg_set_default_size(32768);
  49. return 0;
  50. }
  51. static struct nl_msg *
  52. wg_genl_msg(struct network *net, bool set)
  53. {
  54. struct nl_msg *msg;
  55. msg = unl_genl_msg(&unl, set ? WG_CMD_SET_DEVICE : WG_CMD_GET_DEVICE, !set);
  56. nla_put_string(msg, WGDEVICE_A_IFNAME, network_name(net));
  57. return msg;
  58. }
  59. static int
  60. wg_genl_call(struct nl_msg *msg)
  61. {
  62. return unl_request(&unl, msg, NULL, NULL);
  63. }
  64. static int
  65. __wg_linux_init(struct network *net, void *key)
  66. {
  67. struct nl_msg *msg;
  68. msg = wg_genl_msg(net, true);
  69. nla_put(msg, WGDEVICE_A_PRIVATE_KEY, WG_KEY_LEN, key);
  70. nla_put_u32(msg, WGDEVICE_A_FLAGS, WGDEVICE_F_REPLACE_PEERS);
  71. return wg_genl_call(msg);
  72. }
  73. static void
  74. wg_linux_cleanup(struct network *net)
  75. {
  76. uint8_t key[WG_KEY_LEN] = {};
  77. __wg_linux_init(net, key);
  78. }
  79. static int
  80. wg_linux_init(struct network *net)
  81. {
  82. if (wg_nl_init())
  83. return -1;
  84. return __wg_linux_init(net, net->config.key);
  85. }
  86. static int
  87. wg_linux_init_local(struct network *net, struct network_peer *peer)
  88. {
  89. struct nl_msg *msg;
  90. msg = wg_genl_msg(net, true);
  91. nla_put_u16(msg, WGDEVICE_A_LISTEN_PORT, peer ? peer->port : 0);
  92. return wg_genl_call(msg);
  93. }
  94. static void
  95. wg_linux_msg_add_ip(struct nl_msg *msg, int af, void *addr, int mask)
  96. {
  97. struct nlattr *ip;
  98. int len;
  99. if (af == AF_INET6)
  100. len = sizeof(struct in6_addr);
  101. else
  102. len = sizeof(struct in_addr);
  103. ip = nla_nest_start(msg, 0);
  104. nla_put_u16(msg, WGALLOWEDIP_A_FAMILY, af);
  105. nla_put(msg, WGALLOWEDIP_A_IPADDR, len, addr);
  106. nla_put_u8(msg, WGALLOWEDIP_A_CIDR_MASK, mask);
  107. nla_nest_end(msg, ip);
  108. }
  109. static struct nl_msg *
  110. wg_linux_peer_req_init(struct network *net, struct network_peer *peer,
  111. struct wg_linux_peer_req *req)
  112. {
  113. req->msg = wg_genl_msg(net, true);
  114. req->peers = nla_nest_start(req->msg, WGDEVICE_A_PEERS);
  115. req->entry = nla_nest_start(req->msg, 0);
  116. nla_put(req->msg, WGPEER_A_PUBLIC_KEY, WG_KEY_LEN, peer->key);
  117. return req->msg;
  118. }
  119. static int
  120. wg_linux_peer_req_done(struct wg_linux_peer_req *req)
  121. {
  122. nla_nest_end(req->msg, req->entry);
  123. nla_nest_end(req->msg, req->peers);
  124. return wg_genl_call(req->msg);
  125. }
  126. static struct nl_msg *
  127. wg_linux_peer_msg_size_check(struct wg_linux_peer_req *req, struct network *net,
  128. struct network_peer *peer)
  129. {
  130. if (nlmsg_get_max_size(req->msg) >
  131. nlmsg_total_size(nlmsg_hdr(req->msg)->nlmsg_len) + 256)
  132. return req->msg;
  133. nla_nest_end(req->msg, req->ips);
  134. wg_linux_peer_req_done(req);
  135. wg_linux_peer_req_init(net, peer, req);
  136. req->ips = nla_nest_start(req->msg, WGPEER_A_ALLOWEDIPS);
  137. return req->msg;
  138. }
  139. static void
  140. wg_linux_peer_msg_add_allowed_ip(struct wg_linux_peer_req *req, struct network *net,
  141. struct network_peer *peer)
  142. {
  143. struct nl_msg *msg = req->msg;
  144. struct blob_attr *cur;
  145. int rem;
  146. wg_linux_msg_add_ip(msg, AF_INET6, &peer->local_addr.in6, 128);
  147. msg = wg_linux_peer_msg_size_check(req, net, peer);
  148. blobmsg_for_each_attr(cur, peer->ipaddr, rem) {
  149. const char *str = blobmsg_get_string(cur);
  150. struct in6_addr in6;
  151. int af, mask;
  152. if (strchr(str, ':')) {
  153. af = AF_INET6;
  154. mask = 128;
  155. } else {
  156. af = AF_INET;
  157. mask = 32;
  158. }
  159. if (inet_pton(af, str, &in6) != 1)
  160. continue;
  161. wg_linux_msg_add_ip(msg, af, &in6, mask);
  162. msg = wg_linux_peer_msg_size_check(req, net, peer);
  163. }
  164. blobmsg_for_each_attr(cur, peer->subnet, rem) {
  165. const char *str = blobmsg_get_string(cur);
  166. union network_addr addr;
  167. int mask;
  168. int af;
  169. af = strchr(str, ':') ? AF_INET6 : AF_INET;
  170. if (network_get_subnet(af, &addr, &mask, str))
  171. continue;
  172. wg_linux_msg_add_ip(msg, af, &addr, mask);
  173. msg = wg_linux_peer_msg_size_check(req, net, peer);
  174. }
  175. }
  176. static int
  177. wg_linux_peer_update(struct network *net, struct network_peer *peer, enum wg_update_cmd cmd)
  178. {
  179. struct wg_linux_peer_req req;
  180. struct network_host *host;
  181. wg_linux_peer_req_init(net, peer, &req);
  182. if (cmd == WG_PEER_DELETE) {
  183. nla_put_u32(req.msg, WGPEER_A_FLAGS, WGPEER_F_REMOVE_ME);
  184. goto out;
  185. }
  186. nla_put_u32(req.msg, WGPEER_A_FLAGS, WGPEER_F_REPLACE_ALLOWEDIPS);
  187. req.ips = nla_nest_start(req.msg, WGPEER_A_ALLOWEDIPS);
  188. wg_linux_peer_msg_add_allowed_ip(&req, net, peer);
  189. for_each_routed_host(host, net, peer)
  190. wg_linux_peer_msg_add_allowed_ip(&req, net, &host->peer);
  191. nla_nest_end(req.msg, req.ips);
  192. out:
  193. return wg_linux_peer_req_done(&req);
  194. }
  195. static void
  196. wg_linux_parse_peer(struct network *net, struct nlattr *data, time_t now)
  197. {
  198. struct network_peer *peer = NULL;
  199. struct nlattr *tb[__WGPEER_A_LAST];
  200. struct nlattr *cur;
  201. nla_parse_nested(tb, WGPEER_A_MAX, data, NULL);
  202. cur = tb[WGPEER_A_PUBLIC_KEY];
  203. if (!cur)
  204. return;
  205. peer = wg_peer_update_start(net, nla_data(cur));
  206. if (!peer)
  207. return;
  208. if ((cur = tb[WGPEER_A_LAST_HANDSHAKE_TIME]) != NULL) {
  209. struct timespec64 *tv = nla_data(cur);
  210. wg_peer_set_last_handshake(net, peer, now, tv->tv_sec);
  211. }
  212. if ((cur = tb[WGPEER_A_RX_BYTES]) != NULL)
  213. wg_peer_set_rx_bytes(net, peer, nla_get_u64(cur));
  214. if ((cur = tb[WGPEER_A_ENDPOINT]) != NULL)
  215. wg_peer_set_endpoint(net, peer, nla_data(cur), nla_len(cur));
  216. wg_peer_update_done(net, peer);
  217. }
  218. static void
  219. wg_linux_parse_peer_list(struct network *net, struct nlattr *data, time_t now)
  220. {
  221. struct nlattr *cur;
  222. int rem;
  223. if (!data)
  224. return;
  225. nla_for_each_nested(cur, data, rem)
  226. wg_linux_parse_peer(net, cur, now);
  227. }
  228. static int
  229. wg_linux_get_cb(struct nl_msg *msg, void *arg)
  230. {
  231. struct nlmsghdr *nh = nlmsg_hdr(msg);
  232. struct network *net = arg;
  233. struct nlattr *tb[__WGDEVICE_A_LAST];
  234. time_t now = time(NULL);
  235. nlmsg_parse(nh, sizeof(struct genlmsghdr), tb, __WGDEVICE_A_LAST, NULL);
  236. wg_linux_parse_peer_list(net, tb[WGDEVICE_A_PEERS], now);
  237. return NL_SKIP;
  238. }
  239. static int
  240. wg_linux_peer_refresh(struct network *net)
  241. {
  242. struct nl_msg *msg = wg_genl_msg(net, false);
  243. return unl_request(&unl, msg, wg_linux_get_cb, net);
  244. }
  245. static int
  246. wg_linux_peer_connect(struct network *net, struct network_peer *peer,
  247. union network_endpoint *ep)
  248. {
  249. struct wg_linux_peer_req req;
  250. struct nl_msg *msg;
  251. int len;
  252. msg = wg_linux_peer_req_init(net, peer, &req);
  253. if (net->net_config.keepalive) {
  254. nla_put_u16(msg, WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL, 0);
  255. wg_linux_peer_req_done(&req);
  256. msg = wg_linux_peer_req_init(net, peer, &req);
  257. nla_put_u16(msg, WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL,
  258. net->net_config.keepalive);
  259. }
  260. if (ep->in.sin_family == AF_INET6)
  261. len = sizeof(ep->in6);
  262. else
  263. len = sizeof(ep->in);
  264. nla_put(msg, WGPEER_A_ENDPOINT, len, &ep->in6);
  265. return wg_linux_peer_req_done(&req);
  266. }
  267. const struct wg_ops wg_linux_ops = {
  268. .name = "user",
  269. .init = wg_linux_init,
  270. .cleanup = wg_linux_cleanup,
  271. .init_local = wg_linux_init_local,
  272. .peer_update = wg_linux_peer_update,
  273. .peer_refresh = wg_linux_peer_refresh,
  274. .peer_connect = wg_linux_peer_connect,
  275. };