net_backend_badvpn.c 7.4 KB

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