NCDIfConfig.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /**
  2. * @file NCDIfConfig.c
  3. * @author Ambroz Bizjak <[email protected]>
  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. int NCDIfConfig_add_ipv6_addr (const char *ifname, struct ipv6_ifaddr ifaddr)
  142. {
  143. ASSERT(ifaddr.prefix >= 0)
  144. ASSERT(ifaddr.prefix <= 128)
  145. if (strlen(ifname) >= IFNAMSIZ) {
  146. BLog(BLOG_ERROR, "ifname too long");
  147. return 0;
  148. }
  149. char addr_str[IPADDR6_PRINT_MAX];
  150. ipaddr6_print_addr(ifaddr.addr, addr_str);
  151. char cmd[40 + IPADDR6_PRINT_MAX + IFNAMSIZ];
  152. sprintf(cmd, IP_CMD" addr add %s/%d dev %s", addr_str, ifaddr.prefix, ifname);
  153. return !run_command(cmd);
  154. }
  155. int NCDIfConfig_remove_ipv6_addr (const char *ifname, struct ipv6_ifaddr ifaddr)
  156. {
  157. ASSERT(ifaddr.prefix >= 0)
  158. ASSERT(ifaddr.prefix <= 128)
  159. if (strlen(ifname) >= IFNAMSIZ) {
  160. BLog(BLOG_ERROR, "ifname too long");
  161. return 0;
  162. }
  163. char addr_str[IPADDR6_PRINT_MAX];
  164. ipaddr6_print_addr(ifaddr.addr, addr_str);
  165. char cmd[40 + IPADDR6_PRINT_MAX + IFNAMSIZ];
  166. sprintf(cmd, IP_CMD" addr del %s/%d dev %s", addr_str, ifaddr.prefix, ifname);
  167. return !run_command(cmd);
  168. }
  169. static int route_cmd (const char *cmdtype, struct ipv4_ifaddr dest, const uint32_t *gateway, int metric, const char *ifname)
  170. {
  171. ASSERT(!strcmp(cmdtype, "add") || !strcmp(cmdtype, "del"))
  172. ASSERT(dest.prefix >= 0)
  173. ASSERT(dest.prefix <= 32)
  174. if (strlen(ifname) >= IFNAMSIZ) {
  175. BLog(BLOG_ERROR, "ifname too long");
  176. return 0;
  177. }
  178. uint8_t *d_addr = (uint8_t *)&dest.addr;
  179. char gwstr[30];
  180. if (gateway) {
  181. const uint8_t *g_addr = (uint8_t *)gateway;
  182. sprintf(gwstr, " via %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8, g_addr[0], g_addr[1], g_addr[2], g_addr[3]);
  183. } else {
  184. gwstr[0] = '\0';
  185. }
  186. char cmd[120 + IFNAMSIZ];
  187. sprintf(cmd, IP_CMD" route %s %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"/%d%s metric %d dev %s",
  188. cmdtype, d_addr[0], d_addr[1], d_addr[2], d_addr[3], dest.prefix, gwstr, metric, ifname);
  189. return !run_command(cmd);
  190. }
  191. int NCDIfConfig_add_ipv4_route (struct ipv4_ifaddr dest, const uint32_t *gateway, int metric, const char *device)
  192. {
  193. return route_cmd("add", dest, gateway, metric, device);
  194. }
  195. int NCDIfConfig_remove_ipv4_route (struct ipv4_ifaddr dest, const uint32_t *gateway, int metric, const char *device)
  196. {
  197. return route_cmd("del", dest, gateway, metric, device);
  198. }
  199. static int blackhole_route_cmd (const char *cmdtype, struct ipv4_ifaddr dest, int metric)
  200. {
  201. ASSERT(!strcmp(cmdtype, "add") || !strcmp(cmdtype, "del"))
  202. ASSERT(dest.prefix >= 0)
  203. ASSERT(dest.prefix <= 32)
  204. uint8_t *d_addr = (uint8_t *)&dest.addr;
  205. char cmd[120];
  206. sprintf(cmd, IP_CMD" route %s blackhole %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"/%d metric %d",
  207. cmdtype, d_addr[0], d_addr[1], d_addr[2], d_addr[3], dest.prefix, metric);
  208. return !run_command(cmd);
  209. }
  210. int NCDIfConfig_add_ipv4_blackhole_route (struct ipv4_ifaddr dest, int metric)
  211. {
  212. return blackhole_route_cmd("add", dest, metric);
  213. }
  214. int NCDIfConfig_remove_ipv4_blackhole_route (struct ipv4_ifaddr dest, int metric)
  215. {
  216. return blackhole_route_cmd("del", dest, metric);
  217. }
  218. int NCDIfConfig_set_dns_servers (uint32_t *servers, size_t num_servers)
  219. {
  220. FILE *temp_file = fopen(RESOLVCONF_TEMP_FILE, "w");
  221. if (!temp_file) {
  222. BLog(BLOG_ERROR, "failed to open resolvconf temp file");
  223. goto fail0;
  224. }
  225. char line[60];
  226. sprintf(line, "# generated by badvpn-ncd\n");
  227. if (!write_to_file((uint8_t *)line, strlen(line), temp_file)) {
  228. BLog(BLOG_ERROR, "failed to write to resolvconf temp file");
  229. goto fail1;
  230. }
  231. for (size_t i = 0; i < num_servers; i++) {
  232. uint8_t *addr = (uint8_t *)&servers[i];
  233. sprintf(line, "nameserver %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"\n",
  234. addr[0], addr[1], addr[2], addr[3]);
  235. if (!write_to_file((uint8_t *)line, strlen(line), temp_file)) {
  236. BLog(BLOG_ERROR, "failed to write to resolvconf temp file");
  237. goto fail1;
  238. }
  239. }
  240. if (fclose(temp_file) != 0) {
  241. BLog(BLOG_ERROR, "failed to close resolvconf temp file");
  242. return 0;
  243. }
  244. if (rename(RESOLVCONF_TEMP_FILE, RESOLVCONF_FILE) < 0) {
  245. BLog(BLOG_ERROR, "failed to rename resolvconf temp file to resolvconf file");
  246. return 0;
  247. }
  248. return 1;
  249. fail1:
  250. fclose(temp_file);
  251. fail0:
  252. return 0;
  253. }
  254. static int open_tuntap (const char *ifname, int flags)
  255. {
  256. if (strlen(ifname) >= IFNAMSIZ) {
  257. BLog(BLOG_ERROR, "ifname too long");
  258. return -1;
  259. }
  260. int fd = open(TUN_DEVNODE, O_RDWR);
  261. if (fd < 0) {
  262. BLog(BLOG_ERROR, "open tun failed");
  263. return -1;
  264. }
  265. struct ifreq ifr;
  266. memset(&ifr, 0, sizeof(ifr));
  267. ifr.ifr_flags = flags;
  268. snprintf(ifr.ifr_name, IFNAMSIZ, "%s", ifname);
  269. if (ioctl(fd, TUNSETIFF, (void *)&ifr) < 0) {
  270. BLog(BLOG_ERROR, "TUNSETIFF failed");
  271. close(fd);
  272. return -1;
  273. }
  274. return fd;
  275. }
  276. int NCDIfConfig_make_tuntap (const char *ifname, const char *owner, int tun)
  277. {
  278. // load tun module if needed
  279. if (access(TUN_DEVNODE, F_OK) < 0) {
  280. if (run_command("/sbin/modprobe tun") != 0) {
  281. BLog(BLOG_ERROR, "modprobe tun failed");
  282. }
  283. }
  284. int fd;
  285. if ((fd = open_tuntap(ifname, (tun ? IFF_TUN : IFF_TAP))) < 0) {
  286. goto fail0;
  287. }
  288. if (owner) {
  289. long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
  290. if (bufsize < 0) {
  291. bufsize = 16384;
  292. }
  293. char *buf = malloc(bufsize);
  294. if (!buf) {
  295. BLog(BLOG_ERROR, "malloc failed");
  296. goto fail1;
  297. }
  298. struct passwd pwd;
  299. struct passwd *res;
  300. getpwnam_r(owner, &pwd, buf, bufsize, &res);
  301. if (!res) {
  302. BLog(BLOG_ERROR, "getpwnam_r failed");
  303. free(buf);
  304. goto fail1;
  305. }
  306. int uid = pwd.pw_uid;
  307. free(buf);
  308. if (ioctl(fd, TUNSETOWNER, uid) < 0) {
  309. BLog(BLOG_ERROR, "TUNSETOWNER failed");
  310. goto fail1;
  311. }
  312. }
  313. if (ioctl(fd, TUNSETPERSIST, (void *)1) < 0) {
  314. BLog(BLOG_ERROR, "TUNSETPERSIST failed");
  315. goto fail1;
  316. }
  317. close(fd);
  318. return 1;
  319. fail1:
  320. close(fd);
  321. fail0:
  322. return 0;
  323. }
  324. int NCDIfConfig_remove_tuntap (const char *ifname, int tun)
  325. {
  326. int fd;
  327. if ((fd = open_tuntap(ifname, (tun ? IFF_TUN : IFF_TAP))) < 0) {
  328. goto fail0;
  329. }
  330. if (ioctl(fd, TUNSETPERSIST, (void *)0) < 0) {
  331. BLog(BLOG_ERROR, "TUNSETPERSIST failed");
  332. goto fail1;
  333. }
  334. close(fd);
  335. return 1;
  336. fail1:
  337. close(fd);
  338. fail0:
  339. return 0;
  340. }