net_backend_badvpn.c 6.3 KB

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