net_backend_wpa_supplicant.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /**
  2. * @file net_backend_wpa_supplicant.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. * Wireless interface module which runs wpa_supplicant.
  25. *
  26. * Note: wpa_supplicant does not monitor the state of rfkill switches and will fail to
  27. * start if the switch is of when it is started, and will stop working indefinitely if the
  28. * switch is turned off while it is running. Therefore, you should put a "net.backend.rfkill"
  29. * statement in front of the wpa_supplicant statement.
  30. *
  31. * Synopsis: net.backend.wpa_supplicant(string ifname, string conf, string exec, list(string) args)
  32. */
  33. #include <stdlib.h>
  34. #include <misc/cmdline.h>
  35. #include <misc/string_begins_with.h>
  36. #include <flow/LineBuffer.h>
  37. #include <system/BInputProcess.h>
  38. #include <ncd/NCDModule.h>
  39. #include <generated/blog_channel_ncd_net_backend_wpa_supplicant.h>
  40. #define MAX_LINE_LEN 512
  41. #define EVENT_STRING_CONNECTED "CTRL-EVENT-CONNECTED"
  42. #define EVENT_STRING_DISCONNECTED "CTRL-EVENT-DISCONNECTED"
  43. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  44. struct instance {
  45. NCDModuleInst *i;
  46. char *ifname;
  47. char *conf;
  48. char *exec;
  49. NCDValue *args;
  50. int dying;
  51. int up;
  52. BInputProcess process;
  53. int have_pipe;
  54. LineBuffer pipe_buffer;
  55. PacketPassInterface pipe_input;
  56. };
  57. static int build_cmdline (struct instance *o, CmdLine *c);
  58. static void process_handler_terminated (struct instance *o, int normally, uint8_t normally_exit_status);
  59. static void process_handler_closed (struct instance *o, int is_error);
  60. static void process_pipe_handler_send (struct instance *o, uint8_t *data, int data_len);
  61. static void instance_free (struct instance *o);
  62. int build_cmdline (struct instance *o, CmdLine *c)
  63. {
  64. if (!CmdLine_Init(c)) {
  65. goto fail0;
  66. }
  67. // append exec
  68. if (!CmdLine_Append(c, o->exec)) {
  69. goto fail1;
  70. }
  71. // append user arguments
  72. NCDValue *arg = NCDValue_ListFirst(o->args);
  73. while (arg) {
  74. if (NCDValue_Type(arg) != NCDVALUE_STRING) {
  75. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  76. goto fail1;
  77. }
  78. // append argument
  79. if (!CmdLine_Append(c, NCDValue_StringValue(arg))) {
  80. goto fail1;
  81. }
  82. arg = NCDValue_ListNext(o->args, arg);
  83. }
  84. // append interface name
  85. if (!CmdLine_Append(c, "-i") || !CmdLine_Append(c, o->ifname)) {
  86. goto fail1;
  87. }
  88. // append config file
  89. if (!CmdLine_Append(c, "-c") || !CmdLine_Append(c, o->conf)) {
  90. goto fail1;
  91. }
  92. // terminate cmdline
  93. if (!CmdLine_Finish(c)) {
  94. goto fail1;
  95. }
  96. return 1;
  97. fail1:
  98. CmdLine_Free(c);
  99. fail0:
  100. return 0;
  101. }
  102. void process_handler_terminated (struct instance *o, int normally, uint8_t normally_exit_status)
  103. {
  104. ModuleLog(o->i, (o->dying ? BLOG_INFO : BLOG_ERROR), "process terminated");
  105. if (!o->dying) {
  106. NCDModuleInst_Backend_SetError(o->i);
  107. }
  108. // die
  109. instance_free(o);
  110. return;
  111. }
  112. void process_handler_closed (struct instance *o, int is_error)
  113. {
  114. ASSERT(o->have_pipe)
  115. if (is_error) {
  116. ModuleLog(o->i, BLOG_ERROR, "pipe error");
  117. } else {
  118. ModuleLog(o->i, BLOG_INFO, "pipe closed");
  119. }
  120. // free buffer
  121. LineBuffer_Free(&o->pipe_buffer);
  122. // free input interface
  123. PacketPassInterface_Free(&o->pipe_input);
  124. // set have no pipe
  125. o->have_pipe = 0;
  126. }
  127. void process_pipe_handler_send (struct instance *o, uint8_t *data, int data_len)
  128. {
  129. ASSERT(o->have_pipe)
  130. ASSERT(data_len > 0)
  131. // accept packet
  132. PacketPassInterface_Done(&o->pipe_input);
  133. if (o->dying) {
  134. return;
  135. }
  136. if (data_begins_with((char *)data, data_len, EVENT_STRING_CONNECTED)) {
  137. ModuleLog(o->i, BLOG_INFO, "connected event");
  138. if (!o->up) {
  139. o->up = 1;
  140. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  141. }
  142. }
  143. else if (data_begins_with((char *)data, data_len, EVENT_STRING_DISCONNECTED)) {
  144. ModuleLog(o->i, BLOG_INFO, "disconnected event");
  145. if (o->up) {
  146. o->up = 0;
  147. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_DOWN);
  148. }
  149. }
  150. }
  151. static void func_new (NCDModuleInst *i)
  152. {
  153. // allocate instance
  154. struct instance *o = malloc(sizeof(*o));
  155. if (!o) {
  156. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  157. goto fail0;
  158. }
  159. NCDModuleInst_Backend_SetUser(i, o);
  160. // init arguments
  161. o->i = i;
  162. // read arguments
  163. NCDValue *ifname_arg;
  164. NCDValue *conf_arg;
  165. NCDValue *exec_arg;
  166. NCDValue *args_arg;
  167. if (!NCDValue_ListRead(o->i->args, 4, &ifname_arg, &conf_arg, &exec_arg, &args_arg)) {
  168. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  169. goto fail1;
  170. }
  171. if (NCDValue_Type(ifname_arg) != NCDVALUE_STRING || NCDValue_Type(conf_arg) != NCDVALUE_STRING ||
  172. NCDValue_Type(exec_arg) != NCDVALUE_STRING || NCDValue_Type(args_arg) != NCDVALUE_LIST) {
  173. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  174. goto fail1;
  175. }
  176. o->ifname = NCDValue_StringValue(ifname_arg);
  177. o->conf = NCDValue_StringValue(conf_arg);
  178. o->exec = NCDValue_StringValue(exec_arg);
  179. o->args = args_arg;
  180. // set not dying
  181. o->dying = 0;
  182. // set not up
  183. o->up = 0;
  184. // build process cmdline
  185. CmdLine c;
  186. if (!build_cmdline(o, &c)) {
  187. ModuleLog(o->i, BLOG_ERROR, "failed to build cmdline");
  188. goto fail1;
  189. }
  190. // init process
  191. if (!BInputProcess_Init(&o->process, o->i->reactor, o->i->manager, o,
  192. (BInputProcess_handler_terminated)process_handler_terminated,
  193. (BInputProcess_handler_closed)process_handler_closed
  194. )) {
  195. ModuleLog(o->i, BLOG_ERROR, "BInputProcess_Init failed");
  196. goto fail2;
  197. }
  198. // init input interface
  199. PacketPassInterface_Init(&o->pipe_input, MAX_LINE_LEN, (PacketPassInterface_handler_send)process_pipe_handler_send, o, BReactor_PendingGroup(o->i->reactor));
  200. // init buffer
  201. if (!LineBuffer_Init(&o->pipe_buffer, BInputProcess_GetInput(&o->process), &o->pipe_input, MAX_LINE_LEN, '\n')) {
  202. ModuleLog(o->i, BLOG_ERROR, "LineBuffer_Init failed");
  203. goto fail3;
  204. }
  205. // set have pipe
  206. o->have_pipe = 1;
  207. // start process
  208. if (!BInputProcess_Start(&o->process, ((char **)c.arr.v)[0], (char **)c.arr.v, NULL)) {
  209. ModuleLog(o->i, BLOG_ERROR, "BInputProcess_Start failed");
  210. goto fail4;
  211. }
  212. CmdLine_Free(&c);
  213. return;
  214. fail4:
  215. LineBuffer_Free(&o->pipe_buffer);
  216. fail3:
  217. PacketPassInterface_Free(&o->pipe_input);
  218. BInputProcess_Free(&o->process);
  219. fail2:
  220. CmdLine_Free(&c);
  221. fail1:
  222. free(o);
  223. fail0:
  224. NCDModuleInst_Backend_SetError(i);
  225. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  226. }
  227. void instance_free (struct instance *o)
  228. {
  229. NCDModuleInst *i = o->i;
  230. if (o->have_pipe) {
  231. // free buffer
  232. LineBuffer_Free(&o->pipe_buffer);
  233. // free input interface
  234. PacketPassInterface_Free(&o->pipe_input);
  235. }
  236. // free process
  237. BInputProcess_Free(&o->process);
  238. // free instance
  239. free(o);
  240. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  241. }
  242. static void func_die (void *vo)
  243. {
  244. struct instance *o = vo;
  245. ASSERT(!o->dying)
  246. // request termination
  247. BInputProcess_Terminate(&o->process);
  248. // remember dying
  249. o->dying = 1;
  250. }
  251. static const struct NCDModule modules[] = {
  252. {
  253. .type = "net.backend.wpa_supplicant",
  254. .func_new = func_new,
  255. .func_die = func_die
  256. }, {
  257. .type = NULL
  258. }
  259. };
  260. const struct NCDModuleGroup ncdmodule_net_backend_wpa_supplicant = {
  261. .modules = modules
  262. };