NCDIfConfig.c 7.8 KB

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