net_backend_badvpn.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. const char *ifname;
  46. const char *user;
  47. const char *exec;
  48. NCDValRef 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. size_t count = NCDVal_ListCount(o->args);
  74. for (size_t j = 0; j < count; j++) {
  75. NCDValRef arg = NCDVal_ListGet(o->args, j);
  76. if (!CmdLine_Append(&c, NCDVal_StringValue(arg))) {
  77. goto fail1;
  78. }
  79. }
  80. // terminate cmdline
  81. if (!CmdLine_Finish(&c)) {
  82. goto fail1;
  83. }
  84. // start process
  85. 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)) {
  86. ModuleLog(o->i, BLOG_ERROR, "BProcess_Init failed");
  87. goto fail1;
  88. }
  89. CmdLine_Free(&c);
  90. // set started
  91. o->started = 1;
  92. return;
  93. fail1:
  94. CmdLine_Free(&c);
  95. fail0:
  96. // retry
  97. o->started = 0;
  98. BReactor_SetTimer(o->i->params->iparams->reactor, &o->timer);
  99. }
  100. void process_handler (struct instance *o, int normally, uint8_t normally_exit_status)
  101. {
  102. ASSERT(o->started)
  103. ModuleLog(o->i, BLOG_INFO, "process terminated");
  104. // free process
  105. BProcess_Free(&o->process);
  106. // set not started
  107. o->started = 0;
  108. if (o->dying) {
  109. instance_free(o);
  110. return;
  111. }
  112. // set timer
  113. BReactor_SetTimer(o->i->params->iparams->reactor, &o->timer);
  114. }
  115. void timer_handler (struct instance *o)
  116. {
  117. ASSERT(!o->started)
  118. ModuleLog(o->i, BLOG_INFO, "retrying");
  119. // try starting process again
  120. try_process(o);
  121. }
  122. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  123. {
  124. struct instance *o = vo;
  125. o->i = i;
  126. // read arguments
  127. NCDValRef ifname_arg;
  128. NCDValRef user_arg;
  129. NCDValRef exec_arg;
  130. NCDValRef args_arg;
  131. if (!NCDVal_ListRead(params->args, 4, &ifname_arg, &user_arg, &exec_arg, &args_arg)) {
  132. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  133. goto fail0;
  134. }
  135. if (!NCDVal_IsStringNoNulls(ifname_arg) || !NCDVal_IsStringNoNulls(user_arg) ||
  136. !NCDVal_IsStringNoNulls(exec_arg) || !NCDVal_IsList(args_arg)) {
  137. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  138. goto fail0;
  139. }
  140. o->ifname = NCDVal_StringValue(ifname_arg);
  141. o->user = NCDVal_StringValue(user_arg);
  142. o->exec = NCDVal_StringValue(exec_arg);
  143. o->args = args_arg;
  144. // check arguments
  145. size_t count = NCDVal_ListCount(o->args);
  146. for (size_t j = 0; j < count; j++) {
  147. NCDValRef arg = NCDVal_ListGet(o->args, j);
  148. if (!NCDVal_IsStringNoNulls(arg)) {
  149. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  150. goto fail0;
  151. }
  152. }
  153. // create TAP device
  154. if (!NCDIfConfig_make_tuntap(o->ifname, o->user, 0)) {
  155. ModuleLog(o->i, BLOG_ERROR, "failed to create TAP device");
  156. goto fail0;
  157. }
  158. // set device up
  159. if (!NCDIfConfig_set_up(o->ifname)) {
  160. ModuleLog(o->i, BLOG_ERROR, "failed to set device up");
  161. goto fail1;
  162. }
  163. // set not dying
  164. o->dying = 0;
  165. // init timer
  166. BTimer_Init(&o->timer, RETRY_TIME, (BTimer_handler)timer_handler, o);
  167. // signal up
  168. NCDModuleInst_Backend_Up(o->i);
  169. // try starting process
  170. try_process(o);
  171. return;
  172. fail1:
  173. if (!NCDIfConfig_remove_tuntap(o->ifname, 0)) {
  174. ModuleLog(o->i, BLOG_ERROR, "failed to remove TAP device");
  175. }
  176. fail0:
  177. NCDModuleInst_Backend_SetError(i);
  178. NCDModuleInst_Backend_Dead(i);
  179. }
  180. void instance_free (struct instance *o)
  181. {
  182. ASSERT(!o->started)
  183. // free timer
  184. BReactor_RemoveTimer(o->i->params->iparams->reactor, &o->timer);
  185. // set device down
  186. if (!NCDIfConfig_set_down(o->ifname)) {
  187. ModuleLog(o->i, BLOG_ERROR, "failed to set device down");
  188. }
  189. // free TAP device
  190. if (!NCDIfConfig_remove_tuntap(o->ifname, 0)) {
  191. ModuleLog(o->i, BLOG_ERROR, "failed to remove TAP device");
  192. }
  193. NCDModuleInst_Backend_Dead(o->i);
  194. }
  195. static void func_die (void *vo)
  196. {
  197. struct instance *o = vo;
  198. ASSERT(!o->dying)
  199. if (!o->started) {
  200. instance_free(o);
  201. return;
  202. }
  203. // request termination
  204. BProcess_Terminate(&o->process);
  205. // remember dying
  206. o->dying = 1;
  207. }
  208. static const struct NCDModule modules[] = {
  209. {
  210. .type = "net.backend.badvpn",
  211. .func_new2 = func_new,
  212. .func_die = func_die,
  213. .alloc_size = sizeof(struct instance)
  214. }, {
  215. .type = NULL
  216. }
  217. };
  218. struct NCDModuleGroup ncdmodule_net_backend_badvpn = {
  219. .modules = modules
  220. };