NCDIfConfig.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /**
  2. * @file NCDIfConfig.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <inttypes.h>
  30. #include <string.h>
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. #include <unistd.h>
  34. #include <sys/socket.h>
  35. #include <net/if.h>
  36. #include <net/if_arp.h>
  37. #include <sys/ioctl.h>
  38. #include <sys/types.h>
  39. #include <sys/stat.h>
  40. #include <fcntl.h>
  41. #include <linux/if_tun.h>
  42. #include <pwd.h>
  43. #include <misc/debug.h>
  44. #include <base/BLog.h>
  45. #include <ncd/NCDIfConfig.h>
  46. #include <generated/blog_channel_NCDIfConfig.h>
  47. #define IP_CMD "ip"
  48. #define RESOLVCONF_FILE "/etc/resolv.conf"
  49. #define RESOLVCONF_TEMP_FILE "/etc/resolv.conf-ncd-temp"
  50. #define TUN_DEVNODE "/dev/net/tun"
  51. static int run_command (const char *cmd)
  52. {
  53. BLog(BLOG_INFO, "run: %s", cmd);
  54. return system(cmd);
  55. }
  56. static int write_to_file (uint8_t *data, size_t data_len, FILE *f)
  57. {
  58. while (data_len > 0) {
  59. size_t bytes = fwrite(data, 1, data_len, f);
  60. if (bytes == 0) {
  61. return 0;
  62. }
  63. data += bytes;
  64. data_len -= bytes;
  65. }
  66. return 1;
  67. }
  68. int NCDIfConfig_query (const char *ifname)
  69. {
  70. struct ifreq ifr;
  71. int flags = 0;
  72. int s = socket(AF_INET, SOCK_DGRAM, 0);
  73. if (!s) {
  74. BLog(BLOG_ERROR, "socket failed");
  75. goto fail0;
  76. }
  77. memset(&ifr, 0, sizeof(ifr));
  78. snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", ifname);
  79. if (ioctl(s, SIOCGIFFLAGS, &ifr)) {
  80. BLog(BLOG_ERROR, "SIOCGIFFLAGS failed");
  81. goto fail1;
  82. }
  83. flags |= NCDIFCONFIG_FLAG_EXISTS;
  84. if ((ifr.ifr_flags&IFF_UP)) {
  85. flags |= NCDIFCONFIG_FLAG_UP;
  86. if ((ifr.ifr_flags&IFF_RUNNING)) {
  87. flags |= NCDIFCONFIG_FLAG_RUNNING;
  88. }
  89. }
  90. fail1:
  91. close(s);
  92. fail0:
  93. return flags;
  94. }
  95. int NCDIfConfig_set_up (const char *ifname)
  96. {
  97. if (strlen(ifname) >= IFNAMSIZ) {
  98. BLog(BLOG_ERROR, "ifname too long");
  99. return 0;
  100. }
  101. char cmd[50 + IFNAMSIZ];
  102. sprintf(cmd, IP_CMD" link set %s up", ifname);
  103. return !run_command(cmd);
  104. }
  105. int NCDIfConfig_set_down (const char *ifname)
  106. {
  107. if (strlen(ifname) >= IFNAMSIZ) {
  108. BLog(BLOG_ERROR, "ifname too long");
  109. return 0;
  110. }
  111. char cmd[50 + IFNAMSIZ];
  112. sprintf(cmd, IP_CMD" link set %s down", ifname);
  113. return !run_command(cmd);
  114. }
  115. int NCDIfConfig_add_ipv4_addr (const char *ifname, struct ipv4_ifaddr ifaddr)
  116. {
  117. ASSERT(ifaddr.prefix >= 0)
  118. ASSERT(ifaddr.prefix <= 32)
  119. if (strlen(ifname) >= IFNAMSIZ) {
  120. BLog(BLOG_ERROR, "ifname too long");
  121. return 0;
  122. }
  123. uint8_t *addr = (uint8_t *)&ifaddr.addr;
  124. char cmd[50 + IFNAMSIZ];
  125. sprintf(cmd, IP_CMD" addr add %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"/%d dev %s", addr[0], addr[1], addr[2], addr[3], ifaddr.prefix, ifname);
  126. return !run_command(cmd);
  127. }
  128. int NCDIfConfig_remove_ipv4_addr (const char *ifname, struct ipv4_ifaddr ifaddr)
  129. {
  130. ASSERT(ifaddr.prefix >= 0)
  131. ASSERT(ifaddr.prefix <= 32)
  132. if (strlen(ifname) >= IFNAMSIZ) {
  133. BLog(BLOG_ERROR, "ifname too long");
  134. return 0;
  135. }
  136. uint8_t *addr = (uint8_t *)&ifaddr.addr;
  137. char cmd[50 + IFNAMSIZ];
  138. sprintf(cmd, IP_CMD" addr del %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"/%d dev %s", addr[0], addr[1], addr[2], addr[3], ifaddr.prefix, ifname);
  139. return !run_command(cmd);
  140. }
  141. static int route_cmd (const char *cmdtype, struct ipv4_ifaddr dest, const uint32_t *gateway, int metric, const char *ifname)
  142. {
  143. ASSERT(!strcmp(cmdtype, "add") || !strcmp(cmdtype, "del"))
  144. ASSERT(dest.prefix >= 0)
  145. ASSERT(dest.prefix <= 32)
  146. if (strlen(ifname) >= IFNAMSIZ) {
  147. BLog(BLOG_ERROR, "ifname too long");
  148. return 0;
  149. }
  150. uint8_t *d_addr = (uint8_t *)&dest.addr;
  151. char gwstr[30];
  152. if (gateway) {
  153. const uint8_t *g_addr = (uint8_t *)gateway;
  154. sprintf(gwstr, " via %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8, g_addr[0], g_addr[1], g_addr[2], g_addr[3]);
  155. } else {
  156. gwstr[0] = '\0';
  157. }
  158. char cmd[120 + IFNAMSIZ];
  159. sprintf(cmd, IP_CMD" route %s %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"/%d%s metric %d dev %s",
  160. cmdtype, d_addr[0], d_addr[1], d_addr[2], d_addr[3], dest.prefix, gwstr, metric, ifname);
  161. return !run_command(cmd);
  162. }
  163. int NCDIfConfig_add_ipv4_route (struct ipv4_ifaddr dest, const uint32_t *gateway, int metric, const char *device)
  164. {
  165. return route_cmd("add", dest, gateway, metric, device);
  166. }
  167. int NCDIfConfig_remove_ipv4_route (struct ipv4_ifaddr dest, const uint32_t *gateway, int metric, const char *device)
  168. {
  169. return route_cmd("del", dest, gateway, metric, device);
  170. }
  171. static int blackhole_route_cmd (const char *cmdtype, struct ipv4_ifaddr dest, int metric)
  172. {
  173. ASSERT(!strcmp(cmdtype, "add") || !strcmp(cmdtype, "del"))
  174. ASSERT(dest.prefix >= 0)
  175. ASSERT(dest.prefix <= 32)
  176. uint8_t *d_addr = (uint8_t *)&dest.addr;
  177. char cmd[120];
  178. sprintf(cmd, IP_CMD" route %s blackhole %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"/%d metric %d",
  179. cmdtype, d_addr[0], d_addr[1], d_addr[2], d_addr[3], dest.prefix, metric);
  180. return !run_command(cmd);
  181. }
  182. int NCDIfConfig_add_ipv4_blackhole_route (struct ipv4_ifaddr dest, int metric)
  183. {
  184. return blackhole_route_cmd("add", dest, metric);
  185. }
  186. int NCDIfConfig_remove_ipv4_blackhole_route (struct ipv4_ifaddr dest, int metric)
  187. {
  188. return blackhole_route_cmd("del", dest, metric);
  189. }
  190. int NCDIfConfig_set_dns_servers (uint32_t *servers, size_t num_servers)
  191. {
  192. FILE *temp_file = fopen(RESOLVCONF_TEMP_FILE, "w");
  193. if (!temp_file) {
  194. BLog(BLOG_ERROR, "failed to open resolvconf temp file");
  195. goto fail0;
  196. }
  197. char line[60];
  198. sprintf(line, "# generated by badvpn-ncd\n");
  199. if (!write_to_file((uint8_t *)line, strlen(line), temp_file)) {
  200. BLog(BLOG_ERROR, "failed to write to resolvconf temp file");
  201. goto fail1;
  202. }
  203. for (size_t i = 0; i < num_servers; i++) {
  204. uint8_t *addr = (uint8_t *)&servers[i];
  205. sprintf(line, "nameserver %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"\n",
  206. addr[0], addr[1], addr[2], addr[3]);
  207. if (!write_to_file((uint8_t *)line, strlen(line), temp_file)) {
  208. BLog(BLOG_ERROR, "failed to write to resolvconf temp file");
  209. goto fail1;
  210. }
  211. }
  212. if (fclose(temp_file) != 0) {
  213. BLog(BLOG_ERROR, "failed to close resolvconf temp file");
  214. return 0;
  215. }
  216. if (rename(RESOLVCONF_TEMP_FILE, RESOLVCONF_FILE) < 0) {
  217. BLog(BLOG_ERROR, "failed to rename resolvconf temp file to resolvconf file");
  218. return 0;
  219. }
  220. return 1;
  221. fail1:
  222. fclose(temp_file);
  223. fail0:
  224. return 0;
  225. }
  226. static int open_tuntap (const char *ifname, int flags)
  227. {
  228. if (strlen(ifname) >= IFNAMSIZ) {
  229. BLog(BLOG_ERROR, "ifname too long");
  230. return -1;
  231. }
  232. int fd = open(TUN_DEVNODE, O_RDWR);
  233. if (fd < 0) {
  234. BLog(BLOG_ERROR, "open tun failed");
  235. return -1;
  236. }
  237. struct ifreq ifr;
  238. memset(&ifr, 0, sizeof(ifr));
  239. ifr.ifr_flags = flags;
  240. snprintf(ifr.ifr_name, IFNAMSIZ, "%s", ifname);
  241. if (ioctl(fd, TUNSETIFF, (void *)&ifr) < 0) {
  242. BLog(BLOG_ERROR, "TUNSETIFF failed");
  243. close(fd);
  244. return -1;
  245. }
  246. return fd;
  247. }
  248. int NCDIfConfig_make_tuntap (const char *ifname, const char *owner, int tun)
  249. {
  250. // load tun module if needed
  251. if (access(TUN_DEVNODE, F_OK) < 0) {
  252. if (run_command("/sbin/modprobe tun") != 0) {
  253. BLog(BLOG_ERROR, "modprobe tun failed");
  254. }
  255. }
  256. int fd;
  257. if ((fd = open_tuntap(ifname, (tun ? IFF_TUN : IFF_TAP))) < 0) {
  258. goto fail0;
  259. }
  260. if (owner) {
  261. long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
  262. if (bufsize < 0) {
  263. bufsize = 16384;
  264. }
  265. char *buf = malloc(bufsize);
  266. if (!buf) {
  267. BLog(BLOG_ERROR, "malloc failed");
  268. goto fail1;
  269. }
  270. struct passwd pwd;
  271. struct passwd *res;
  272. getpwnam_r(owner, &pwd, buf, bufsize, &res);
  273. if (!res) {
  274. BLog(BLOG_ERROR, "getpwnam_r failed");
  275. free(buf);
  276. goto fail1;
  277. }
  278. int uid = pwd.pw_uid;
  279. free(buf);
  280. if (ioctl(fd, TUNSETOWNER, uid) < 0) {
  281. BLog(BLOG_ERROR, "TUNSETOWNER failed");
  282. goto fail1;
  283. }
  284. }
  285. if (ioctl(fd, TUNSETPERSIST, (void *)1) < 0) {
  286. BLog(BLOG_ERROR, "TUNSETPERSIST failed");
  287. goto fail1;
  288. }
  289. close(fd);
  290. return 1;
  291. fail1:
  292. close(fd);
  293. fail0:
  294. return 0;
  295. }
  296. int NCDIfConfig_remove_tuntap (const char *ifname, int tun)
  297. {
  298. int fd;
  299. if ((fd = open_tuntap(ifname, (tun ? IFF_TUN : IFF_TAP))) < 0) {
  300. goto fail0;
  301. }
  302. if (ioctl(fd, TUNSETPERSIST, (void *)0) < 0) {
  303. BLog(BLOG_ERROR, "TUNSETPERSIST failed");
  304. goto fail1;
  305. }
  306. close(fd);
  307. return 1;
  308. fail1:
  309. close(fd);
  310. fail0:
  311. return 0;
  312. }