interface_badvpn.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /**
  2. * @file interface_badvpn.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * BadVPN interface backend.
  25. */
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <misc/cmdline.h>
  29. #include <system/BProcess.h>
  30. #include <ncd/NCDInterfaceModule.h>
  31. #include <ncd/NCDIfConfig.h>
  32. struct instance {
  33. NCDInterfaceModuleInst *i;
  34. BProcess process;
  35. int need_terminate;
  36. };
  37. static int build_cmdline (struct instance *o, CmdLine *c)
  38. {
  39. if (!CmdLine_Init(c)) {
  40. goto fail0;
  41. }
  42. // find exec statement
  43. struct NCDConfig_statements *exec_st = NCDConfig_find_statement(o->i->conf->statements, "badvpn.exec");
  44. if (!exec_st) {
  45. NCDInterfaceModuleInst_Backend_Log(o->i, BLOG_ERROR, "badvpn.exec missing");
  46. goto fail1;
  47. }
  48. // check arity
  49. char *exec_arg;
  50. if (!NCDConfig_statement_has_one_arg(exec_st, &exec_arg)) {
  51. NCDInterfaceModuleInst_Backend_Log(o->i, BLOG_ERROR, "badvpn.exec: wrong arity");
  52. goto fail1;
  53. }
  54. // append to cmdline
  55. if (!CmdLine_Append(c, exec_arg)) {
  56. goto fail1;
  57. }
  58. // append tapdev
  59. if (!CmdLine_Append(c, "--tapdev") || !CmdLine_Append(c, o->i->conf->name)) {
  60. goto fail1;
  61. }
  62. // iterate arg statements
  63. struct NCDConfig_statements *st = o->i->conf->statements;
  64. while (st = NCDConfig_find_statement(st, "badvpn.arg")) {
  65. // iterate arguments
  66. struct NCDConfig_strings *arg = st->args;
  67. while (arg) {
  68. if (!CmdLine_Append(c, arg->value)) {
  69. goto fail1;
  70. }
  71. arg = arg->next;
  72. }
  73. st = st->next;
  74. }
  75. // terminate
  76. if (!CmdLine_Finish(c)) {
  77. goto fail1;
  78. }
  79. return 1;
  80. fail1:
  81. CmdLine_Free(c);
  82. fail0:
  83. return 0;
  84. }
  85. static void process_handler (struct instance *o, int normally, uint8_t normally_exit_status)
  86. {
  87. NCDInterfaceModuleInst_Backend_Log(o->i, BLOG_ERROR, "process terminated");
  88. // set not need terminate
  89. o->need_terminate = 0;
  90. // report error
  91. NCDInterfaceModuleInst_Backend_Error(o->i);
  92. return;
  93. }
  94. static void * func_new (NCDInterfaceModuleInst *i)
  95. {
  96. // allocate instance
  97. struct instance *o = malloc(sizeof(*o));
  98. if (!o) {
  99. NCDInterfaceModuleInst_Backend_Log(i, BLOG_ERROR, "failed to allocate instance");
  100. goto fail0;
  101. }
  102. // init arguments
  103. o->i = i;
  104. // create TAP device
  105. if (!NCDIfConfig_make_tuntap(o->i->conf->name, NULL, 0)) {
  106. NCDInterfaceModuleInst_Backend_Log(o->i, BLOG_ERROR, "failed to create TAP device");
  107. goto fail1;
  108. }
  109. // set device up
  110. if (!NCDIfConfig_set_up(o->i->conf->name)) {
  111. NCDInterfaceModuleInst_Backend_Log(o->i, BLOG_ERROR, "failed to set device up");
  112. goto fail2;
  113. }
  114. // build cmdline
  115. CmdLine cl;
  116. if (!build_cmdline(o, &cl)) {
  117. NCDInterfaceModuleInst_Backend_Log(o->i, BLOG_ERROR, "failed to build cmdline");
  118. goto fail2;
  119. }
  120. // start process
  121. if (!BProcess_Init(&o->process, o->i->manager, (BProcess_handler)process_handler, o, ((char **)cl.arr.v)[0], (char **)cl.arr.v)) {
  122. NCDInterfaceModuleInst_Backend_Log(o->i, BLOG_ERROR, "BProcess_Init failed");
  123. CmdLine_Free(&cl);
  124. goto fail2;
  125. }
  126. CmdLine_Free(&cl);
  127. // set need terminate
  128. o->need_terminate = 1;
  129. // report up
  130. NCDInterfaceModuleInst_Backend_Event(o->i, NCDINTERFACEMODULE_EVENT_UP);
  131. return o;
  132. fail2:
  133. NCDIfConfig_remove_tuntap(o->i->conf->name, 0);
  134. fail1:
  135. free(o);
  136. fail0:
  137. return NULL;
  138. }
  139. static void func_free (void *vo)
  140. {
  141. struct instance *o = vo;
  142. // order process to terminate
  143. if (o->need_terminate) {
  144. BProcess_Terminate(&o->process);
  145. }
  146. // free process
  147. BProcess_Free(&o->process);
  148. // remove TAP device
  149. NCDIfConfig_remove_tuntap(o->i->conf->name, 0);
  150. // free instance
  151. free(o);
  152. }
  153. static void func_finish (void *vo)
  154. {
  155. struct instance *o = vo;
  156. ASSERT(o->need_terminate)
  157. // order process to terminate
  158. BProcess_Terminate(&o->process);
  159. // set not need terminate
  160. o->need_terminate = 0;
  161. }
  162. const struct NCDInterfaceModule ncd_interface_badvpn = {
  163. .type = "badvpn",
  164. .func_new = func_new,
  165. .func_free = func_free,
  166. .func_finish = func_finish
  167. };