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. 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. // append stdbuf part
  193. if (!build_stdbuf_cmdline(c, o->exec, o->exec_len)) {
  194. goto fail1;
  195. }
  196. // append user arguments
  197. size_t count = NCDVal_ListCount(o->args);
  198. for (size_t j = 0; j < count; j++) {
  199. NCDValRef arg = NCDVal_ListGet(o->args, j);
  200. if (!NCDVal_IsStringNoNulls(arg)) {
  201. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  202. goto fail1;
  203. }
  204. // append argument
  205. if (!CmdLine_AppendNoNull(c, NCDVal_StringData(arg), NCDVal_StringLength(arg))) {
  206. goto fail1;
  207. }
  208. }
  209. // append interface name
  210. if (!CmdLine_Append(c, "-i") || !CmdLine_AppendNoNull(c, o->ifname, o->ifname_len)) {
  211. goto fail1;
  212. }
  213. // append config file
  214. if (!CmdLine_Append(c, "-c") || !CmdLine_AppendNoNull(c, o->conf, o->conf_len)) {
  215. goto fail1;
  216. }
  217. // terminate cmdline
  218. if (!CmdLine_Finish(c)) {
  219. goto fail1;
  220. }
  221. return 1;
  222. fail1:
  223. CmdLine_Free(c);
  224. fail0:
  225. return 0;
  226. }
  227. int init_info (struct instance *o, int have_bssid, const uint8_t *bssid, const uint8_t *ssid, size_t ssid_len)
  228. {
  229. ASSERT(!o->have_info)
  230. // set bssid
  231. o->info_have_bssid = have_bssid;
  232. if (have_bssid) {
  233. memcpy(o->info_bssid, bssid, 6);
  234. }
  235. // set ssid
  236. if (!(o->info_ssid = BAllocSize(bsize_add(bsize_fromsize(ssid_len), bsize_fromsize(1))))) {
  237. ModuleLog(o->i, BLOG_ERROR, "BAllocSize failed");
  238. return 0;
  239. }
  240. memcpy(o->info_ssid, ssid, ssid_len);
  241. o->info_ssid[ssid_len] = '\0';
  242. // set have info
  243. o->have_info = 1;
  244. return 1;
  245. }
  246. void free_info (struct instance *o)
  247. {
  248. ASSERT(o->have_info)
  249. // free ssid
  250. BFree(o->info_ssid);
  251. // set not have info
  252. o->have_info = 0;
  253. }
  254. void process_error (struct instance *o)
  255. {
  256. BInputProcess_Terminate(&o->process);
  257. }
  258. void process_handler_terminated (struct instance *o, int normally, uint8_t normally_exit_status)
  259. {
  260. ModuleLog(o->i, (o->dying ? BLOG_INFO : BLOG_ERROR), "process terminated");
  261. // die
  262. instance_free(o, !o->dying);
  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 (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, ": "))) {
  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, const struct NCDModuleInst_new_params *params)
  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(params->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_StringData(ifname_arg);
  357. o->ifname_len = NCDVal_StringLength(ifname_arg);
  358. o->conf = NCDVal_StringData(conf_arg);
  359. o->conf_len = NCDVal_StringLength(conf_arg);
  360. o->exec = NCDVal_StringData(exec_arg);
  361. o->exec_len = NCDVal_StringLength(exec_arg);
  362. o->args = args_arg;
  363. // set not dying
  364. o->dying = 0;
  365. // set not up
  366. o->up = 0;
  367. // build process cmdline
  368. CmdLine c;
  369. if (!build_cmdline(o, &c)) {
  370. ModuleLog(o->i, BLOG_ERROR, "failed to build cmdline");
  371. goto fail0;
  372. }
  373. // init process
  374. if (!BInputProcess_Init(&o->process, o->i->params->iparams->reactor, o->i->params->iparams->manager, o,
  375. (BInputProcess_handler_terminated)process_handler_terminated,
  376. (BInputProcess_handler_closed)process_handler_closed
  377. )) {
  378. ModuleLog(o->i, BLOG_ERROR, "BInputProcess_Init failed");
  379. goto fail1;
  380. }
  381. // init input interface
  382. PacketPassInterface_Init(&o->pipe_input, MAX_LINE_LEN, (PacketPassInterface_handler_send)process_pipe_handler_send, o, BReactor_PendingGroup(o->i->params->iparams->reactor));
  383. // init buffer
  384. if (!LineBuffer_Init(&o->pipe_buffer, BInputProcess_GetInput(&o->process), &o->pipe_input, MAX_LINE_LEN, '\n')) {
  385. ModuleLog(o->i, BLOG_ERROR, "LineBuffer_Init failed");
  386. goto fail2;
  387. }
  388. // set have pipe
  389. o->have_pipe = 1;
  390. // start process
  391. if (!BInputProcess_Start(&o->process, ((char **)c.arr.v)[0], (char **)c.arr.v, NULL)) {
  392. ModuleLog(o->i, BLOG_ERROR, "BInputProcess_Start failed");
  393. goto fail3;
  394. }
  395. // set not have info
  396. o->have_info = 0;
  397. CmdLine_Free(&c);
  398. return;
  399. fail3:
  400. LineBuffer_Free(&o->pipe_buffer);
  401. fail2:
  402. PacketPassInterface_Free(&o->pipe_input);
  403. BInputProcess_Free(&o->process);
  404. fail1:
  405. CmdLine_Free(&c);
  406. fail0:
  407. NCDModuleInst_Backend_DeadError(i);
  408. }
  409. void instance_free (struct instance *o, int is_error)
  410. {
  411. // free info
  412. if (o->have_info) {
  413. free_info(o);
  414. }
  415. if (o->have_pipe) {
  416. // free buffer
  417. LineBuffer_Free(&o->pipe_buffer);
  418. // free input interface
  419. PacketPassInterface_Free(&o->pipe_input);
  420. }
  421. // free process
  422. BInputProcess_Free(&o->process);
  423. if (is_error) {
  424. NCDModuleInst_Backend_DeadError(o->i);
  425. } else {
  426. NCDModuleInst_Backend_Dead(o->i);
  427. }
  428. }
  429. static void func_die (void *vo)
  430. {
  431. struct instance *o = vo;
  432. ASSERT(!o->dying)
  433. // request termination
  434. BInputProcess_Terminate(&o->process);
  435. // remember dying
  436. o->dying = 1;
  437. }
  438. static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
  439. {
  440. struct instance *o = vo;
  441. ASSERT(o->up)
  442. ASSERT(o->have_info)
  443. if (!strcmp(name, "bssid")) {
  444. char str[18];
  445. if (!o->info_have_bssid) {
  446. sprintf(str, "none");
  447. } else {
  448. uint8_t *id = o->info_bssid;
  449. 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]);
  450. }
  451. *out = NCDVal_NewString(mem, str);
  452. return 1;
  453. }
  454. if (!strcmp(name, "ssid")) {
  455. *out = NCDVal_NewString(mem, o->info_ssid);
  456. return 1;
  457. }
  458. return 0;
  459. }
  460. static 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. };