NCDIfConfig.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /**
  2. * @file NCDIfConfig.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <inttypes.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <unistd.h>
  27. #include <sys/socket.h>
  28. #include <net/if.h>
  29. #include <net/if_arp.h>
  30. #include <sys/ioctl.h>
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #include <fcntl.h>
  34. #include <linux/if_tun.h>
  35. #include <pwd.h>
  36. #include <misc/debug.h>
  37. #include <base/BLog.h>
  38. #include <ncd/NCDIfConfig.h>
  39. #include <generated/blog_channel_NCDIfConfig.h>
  40. #define IP_CMD "ip"
  41. #define RESOLVCONF_FILE "/etc/resolv.conf"
  42. #define RESOLVCONF_TEMP_FILE "/etc/resolv.conf-ncd-temp"
  43. #define TUN_DEVNODE "/dev/net/tun"
  44. static int run_command (const char *cmd)
  45. {
  46. BLog(BLOG_INFO, "run: %s", cmd);
  47. return system(cmd);
  48. }
  49. static int write_to_file (uint8_t *data, size_t data_len, FILE *f)
  50. {
  51. while (data_len > 0) {
  52. size_t bytes = fwrite(data, 1, data_len, f);
  53. if (bytes == 0) {
  54. return 0;
  55. }
  56. data += bytes;
  57. data_len -= bytes;
  58. }
  59. return 1;
  60. }
  61. int NCDIfConfig_query (const char *ifname)
  62. {
  63. struct ifreq ifr;
  64. int flags = 0;
  65. int s = socket(AF_INET, SOCK_DGRAM, 0);
  66. if (!s) {
  67. BLog(BLOG_ERROR, "socket failed");
  68. goto fail0;
  69. }
  70. memset(&ifr, 0, sizeof(ifr));
  71. snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", ifname);
  72. if (ioctl(s, SIOCGIFFLAGS, &ifr)) {
  73. BLog(BLOG_ERROR, "SIOCGIFFLAGS failed");
  74. goto fail1;
  75. }
  76. flags |= NCDIFCONFIG_FLAG_EXISTS;
  77. if ((ifr.ifr_flags&IFF_UP)) {
  78. flags |= NCDIFCONFIG_FLAG_UP;
  79. if ((ifr.ifr_flags&IFF_RUNNING)) {
  80. flags |= NCDIFCONFIG_FLAG_RUNNING;
  81. }
  82. }
  83. fail1:
  84. close(s);
  85. fail0:
  86. return flags;
  87. }
  88. int NCDIfConfig_set_up (const char *ifname)
  89. {
  90. if (strlen(ifname) >= IFNAMSIZ) {
  91. BLog(BLOG_ERROR, "ifname too long");
  92. return 0;
  93. }
  94. char cmd[50 + IFNAMSIZ];
  95. sprintf(cmd, IP_CMD" link set %s up", ifname);
  96. return !run_command(cmd);
  97. }
  98. int NCDIfConfig_set_down (const char *ifname)
  99. {
  100. if (strlen(ifname) >= IFNAMSIZ) {
  101. BLog(BLOG_ERROR, "ifname too long");
  102. return 0;
  103. }
  104. char cmd[50 + IFNAMSIZ];
  105. sprintf(cmd, IP_CMD" link set %s down", ifname);
  106. return !run_command(cmd);
  107. }
  108. int NCDIfConfig_add_ipv4_addr (const char *ifname, struct ipv4_ifaddr ifaddr)
  109. {
  110. ASSERT(ifaddr.prefix >= 0)
  111. ASSERT(ifaddr.prefix <= 32)
  112. if (strlen(ifname) >= IFNAMSIZ) {
  113. BLog(BLOG_ERROR, "ifname too long");
  114. return 0;
  115. }
  116. uint8_t *addr = (uint8_t *)&ifaddr.addr;
  117. char cmd[50 + IFNAMSIZ];
  118. sprintf(cmd, IP_CMD" addr add %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"/%d dev %s", addr[0], addr[1], addr[2], addr[3], ifaddr.prefix, ifname);
  119. return !run_command(cmd);
  120. }
  121. int NCDIfConfig_remove_ipv4_addr (const char *ifname, struct ipv4_ifaddr ifaddr)
  122. {
  123. ASSERT(ifaddr.prefix >= 0)
  124. ASSERT(ifaddr.prefix <= 32)
  125. if (strlen(ifname) >= IFNAMSIZ) {
  126. BLog(BLOG_ERROR, "ifname too long");
  127. return 0;
  128. }
  129. uint8_t *addr = (uint8_t *)&ifaddr.addr;
  130. char cmd[50 + IFNAMSIZ];
  131. sprintf(cmd, IP_CMD" addr del %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"/%d dev %s", addr[0], addr[1], addr[2], addr[3], ifaddr.prefix, ifname);
  132. return !run_command(cmd);
  133. }
  134. static int route_cmd (const char *cmdtype, struct ipv4_ifaddr dest, const uint32_t *gateway, int metric, const char *ifname)
  135. {
  136. ASSERT(!strcmp(cmdtype, "add") || !strcmp(cmdtype, "del"))
  137. ASSERT(dest.prefix >= 0)
  138. ASSERT(dest.prefix <= 32)
  139. if (strlen(ifname) >= IFNAMSIZ) {
  140. BLog(BLOG_ERROR, "ifname too long");
  141. return 0;
  142. }
  143. uint8_t *d_addr = (uint8_t *)&dest.addr;
  144. char gwstr[30];
  145. if (gateway) {
  146. const uint8_t *g_addr = (uint8_t *)gateway;
  147. sprintf(gwstr, " via %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8, g_addr[0], g_addr[1], g_addr[2], g_addr[3]);
  148. } else {
  149. gwstr[0] = '\0';
  150. }
  151. char cmd[120 + IFNAMSIZ];
  152. sprintf(cmd, IP_CMD" route %s %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"/%d%s metric %d dev %s",
  153. cmdtype, d_addr[0], d_addr[1], d_addr[2], d_addr[3], dest.prefix, gwstr, metric, ifname);
  154. return !run_command(cmd);
  155. }
  156. int NCDIfConfig_add_ipv4_route (struct ipv4_ifaddr dest, const uint32_t *gateway, int metric, const char *device)
  157. {
  158. return route_cmd("add", dest, gateway, metric, device);
  159. }
  160. int NCDIfConfig_remove_ipv4_route (struct ipv4_ifaddr dest, const uint32_t *gateway, int metric, const char *device)
  161. {
  162. return route_cmd("del", dest, gateway, metric, device);
  163. }
  164. static int blackhole_route_cmd (const char *cmdtype, struct ipv4_ifaddr dest, int metric)
  165. {
  166. ASSERT(!strcmp(cmdtype, "add") || !strcmp(cmdtype, "del"))
  167. ASSERT(dest.prefix >= 0)
  168. ASSERT(dest.prefix <= 32)
  169. uint8_t *d_addr = (uint8_t *)&dest.addr;
  170. char cmd[120];
  171. sprintf(cmd, IP_CMD" route %s blackhole %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"/%d metric %d",
  172. cmdtype, d_addr[0], d_addr[1], d_addr[2], d_addr[3], dest.prefix, metric);
  173. return !run_command(cmd);
  174. }
  175. int NCDIfConfig_add_ipv4_blackhole_route (struct ipv4_ifaddr dest, int metric)
  176. {
  177. return blackhole_route_cmd("add", dest, metric);
  178. }
  179. int NCDIfConfig_remove_ipv4_blackhole_route (struct ipv4_ifaddr dest, int metric)
  180. {
  181. return blackhole_route_cmd("del", dest, metric);
  182. }
  183. int NCDIfConfig_set_dns_servers (uint32_t *servers, size_t num_servers)
  184. {
  185. FILE *temp_file = fopen(RESOLVCONF_TEMP_FILE, "w");
  186. if (!temp_file) {
  187. BLog(BLOG_ERROR, "failed to open resolvconf temp file");
  188. goto fail0;
  189. }
  190. char line[60];
  191. sprintf(line, "# generated by badvpn-ncd\n");
  192. if (!write_to_file((uint8_t *)line, strlen(line), temp_file)) {
  193. BLog(BLOG_ERROR, "failed to write to resolvconf temp file");
  194. goto fail1;
  195. }
  196. for (size_t i = 0; i < num_servers; i++) {
  197. uint8_t *addr = (uint8_t *)&servers[i];
  198. sprintf(line, "nameserver %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"\n",
  199. addr[0], addr[1], addr[2], addr[3]);
  200. if (!write_to_file((uint8_t *)line, strlen(line), temp_file)) {
  201. BLog(BLOG_ERROR, "failed to write to resolvconf temp file");
  202. goto fail1;
  203. }
  204. }
  205. if (fclose(temp_file) != 0) {
  206. BLog(BLOG_ERROR, "failed to close resolvconf temp file");
  207. return 0;
  208. }
  209. if (rename(RESOLVCONF_TEMP_FILE, RESOLVCONF_FILE) < 0) {
  210. BLog(BLOG_ERROR, "failed to rename resolvconf temp file to resolvconf file");
  211. return 0;
  212. }
  213. return 1;
  214. fail1:
  215. fclose(temp_file);
  216. fail0:
  217. return 0;
  218. }
  219. static int open_tuntap (const char *ifname, int flags)
  220. {
  221. if (strlen(ifname) >= IFNAMSIZ) {
  222. BLog(BLOG_ERROR, "ifname too long");
  223. return -1;
  224. }
  225. int fd = open(TUN_DEVNODE, O_RDWR);
  226. if (fd < 0) {
  227. BLog(BLOG_ERROR, "open tun failed");
  228. return -1;
  229. }
  230. struct ifreq ifr;
  231. memset(&ifr, 0, sizeof(ifr));
  232. ifr.ifr_flags = flags;
  233. snprintf(ifr.ifr_name, IFNAMSIZ, "%s", ifname);
  234. if (ioctl(fd, TUNSETIFF, (void *)&ifr) < 0) {
  235. BLog(BLOG_ERROR, "TUNSETIFF failed");
  236. close(fd);
  237. return -1;
  238. }
  239. return fd;
  240. }
  241. int NCDIfConfig_make_tuntap (const char *ifname, const char *owner, int tun)
  242. {
  243. // load tun module if needed
  244. if (access(TUN_DEVNODE, F_OK) < 0) {
  245. if (run_command("/sbin/modprobe tun") != 0) {
  246. BLog(BLOG_ERROR, "modprobe tun failed");
  247. }
  248. }
  249. int fd;
  250. if ((fd = open_tuntap(ifname, (tun ? IFF_TUN : IFF_TAP))) < 0) {
  251. goto fail0;
  252. }
  253. if (owner) {
  254. long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
  255. if (bufsize < 0) {
  256. bufsize = 16384;
  257. }
  258. char *buf = malloc(bufsize);
  259. if (!buf) {
  260. BLog(BLOG_ERROR, "malloc failed");
  261. goto fail1;
  262. }
  263. struct passwd pwd;
  264. struct passwd *res;
  265. getpwnam_r(owner, &pwd, buf, bufsize, &res);
  266. if (!res) {
  267. BLog(BLOG_ERROR, "getpwnam_r failed");
  268. free(buf);
  269. goto fail1;
  270. }
  271. int uid = pwd.pw_uid;
  272. free(buf);
  273. if (ioctl(fd, TUNSETOWNER, uid) < 0) {
  274. BLog(BLOG_ERROR, "TUNSETOWNER failed");
  275. goto fail1;
  276. }
  277. }
  278. if (ioctl(fd, TUNSETPERSIST, (void *)1) < 0) {
  279. BLog(BLOG_ERROR, "TUNSETPERSIST failed");
  280. goto fail1;
  281. }
  282. close(fd);
  283. return 1;
  284. fail1:
  285. close(fd);
  286. fail0:
  287. return 0;
  288. }
  289. int NCDIfConfig_remove_tuntap (const char *ifname, int tun)
  290. {
  291. int fd;
  292. if ((fd = open_tuntap(ifname, (tun ? IFF_TUN : IFF_TAP))) < 0) {
  293. goto fail0;
  294. }
  295. if (ioctl(fd, TUNSETPERSIST, (void *)0) < 0) {
  296. BLog(BLOG_ERROR, "TUNSETPERSIST failed");
  297. goto fail1;
  298. }
  299. close(fd);
  300. return 1;
  301. fail1:
  302. close(fd);
  303. fail0:
  304. return 0;
  305. }