NCDIfConfig.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 <system/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. static int run_command (const char *cmd)
  44. {
  45. BLog(BLOG_INFO, "run: %s", cmd);
  46. return system(cmd);
  47. }
  48. static int write_to_file (uint8_t *data, size_t data_len, FILE *f)
  49. {
  50. while (data_len > 0) {
  51. size_t bytes = fwrite(data, 1, data_len, f);
  52. if (bytes == 0) {
  53. return 0;
  54. }
  55. data += bytes;
  56. data_len -= bytes;
  57. }
  58. return 1;
  59. }
  60. int NCDIfConfig_query (const char *ifname)
  61. {
  62. struct ifreq ifr;
  63. int flags = 0;
  64. int s = socket(AF_INET, SOCK_DGRAM, 0);
  65. if (!s) {
  66. BLog(BLOG_ERROR, "socket failed");
  67. goto fail0;
  68. }
  69. memset(&ifr, 0, sizeof(ifr));
  70. snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", ifname);
  71. if (ioctl(s, SIOCGIFFLAGS, &ifr)) {
  72. BLog(BLOG_ERROR, "ioctl(SIOCGIFFLAGS) failed");
  73. goto fail1;
  74. }
  75. flags |= NCDIFCONFIG_FLAG_EXISTS;
  76. if ((ifr.ifr_flags&IFF_UP)) {
  77. flags |= NCDIFCONFIG_FLAG_UP;
  78. if ((ifr.ifr_flags&IFF_RUNNING)) {
  79. flags |= NCDIFCONFIG_FLAG_RUNNING;
  80. }
  81. }
  82. fail1:
  83. close(s);
  84. fail0:
  85. return flags;
  86. }
  87. int NCDIfConfig_set_up (const char *ifname)
  88. {
  89. char cmd[50 + strlen(ifname)];
  90. sprintf(cmd, IP_CMD" link set %s up", ifname);
  91. return !run_command(cmd);
  92. }
  93. int NCDIfConfig_set_down (const char *ifname)
  94. {
  95. char cmd[50 + strlen(ifname)];
  96. sprintf(cmd, IP_CMD" link set %s down", ifname);
  97. return !run_command(cmd);
  98. }
  99. int NCDIfConfig_add_ipv4_addr (const char *ifname, struct ipv4_ifaddr ifaddr)
  100. {
  101. ASSERT(ifaddr.prefix >= 0)
  102. ASSERT(ifaddr.prefix <= 32)
  103. uint8_t *addr = (uint8_t *)&ifaddr.addr;
  104. char cmd[50 + strlen(ifname)];
  105. sprintf(cmd, IP_CMD" addr add %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"/%d dev %s", addr[0], addr[1], addr[2], addr[3], ifaddr.prefix, ifname);
  106. return !run_command(cmd);
  107. }
  108. int NCDIfConfig_remove_ipv4_addr (const char *ifname, struct ipv4_ifaddr ifaddr)
  109. {
  110. ASSERT(ifaddr.prefix >= 0)
  111. ASSERT(ifaddr.prefix <= 32)
  112. uint8_t *addr = (uint8_t *)&ifaddr.addr;
  113. char cmd[50 + strlen(ifname)];
  114. sprintf(cmd, IP_CMD" addr del %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"/%d dev %s", addr[0], addr[1], addr[2], addr[3], ifaddr.prefix, ifname);
  115. return !run_command(cmd);
  116. }
  117. static int route_cmd (const char *cmdtype, struct ipv4_ifaddr dest, const uint32_t *gateway, int metric, const char *device)
  118. {
  119. ASSERT(dest.prefix >= 0)
  120. ASSERT(dest.prefix <= 32)
  121. uint8_t *d_addr = (uint8_t *)&dest.addr;
  122. char gwstr[60];
  123. if (gateway) {
  124. const uint8_t *g_addr = (uint8_t *)gateway;
  125. sprintf(gwstr, " via %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8, g_addr[0], g_addr[1], g_addr[2], g_addr[3]);
  126. } else {
  127. sprintf(gwstr, "");
  128. }
  129. char cmd[100];
  130. sprintf(cmd, IP_CMD" route %s %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"/%d%s metric %d dev %s",
  131. cmdtype, d_addr[0], d_addr[1], d_addr[2], d_addr[3], dest.prefix, gwstr, metric, device);
  132. return !run_command(cmd);
  133. }
  134. int NCDIfConfig_add_ipv4_route (struct ipv4_ifaddr dest, const uint32_t *gateway, int metric, const char *device)
  135. {
  136. return route_cmd("add", dest, gateway, metric, device);
  137. }
  138. int NCDIfConfig_remove_ipv4_route (struct ipv4_ifaddr dest, const uint32_t *gateway, int metric, const char *device)
  139. {
  140. return route_cmd("del", dest, gateway, metric, device);
  141. }
  142. int NCDIfConfig_set_dns_servers (uint32_t *servers, size_t num_servers)
  143. {
  144. FILE *temp_file = fopen(RESOLVCONF_TEMP_FILE, "w");
  145. if (!temp_file) {
  146. BLog(BLOG_ERROR, "failed to open resolvconf temp file");
  147. goto fail0;
  148. }
  149. char line[60];
  150. sprintf(line, "# generated by badvpn-ncd\n");
  151. if (!write_to_file((uint8_t *)line, strlen(line), temp_file)) {
  152. BLog(BLOG_ERROR, "failed to write to resolvconf temp file");
  153. goto fail1;
  154. }
  155. for (size_t i = 0; i < num_servers; i++) {
  156. uint8_t *addr = (uint8_t *)&servers[i];
  157. sprintf(line, "nameserver %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"\n",
  158. addr[0], addr[1], addr[2], addr[3]);
  159. if (!write_to_file((uint8_t *)line, strlen(line), temp_file)) {
  160. BLog(BLOG_ERROR, "failed to write to resolvconf temp file");
  161. goto fail1;
  162. }
  163. }
  164. if (fclose(temp_file) != 0) {
  165. BLog(BLOG_ERROR, "failed to close resolvconf temp file");
  166. return 0;
  167. }
  168. if (rename(RESOLVCONF_TEMP_FILE, RESOLVCONF_FILE) < 0) {
  169. BLog(BLOG_ERROR, "failed to rename resolvconf temp file to resolvconf file");
  170. return 0;
  171. }
  172. return 1;
  173. fail1:
  174. fclose(temp_file);
  175. fail0:
  176. return 0;
  177. }
  178. static int open_tuntap (const char *ifname, int flags)
  179. {
  180. int fd = open("/dev/net/tun", O_RDWR);
  181. if (fd < 0) {
  182. BLog(BLOG_ERROR, "open tun failed");
  183. return -1;
  184. }
  185. struct ifreq ifr;
  186. memset(&ifr, 0, sizeof(ifr));
  187. ifr.ifr_flags = flags;
  188. snprintf(ifr.ifr_name, IFNAMSIZ, "%s", ifname);
  189. if (ioctl(fd, TUNSETIFF, (void *)&ifr) < 0) {
  190. BLog(BLOG_ERROR, "TUNSETIFF failed");
  191. close(fd);
  192. return -1;
  193. }
  194. return fd;
  195. }
  196. int NCDIfConfig_make_tuntap (const char *ifname, const char *owner, int tun)
  197. {
  198. int fd;
  199. if ((fd = open_tuntap(ifname, (tun ? IFF_TUN : IFF_TAP))) < 0) {
  200. goto fail0;
  201. }
  202. if (owner) {
  203. long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
  204. if (bufsize < 0) {
  205. bufsize = 16384;
  206. }
  207. char *buf = malloc(bufsize);
  208. if (!buf) {
  209. BLog(BLOG_ERROR, "malloc failed");
  210. goto fail1;
  211. }
  212. struct passwd pwd;
  213. struct passwd *res;
  214. getpwnam_r(owner, &pwd, buf, bufsize, &res);
  215. if (!res) {
  216. BLog(BLOG_ERROR, "getpwnam_r failed");
  217. free(buf);
  218. goto fail1;
  219. }
  220. int uid = pwd.pw_uid;
  221. free(buf);
  222. if (ioctl(fd, TUNSETOWNER, uid) < 0) {
  223. BLog(BLOG_ERROR, "TUNSETOWNER failed");
  224. goto fail1;
  225. }
  226. }
  227. if (ioctl(fd, TUNSETPERSIST, (void *)1) < 0) {
  228. BLog(BLOG_ERROR, "TUNSETPERSIST failed");
  229. goto fail1;
  230. }
  231. close(fd);
  232. return 1;
  233. fail1:
  234. close(fd);
  235. fail0:
  236. return 0;
  237. }
  238. int NCDIfConfig_remove_tuntap (const char *ifname, int tun)
  239. {
  240. int fd;
  241. if ((fd = open_tuntap(ifname, (tun ? IFF_TUN : IFF_TAP))) < 0) {
  242. goto fail0;
  243. }
  244. if (ioctl(fd, TUNSETPERSIST, (void *)0) < 0) {
  245. BLog(BLOG_ERROR, "TUNSETPERSIST failed");
  246. goto fail1;
  247. }
  248. close(fd);
  249. return 1;
  250. fail1:
  251. close(fd);
  252. fail0:
  253. return 0;
  254. }