net_backend_badvpn.c 6.3 KB

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