NCDIfConfig.c 5.8 KB

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