NCDIfConfig.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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. 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 route_cmd6 (const char *cmdtype, struct ipv6_ifaddr dest, const struct ipv6_addr *gateway, int metric, const char *ifname)
  200. {
  201. ASSERT(!strcmp(cmdtype, "add") || !strcmp(cmdtype, "del"))
  202. ASSERT(dest.prefix >= 0)
  203. ASSERT(dest.prefix <= 128)
  204. if (strlen(ifname) >= IFNAMSIZ) {
  205. BLog(BLOG_ERROR, "ifname too long");
  206. return 0;
  207. }
  208. char dest_str[IPADDR6_PRINT_MAX];
  209. ipaddr6_print_addr(dest.addr, dest_str);
  210. char gwstr[10 + IPADDR6_PRINT_MAX];
  211. if (gateway) {
  212. strcpy(gwstr, " via ");
  213. ipaddr6_print_addr(*gateway, gwstr + strlen(gwstr));
  214. } else {
  215. gwstr[0] = '\0';
  216. }
  217. char cmd[70 + IPADDR6_PRINT_MAX + IPADDR6_PRINT_MAX + IFNAMSIZ];
  218. sprintf(cmd, IP_CMD" route %s %s/%d%s metric %d dev %s",
  219. cmdtype, dest_str, dest.prefix, gwstr, metric, ifname);
  220. return !run_command(cmd);
  221. }
  222. int NCDIfConfig_add_ipv6_route (struct ipv6_ifaddr dest, const struct ipv6_addr *gateway, int metric, const char *device)
  223. {
  224. return route_cmd6("add", dest, gateway, metric, device);
  225. }
  226. int NCDIfConfig_remove_ipv6_route (struct ipv6_ifaddr dest, const struct ipv6_addr *gateway, int metric, const char *device)
  227. {
  228. return route_cmd6("del", dest, gateway, metric, device);
  229. }
  230. static int blackhole_route_cmd (const char *cmdtype, struct ipv4_ifaddr dest, int metric)
  231. {
  232. ASSERT(!strcmp(cmdtype, "add") || !strcmp(cmdtype, "del"))
  233. ASSERT(dest.prefix >= 0)
  234. ASSERT(dest.prefix <= 32)
  235. uint8_t *d_addr = (uint8_t *)&dest.addr;
  236. char cmd[120];
  237. sprintf(cmd, IP_CMD" route %s blackhole %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"/%d metric %d",
  238. cmdtype, d_addr[0], d_addr[1], d_addr[2], d_addr[3], dest.prefix, metric);
  239. return !run_command(cmd);
  240. }
  241. int NCDIfConfig_add_ipv4_blackhole_route (struct ipv4_ifaddr dest, int metric)
  242. {
  243. return blackhole_route_cmd("add", dest, metric);
  244. }
  245. int NCDIfConfig_remove_ipv4_blackhole_route (struct ipv4_ifaddr dest, int metric)
  246. {
  247. return blackhole_route_cmd("del", dest, metric);
  248. }
  249. static int blackhole_route_cmd6 (const char *cmdtype, struct ipv6_ifaddr dest, int metric)
  250. {
  251. ASSERT(!strcmp(cmdtype, "add") || !strcmp(cmdtype, "del"))
  252. ASSERT(dest.prefix >= 0)
  253. ASSERT(dest.prefix <= 128)
  254. char dest_str[IPADDR6_PRINT_MAX];
  255. ipaddr6_print_addr(dest.addr, dest_str);
  256. char cmd[70 + IPADDR6_PRINT_MAX];
  257. sprintf(cmd, IP_CMD" route %s blackhole %s/%d metric %d",
  258. cmdtype, dest_str, dest.prefix, metric);
  259. return !run_command(cmd);
  260. }
  261. int NCDIfConfig_add_ipv6_blackhole_route (struct ipv6_ifaddr dest, int metric)
  262. {
  263. return blackhole_route_cmd6("add", dest, metric);
  264. }
  265. int NCDIfConfig_remove_ipv6_blackhole_route (struct ipv6_ifaddr dest, int metric)
  266. {
  267. return blackhole_route_cmd6("del", dest, metric);
  268. }
  269. int NCDIfConfig_set_dns_servers (uint32_t *servers, size_t num_servers)
  270. {
  271. FILE *temp_file = fopen(RESOLVCONF_TEMP_FILE, "w");
  272. if (!temp_file) {
  273. BLog(BLOG_ERROR, "failed to open resolvconf temp file");
  274. goto fail0;
  275. }
  276. char line[60];
  277. sprintf(line, "# generated by badvpn-ncd\n");
  278. if (!write_to_file((uint8_t *)line, strlen(line), temp_file)) {
  279. BLog(BLOG_ERROR, "failed to write to resolvconf temp file");
  280. goto fail1;
  281. }
  282. for (size_t i = 0; i < num_servers; i++) {
  283. uint8_t *addr = (uint8_t *)&servers[i];
  284. sprintf(line, "nameserver %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"\n",
  285. addr[0], addr[1], addr[2], addr[3]);
  286. if (!write_to_file((uint8_t *)line, strlen(line), temp_file)) {
  287. BLog(BLOG_ERROR, "failed to write to resolvconf temp file");
  288. goto fail1;
  289. }
  290. }
  291. if (fclose(temp_file) != 0) {
  292. BLog(BLOG_ERROR, "failed to close resolvconf temp file");
  293. return 0;
  294. }
  295. if (rename(RESOLVCONF_TEMP_FILE, RESOLVCONF_FILE) < 0) {
  296. BLog(BLOG_ERROR, "failed to rename resolvconf temp file to resolvconf file");
  297. return 0;
  298. }
  299. return 1;
  300. fail1:
  301. fclose(temp_file);
  302. fail0:
  303. return 0;
  304. }
  305. static int open_tuntap (const char *ifname, int flags)
  306. {
  307. if (strlen(ifname) >= IFNAMSIZ) {
  308. BLog(BLOG_ERROR, "ifname too long");
  309. return -1;
  310. }
  311. int fd = open(TUN_DEVNODE, O_RDWR);
  312. if (fd < 0) {
  313. BLog(BLOG_ERROR, "open tun failed");
  314. return -1;
  315. }
  316. struct ifreq ifr;
  317. memset(&ifr, 0, sizeof(ifr));
  318. ifr.ifr_flags = flags;
  319. snprintf(ifr.ifr_name, IFNAMSIZ, "%s", ifname);
  320. if (ioctl(fd, TUNSETIFF, (void *)&ifr) < 0) {
  321. BLog(BLOG_ERROR, "TUNSETIFF failed");
  322. close(fd);
  323. return -1;
  324. }
  325. return fd;
  326. }
  327. int NCDIfConfig_make_tuntap (const char *ifname, const char *owner, int tun)
  328. {
  329. // load tun module if needed
  330. if (access(TUN_DEVNODE, F_OK) < 0) {
  331. if (run_command("/sbin/modprobe tun") != 0) {
  332. BLog(BLOG_ERROR, "modprobe tun failed");
  333. }
  334. }
  335. int fd;
  336. if ((fd = open_tuntap(ifname, (tun ? IFF_TUN : IFF_TAP))) < 0) {
  337. goto fail0;
  338. }
  339. if (owner) {
  340. long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
  341. if (bufsize < 0) {
  342. bufsize = 16384;
  343. }
  344. char *buf = malloc(bufsize);
  345. if (!buf) {
  346. BLog(BLOG_ERROR, "malloc failed");
  347. goto fail1;
  348. }
  349. struct passwd pwd;
  350. struct passwd *res;
  351. getpwnam_r(owner, &pwd, buf, bufsize, &res);
  352. if (!res) {
  353. BLog(BLOG_ERROR, "getpwnam_r failed");
  354. free(buf);
  355. goto fail1;
  356. }
  357. int uid = pwd.pw_uid;
  358. free(buf);
  359. if (ioctl(fd, TUNSETOWNER, uid) < 0) {
  360. BLog(BLOG_ERROR, "TUNSETOWNER failed");
  361. goto fail1;
  362. }
  363. }
  364. if (ioctl(fd, TUNSETPERSIST, (void *)1) < 0) {
  365. BLog(BLOG_ERROR, "TUNSETPERSIST failed");
  366. goto fail1;
  367. }
  368. close(fd);
  369. return 1;
  370. fail1:
  371. close(fd);
  372. fail0:
  373. return 0;
  374. }
  375. int NCDIfConfig_remove_tuntap (const char *ifname, int tun)
  376. {
  377. int fd;
  378. if ((fd = open_tuntap(ifname, (tun ? IFF_TUN : IFF_TAP))) < 0) {
  379. goto fail0;
  380. }
  381. if (ioctl(fd, TUNSETPERSIST, (void *)0) < 0) {
  382. BLog(BLOG_ERROR, "TUNSETPERSIST failed");
  383. goto fail1;
  384. }
  385. close(fd);
  386. return 1;
  387. fail1:
  388. close(fd);
  389. fail0:
  390. return 0;
  391. }