NCDIfConfig.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 <misc/debug.h>
  32. #include <system/BLog.h>
  33. #include <ncd/NCDIfConfig.h>
  34. #include <generated/blog_channel_NCDIfConfig.h>
  35. #define IP_CMD "ip"
  36. #define ROUTE_CMD "route"
  37. #define RESOLVCONF_FILE "/etc/resolv.conf"
  38. #define RESOLVCONF_TEMP_FILE "/etc/resolv.conf-ncd-temp"
  39. static int run_command (const char *cmd)
  40. {
  41. BLog(BLOG_INFO, "run: %s", cmd);
  42. return system(cmd);
  43. }
  44. static int write_to_file (uint8_t *data, size_t data_len, FILE *f)
  45. {
  46. while (data_len > 0) {
  47. size_t bytes = fwrite(data, 1, data_len, f);
  48. if (bytes == 0) {
  49. return 0;
  50. }
  51. data += bytes;
  52. data_len -= bytes;
  53. }
  54. return 1;
  55. }
  56. int NCDIfConfig_query (const char *ifname)
  57. {
  58. struct ifreq ifr;
  59. int flags = 0;
  60. int s = socket(AF_INET, SOCK_DGRAM, 0);
  61. if (!s) {
  62. BLog(BLOG_ERROR, "socket failed");
  63. goto fail0;
  64. }
  65. memset(&ifr, 0, sizeof(ifr));
  66. snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", ifname);
  67. if (ioctl(s, SIOCGIFFLAGS, &ifr)) {
  68. BLog(BLOG_ERROR, "ioctl(SIOCGIFFLAGS) failed");
  69. goto fail1;
  70. }
  71. flags |= NCDIFCONFIG_FLAG_EXISTS;
  72. if ((ifr.ifr_flags&IFF_UP)) {
  73. flags |= NCDIFCONFIG_FLAG_UP;
  74. if ((ifr.ifr_flags&IFF_RUNNING)) {
  75. flags |= NCDIFCONFIG_FLAG_RUNNING;
  76. }
  77. }
  78. fail1:
  79. close(s);
  80. fail0:
  81. return flags;
  82. }
  83. int NCDIfConfig_set_up (const char *ifname)
  84. {
  85. char cmd[50 + strlen(ifname)];
  86. sprintf(cmd, IP_CMD" link set %s up", ifname);
  87. return !run_command(cmd);
  88. }
  89. int NCDIfConfig_set_down (const char *ifname)
  90. {
  91. char cmd[50 + strlen(ifname)];
  92. sprintf(cmd, IP_CMD" link set %s down", ifname);
  93. return !run_command(cmd);
  94. }
  95. int NCDIfConfig_add_ipv4_addr (const char *ifname, struct ipv4_ifaddr ifaddr)
  96. {
  97. ASSERT(ifaddr.prefix >= 0)
  98. ASSERT(ifaddr.prefix <= 32)
  99. uint8_t *addr = (uint8_t *)&ifaddr.addr;
  100. char cmd[50 + strlen(ifname)];
  101. sprintf(cmd, IP_CMD" addr add %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"/%d dev %s", addr[0], addr[1], addr[2], addr[3], ifaddr.prefix, ifname);
  102. return !run_command(cmd);
  103. }
  104. int NCDIfConfig_remove_ipv4_addr (const char *ifname, struct ipv4_ifaddr ifaddr)
  105. {
  106. ASSERT(ifaddr.prefix >= 0)
  107. ASSERT(ifaddr.prefix <= 32)
  108. uint8_t *addr = (uint8_t *)&ifaddr.addr;
  109. char cmd[50 + strlen(ifname)];
  110. sprintf(cmd, IP_CMD" addr del %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"/%d dev %s", addr[0], addr[1], addr[2], addr[3], ifaddr.prefix, ifname);
  111. return !run_command(cmd);
  112. }
  113. int NCDIfConfig_add_ipv4_route (struct ipv4_ifaddr dest, uint32_t gateway, int metric, const char *device)
  114. {
  115. ASSERT(dest.prefix >= 0)
  116. ASSERT(dest.prefix <= 32)
  117. uint8_t *d_addr = (uint8_t *)&dest.addr;
  118. uint8_t *g_addr = (uint8_t *)&gateway;
  119. char cmd[100];
  120. sprintf(cmd, ROUTE_CMD" add -net %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"/%d gw %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8" metric %d dev %s",
  121. d_addr[0], d_addr[1], d_addr[2], d_addr[3], dest.prefix, g_addr[0], g_addr[1], g_addr[2], g_addr[3], metric, device);
  122. return !run_command(cmd);
  123. }
  124. int NCDIfConfig_remove_ipv4_route (struct ipv4_ifaddr dest, uint32_t gateway, int metric, const char *device)
  125. {
  126. ASSERT(dest.prefix >= 0)
  127. ASSERT(dest.prefix <= 32)
  128. uint8_t *d_addr = (uint8_t *)&dest.addr;
  129. uint8_t *g_addr = (uint8_t *)&gateway;
  130. char cmd[100];
  131. sprintf(cmd, ROUTE_CMD" del -net %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"/%d gw %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8" metric %d dev %s",
  132. d_addr[0], d_addr[1], d_addr[2], d_addr[3], dest.prefix, g_addr[0], g_addr[1], g_addr[2], g_addr[3], metric, device);
  133. return !run_command(cmd);
  134. }
  135. int NCDIfConfig_set_dns_servers (uint32_t *servers, size_t num_servers)
  136. {
  137. FILE *temp_file = fopen(RESOLVCONF_TEMP_FILE, "w");
  138. if (!temp_file) {
  139. BLog(BLOG_ERROR, "failed to open resolvconf temp file");
  140. goto fail0;
  141. }
  142. char line[60];
  143. sprintf(line, "# generated by badvpn-ncd\n");
  144. if (!write_to_file((uint8_t *)line, strlen(line), temp_file)) {
  145. BLog(BLOG_ERROR, "failed to write to resolvconf temp file");
  146. goto fail1;
  147. }
  148. for (size_t i = 0; i < num_servers; i++) {
  149. uint8_t *addr = (uint8_t *)&servers[i];
  150. sprintf(line, "nameserver %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"\n",
  151. addr[0], addr[1], addr[2], addr[3]);
  152. if (!write_to_file((uint8_t *)line, strlen(line), temp_file)) {
  153. BLog(BLOG_ERROR, "failed to write to resolvconf temp file");
  154. goto fail1;
  155. }
  156. }
  157. if (fclose(temp_file) != 0) {
  158. BLog(BLOG_ERROR, "failed to close resolvconf temp file");
  159. return 0;
  160. }
  161. if (rename(RESOLVCONF_TEMP_FILE, RESOLVCONF_FILE) < 0) {
  162. BLog(BLOG_ERROR, "failed to rename resolvconf temp file to resolvconf file");
  163. return 0;
  164. }
  165. return 1;
  166. fail1:
  167. fclose(temp_file);
  168. fail0:
  169. return 0;
  170. }