net_backend_wpa_supplicant.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /**
  2. * @file net_backend_wpa_supplicant.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. * Wireless interface module which runs wpa_supplicant to connect to a wireless network.
  32. *
  33. * Note: wpa_supplicant does not monitor the state of rfkill switches and will fail to
  34. * start if the switch is of when it is started, and will stop working indefinitely if the
  35. * switch is turned off while it is running. Therefore, you should put a "net.backend.rfkill"
  36. * statement in front of the wpa_supplicant statement.
  37. *
  38. * Synopsis: net.backend.wpa_supplicant(string ifname, string conf, string exec, list(string) args)
  39. * Variables:
  40. * bssid - BSSID of the wireless network we connected to, or "none".
  41. * Consists of 6 capital, two-character hexadecimal numbers, separated with colons.
  42. * Example: "01:B2:C3:04:E5:F6"
  43. * ssid - SSID of the wireless network we connected to. Note that this is after what
  44. * wpa_supplicant does to it before it prints it. In particular, it replaces all bytes
  45. * outside [32, 126] with underscores.
  46. */
  47. #include <stdlib.h>
  48. #include <string.h>
  49. #include <stdio.h>
  50. #include <inttypes.h>
  51. #include <misc/cmdline.h>
  52. #include <misc/string_begins_with.h>
  53. #include <misc/stdbuf_cmdline.h>
  54. #include <misc/balloc.h>
  55. #include <misc/find_program.h>
  56. #include <flow/LineBuffer.h>
  57. #include <system/BInputProcess.h>
  58. #include <ncd/module_common.h>
  59. #include <generated/blog_channel_ncd_net_backend_wpa_supplicant.h>
  60. #define MAX_LINE_LEN 512
  61. #define EVENT_STRING_CONNECTED "CTRL-EVENT-CONNECTED"
  62. #define EVENT_STRING_DISCONNECTED "CTRL-EVENT-DISCONNECTED"
  63. struct instance {
  64. NCDModuleInst *i;
  65. MemRef ifname;
  66. MemRef conf;
  67. MemRef exec;
  68. NCDValRef args;
  69. int dying;
  70. int up;
  71. BInputProcess process;
  72. int have_pipe;
  73. LineBuffer pipe_buffer;
  74. PacketPassInterface pipe_input;
  75. int have_info;
  76. int info_have_bssid;
  77. uint8_t info_bssid[6];
  78. char *info_ssid;
  79. };
  80. static int parse_hex_digit (uint8_t d, uint8_t *out);
  81. static int parse_trying (uint8_t *data, int data_len, uint8_t *out_bssid, uint8_t **out_ssid, int *out_ssid_len);
  82. static int parse_trying_nobssid (uint8_t *data, int data_len, uint8_t **out_ssid, int *out_ssid_len);
  83. static int build_cmdline (struct instance *o, CmdLine *c);
  84. static int init_info (struct instance *o, int have_bssid, const uint8_t *bssid, const uint8_t *ssid, size_t ssid_len);
  85. static void free_info (struct instance *o);
  86. static void process_error (struct instance *o);
  87. static void process_handler_terminated (struct instance *o, int normally, uint8_t normally_exit_status);
  88. static void process_handler_closed (struct instance *o, int is_error);
  89. static void process_pipe_handler_send (struct instance *o, uint8_t *data, int data_len);
  90. static void instance_free (struct instance *o, int is_error);
  91. int parse_hex_digit (uint8_t d, uint8_t *out)
  92. {
  93. switch (d) {
  94. case '0': *out = 0; return 1;
  95. case '1': *out = 1; return 1;
  96. case '2': *out = 2; return 1;
  97. case '3': *out = 3; return 1;
  98. case '4': *out = 4; return 1;
  99. case '5': *out = 5; return 1;
  100. case '6': *out = 6; return 1;
  101. case '7': *out = 7; return 1;
  102. case '8': *out = 8; return 1;
  103. case '9': *out = 9; return 1;
  104. case 'A': case 'a': *out = 10; return 1;
  105. case 'B': case 'b': *out = 11; return 1;
  106. case 'C': case 'c': *out = 12; return 1;
  107. case 'D': case 'd': *out = 13; return 1;
  108. case 'E': case 'e': *out = 14; return 1;
  109. case 'F': case 'f': *out = 15; return 1;
  110. }
  111. return 0;
  112. }
  113. int parse_trying (uint8_t *data, int data_len, uint8_t *out_bssid, uint8_t **out_ssid, int *out_ssid_len)
  114. {
  115. // Trying to associate with AB:CD:EF:01:23:45 (SSID='Some SSID' freq=2462 MHz)
  116. int p;
  117. if (!(p = data_begins_with((char *)data, data_len, "Trying to associate with "))) {
  118. return 0;
  119. }
  120. data += p;
  121. data_len -= p;
  122. for (int i = 0; i < 6; i++) {
  123. uint8_t d1;
  124. uint8_t d2;
  125. if (data_len < 2 || !parse_hex_digit(data[0], &d1) || !parse_hex_digit(data[1], &d2)) {
  126. return 0;
  127. }
  128. data += 2;
  129. data_len -= 2;
  130. out_bssid[i] = ((d1 << 4) | d2);
  131. if (i != 5) {
  132. if (data_len < 1 || data[0] != ':') {
  133. return 0;
  134. }
  135. data += 1;
  136. data_len -= 1;
  137. }
  138. }
  139. if (!(p = data_begins_with((char *)data, data_len, " (SSID='"))) {
  140. return 0;
  141. }
  142. data += p;
  143. data_len -= p;
  144. // find last '
  145. uint8_t *q = NULL;
  146. for (int i = data_len; i > 0; i--) {
  147. if (data[i - 1] == '\'') {
  148. q = &data[i - 1];
  149. break;
  150. }
  151. }
  152. if (!q) {
  153. return 0;
  154. }
  155. *out_ssid = data;
  156. *out_ssid_len = q - data;
  157. return 1;
  158. }
  159. int parse_trying_nobssid (uint8_t *data, int data_len, uint8_t **out_ssid, int *out_ssid_len)
  160. {
  161. // Trying to associate with SSID 'Some SSID'
  162. int p;
  163. if (!(p = data_begins_with((char *)data, data_len, "Trying to associate with SSID '"))) {
  164. return 0;
  165. }
  166. data += p;
  167. data_len -= p;
  168. // find last '
  169. uint8_t *q = NULL;
  170. for (int i = data_len; i > 0; i--) {
  171. if (data[i - 1] == '\'') {
  172. q = &data[i - 1];
  173. break;
  174. }
  175. }
  176. if (!q) {
  177. return 0;
  178. }
  179. *out_ssid = data;
  180. *out_ssid_len = q - data;
  181. return 1;
  182. }
  183. int build_cmdline (struct instance *o, CmdLine *c)
  184. {
  185. // init cmdline
  186. if (!CmdLine_Init(c)) {
  187. goto fail0;
  188. }
  189. // find stdbuf executable
  190. char *stdbuf_exec = badvpn_find_program("stdbuf");
  191. if (!stdbuf_exec) {
  192. ModuleLog(o->i, BLOG_ERROR, "cannot find stdbuf executable");
  193. goto fail1;
  194. }
  195. // append stdbuf part
  196. int res = build_stdbuf_cmdline(c, stdbuf_exec, o->exec.ptr, o->exec.len);
  197. free(stdbuf_exec);
  198. if (!res) {
  199. goto fail1;
  200. }
  201. // append user arguments
  202. size_t count = NCDVal_ListCount(o->args);
  203. for (size_t j = 0; j < count; j++) {
  204. NCDValRef arg = NCDVal_ListGet(o->args, j);
  205. if (!NCDVal_IsStringNoNulls(arg)) {
  206. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  207. goto fail1;
  208. }
  209. // append argument
  210. if (!CmdLine_AppendNoNullMr(c, NCDVal_StringMemRef(arg))) {
  211. goto fail1;
  212. }
  213. }
  214. // append interface name
  215. if (!CmdLine_Append(c, "-i") || !CmdLine_AppendNoNullMr(c, o->ifname)) {
  216. goto fail1;
  217. }
  218. // append config file
  219. if (!CmdLine_Append(c, "-c") || !CmdLine_AppendNoNullMr(c, o->conf)) {
  220. goto fail1;
  221. }
  222. // terminate cmdline
  223. if (!CmdLine_Finish(c)) {
  224. goto fail1;
  225. }
  226. return 1;
  227. fail1:
  228. CmdLine_Free(c);
  229. fail0:
  230. return 0;
  231. }
  232. int init_info (struct instance *o, int have_bssid, const uint8_t *bssid, const uint8_t *ssid, size_t ssid_len)
  233. {
  234. ASSERT(!o->have_info)
  235. // set bssid
  236. o->info_have_bssid = have_bssid;
  237. if (have_bssid) {
  238. memcpy(o->info_bssid, bssid, 6);
  239. }
  240. // set ssid
  241. if (!(o->info_ssid = BAllocSize(bsize_add(bsize_fromsize(ssid_len), bsize_fromsize(1))))) {
  242. ModuleLog(o->i, BLOG_ERROR, "BAllocSize failed");
  243. return 0;
  244. }
  245. memcpy(o->info_ssid, ssid, ssid_len);
  246. o->info_ssid[ssid_len] = '\0';
  247. // set have info
  248. o->have_info = 1;
  249. return 1;
  250. }
  251. void free_info (struct instance *o)
  252. {
  253. ASSERT(o->have_info)
  254. // free ssid
  255. BFree(o->info_ssid);
  256. // set not have info
  257. o->have_info = 0;
  258. }
  259. void process_error (struct instance *o)
  260. {
  261. BInputProcess_Terminate(&o->process);
  262. }
  263. void process_handler_terminated (struct instance *o, int normally, uint8_t normally_exit_status)
  264. {
  265. ModuleLog(o->i, (o->dying ? BLOG_INFO : BLOG_ERROR), "process terminated");
  266. // die
  267. instance_free(o, !o->dying);
  268. return;
  269. }
  270. void process_handler_closed (struct instance *o, int is_error)
  271. {
  272. ASSERT(o->have_pipe)
  273. if (is_error) {
  274. ModuleLog(o->i, BLOG_ERROR, "pipe error");
  275. } else {
  276. ModuleLog(o->i, BLOG_INFO, "pipe closed");
  277. }
  278. // free buffer
  279. LineBuffer_Free(&o->pipe_buffer);
  280. // free input interface
  281. PacketPassInterface_Free(&o->pipe_input);
  282. // set have no pipe
  283. o->have_pipe = 0;
  284. }
  285. void process_pipe_handler_send (struct instance *o, uint8_t *data, int data_len)
  286. {
  287. ASSERT(o->have_pipe)
  288. ASSERT(data_len > 0)
  289. // accept packet
  290. PacketPassInterface_Done(&o->pipe_input);
  291. if (o->dying) {
  292. return;
  293. }
  294. // strip "interface: " from beginning of line. Older wpa_supplicant versions (<1.0) don't add this
  295. // prefix, so don't fail if there isn't one.
  296. size_t l1;
  297. size_t l2;
  298. if (o->ifname.len > 0 && (l1 = data_begins_with_bin((char *)data, data_len, o->ifname.ptr, o->ifname.len)) && (l2 = data_begins_with((char *)data + l1, data_len - l1, ": "))) {
  299. data += l1 + l2;
  300. data_len -= l1 + l2;
  301. }
  302. int have_bssid = 1;
  303. uint8_t bssid[6];
  304. uint8_t *ssid;
  305. int ssid_len;
  306. if (parse_trying(data, data_len, bssid, &ssid, &ssid_len) || (have_bssid = 0, parse_trying_nobssid(data, data_len, &ssid, &ssid_len))) {
  307. ModuleLog(o->i, BLOG_INFO, "trying event");
  308. if (o->up) {
  309. ModuleLog(o->i, BLOG_ERROR, "trying unexpected!");
  310. process_error(o);
  311. return;
  312. }
  313. if (o->have_info) {
  314. free_info(o);
  315. }
  316. if (!init_info(o, have_bssid, bssid, ssid, ssid_len)) {
  317. ModuleLog(o->i, BLOG_ERROR, "init_info failed");
  318. process_error(o);
  319. return;
  320. }
  321. }
  322. else if (data_begins_with((char *)data, data_len, EVENT_STRING_CONNECTED)) {
  323. ModuleLog(o->i, BLOG_INFO, "connected event");
  324. if (o->up || !o->have_info) {
  325. ModuleLog(o->i, BLOG_ERROR, "connected unexpected!");
  326. process_error(o);
  327. return;
  328. }
  329. o->up = 1;
  330. NCDModuleInst_Backend_Up(o->i);
  331. }
  332. else if (data_begins_with((char *)data, data_len, EVENT_STRING_DISCONNECTED)) {
  333. ModuleLog(o->i, BLOG_INFO, "disconnected event");
  334. if (o->have_info) {
  335. free_info(o);
  336. }
  337. if (o->up) {
  338. o->up = 0;
  339. NCDModuleInst_Backend_Down(o->i);
  340. }
  341. }
  342. }
  343. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  344. {
  345. struct instance *o = vo;
  346. o->i = i;
  347. // read arguments
  348. NCDValRef ifname_arg;
  349. NCDValRef conf_arg;
  350. NCDValRef exec_arg;
  351. NCDValRef args_arg;
  352. if (!NCDVal_ListRead(params->args, 4, &ifname_arg, &conf_arg, &exec_arg, &args_arg)) {
  353. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  354. goto fail0;
  355. }
  356. if (!NCDVal_IsStringNoNulls(ifname_arg) || !NCDVal_IsStringNoNulls(conf_arg) ||
  357. !NCDVal_IsStringNoNulls(exec_arg) || !NCDVal_IsList(args_arg)) {
  358. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  359. goto fail0;
  360. }
  361. o->ifname = NCDVal_StringMemRef(ifname_arg);
  362. o->conf = NCDVal_StringMemRef(conf_arg);
  363. o->exec = NCDVal_StringMemRef(exec_arg);
  364. o->args = args_arg;
  365. // set not dying
  366. o->dying = 0;
  367. // set not up
  368. o->up = 0;
  369. // build process cmdline
  370. CmdLine c;
  371. if (!build_cmdline(o, &c)) {
  372. ModuleLog(o->i, BLOG_ERROR, "failed to build cmdline");
  373. goto fail0;
  374. }
  375. // init process
  376. if (!BInputProcess_Init(&o->process, o->i->params->iparams->reactor, o->i->params->iparams->manager, o,
  377. (BInputProcess_handler_terminated)process_handler_terminated,
  378. (BInputProcess_handler_closed)process_handler_closed
  379. )) {
  380. ModuleLog(o->i, BLOG_ERROR, "BInputProcess_Init failed");
  381. goto fail1;
  382. }
  383. // init input interface
  384. PacketPassInterface_Init(&o->pipe_input, MAX_LINE_LEN, (PacketPassInterface_handler_send)process_pipe_handler_send, o, BReactor_PendingGroup(o->i->params->iparams->reactor));
  385. // init buffer
  386. if (!LineBuffer_Init(&o->pipe_buffer, BInputProcess_GetInput(&o->process), &o->pipe_input, MAX_LINE_LEN, '\n')) {
  387. ModuleLog(o->i, BLOG_ERROR, "LineBuffer_Init failed");
  388. goto fail2;
  389. }
  390. // set have pipe
  391. o->have_pipe = 1;
  392. // start process
  393. if (!BInputProcess_Start(&o->process, ((char **)c.arr.v)[0], (char **)c.arr.v, NULL)) {
  394. ModuleLog(o->i, BLOG_ERROR, "BInputProcess_Start failed");
  395. goto fail3;
  396. }
  397. // set not have info
  398. o->have_info = 0;
  399. CmdLine_Free(&c);
  400. return;
  401. fail3:
  402. LineBuffer_Free(&o->pipe_buffer);
  403. fail2:
  404. PacketPassInterface_Free(&o->pipe_input);
  405. BInputProcess_Free(&o->process);
  406. fail1:
  407. CmdLine_Free(&c);
  408. fail0:
  409. NCDModuleInst_Backend_DeadError(i);
  410. }
  411. void instance_free (struct instance *o, int is_error)
  412. {
  413. // free info
  414. if (o->have_info) {
  415. free_info(o);
  416. }
  417. if (o->have_pipe) {
  418. // free buffer
  419. LineBuffer_Free(&o->pipe_buffer);
  420. // free input interface
  421. PacketPassInterface_Free(&o->pipe_input);
  422. }
  423. // free process
  424. BInputProcess_Free(&o->process);
  425. if (is_error) {
  426. NCDModuleInst_Backend_DeadError(o->i);
  427. } else {
  428. NCDModuleInst_Backend_Dead(o->i);
  429. }
  430. }
  431. static void func_die (void *vo)
  432. {
  433. struct instance *o = vo;
  434. ASSERT(!o->dying)
  435. // request termination
  436. BInputProcess_Terminate(&o->process);
  437. // remember dying
  438. o->dying = 1;
  439. }
  440. static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
  441. {
  442. struct instance *o = vo;
  443. ASSERT(o->up)
  444. ASSERT(o->have_info)
  445. if (!strcmp(name, "bssid")) {
  446. char str[18];
  447. if (!o->info_have_bssid) {
  448. sprintf(str, "none");
  449. } else {
  450. uint8_t *id = o->info_bssid;
  451. sprintf(str, "%02"PRIX8":%02"PRIX8":%02"PRIX8":%02"PRIX8":%02"PRIX8":%02"PRIX8, id[0], id[1], id[2], id[3], id[4], id[5]);
  452. }
  453. *out = NCDVal_NewString(mem, str);
  454. return 1;
  455. }
  456. if (!strcmp(name, "ssid")) {
  457. *out = NCDVal_NewString(mem, o->info_ssid);
  458. return 1;
  459. }
  460. return 0;
  461. }
  462. static struct NCDModule modules[] = {
  463. {
  464. .type = "net.backend.wpa_supplicant",
  465. .func_new2 = func_new,
  466. .func_die = func_die,
  467. .func_getvar = func_getvar,
  468. .alloc_size = sizeof(struct instance)
  469. }, {
  470. .type = NULL
  471. }
  472. };
  473. const struct NCDModuleGroup ncdmodule_net_backend_wpa_supplicant = {
  474. .modules = modules
  475. };