net_backend_badvpn.c 7.7 KB

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