net_backend_wpa_supplicant.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /**
  2. * @file net_backend_wpa_supplicant.c
  3. * @author Ambroz Bizjak <[email protected]>
  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 <string.h>
  35. #include <unistd.h>
  36. #include <misc/cmdline.h>
  37. #include <misc/string_begins_with.h>
  38. #include <system/BSocket.h>
  39. #include <flow/StreamSocketSource.h>
  40. #include <flow/LineBuffer.h>
  41. #include <ncd/NCDModule.h>
  42. #include <generated/blog_channel_ncd_net_backend_wpa_supplicant.h>
  43. #define MAX_LINE_LEN 512
  44. #define EVENT_STRING_CONNECTED "CTRL-EVENT-CONNECTED"
  45. #define EVENT_STRING_DISCONNECTED "CTRL-EVENT-DISCONNECTED"
  46. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  47. struct instance {
  48. NCDModuleInst *i;
  49. char *ifname;
  50. char *conf;
  51. char *exec;
  52. NCDValue *args;
  53. int dying;
  54. int up;
  55. BProcess process;
  56. int pipe_fd;
  57. BSocket pipe_sock;
  58. FlowErrorDomain pipe_domain;
  59. StreamSocketSource pipe_source;
  60. LineBuffer pipe_buffer;
  61. PacketPassInterface pipe_input;
  62. };
  63. static int build_cmdline (struct instance *o, CmdLine *c);
  64. static int init_pipe (struct instance *o, int pipe_fd);
  65. static void free_pipe (struct instance *o);
  66. static void process_handler (struct instance *o, int normally, uint8_t normally_exit_status);
  67. static void process_pipe_handler_error (struct instance *o, int component, int code);
  68. static void process_pipe_handler_send (struct instance *o, uint8_t *data, int data_len);
  69. int build_cmdline (struct instance *o, CmdLine *c)
  70. {
  71. if (!CmdLine_Init(c)) {
  72. goto fail0;
  73. }
  74. // append exec
  75. if (!CmdLine_Append(c, o->exec)) {
  76. goto fail1;
  77. }
  78. // append user arguments
  79. NCDValue *arg = NCDValue_ListFirst(o->args);
  80. while (arg) {
  81. if (NCDValue_Type(arg) != NCDVALUE_STRING) {
  82. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  83. goto fail1;
  84. }
  85. // append argument
  86. if (!CmdLine_Append(c, NCDValue_StringValue(arg))) {
  87. goto fail1;
  88. }
  89. arg = NCDValue_ListNext(o->args, arg);
  90. }
  91. // append interface name
  92. if (!CmdLine_Append(c, "-i") || !CmdLine_Append(c, o->ifname)) {
  93. goto fail1;
  94. }
  95. // append config file
  96. if (!CmdLine_Append(c, "-c") || !CmdLine_Append(c, o->conf)) {
  97. goto fail1;
  98. }
  99. // terminate cmdline
  100. if (!CmdLine_Finish(c)) {
  101. goto fail1;
  102. }
  103. return 1;
  104. fail1:
  105. CmdLine_Free(c);
  106. fail0:
  107. return 0;
  108. }
  109. int init_pipe (struct instance *o, int pipe_fd)
  110. {
  111. // init socket
  112. if (BSocket_InitPipe(&o->pipe_sock, o->i->reactor, pipe_fd) < 0) {
  113. ModuleLog(o->i, BLOG_ERROR, "BSocket_InitPipe failed");
  114. goto fail0;
  115. }
  116. // init domain
  117. FlowErrorDomain_Init(&o->pipe_domain, (FlowErrorDomain_handler)process_pipe_handler_error, o);
  118. // init source
  119. StreamSocketSource_Init(&o->pipe_source, FlowErrorReporter_Create(&o->pipe_domain, 0), &o->pipe_sock, BReactor_PendingGroup(o->i->reactor));
  120. // init input interface
  121. PacketPassInterface_Init(&o->pipe_input, MAX_LINE_LEN, (PacketPassInterface_handler_send)process_pipe_handler_send, o, BReactor_PendingGroup(o->i->reactor));
  122. // init buffer
  123. if (!LineBuffer_Init(&o->pipe_buffer, StreamSocketSource_GetOutput(&o->pipe_source), &o->pipe_input, MAX_LINE_LEN, '\n')) {
  124. ModuleLog(o->i, BLOG_ERROR, "LineBuffer_Init failed");
  125. goto fail1;
  126. }
  127. return 1;
  128. fail1:
  129. PacketPassInterface_Free(&o->pipe_input);
  130. StreamSocketSource_Free(&o->pipe_source);
  131. BSocket_Free(&o->pipe_sock);
  132. fail0:
  133. return 0;
  134. }
  135. void free_pipe (struct instance *o)
  136. {
  137. // free buffer
  138. LineBuffer_Free(&o->pipe_buffer);
  139. // free input interface
  140. PacketPassInterface_Free(&o->pipe_input);
  141. // free source
  142. StreamSocketSource_Free(&o->pipe_source);
  143. // free socket
  144. BSocket_Free(&o->pipe_sock);
  145. }
  146. void process_handler (struct instance *o, int normally, uint8_t normally_exit_status)
  147. {
  148. ModuleLog(o->i, (o->dying ? BLOG_INFO : BLOG_ERROR), "process terminated");
  149. int was_dying = o->dying;
  150. // set dying so free doesn't attempt kill
  151. o->dying = 1;
  152. // die
  153. NCDModuleInst_Backend_Died(o->i, !was_dying);
  154. return;
  155. }
  156. void process_pipe_handler_error (struct instance *o, int component, int code)
  157. {
  158. ASSERT(o->pipe_fd >= 0)
  159. if (code == STREAMSOCKETSOURCE_ERROR_CLOSED) {
  160. ModuleLog(o->i, BLOG_INFO, "pipe eof");
  161. } else {
  162. ModuleLog(o->i, BLOG_ERROR, "pipe error");
  163. }
  164. // free pipe reading
  165. free_pipe(o);
  166. // close pipe read end
  167. ASSERT_FORCE(close(o->pipe_fd) == 0)
  168. // forget pipe
  169. o->pipe_fd = -1;
  170. }
  171. void process_pipe_handler_send (struct instance *o, uint8_t *data, int data_len)
  172. {
  173. ASSERT(o->pipe_fd >= 0)
  174. ASSERT(data_len > 0)
  175. // accept packet
  176. PacketPassInterface_Done(&o->pipe_input);
  177. if (o->dying) {
  178. return;
  179. }
  180. if (data_begins_with(data, data_len, EVENT_STRING_CONNECTED)) {
  181. ModuleLog(o->i, BLOG_INFO, "connected event");
  182. if (!o->up) {
  183. o->up = 1;
  184. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  185. }
  186. }
  187. else if (data_begins_with(data, data_len, EVENT_STRING_DISCONNECTED)) {
  188. ModuleLog(o->i, BLOG_INFO, "disconnected event");
  189. if (o->up) {
  190. o->up = 0;
  191. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_DOWN);
  192. }
  193. }
  194. }
  195. static void * func_new (NCDModuleInst *i)
  196. {
  197. // allocate instance
  198. struct instance *o = malloc(sizeof(*o));
  199. if (!o) {
  200. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  201. goto fail0;
  202. }
  203. // init arguments
  204. o->i = i;
  205. // read arguments
  206. NCDValue *ifname_arg;
  207. NCDValue *conf_arg;
  208. NCDValue *exec_arg;
  209. NCDValue *args_arg;
  210. if (!NCDValue_ListRead(o->i->args, 4, &ifname_arg, &conf_arg, &exec_arg, &args_arg)) {
  211. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  212. goto fail1;
  213. }
  214. if (NCDValue_Type(ifname_arg) != NCDVALUE_STRING || NCDValue_Type(conf_arg) != NCDVALUE_STRING ||
  215. NCDValue_Type(exec_arg) != NCDVALUE_STRING || NCDValue_Type(args_arg) != NCDVALUE_LIST) {
  216. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  217. goto fail1;
  218. }
  219. o->ifname = NCDValue_StringValue(ifname_arg);
  220. o->conf = NCDValue_StringValue(conf_arg);
  221. o->exec = NCDValue_StringValue(exec_arg);
  222. o->args = args_arg;
  223. // set not dying
  224. o->dying = 0;
  225. // set not up
  226. o->up = 0;
  227. // create pipe
  228. int pipefds[2];
  229. if (pipe(pipefds) < 0) {
  230. ModuleLog(o->i, BLOG_ERROR, "pipe failed");
  231. goto fail1;
  232. }
  233. // init pipe reading
  234. if (!init_pipe(o, pipefds[0])) {
  235. goto fail2;
  236. }
  237. // build process cmdline
  238. CmdLine c;
  239. if (!build_cmdline(o, &c)) {
  240. ModuleLog(o->i, BLOG_ERROR, "failed to build cmdline");
  241. goto fail3;
  242. }
  243. // start process
  244. int fds[] = { pipefds[1], -1 };
  245. int fds_map[] = { 1 };
  246. if (!BProcess_InitWithFds(&o->process, o->i->manager, (BProcess_handler)process_handler, o, ((char **)c.arr.v)[0], (char **)c.arr.v, NULL, fds, fds_map)) {
  247. ModuleLog(o->i, BLOG_ERROR, "BProcess_Init failed");
  248. goto fail4;
  249. }
  250. // remember pipe read end
  251. o->pipe_fd = pipefds[0];
  252. CmdLine_Free(&c);
  253. ASSERT_FORCE(close(pipefds[1]) == 0)
  254. return o;
  255. fail4:
  256. CmdLine_Free(&c);
  257. fail3:
  258. free_pipe(o);
  259. fail2:
  260. ASSERT_FORCE(close(pipefds[0]) == 0)
  261. ASSERT_FORCE(close(pipefds[1]) == 0)
  262. fail1:
  263. free(o);
  264. fail0:
  265. return NULL;
  266. }
  267. static void func_free (void *vo)
  268. {
  269. struct instance *o = vo;
  270. if (!o->dying) {
  271. // kill process
  272. BProcess_Kill(&o->process);
  273. }
  274. // free process
  275. BProcess_Free(&o->process);
  276. if (o->pipe_fd >= 0) {
  277. // free pipe reading
  278. free_pipe(o);
  279. // close pipe read end
  280. ASSERT_FORCE(close(o->pipe_fd) == 0)
  281. }
  282. // free instance
  283. free(o);
  284. }
  285. static void func_die (void *vo)
  286. {
  287. struct instance *o = vo;
  288. ASSERT(!o->dying)
  289. // request termination
  290. BProcess_Terminate(&o->process);
  291. // remember dying
  292. o->dying = 1;
  293. }
  294. static const struct NCDModule modules[] = {
  295. {
  296. .type = "net.backend.wpa_supplicant",
  297. .func_new = func_new,
  298. .func_free = func_free,
  299. .func_die = func_die
  300. }, {
  301. .type = NULL
  302. }
  303. };
  304. const struct NCDModuleGroup ncdmodule_net_backend_wpa_supplicant = {
  305. .modules = modules
  306. };