net_backend_badvpn.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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/extra/NCDIfConfig.h>
  39. #include <ncd/module_common.h>
  40. #include <generated/blog_channel_ncd_net_backend_badvpn.h>
  41. #define RETRY_TIME 5000
  42. struct instance {
  43. NCDModuleInst *i;
  44. NCDValNullTermString ifname_nts;
  45. const char *user;
  46. size_t user_len;
  47. const char *exec;
  48. size_t exec_len;
  49. NCDValRef args;
  50. int dying;
  51. int started;
  52. BTimer timer;
  53. BProcess process;
  54. };
  55. static void try_process (struct instance *o);
  56. static void process_handler (struct instance *o, int normally, uint8_t normally_exit_status);
  57. static void timer_handler (struct instance *o);
  58. static void instance_free (struct instance *o);
  59. void try_process (struct instance *o)
  60. {
  61. CmdLine c;
  62. if (!CmdLine_Init(&c)) {
  63. goto fail0;
  64. }
  65. // append exec
  66. if (!CmdLine_AppendNoNull(&c, o->exec, o->exec_len)) {
  67. goto fail1;
  68. }
  69. // append tapdev
  70. if (!CmdLine_Append(&c, "--tapdev") || !CmdLine_Append(&c, o->ifname_nts.data)) {
  71. goto fail1;
  72. }
  73. // append arguments
  74. size_t count = NCDVal_ListCount(o->args);
  75. for (size_t j = 0; j < count; j++) {
  76. NCDValRef arg = NCDVal_ListGet(o->args, j);
  77. if (!CmdLine_AppendNoNull(&c, NCDVal_StringData(arg), NCDVal_StringLength(arg))) {
  78. goto fail1;
  79. }
  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->iparams->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->iparams->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->iparams->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 (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  124. {
  125. struct instance *o = vo;
  126. o->i = i;
  127. // read arguments
  128. NCDValRef ifname_arg;
  129. NCDValRef user_arg;
  130. NCDValRef exec_arg;
  131. NCDValRef args_arg;
  132. if (!NCDVal_ListRead(params->args, 4, &ifname_arg, &user_arg, &exec_arg, &args_arg)) {
  133. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  134. goto fail0;
  135. }
  136. if (!NCDVal_IsStringNoNulls(ifname_arg) || !NCDVal_IsStringNoNulls(user_arg) ||
  137. !NCDVal_IsStringNoNulls(exec_arg) || !NCDVal_IsList(args_arg)) {
  138. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  139. goto fail0;
  140. }
  141. o->user = NCDVal_StringData(user_arg);
  142. o->user_len = NCDVal_StringLength(user_arg);
  143. o->exec = NCDVal_StringData(exec_arg);
  144. o->exec_len = NCDVal_StringLength(exec_arg);
  145. o->args = args_arg;
  146. // check arguments
  147. size_t count = NCDVal_ListCount(o->args);
  148. for (size_t j = 0; j < count; j++) {
  149. NCDValRef arg = NCDVal_ListGet(o->args, j);
  150. if (!NCDVal_IsStringNoNulls(arg)) {
  151. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  152. goto fail0;
  153. }
  154. }
  155. // null terminate ifname
  156. if (!NCDVal_StringNullTerminate(ifname_arg, &o->ifname_nts)) {
  157. ModuleLog(i, BLOG_ERROR, "NCDVal_StringNullTerminate failed");
  158. goto fail0;
  159. }
  160. // create TAP device
  161. if (!NCDIfConfig_make_tuntap(o->ifname_nts.data, o->user, 0)) {
  162. ModuleLog(o->i, BLOG_ERROR, "failed to create TAP device");
  163. goto fail1;
  164. }
  165. // set device up
  166. if (!NCDIfConfig_set_up(o->ifname_nts.data)) {
  167. ModuleLog(o->i, BLOG_ERROR, "failed to set device up");
  168. goto fail2;
  169. }
  170. // set not dying
  171. o->dying = 0;
  172. // init timer
  173. BTimer_Init(&o->timer, RETRY_TIME, (BTimer_handler)timer_handler, o);
  174. // signal up
  175. NCDModuleInst_Backend_Up(o->i);
  176. // try starting process
  177. try_process(o);
  178. return;
  179. fail2:
  180. if (!NCDIfConfig_remove_tuntap(o->ifname_nts.data, 0)) {
  181. ModuleLog(o->i, BLOG_ERROR, "failed to remove TAP device");
  182. }
  183. fail1:
  184. NCDValNullTermString_Free(&o->ifname_nts);
  185. fail0:
  186. NCDModuleInst_Backend_DeadError(i);
  187. }
  188. void instance_free (struct instance *o)
  189. {
  190. ASSERT(!o->started)
  191. // free timer
  192. BReactor_RemoveTimer(o->i->params->iparams->reactor, &o->timer);
  193. // set device down
  194. if (!NCDIfConfig_set_down(o->ifname_nts.data)) {
  195. ModuleLog(o->i, BLOG_ERROR, "failed to set device down");
  196. }
  197. // free TAP device
  198. if (!NCDIfConfig_remove_tuntap(o->ifname_nts.data, 0)) {
  199. ModuleLog(o->i, BLOG_ERROR, "failed to remove TAP device");
  200. }
  201. // free ifname nts
  202. NCDValNullTermString_Free(&o->ifname_nts);
  203. NCDModuleInst_Backend_Dead(o->i);
  204. }
  205. static void func_die (void *vo)
  206. {
  207. struct instance *o = vo;
  208. ASSERT(!o->dying)
  209. if (!o->started) {
  210. instance_free(o);
  211. return;
  212. }
  213. // request termination
  214. BProcess_Terminate(&o->process);
  215. // remember dying
  216. o->dying = 1;
  217. }
  218. static struct NCDModule modules[] = {
  219. {
  220. .type = "net.backend.badvpn",
  221. .func_new2 = func_new,
  222. .func_die = func_die,
  223. .alloc_size = sizeof(struct instance)
  224. }, {
  225. .type = NULL
  226. }
  227. };
  228. const struct NCDModuleGroup ncdmodule_net_backend_badvpn = {
  229. .modules = modules
  230. };