net_backend_wpa_supplicant.c 16 KB

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