net_backend_badvpn.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /**
  2. * @file net_backend_badvpn.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. * @section DESCRIPTION
  23. *
  24. * BadVPN interface module.
  25. *
  26. * Synopsis: net.backend.badvpn(string ifname, string user, string exec, list(string) args)
  27. */
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <misc/cmdline.h>
  31. #include <ncd/NCDModule.h>
  32. #include <ncd/NCDIfConfig.h>
  33. #include <generated/blog_channel_ncd_net_backend_badvpn.h>
  34. #define RETRY_TIME 5000
  35. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  36. struct instance {
  37. NCDModuleInst *i;
  38. char *ifname;
  39. char *user;
  40. char *exec;
  41. NCDValue *args;
  42. int dying;
  43. int started;
  44. BTimer timer;
  45. BProcess process;
  46. };
  47. static void try_process (struct instance *o);
  48. static void process_handler (struct instance *o, int normally, uint8_t normally_exit_status);
  49. static void timer_handler (struct instance *o);
  50. static void instance_free (struct instance *o);
  51. void try_process (struct instance *o)
  52. {
  53. CmdLine c;
  54. if (!CmdLine_Init(&c)) {
  55. goto fail0;
  56. }
  57. // append exec
  58. if (!CmdLine_Append(&c, o->exec)) {
  59. goto fail1;
  60. }
  61. // append tapdev
  62. if (!CmdLine_Append(&c, "--tapdev") || !CmdLine_Append(&c, o->ifname)) {
  63. goto fail1;
  64. }
  65. // append arguments
  66. NCDValue *arg = NCDValue_ListFirst(o->args);
  67. while (arg) {
  68. // append argument
  69. if (!CmdLine_Append(&c, NCDValue_StringValue(arg))) {
  70. goto fail1;
  71. }
  72. arg = NCDValue_ListNext(o->args, arg);
  73. }
  74. // terminate cmdline
  75. if (!CmdLine_Finish(&c)) {
  76. goto fail1;
  77. }
  78. // start process
  79. if (!BProcess_Init(&o->process, o->i->manager, (BProcess_handler)process_handler, o, ((char **)c.arr.v)[0], (char **)c.arr.v, o->user)) {
  80. ModuleLog(o->i, BLOG_ERROR, "BProcess_Init failed");
  81. goto fail1;
  82. }
  83. CmdLine_Free(&c);
  84. // set started
  85. o->started = 1;
  86. return;
  87. fail1:
  88. CmdLine_Free(&c);
  89. fail0:
  90. // retry
  91. o->started = 0;
  92. BReactor_SetTimer(o->i->reactor, &o->timer);
  93. }
  94. void process_handler (struct instance *o, int normally, uint8_t normally_exit_status)
  95. {
  96. ASSERT(o->started)
  97. ModuleLog(o->i, BLOG_INFO, "process terminated");
  98. // free process
  99. BProcess_Free(&o->process);
  100. // set not started
  101. o->started = 0;
  102. if (o->dying) {
  103. instance_free(o);
  104. return;
  105. }
  106. // set timer
  107. BReactor_SetTimer(o->i->reactor, &o->timer);
  108. }
  109. void timer_handler (struct instance *o)
  110. {
  111. ASSERT(!o->started)
  112. ModuleLog(o->i, BLOG_INFO, "retrying");
  113. // try starting process again
  114. try_process(o);
  115. }
  116. static void func_new (NCDModuleInst *i)
  117. {
  118. // allocate instance
  119. struct instance *o = malloc(sizeof(*o));
  120. if (!o) {
  121. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  122. goto fail0;
  123. }
  124. NCDModuleInst_Backend_SetUser(i, o);
  125. // init arguments
  126. o->i = i;
  127. // read arguments
  128. NCDValue *ifname_arg;
  129. NCDValue *user_arg;
  130. NCDValue *exec_arg;
  131. NCDValue *args_arg;
  132. if (!NCDValue_ListRead(o->i->args, 4, &ifname_arg, &user_arg, &exec_arg, &args_arg)) {
  133. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  134. goto fail1;
  135. }
  136. if (NCDValue_Type(ifname_arg) != NCDVALUE_STRING || NCDValue_Type(user_arg) != NCDVALUE_STRING ||
  137. NCDValue_Type(exec_arg) != NCDVALUE_STRING || NCDValue_Type(args_arg) != NCDVALUE_LIST) {
  138. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  139. goto fail1;
  140. }
  141. o->ifname = NCDValue_StringValue(ifname_arg);
  142. o->user = NCDValue_StringValue(user_arg);
  143. o->exec = NCDValue_StringValue(exec_arg);
  144. o->args = args_arg;
  145. // check arguments
  146. NCDValue *arg = NCDValue_ListFirst(o->args);
  147. while (arg) {
  148. if (NCDValue_Type(arg) != NCDVALUE_STRING) {
  149. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  150. goto fail1;
  151. }
  152. arg = NCDValue_ListNext(o->args, arg);
  153. }
  154. // create TAP device
  155. if (!NCDIfConfig_make_tuntap(o->ifname, o->user, 0)) {
  156. ModuleLog(o->i, BLOG_ERROR, "failed to create TAP device");
  157. goto fail1;
  158. }
  159. // set device up
  160. if (!NCDIfConfig_set_up(o->ifname)) {
  161. ModuleLog(o->i, BLOG_ERROR, "failed to set device up");
  162. goto fail2;
  163. }
  164. // set not dying
  165. o->dying = 0;
  166. // init timer
  167. BTimer_Init(&o->timer, RETRY_TIME, (BTimer_handler)timer_handler, o);
  168. // signal up
  169. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  170. // try starting process
  171. try_process(o);
  172. return;
  173. fail2:
  174. if (!NCDIfConfig_remove_tuntap(o->ifname, 0)) {
  175. ModuleLog(o->i, BLOG_ERROR, "failed to remove TAP device");
  176. }
  177. fail1:
  178. free(o);
  179. fail0:
  180. NCDModuleInst_Backend_SetError(i);
  181. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  182. }
  183. void instance_free (struct instance *o)
  184. {
  185. ASSERT(!o->started)
  186. NCDModuleInst *i = o->i;
  187. // free timer
  188. BReactor_RemoveTimer(o->i->reactor, &o->timer);
  189. // set device down
  190. if (!NCDIfConfig_set_down(o->ifname)) {
  191. ModuleLog(o->i, BLOG_ERROR, "failed to set device down");
  192. }
  193. // free TAP device
  194. if (!NCDIfConfig_remove_tuntap(o->ifname, 0)) {
  195. ModuleLog(o->i, BLOG_ERROR, "failed to remove TAP device");
  196. }
  197. // free instance
  198. free(o);
  199. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  200. }
  201. static void func_die (void *vo)
  202. {
  203. struct instance *o = vo;
  204. ASSERT(!o->dying)
  205. if (!o->started) {
  206. instance_free(o);
  207. return;
  208. }
  209. // request termination
  210. BProcess_Terminate(&o->process);
  211. // remember dying
  212. o->dying = 1;
  213. }
  214. static const struct NCDModule modules[] = {
  215. {
  216. .type = "net.backend.badvpn",
  217. .func_new = func_new,
  218. .func_die = func_die
  219. }, {
  220. .type = NULL
  221. }
  222. };
  223. const struct NCDModuleGroup ncdmodule_net_backend_badvpn = {
  224. .modules = modules
  225. };