NCDIfConfig.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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 "NCDIfConfig.h"
  46. #include <generated/blog_channel_NCDIfConfig.h>
  47. #define IP_CMD "ip"
  48. #define MODPROBE_CMD "modprobe"
  49. #define RESOLVCONF_FILE "/etc/resolv.conf"
  50. #define RESOLVCONF_TEMP_FILE "/etc/resolv.conf-ncd-temp"
  51. #define TUN_DEVNODE "/dev/net/tun"
  52. static int run_command (const char *cmd)
  53. {
  54. BLog(BLOG_INFO, "run: %s", cmd);
  55. return system(cmd);
  56. }
  57. static int write_to_file (uint8_t *data, size_t data_len, FILE *f)
  58. {
  59. while (data_len > 0) {
  60. size_t bytes = fwrite(data, 1, data_len, f);
  61. if (bytes == 0) {
  62. return 0;
  63. }
  64. data += bytes;
  65. data_len -= bytes;
  66. }
  67. return 1;
  68. }
  69. int NCDIfConfig_query (const char *ifname)
  70. {
  71. struct ifreq ifr;
  72. int flags = 0;
  73. int s = socket(AF_INET, SOCK_DGRAM, 0);
  74. if (!s) {
  75. BLog(BLOG_ERROR, "socket failed");
  76. goto fail0;
  77. }
  78. memset(&ifr, 0, sizeof(ifr));
  79. snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", ifname);
  80. if (ioctl(s, SIOCGIFFLAGS, &ifr)) {
  81. BLog(BLOG_ERROR, "SIOCGIFFLAGS failed");
  82. goto fail1;
  83. }
  84. flags |= NCDIFCONFIG_FLAG_EXISTS;
  85. if ((ifr.ifr_flags&IFF_UP)) {
  86. flags |= NCDIFCONFIG_FLAG_UP;
  87. if ((ifr.ifr_flags&IFF_RUNNING)) {
  88. flags |= NCDIFCONFIG_FLAG_RUNNING;
  89. }
  90. }
  91. fail1:
  92. close(s);
  93. fail0:
  94. return flags;
  95. }
  96. int NCDIfConfig_set_up (const char *ifname)
  97. {
  98. if (strlen(ifname) >= IFNAMSIZ) {
  99. BLog(BLOG_ERROR, "ifname too long");
  100. return 0;
  101. }
  102. char cmd[50 + IFNAMSIZ];
  103. sprintf(cmd, IP_CMD" link set %s up", ifname);
  104. return !run_command(cmd);
  105. }
  106. int NCDIfConfig_set_down (const char *ifname)
  107. {
  108. if (strlen(ifname) >= IFNAMSIZ) {
  109. BLog(BLOG_ERROR, "ifname too long");
  110. return 0;
  111. }
  112. char cmd[50 + IFNAMSIZ];
  113. sprintf(cmd, IP_CMD" link set %s down", ifname);
  114. return !run_command(cmd);
  115. }
  116. int NCDIfConfig_add_ipv4_addr (const char *ifname, struct ipv4_ifaddr ifaddr)
  117. {
  118. ASSERT(ifaddr.prefix >= 0)
  119. ASSERT(ifaddr.prefix <= 32)
  120. if (strlen(ifname) >= IFNAMSIZ) {
  121. BLog(BLOG_ERROR, "ifname too long");
  122. return 0;
  123. }
  124. uint8_t *addr = (uint8_t *)&ifaddr.addr;
  125. char cmd[50 + IFNAMSIZ];
  126. sprintf(cmd, IP_CMD" addr add %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"/%d dev %s", addr[0], addr[1], addr[2], addr[3], ifaddr.prefix, ifname);
  127. return !run_command(cmd);
  128. }
  129. int NCDIfConfig_remove_ipv4_addr (const char *ifname, struct ipv4_ifaddr ifaddr)
  130. {
  131. ASSERT(ifaddr.prefix >= 0)
  132. ASSERT(ifaddr.prefix <= 32)
  133. if (strlen(ifname) >= IFNAMSIZ) {
  134. BLog(BLOG_ERROR, "ifname too long");
  135. return 0;
  136. }
  137. uint8_t *addr = (uint8_t *)&ifaddr.addr;
  138. char cmd[50 + IFNAMSIZ];
  139. sprintf(cmd, IP_CMD" addr del %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"/%d dev %s", addr[0], addr[1], addr[2], addr[3], ifaddr.prefix, ifname);
  140. return !run_command(cmd);
  141. }
  142. int NCDIfConfig_add_ipv6_addr (const char *ifname, struct ipv6_ifaddr ifaddr)
  143. {
  144. ASSERT(ifaddr.prefix >= 0)
  145. ASSERT(ifaddr.prefix <= 128)
  146. if (strlen(ifname) >= IFNAMSIZ) {
  147. BLog(BLOG_ERROR, "ifname too long");
  148. return 0;
  149. }
  150. char addr_str[IPADDR6_PRINT_MAX];
  151. ipaddr6_print_addr(ifaddr.addr, addr_str);
  152. char cmd[40 + IPADDR6_PRINT_MAX + IFNAMSIZ];
  153. sprintf(cmd, IP_CMD" addr add %s/%d dev %s", addr_str, ifaddr.prefix, ifname);
  154. return !run_command(cmd);
  155. }
  156. int NCDIfConfig_remove_ipv6_addr (const char *ifname, struct ipv6_ifaddr ifaddr)
  157. {
  158. ASSERT(ifaddr.prefix >= 0)
  159. ASSERT(ifaddr.prefix <= 128)
  160. if (strlen(ifname) >= IFNAMSIZ) {
  161. BLog(BLOG_ERROR, "ifname too long");
  162. return 0;
  163. }
  164. char addr_str[IPADDR6_PRINT_MAX];
  165. ipaddr6_print_addr(ifaddr.addr, addr_str);
  166. char cmd[40 + IPADDR6_PRINT_MAX + IFNAMSIZ];
  167. sprintf(cmd, IP_CMD" addr del %s/%d dev %s", addr_str, ifaddr.prefix, ifname);
  168. return !run_command(cmd);
  169. }
  170. static int route_cmd (const char *cmdtype, struct ipv4_ifaddr dest, const uint32_t *gateway, int metric, const char *ifname)
  171. {
  172. ASSERT(!strcmp(cmdtype, "add") || !strcmp(cmdtype, "del"))
  173. ASSERT(dest.prefix >= 0)
  174. ASSERT(dest.prefix <= 32)
  175. if (strlen(ifname) >= IFNAMSIZ) {
  176. BLog(BLOG_ERROR, "ifname too long");
  177. return 0;
  178. }
  179. uint8_t *d_addr = (uint8_t *)&dest.addr;
  180. char gwstr[30];
  181. if (gateway) {
  182. const uint8_t *g_addr = (uint8_t *)gateway;
  183. sprintf(gwstr, " via %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8, g_addr[0], g_addr[1], g_addr[2], g_addr[3]);
  184. } else {
  185. gwstr[0] = '\0';
  186. }
  187. char cmd[120 + IFNAMSIZ];
  188. sprintf(cmd, IP_CMD" route %s %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"/%d%s metric %d dev %s",
  189. cmdtype, d_addr[0], d_addr[1], d_addr[2], d_addr[3], dest.prefix, gwstr, metric, ifname);
  190. return !run_command(cmd);
  191. }
  192. int NCDIfConfig_add_ipv4_route (struct ipv4_ifaddr dest, const uint32_t *gateway, int metric, const char *device)
  193. {
  194. return route_cmd("add", dest, gateway, metric, device);
  195. }
  196. int NCDIfConfig_remove_ipv4_route (struct ipv4_ifaddr dest, const uint32_t *gateway, int metric, const char *device)
  197. {
  198. return route_cmd("del", dest, gateway, metric, device);
  199. }
  200. static int route_cmd6 (const char *cmdtype, struct ipv6_ifaddr dest, const struct ipv6_addr *gateway, int metric, const char *ifname)
  201. {
  202. ASSERT(!strcmp(cmdtype, "add") || !strcmp(cmdtype, "del"))
  203. ASSERT(dest.prefix >= 0)
  204. ASSERT(dest.prefix <= 128)
  205. if (strlen(ifname) >= IFNAMSIZ) {
  206. BLog(BLOG_ERROR, "ifname too long");
  207. return 0;
  208. }
  209. char dest_str[IPADDR6_PRINT_MAX];
  210. ipaddr6_print_addr(dest.addr, dest_str);
  211. char gwstr[10 + IPADDR6_PRINT_MAX];
  212. if (gateway) {
  213. strcpy(gwstr, " via ");
  214. ipaddr6_print_addr(*gateway, gwstr + strlen(gwstr));
  215. } else {
  216. gwstr[0] = '\0';
  217. }
  218. char cmd[70 + IPADDR6_PRINT_MAX + IPADDR6_PRINT_MAX + IFNAMSIZ];
  219. sprintf(cmd, IP_CMD" route %s %s/%d%s metric %d dev %s",
  220. cmdtype, dest_str, dest.prefix, gwstr, metric, ifname);
  221. return !run_command(cmd);
  222. }
  223. int NCDIfConfig_add_ipv6_route (struct ipv6_ifaddr dest, const struct ipv6_addr *gateway, int metric, const char *device)
  224. {
  225. return route_cmd6("add", dest, gateway, metric, device);
  226. }
  227. int NCDIfConfig_remove_ipv6_route (struct ipv6_ifaddr dest, const struct ipv6_addr *gateway, int metric, const char *device)
  228. {
  229. return route_cmd6("del", dest, gateway, metric, device);
  230. }
  231. static int blackhole_route_cmd (const char *cmdtype, struct ipv4_ifaddr dest, int metric)
  232. {
  233. ASSERT(!strcmp(cmdtype, "add") || !strcmp(cmdtype, "del"))
  234. ASSERT(dest.prefix >= 0)
  235. ASSERT(dest.prefix <= 32)
  236. uint8_t *d_addr = (uint8_t *)&dest.addr;
  237. char cmd[120];
  238. sprintf(cmd, IP_CMD" route %s blackhole %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"/%d metric %d",
  239. cmdtype, d_addr[0], d_addr[1], d_addr[2], d_addr[3], dest.prefix, metric);
  240. return !run_command(cmd);
  241. }
  242. int NCDIfConfig_add_ipv4_blackhole_route (struct ipv4_ifaddr dest, int metric)
  243. {
  244. return blackhole_route_cmd("add", dest, metric);
  245. }
  246. int NCDIfConfig_remove_ipv4_blackhole_route (struct ipv4_ifaddr dest, int metric)
  247. {
  248. return blackhole_route_cmd("del", dest, metric);
  249. }
  250. static int blackhole_route_cmd6 (const char *cmdtype, struct ipv6_ifaddr dest, int metric)
  251. {
  252. ASSERT(!strcmp(cmdtype, "add") || !strcmp(cmdtype, "del"))
  253. ASSERT(dest.prefix >= 0)
  254. ASSERT(dest.prefix <= 128)
  255. char dest_str[IPADDR6_PRINT_MAX];
  256. ipaddr6_print_addr(dest.addr, dest_str);
  257. char cmd[70 + IPADDR6_PRINT_MAX];
  258. sprintf(cmd, IP_CMD" route %s blackhole %s/%d metric %d",
  259. cmdtype, dest_str, dest.prefix, metric);
  260. return !run_command(cmd);
  261. }
  262. int NCDIfConfig_add_ipv6_blackhole_route (struct ipv6_ifaddr dest, int metric)
  263. {
  264. return blackhole_route_cmd6("add", dest, metric);
  265. }
  266. int NCDIfConfig_remove_ipv6_blackhole_route (struct ipv6_ifaddr dest, int metric)
  267. {
  268. return blackhole_route_cmd6("del", dest, metric);
  269. }
  270. int NCDIfConfig_set_resolv_conf (const char *data, size_t data_len)
  271. {
  272. FILE *temp_file = fopen(RESOLVCONF_TEMP_FILE, "w");
  273. if (!temp_file) {
  274. BLog(BLOG_ERROR, "failed to open resolvconf temp file");
  275. goto fail0;
  276. }
  277. char line[] = "# generated by badvpn-ncd\n";
  278. if (!write_to_file((uint8_t *)line, strlen(line), temp_file) ||
  279. !write_to_file((uint8_t *)data, data_len, temp_file)
  280. ) {
  281. BLog(BLOG_ERROR, "failed to write to resolvconf temp file");
  282. goto fail1;
  283. }
  284. if (fclose(temp_file) != 0) {
  285. BLog(BLOG_ERROR, "failed to close resolvconf temp file");
  286. return 0;
  287. }
  288. if (rename(RESOLVCONF_TEMP_FILE, RESOLVCONF_FILE) < 0) {
  289. BLog(BLOG_ERROR, "failed to rename resolvconf temp file to resolvconf file");
  290. return 0;
  291. }
  292. return 1;
  293. fail1:
  294. fclose(temp_file);
  295. fail0:
  296. return 0;
  297. }
  298. static int open_tuntap (const char *ifname, int flags)
  299. {
  300. if (strlen(ifname) >= IFNAMSIZ) {
  301. BLog(BLOG_ERROR, "ifname too long");
  302. return -1;
  303. }
  304. int fd = open(TUN_DEVNODE, O_RDWR);
  305. if (fd < 0) {
  306. BLog(BLOG_ERROR, "open tun failed");
  307. return -1;
  308. }
  309. struct ifreq ifr;
  310. memset(&ifr, 0, sizeof(ifr));
  311. ifr.ifr_flags = flags;
  312. snprintf(ifr.ifr_name, IFNAMSIZ, "%s", ifname);
  313. if (ioctl(fd, TUNSETIFF, (void *)&ifr) < 0) {
  314. BLog(BLOG_ERROR, "TUNSETIFF failed");
  315. close(fd);
  316. return -1;
  317. }
  318. return fd;
  319. }
  320. int NCDIfConfig_make_tuntap (const char *ifname, const char *owner, int tun)
  321. {
  322. // load tun module if needed
  323. if (access(TUN_DEVNODE, F_OK) < 0) {
  324. if (run_command(MODPROBE_CMD" tun") != 0) {
  325. BLog(BLOG_ERROR, "modprobe tun failed");
  326. }
  327. }
  328. int fd;
  329. if ((fd = open_tuntap(ifname, (tun ? IFF_TUN : IFF_TAP))) < 0) {
  330. goto fail0;
  331. }
  332. if (owner) {
  333. long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
  334. if (bufsize < 0) {
  335. bufsize = 16384;
  336. }
  337. char *buf = malloc(bufsize);
  338. if (!buf) {
  339. BLog(BLOG_ERROR, "malloc failed");
  340. goto fail1;
  341. }
  342. struct passwd pwd;
  343. struct passwd *res;
  344. getpwnam_r(owner, &pwd, buf, bufsize, &res);
  345. if (!res) {
  346. BLog(BLOG_ERROR, "getpwnam_r failed");
  347. free(buf);
  348. goto fail1;
  349. }
  350. int uid = pwd.pw_uid;
  351. free(buf);
  352. if (ioctl(fd, TUNSETOWNER, uid) < 0) {
  353. BLog(BLOG_ERROR, "TUNSETOWNER failed");
  354. goto fail1;
  355. }
  356. }
  357. if (ioctl(fd, TUNSETPERSIST, (void *)1) < 0) {
  358. BLog(BLOG_ERROR, "TUNSETPERSIST failed");
  359. goto fail1;
  360. }
  361. close(fd);
  362. return 1;
  363. fail1:
  364. close(fd);
  365. fail0:
  366. return 0;
  367. }
  368. int NCDIfConfig_remove_tuntap (const char *ifname, int tun)
  369. {
  370. int fd;
  371. if ((fd = open_tuntap(ifname, (tun ? IFF_TUN : IFF_TAP))) < 0) {
  372. goto fail0;
  373. }
  374. if (ioctl(fd, TUNSETPERSIST, (void *)0) < 0) {
  375. BLog(BLOG_ERROR, "TUNSETPERSIST failed");
  376. goto fail1;
  377. }
  378. close(fd);
  379. return 1;
  380. fail1:
  381. close(fd);
  382. fail0:
  383. return 0;
  384. }