net_backend_badvpn.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. NCDValNullTermString user_nts;
  46. MemRef exec;
  47. NCDValRef args;
  48. int dying;
  49. int started;
  50. BTimer timer;
  51. BProcess process;
  52. };
  53. static void try_process (struct instance *o);
  54. static void process_handler (struct instance *o, int normally, uint8_t normally_exit_status);
  55. static void timer_handler (struct instance *o);
  56. static void instance_free (struct instance *o);
  57. void try_process (struct instance *o)
  58. {
  59. CmdLine c;
  60. if (!CmdLine_Init(&c)) {
  61. goto fail0;
  62. }
  63. // append exec
  64. if (!CmdLine_AppendNoNullMr(&c, o->exec)) {
  65. goto fail1;
  66. }
  67. // append tapdev
  68. if (!CmdLine_Append(&c, "--tapdev") || !CmdLine_Append(&c, o->ifname_nts.data)) {
  69. goto fail1;
  70. }
  71. // append arguments
  72. size_t count = NCDVal_ListCount(o->args);
  73. for (size_t j = 0; j < count; j++) {
  74. NCDValRef arg = NCDVal_ListGet(o->args, j);
  75. if (!CmdLine_AppendNoNullMr(&c, NCDVal_StringMemRef(arg))) {
  76. goto fail1;
  77. }
  78. }
  79. // terminate cmdline
  80. if (!CmdLine_Finish(&c)) {
  81. goto fail1;
  82. }
  83. // start process
  84. 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_nts.data)) {
  85. ModuleLog(o->i, BLOG_ERROR, "BProcess_Init failed");
  86. goto fail1;
  87. }
  88. CmdLine_Free(&c);
  89. // set started
  90. o->started = 1;
  91. return;
  92. fail1:
  93. CmdLine_Free(&c);
  94. fail0:
  95. // retry
  96. o->started = 0;
  97. BReactor_SetTimer(o->i->params->iparams->reactor, &o->timer);
  98. }
  99. void process_handler (struct instance *o, int normally, uint8_t normally_exit_status)
  100. {
  101. ASSERT(o->started)
  102. ModuleLog(o->i, BLOG_INFO, "process terminated");
  103. // free process
  104. BProcess_Free(&o->process);
  105. // set not started
  106. o->started = 0;
  107. if (o->dying) {
  108. instance_free(o);
  109. return;
  110. }
  111. // set timer
  112. BReactor_SetTimer(o->i->params->iparams->reactor, &o->timer);
  113. }
  114. void timer_handler (struct instance *o)
  115. {
  116. ASSERT(!o->started)
  117. ModuleLog(o->i, BLOG_INFO, "retrying");
  118. // try starting process again
  119. try_process(o);
  120. }
  121. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  122. {
  123. struct instance *o = vo;
  124. o->i = i;
  125. // read arguments
  126. NCDValRef ifname_arg;
  127. NCDValRef user_arg;
  128. NCDValRef exec_arg;
  129. NCDValRef args_arg;
  130. if (!NCDVal_ListRead(params->args, 4, &ifname_arg, &user_arg, &exec_arg, &args_arg)) {
  131. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  132. goto fail0;
  133. }
  134. if (!NCDVal_IsStringNoNulls(ifname_arg) || !NCDVal_IsStringNoNulls(user_arg) ||
  135. !NCDVal_IsStringNoNulls(exec_arg) || !NCDVal_IsList(args_arg)) {
  136. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  137. goto fail0;
  138. }
  139. o->exec = NCDVal_StringMemRef(exec_arg);
  140. o->args = args_arg;
  141. // check arguments
  142. size_t count = NCDVal_ListCount(o->args);
  143. for (size_t j = 0; j < count; j++) {
  144. NCDValRef arg = NCDVal_ListGet(o->args, j);
  145. if (!NCDVal_IsStringNoNulls(arg)) {
  146. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  147. goto fail0;
  148. }
  149. }
  150. // null terminate user
  151. if (!NCDVal_StringNullTerminate(user_arg, &o->user_nts)) {
  152. ModuleLog(i, BLOG_ERROR, "NCDVal_StringNullTerminate failed");
  153. goto fail0;
  154. }
  155. // null terminate ifname
  156. if (!NCDVal_StringNullTerminate(ifname_arg, &o->ifname_nts)) {
  157. ModuleLog(i, BLOG_ERROR, "NCDVal_StringNullTerminate failed");
  158. goto fail1;
  159. }
  160. // create TAP device
  161. if (!NCDIfConfig_make_tuntap(o->ifname_nts.data, o->user_nts.data, 0)) {
  162. ModuleLog(o->i, BLOG_ERROR, "failed to create TAP device");
  163. goto fail2;
  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 fail3;
  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. fail3:
  180. if (!NCDIfConfig_remove_tuntap(o->ifname_nts.data, 0)) {
  181. ModuleLog(o->i, BLOG_ERROR, "failed to remove TAP device");
  182. }
  183. fail2:
  184. NCDValNullTermString_Free(&o->ifname_nts);
  185. fail1:
  186. NCDValNullTermString_Free(&o->user_nts);
  187. fail0:
  188. NCDModuleInst_Backend_DeadError(i);
  189. }
  190. void instance_free (struct instance *o)
  191. {
  192. ASSERT(!o->started)
  193. // free timer
  194. BReactor_RemoveTimer(o->i->params->iparams->reactor, &o->timer);
  195. // set device down
  196. if (!NCDIfConfig_set_down(o->ifname_nts.data)) {
  197. ModuleLog(o->i, BLOG_ERROR, "failed to set device down");
  198. }
  199. // free TAP device
  200. if (!NCDIfConfig_remove_tuntap(o->ifname_nts.data, 0)) {
  201. ModuleLog(o->i, BLOG_ERROR, "failed to remove TAP device");
  202. }
  203. // free ifname nts
  204. NCDValNullTermString_Free(&o->ifname_nts);
  205. // free user nts
  206. NCDValNullTermString_Free(&o->user_nts);
  207. NCDModuleInst_Backend_Dead(o->i);
  208. }
  209. static void func_die (void *vo)
  210. {
  211. struct instance *o = vo;
  212. ASSERT(!o->dying)
  213. if (!o->started) {
  214. instance_free(o);
  215. return;
  216. }
  217. // request termination
  218. BProcess_Terminate(&o->process);
  219. // remember dying
  220. o->dying = 1;
  221. }
  222. static struct NCDModule modules[] = {
  223. {
  224. .type = "net.backend.badvpn",
  225. .func_new2 = func_new,
  226. .func_die = func_die,
  227. .alloc_size = sizeof(struct instance)
  228. }, {
  229. .type = NULL
  230. }
  231. };
  232. const struct NCDModuleGroup ncdmodule_net_backend_badvpn = {
  233. .modules = modules
  234. };