NCDIfConfig.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 *device)
  135. {
  136. ASSERT(dest.prefix >= 0)
  137. ASSERT(dest.prefix <= 32)
  138. uint8_t *d_addr = (uint8_t *)&dest.addr;
  139. char gwstr[60];
  140. if (gateway) {
  141. const uint8_t *g_addr = (uint8_t *)gateway;
  142. sprintf(gwstr, " via %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8, g_addr[0], g_addr[1], g_addr[2], g_addr[3]);
  143. } else {
  144. gwstr[0] = '\0';
  145. }
  146. char cmd[100];
  147. sprintf(cmd, IP_CMD" route %s %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"/%d%s metric %d dev %s",
  148. cmdtype, d_addr[0], d_addr[1], d_addr[2], d_addr[3], dest.prefix, gwstr, metric, device);
  149. return !run_command(cmd);
  150. }
  151. int NCDIfConfig_add_ipv4_route (struct ipv4_ifaddr dest, const uint32_t *gateway, int metric, const char *device)
  152. {
  153. return route_cmd("add", dest, gateway, metric, device);
  154. }
  155. int NCDIfConfig_remove_ipv4_route (struct ipv4_ifaddr dest, const uint32_t *gateway, int metric, const char *device)
  156. {
  157. return route_cmd("del", dest, gateway, metric, device);
  158. }
  159. int NCDIfConfig_set_dns_servers (uint32_t *servers, size_t num_servers)
  160. {
  161. FILE *temp_file = fopen(RESOLVCONF_TEMP_FILE, "w");
  162. if (!temp_file) {
  163. BLog(BLOG_ERROR, "failed to open resolvconf temp file");
  164. goto fail0;
  165. }
  166. char line[60];
  167. sprintf(line, "# generated by badvpn-ncd\n");
  168. if (!write_to_file((uint8_t *)line, strlen(line), temp_file)) {
  169. BLog(BLOG_ERROR, "failed to write to resolvconf temp file");
  170. goto fail1;
  171. }
  172. for (size_t i = 0; i < num_servers; i++) {
  173. uint8_t *addr = (uint8_t *)&servers[i];
  174. sprintf(line, "nameserver %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"\n",
  175. addr[0], addr[1], addr[2], addr[3]);
  176. if (!write_to_file((uint8_t *)line, strlen(line), temp_file)) {
  177. BLog(BLOG_ERROR, "failed to write to resolvconf temp file");
  178. goto fail1;
  179. }
  180. }
  181. if (fclose(temp_file) != 0) {
  182. BLog(BLOG_ERROR, "failed to close resolvconf temp file");
  183. return 0;
  184. }
  185. if (rename(RESOLVCONF_TEMP_FILE, RESOLVCONF_FILE) < 0) {
  186. BLog(BLOG_ERROR, "failed to rename resolvconf temp file to resolvconf file");
  187. return 0;
  188. }
  189. return 1;
  190. fail1:
  191. fclose(temp_file);
  192. fail0:
  193. return 0;
  194. }
  195. static int open_tuntap (const char *ifname, int flags)
  196. {
  197. if (strlen(ifname) >= IFNAMSIZ) {
  198. BLog(BLOG_ERROR, "ifname too long");
  199. return -1;
  200. }
  201. int fd = open(TUN_DEVNODE, O_RDWR);
  202. if (fd < 0) {
  203. BLog(BLOG_ERROR, "open tun failed");
  204. return -1;
  205. }
  206. struct ifreq ifr;
  207. memset(&ifr, 0, sizeof(ifr));
  208. ifr.ifr_flags = flags;
  209. snprintf(ifr.ifr_name, IFNAMSIZ, "%s", ifname);
  210. if (ioctl(fd, TUNSETIFF, (void *)&ifr) < 0) {
  211. BLog(BLOG_ERROR, "TUNSETIFF failed");
  212. close(fd);
  213. return -1;
  214. }
  215. return fd;
  216. }
  217. int NCDIfConfig_make_tuntap (const char *ifname, const char *owner, int tun)
  218. {
  219. // load tun module if needed
  220. if (access(TUN_DEVNODE, F_OK) < 0) {
  221. if (run_command("/sbin/modprobe tun") != 0) {
  222. BLog(BLOG_ERROR, "modprobe tun failed");
  223. }
  224. }
  225. int fd;
  226. if ((fd = open_tuntap(ifname, (tun ? IFF_TUN : IFF_TAP))) < 0) {
  227. goto fail0;
  228. }
  229. if (owner) {
  230. long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
  231. if (bufsize < 0) {
  232. bufsize = 16384;
  233. }
  234. char *buf = malloc(bufsize);
  235. if (!buf) {
  236. BLog(BLOG_ERROR, "malloc failed");
  237. goto fail1;
  238. }
  239. struct passwd pwd;
  240. struct passwd *res;
  241. getpwnam_r(owner, &pwd, buf, bufsize, &res);
  242. if (!res) {
  243. BLog(BLOG_ERROR, "getpwnam_r failed");
  244. free(buf);
  245. goto fail1;
  246. }
  247. int uid = pwd.pw_uid;
  248. free(buf);
  249. if (ioctl(fd, TUNSETOWNER, uid) < 0) {
  250. BLog(BLOG_ERROR, "TUNSETOWNER failed");
  251. goto fail1;
  252. }
  253. }
  254. if (ioctl(fd, TUNSETPERSIST, (void *)1) < 0) {
  255. BLog(BLOG_ERROR, "TUNSETPERSIST failed");
  256. goto fail1;
  257. }
  258. close(fd);
  259. return 1;
  260. fail1:
  261. close(fd);
  262. fail0:
  263. return 0;
  264. }
  265. int NCDIfConfig_remove_tuntap (const char *ifname, int tun)
  266. {
  267. int fd;
  268. if ((fd = open_tuntap(ifname, (tun ? IFF_TUN : IFF_TAP))) < 0) {
  269. goto fail0;
  270. }
  271. if (ioctl(fd, TUNSETPERSIST, (void *)0) < 0) {
  272. BLog(BLOG_ERROR, "TUNSETPERSIST failed");
  273. goto fail1;
  274. }
  275. close(fd);
  276. return 1;
  277. fail1:
  278. close(fd);
  279. fail0:
  280. return 0;
  281. }