net_backend_wpa_supplicant.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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 <flow/LineBuffer.h>
  56. #include <system/BInputProcess.h>
  57. #include <ncd/NCDModule.h>
  58. #include <generated/blog_channel_ncd_net_backend_wpa_supplicant.h>
  59. #define MAX_LINE_LEN 512
  60. #define EVENT_STRING_CONNECTED "CTRL-EVENT-CONNECTED"
  61. #define EVENT_STRING_DISCONNECTED "CTRL-EVENT-DISCONNECTED"
  62. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  63. struct instance {
  64. NCDModuleInst *i;
  65. const char *ifname;
  66. const char *conf;
  67. const char *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);
  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. // append stdbuf part
  190. if (!build_stdbuf_cmdline(c, o->exec)) {
  191. goto fail1;
  192. }
  193. // append user arguments
  194. size_t count = NCDVal_ListCount(o->args);
  195. for (size_t j = 0; j < count; j++) {
  196. NCDValRef arg = NCDVal_ListGet(o->args, j);
  197. if (!NCDVal_IsStringNoNulls(arg)) {
  198. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  199. goto fail1;
  200. }
  201. // append argument
  202. if (!CmdLine_Append(c, NCDVal_StringValue(arg))) {
  203. goto fail1;
  204. }
  205. }
  206. // append interface name
  207. if (!CmdLine_Append(c, "-i") || !CmdLine_Append(c, o->ifname)) {
  208. goto fail1;
  209. }
  210. // append config file
  211. if (!CmdLine_Append(c, "-c") || !CmdLine_Append(c, o->conf)) {
  212. goto fail1;
  213. }
  214. // terminate cmdline
  215. if (!CmdLine_Finish(c)) {
  216. goto fail1;
  217. }
  218. return 1;
  219. fail1:
  220. CmdLine_Free(c);
  221. fail0:
  222. return 0;
  223. }
  224. int init_info (struct instance *o, int have_bssid, const uint8_t *bssid, const uint8_t *ssid, size_t ssid_len)
  225. {
  226. ASSERT(!o->have_info)
  227. // set bssid
  228. o->info_have_bssid = have_bssid;
  229. if (have_bssid) {
  230. memcpy(o->info_bssid, bssid, 6);
  231. }
  232. // set ssid
  233. if (!(o->info_ssid = BAllocSize(bsize_add(bsize_fromsize(ssid_len), bsize_fromsize(1))))) {
  234. ModuleLog(o->i, BLOG_ERROR, "BAllocSize failed");
  235. return 0;
  236. }
  237. memcpy(o->info_ssid, ssid, ssid_len);
  238. o->info_ssid[ssid_len] = '\0';
  239. // set have info
  240. o->have_info = 1;
  241. return 1;
  242. }
  243. void free_info (struct instance *o)
  244. {
  245. ASSERT(o->have_info)
  246. // free ssid
  247. BFree(o->info_ssid);
  248. // set not have info
  249. o->have_info = 0;
  250. }
  251. void process_error (struct instance *o)
  252. {
  253. BInputProcess_Terminate(&o->process);
  254. }
  255. void process_handler_terminated (struct instance *o, int normally, uint8_t normally_exit_status)
  256. {
  257. ModuleLog(o->i, (o->dying ? BLOG_INFO : BLOG_ERROR), "process terminated");
  258. if (!o->dying) {
  259. NCDModuleInst_Backend_SetError(o->i);
  260. }
  261. // die
  262. instance_free(o);
  263. return;
  264. }
  265. void process_handler_closed (struct instance *o, int is_error)
  266. {
  267. ASSERT(o->have_pipe)
  268. if (is_error) {
  269. ModuleLog(o->i, BLOG_ERROR, "pipe error");
  270. } else {
  271. ModuleLog(o->i, BLOG_INFO, "pipe closed");
  272. }
  273. // free buffer
  274. LineBuffer_Free(&o->pipe_buffer);
  275. // free input interface
  276. PacketPassInterface_Free(&o->pipe_input);
  277. // set have no pipe
  278. o->have_pipe = 0;
  279. }
  280. void process_pipe_handler_send (struct instance *o, uint8_t *data, int data_len)
  281. {
  282. ASSERT(o->have_pipe)
  283. ASSERT(data_len > 0)
  284. // accept packet
  285. PacketPassInterface_Done(&o->pipe_input);
  286. if (o->dying) {
  287. return;
  288. }
  289. // strip "interface: " from beginning of line. Older wpa_supplicant versions (<1.0) don't add this
  290. // prefix, so don't fail if there isn't one.
  291. size_t l1;
  292. size_t l2;
  293. if (strlen(o->ifname) > 0 && (l1 = data_begins_with((char *)data, data_len, o->ifname)) && (l2 = data_begins_with((char *)data + l1, data_len - l1, ": "))) {
  294. data += l1 + l2;
  295. data_len -= l1 + l2;
  296. }
  297. int have_bssid = 1;
  298. uint8_t bssid[6];
  299. uint8_t *ssid;
  300. int ssid_len;
  301. if (parse_trying(data, data_len, bssid, &ssid, &ssid_len) || (have_bssid = 0, parse_trying_nobssid(data, data_len, &ssid, &ssid_len))) {
  302. ModuleLog(o->i, BLOG_INFO, "trying event");
  303. if (o->up) {
  304. ModuleLog(o->i, BLOG_ERROR, "trying unexpected!");
  305. process_error(o);
  306. return;
  307. }
  308. if (o->have_info) {
  309. free_info(o);
  310. }
  311. if (!init_info(o, have_bssid, bssid, ssid, ssid_len)) {
  312. ModuleLog(o->i, BLOG_ERROR, "init_info failed");
  313. process_error(o);
  314. return;
  315. }
  316. }
  317. else if (data_begins_with((char *)data, data_len, EVENT_STRING_CONNECTED)) {
  318. ModuleLog(o->i, BLOG_INFO, "connected event");
  319. if (o->up || !o->have_info) {
  320. ModuleLog(o->i, BLOG_ERROR, "connected unexpected!");
  321. process_error(o);
  322. return;
  323. }
  324. o->up = 1;
  325. NCDModuleInst_Backend_Up(o->i);
  326. }
  327. else if (data_begins_with((char *)data, data_len, EVENT_STRING_DISCONNECTED)) {
  328. ModuleLog(o->i, BLOG_INFO, "disconnected event");
  329. if (o->have_info) {
  330. free_info(o);
  331. }
  332. if (o->up) {
  333. o->up = 0;
  334. NCDModuleInst_Backend_Down(o->i);
  335. }
  336. }
  337. }
  338. static void func_new (void *vo, NCDModuleInst *i)
  339. {
  340. struct instance *o = vo;
  341. o->i = i;
  342. // read arguments
  343. NCDValRef ifname_arg;
  344. NCDValRef conf_arg;
  345. NCDValRef exec_arg;
  346. NCDValRef args_arg;
  347. if (!NCDVal_ListRead(o->i->args, 4, &ifname_arg, &conf_arg, &exec_arg, &args_arg)) {
  348. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  349. goto fail0;
  350. }
  351. if (!NCDVal_IsStringNoNulls(ifname_arg) || !NCDVal_IsStringNoNulls(conf_arg) ||
  352. !NCDVal_IsStringNoNulls(exec_arg) || !NCDVal_IsList(args_arg)) {
  353. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  354. goto fail0;
  355. }
  356. o->ifname = NCDVal_StringValue(ifname_arg);
  357. o->conf = NCDVal_StringValue(conf_arg);
  358. o->exec = NCDVal_StringValue(exec_arg);
  359. o->args = args_arg;
  360. // set not dying
  361. o->dying = 0;
  362. // set not up
  363. o->up = 0;
  364. // build process cmdline
  365. CmdLine c;
  366. if (!build_cmdline(o, &c)) {
  367. ModuleLog(o->i, BLOG_ERROR, "failed to build cmdline");
  368. goto fail0;
  369. }
  370. // init process
  371. if (!BInputProcess_Init(&o->process, o->i->iparams->reactor, o->i->iparams->manager, o,
  372. (BInputProcess_handler_terminated)process_handler_terminated,
  373. (BInputProcess_handler_closed)process_handler_closed
  374. )) {
  375. ModuleLog(o->i, BLOG_ERROR, "BInputProcess_Init failed");
  376. goto fail1;
  377. }
  378. // init input interface
  379. PacketPassInterface_Init(&o->pipe_input, MAX_LINE_LEN, (PacketPassInterface_handler_send)process_pipe_handler_send, o, BReactor_PendingGroup(o->i->iparams->reactor));
  380. // init buffer
  381. if (!LineBuffer_Init(&o->pipe_buffer, BInputProcess_GetInput(&o->process), &o->pipe_input, MAX_LINE_LEN, '\n')) {
  382. ModuleLog(o->i, BLOG_ERROR, "LineBuffer_Init failed");
  383. goto fail2;
  384. }
  385. // set have pipe
  386. o->have_pipe = 1;
  387. // start process
  388. if (!BInputProcess_Start(&o->process, ((char **)c.arr.v)[0], (char **)c.arr.v, NULL)) {
  389. ModuleLog(o->i, BLOG_ERROR, "BInputProcess_Start failed");
  390. goto fail3;
  391. }
  392. // set not have info
  393. o->have_info = 0;
  394. CmdLine_Free(&c);
  395. return;
  396. fail3:
  397. LineBuffer_Free(&o->pipe_buffer);
  398. fail2:
  399. PacketPassInterface_Free(&o->pipe_input);
  400. BInputProcess_Free(&o->process);
  401. fail1:
  402. CmdLine_Free(&c);
  403. fail0:
  404. NCDModuleInst_Backend_SetError(i);
  405. NCDModuleInst_Backend_Dead(i);
  406. }
  407. void instance_free (struct instance *o)
  408. {
  409. // free info
  410. if (o->have_info) {
  411. free_info(o);
  412. }
  413. if (o->have_pipe) {
  414. // free buffer
  415. LineBuffer_Free(&o->pipe_buffer);
  416. // free input interface
  417. PacketPassInterface_Free(&o->pipe_input);
  418. }
  419. // free process
  420. BInputProcess_Free(&o->process);
  421. NCDModuleInst_Backend_Dead(o->i);
  422. }
  423. static void func_die (void *vo)
  424. {
  425. struct instance *o = vo;
  426. ASSERT(!o->dying)
  427. // request termination
  428. BInputProcess_Terminate(&o->process);
  429. // remember dying
  430. o->dying = 1;
  431. }
  432. static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
  433. {
  434. struct instance *o = vo;
  435. ASSERT(o->up)
  436. ASSERT(o->have_info)
  437. if (!strcmp(name, "bssid")) {
  438. char str[18];
  439. if (!o->info_have_bssid) {
  440. sprintf(str, "none");
  441. } else {
  442. uint8_t *id = o->info_bssid;
  443. 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]);
  444. }
  445. *out = NCDVal_NewString(mem, str);
  446. if (NCDVal_IsInvalid(*out)) {
  447. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  448. }
  449. return 1;
  450. }
  451. if (!strcmp(name, "ssid")) {
  452. *out = NCDVal_NewString(mem, o->info_ssid);
  453. if (NCDVal_IsInvalid(*out)) {
  454. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  455. }
  456. return 1;
  457. }
  458. return 0;
  459. }
  460. static const struct NCDModule modules[] = {
  461. {
  462. .type = "net.backend.wpa_supplicant",
  463. .func_new2 = func_new,
  464. .func_die = func_die,
  465. .func_getvar = func_getvar,
  466. .alloc_size = sizeof(struct instance)
  467. }, {
  468. .type = NULL
  469. }
  470. };
  471. const struct NCDModuleGroup ncdmodule_net_backend_wpa_supplicant = {
  472. .modules = modules
  473. };