net_backend_badvpn.c 7.3 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/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->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->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->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 (NCDModuleInst *i)
  123. {
  124. // allocate instance
  125. struct instance *o = malloc(sizeof(*o));
  126. if (!o) {
  127. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  128. goto fail0;
  129. }
  130. NCDModuleInst_Backend_SetUser(i, o);
  131. // init arguments
  132. o->i = i;
  133. // read arguments
  134. NCDValRef ifname_arg;
  135. NCDValRef user_arg;
  136. NCDValRef exec_arg;
  137. NCDValRef args_arg;
  138. if (!NCDVal_ListRead(o->i->args, 4, &ifname_arg, &user_arg, &exec_arg, &args_arg)) {
  139. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  140. goto fail1;
  141. }
  142. if (!NCDVal_IsStringNoNulls(ifname_arg) || !NCDVal_IsStringNoNulls(user_arg) ||
  143. !NCDVal_IsStringNoNulls(exec_arg) || !NCDVal_IsList(args_arg)) {
  144. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  145. goto fail1;
  146. }
  147. o->ifname = NCDVal_StringValue(ifname_arg);
  148. o->user = NCDVal_StringValue(user_arg);
  149. o->exec = NCDVal_StringValue(exec_arg);
  150. o->args = args_arg;
  151. // check arguments
  152. size_t count = NCDVal_ListCount(o->args);
  153. for (size_t j = 0; j < count; j++) {
  154. NCDValRef arg = NCDVal_ListGet(o->args, j);
  155. if (!NCDVal_IsStringNoNulls(arg)) {
  156. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  157. goto fail1;
  158. }
  159. }
  160. // create TAP device
  161. if (!NCDIfConfig_make_tuntap(o->ifname, 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)) {
  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, 0)) {
  181. ModuleLog(o->i, BLOG_ERROR, "failed to remove TAP device");
  182. }
  183. fail1:
  184. free(o);
  185. fail0:
  186. NCDModuleInst_Backend_SetError(i);
  187. NCDModuleInst_Backend_Dead(i);
  188. }
  189. void instance_free (struct instance *o)
  190. {
  191. ASSERT(!o->started)
  192. NCDModuleInst *i = o->i;
  193. // free timer
  194. BReactor_RemoveTimer(o->i->iparams->reactor, &o->timer);
  195. // set device down
  196. if (!NCDIfConfig_set_down(o->ifname)) {
  197. ModuleLog(o->i, BLOG_ERROR, "failed to set device down");
  198. }
  199. // free TAP device
  200. if (!NCDIfConfig_remove_tuntap(o->ifname, 0)) {
  201. ModuleLog(o->i, BLOG_ERROR, "failed to remove TAP device");
  202. }
  203. // free instance
  204. free(o);
  205. NCDModuleInst_Backend_Dead(i);
  206. }
  207. static void func_die (void *vo)
  208. {
  209. struct instance *o = vo;
  210. ASSERT(!o->dying)
  211. if (!o->started) {
  212. instance_free(o);
  213. return;
  214. }
  215. // request termination
  216. BProcess_Terminate(&o->process);
  217. // remember dying
  218. o->dying = 1;
  219. }
  220. static const struct NCDModule modules[] = {
  221. {
  222. .type = "net.backend.badvpn",
  223. .func_new = func_new,
  224. .func_die = func_die
  225. }, {
  226. .type = NULL
  227. }
  228. };
  229. const struct NCDModuleGroup ncdmodule_net_backend_badvpn = {
  230. .modules = modules
  231. };