wpa_priv.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  1. /*
  2. * WPA Supplicant / privileged helper program
  3. * Copyright (c) 2007-2009, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #ifdef __linux__
  16. #include <fcntl.h>
  17. #endif /* __linux__ */
  18. #include <sys/un.h>
  19. #include <sys/stat.h>
  20. #include "common.h"
  21. #include "eloop.h"
  22. #include "version.h"
  23. #include "drivers/driver.h"
  24. #include "l2_packet/l2_packet.h"
  25. #include "privsep_commands.h"
  26. #include "ieee802_11_defs.h"
  27. #ifndef ETH_P_EAPOL
  28. #define ETH_P_EAPOL 0x888e
  29. #endif
  30. #ifndef ETH_P_RSN_PREAUTH
  31. #define ETH_P_RSN_PREAUTH 0x88c7
  32. #endif
  33. struct wpa_priv_interface {
  34. struct wpa_priv_interface *next;
  35. char *driver_name;
  36. char *ifname;
  37. char *sock_name;
  38. int fd;
  39. struct wpa_driver_ops *driver;
  40. void *drv_priv;
  41. struct sockaddr_un drv_addr;
  42. int wpas_registered;
  43. /* TODO: add support for multiple l2 connections */
  44. struct l2_packet_data *l2;
  45. struct sockaddr_un l2_addr;
  46. };
  47. static void wpa_priv_cmd_register(struct wpa_priv_interface *iface,
  48. struct sockaddr_un *from)
  49. {
  50. if (iface->drv_priv) {
  51. wpa_printf(MSG_DEBUG, "Cleaning up forgotten driver instance");
  52. if (iface->driver->set_wpa)
  53. iface->driver->set_wpa(iface->drv_priv, 0);
  54. if (iface->driver->deinit)
  55. iface->driver->deinit(iface->drv_priv);
  56. iface->drv_priv = NULL;
  57. iface->wpas_registered = 0;
  58. }
  59. if (iface->l2) {
  60. wpa_printf(MSG_DEBUG, "Cleaning up forgotten l2_packet "
  61. "instance");
  62. l2_packet_deinit(iface->l2);
  63. iface->l2 = NULL;
  64. }
  65. if (iface->driver->init == NULL)
  66. return;
  67. iface->drv_priv = iface->driver->init(iface, iface->ifname);
  68. if (iface->drv_priv == NULL) {
  69. wpa_printf(MSG_DEBUG, "Failed to initialize driver wrapper");
  70. return;
  71. }
  72. wpa_printf(MSG_DEBUG, "Driver wrapper '%s' initialized for interface "
  73. "'%s'", iface->driver_name, iface->ifname);
  74. os_memcpy(&iface->drv_addr, from, sizeof(iface->drv_addr));
  75. iface->wpas_registered = 1;
  76. if (iface->driver->set_param &&
  77. iface->driver->set_param(iface->drv_priv, NULL) < 0) {
  78. wpa_printf(MSG_ERROR, "Driver interface rejected param");
  79. }
  80. if (iface->driver->set_wpa)
  81. iface->driver->set_wpa(iface->drv_priv, 1);
  82. }
  83. static void wpa_priv_cmd_unregister(struct wpa_priv_interface *iface,
  84. struct sockaddr_un *from)
  85. {
  86. if (iface->drv_priv) {
  87. if (iface->driver->set_wpa)
  88. iface->driver->set_wpa(iface->drv_priv, 0);
  89. if (iface->driver->deinit)
  90. iface->driver->deinit(iface->drv_priv);
  91. iface->drv_priv = NULL;
  92. iface->wpas_registered = 0;
  93. }
  94. }
  95. static void wpa_priv_cmd_set_wpa(struct wpa_priv_interface *iface,
  96. char *buf, size_t len)
  97. {
  98. if (iface->drv_priv == NULL || len != sizeof(int))
  99. return;
  100. if (iface->driver->set_wpa)
  101. iface->driver->set_wpa(iface->drv_priv, *((int *) buf));
  102. }
  103. static void wpa_priv_cmd_scan(struct wpa_priv_interface *iface,
  104. char *buf, size_t len)
  105. {
  106. if (iface->drv_priv == NULL)
  107. return;
  108. if (iface->driver->scan)
  109. iface->driver->scan(iface->drv_priv, len ? (u8 *) buf : NULL,
  110. len);
  111. }
  112. static void wpa_priv_get_scan_results2(struct wpa_priv_interface *iface,
  113. struct sockaddr_un *from)
  114. {
  115. struct wpa_scan_results *res;
  116. u8 *buf = NULL, *pos, *end;
  117. int val;
  118. size_t i;
  119. res = iface->driver->get_scan_results2(iface->drv_priv);
  120. if (res == NULL)
  121. goto fail;
  122. buf = os_malloc(60000);
  123. if (buf == NULL)
  124. goto fail;
  125. pos = buf;
  126. end = buf + 60000;
  127. val = res->num;
  128. os_memcpy(pos, &val, sizeof(int));
  129. pos += sizeof(int);
  130. for (i = 0; i < res->num; i++) {
  131. struct wpa_scan_res *r = res->res[i];
  132. val = sizeof(*r) + r->ie_len;
  133. if (end - pos < (int) sizeof(int) + val)
  134. break;
  135. os_memcpy(pos, &val, sizeof(int));
  136. pos += sizeof(int);
  137. os_memcpy(pos, r, val);
  138. pos += val;
  139. }
  140. sendto(iface->fd, buf, pos - buf, 0, (struct sockaddr *) from,
  141. sizeof(*from));
  142. os_free(buf);
  143. os_free(res);
  144. return;
  145. fail:
  146. os_free(buf);
  147. os_free(res);
  148. sendto(iface->fd, "", 0, 0, (struct sockaddr *) from, sizeof(*from));
  149. }
  150. static void wpa_priv_send_old_scan_results(struct wpa_priv_interface *iface,
  151. struct sockaddr_un *from)
  152. {
  153. #define SCAN_AP_LIMIT 128
  154. int i, res, val;
  155. struct wpa_scan_result *results = NULL;
  156. u8 *buf = NULL, *pos, *end;
  157. struct wpa_scan_res nres;
  158. results = os_malloc(SCAN_AP_LIMIT * sizeof(*results));
  159. if (results == NULL)
  160. goto fail;
  161. res = iface->driver->get_scan_results(iface->drv_priv, results,
  162. SCAN_AP_LIMIT);
  163. if (res < 0 || res > SCAN_AP_LIMIT)
  164. goto fail;
  165. buf = os_malloc(60000);
  166. if (buf == NULL)
  167. goto fail;
  168. pos = buf;
  169. end = buf + 60000;
  170. os_memcpy(pos, &res, sizeof(int));
  171. pos += sizeof(int);
  172. os_memset(&nres, 0, sizeof(nres));
  173. for (i = 0; i < res; i++) {
  174. struct wpa_scan_result *r = &results[i];
  175. size_t ie_len;
  176. ie_len = 2 + r->ssid_len + r->rsn_ie_len + r->wpa_ie_len;
  177. if (r->maxrate)
  178. ie_len += 3;
  179. if (r->mdie_present)
  180. ie_len += 5;
  181. val = sizeof(nres) + ie_len;
  182. if (end - pos < (int) sizeof(int) + val)
  183. break;
  184. os_memcpy(pos, &val, sizeof(int));
  185. pos += sizeof(int);
  186. os_memcpy(nres.bssid, r->bssid, ETH_ALEN);
  187. nres.freq = r->freq;
  188. nres.caps = r->caps;
  189. nres.qual = r->qual;
  190. nres.noise = r->noise;
  191. nres.level = r->level;
  192. nres.tsf = r->tsf;
  193. nres.ie_len = ie_len;
  194. os_memcpy(pos, &nres, sizeof(nres));
  195. pos += sizeof(nres);
  196. /* SSID IE */
  197. *pos++ = WLAN_EID_SSID;
  198. *pos++ = r->ssid_len;
  199. os_memcpy(pos, r->ssid, r->ssid_len);
  200. pos += r->ssid_len;
  201. if (r->maxrate) {
  202. /* Fake Supported Rate IE to include max rate */
  203. *pos++ = WLAN_EID_SUPP_RATES;
  204. *pos++ = 1;
  205. *pos++ = r->maxrate;
  206. }
  207. if (r->rsn_ie_len) {
  208. os_memcpy(pos, r->rsn_ie, r->rsn_ie_len);
  209. pos += r->rsn_ie_len;
  210. }
  211. if (r->mdie_present) {
  212. os_memcpy(pos, r->mdie, 5);
  213. pos += 5;
  214. }
  215. if (r->wpa_ie_len) {
  216. os_memcpy(pos, r->wpa_ie, r->wpa_ie_len);
  217. pos += r->wpa_ie_len;
  218. }
  219. }
  220. sendto(iface->fd, buf, pos - buf, 0, (struct sockaddr *) from,
  221. sizeof(*from));
  222. os_free(buf);
  223. os_free(results);
  224. return;
  225. fail:
  226. os_free(buf);
  227. os_free(results);
  228. sendto(iface->fd, "", 0, 0, (struct sockaddr *) from, sizeof(*from));
  229. }
  230. static void wpa_priv_cmd_get_scan_results(struct wpa_priv_interface *iface,
  231. struct sockaddr_un *from)
  232. {
  233. if (iface->drv_priv == NULL)
  234. return;
  235. if (iface->driver->get_scan_results2)
  236. wpa_priv_get_scan_results2(iface, from);
  237. else if (iface->driver->get_scan_results)
  238. wpa_priv_send_old_scan_results(iface, from);
  239. else
  240. sendto(iface->fd, "", 0, 0, (struct sockaddr *) from,
  241. sizeof(*from));
  242. }
  243. static void wpa_priv_cmd_associate(struct wpa_priv_interface *iface,
  244. void *buf, size_t len)
  245. {
  246. struct wpa_driver_associate_params params;
  247. struct privsep_cmd_associate *assoc;
  248. u8 *bssid;
  249. int res;
  250. if (iface->drv_priv == NULL || iface->driver->associate == NULL)
  251. return;
  252. if (len < sizeof(*assoc)) {
  253. wpa_printf(MSG_DEBUG, "Invalid association request");
  254. return;
  255. }
  256. assoc = buf;
  257. if (sizeof(*assoc) + assoc->wpa_ie_len > len) {
  258. wpa_printf(MSG_DEBUG, "Association request overflow");
  259. return;
  260. }
  261. os_memset(&params, 0, sizeof(params));
  262. bssid = assoc->bssid;
  263. if (bssid[0] | bssid[1] | bssid[2] | bssid[3] | bssid[4] | bssid[5])
  264. params.bssid = bssid;
  265. params.ssid = assoc->ssid;
  266. if (assoc->ssid_len > 32)
  267. return;
  268. params.ssid_len = assoc->ssid_len;
  269. params.freq = assoc->freq;
  270. if (assoc->wpa_ie_len) {
  271. params.wpa_ie = (u8 *) (assoc + 1);
  272. params.wpa_ie_len = assoc->wpa_ie_len;
  273. }
  274. params.pairwise_suite = assoc->pairwise_suite;
  275. params.group_suite = assoc->group_suite;
  276. params.key_mgmt_suite = assoc->key_mgmt_suite;
  277. params.auth_alg = assoc->auth_alg;
  278. params.mode = assoc->mode;
  279. res = iface->driver->associate(iface->drv_priv, &params);
  280. wpa_printf(MSG_DEBUG, "drv->associate: res=%d", res);
  281. }
  282. static void wpa_priv_cmd_get_bssid(struct wpa_priv_interface *iface,
  283. struct sockaddr_un *from)
  284. {
  285. u8 bssid[ETH_ALEN];
  286. if (iface->drv_priv == NULL)
  287. goto fail;
  288. if (iface->driver->get_bssid == NULL ||
  289. iface->driver->get_bssid(iface->drv_priv, bssid) < 0)
  290. goto fail;
  291. sendto(iface->fd, bssid, ETH_ALEN, 0, (struct sockaddr *) from,
  292. sizeof(*from));
  293. return;
  294. fail:
  295. sendto(iface->fd, "", 0, 0, (struct sockaddr *) from, sizeof(*from));
  296. }
  297. static void wpa_priv_cmd_get_ssid(struct wpa_priv_interface *iface,
  298. struct sockaddr_un *from)
  299. {
  300. u8 ssid[sizeof(int) + 32];
  301. int res;
  302. if (iface->drv_priv == NULL)
  303. goto fail;
  304. if (iface->driver->get_ssid == NULL)
  305. goto fail;
  306. res = iface->driver->get_ssid(iface->drv_priv, &ssid[sizeof(int)]);
  307. if (res < 0 || res > 32)
  308. goto fail;
  309. os_memcpy(ssid, &res, sizeof(int));
  310. sendto(iface->fd, ssid, sizeof(ssid), 0, (struct sockaddr *) from,
  311. sizeof(*from));
  312. return;
  313. fail:
  314. sendto(iface->fd, "", 0, 0, (struct sockaddr *) from, sizeof(*from));
  315. }
  316. static void wpa_priv_cmd_set_key(struct wpa_priv_interface *iface,
  317. void *buf, size_t len)
  318. {
  319. struct privsep_cmd_set_key *params;
  320. int res;
  321. if (iface->drv_priv == NULL || iface->driver->set_key == NULL)
  322. return;
  323. if (len != sizeof(*params)) {
  324. wpa_printf(MSG_DEBUG, "Invalid set_key request");
  325. return;
  326. }
  327. params = buf;
  328. res = iface->driver->set_key(iface->drv_priv, params->alg,
  329. params->addr, params->key_idx,
  330. params->set_tx,
  331. params->seq_len ? params->seq : NULL,
  332. params->seq_len,
  333. params->key_len ? params->key : NULL,
  334. params->key_len);
  335. wpa_printf(MSG_DEBUG, "drv->set_key: res=%d", res);
  336. }
  337. static void wpa_priv_cmd_get_capa(struct wpa_priv_interface *iface,
  338. struct sockaddr_un *from)
  339. {
  340. struct wpa_driver_capa capa;
  341. if (iface->drv_priv == NULL)
  342. goto fail;
  343. if (iface->driver->get_capa == NULL ||
  344. iface->driver->get_capa(iface->drv_priv, &capa) < 0)
  345. goto fail;
  346. sendto(iface->fd, &capa, sizeof(capa), 0, (struct sockaddr *) from,
  347. sizeof(*from));
  348. return;
  349. fail:
  350. sendto(iface->fd, "", 0, 0, (struct sockaddr *) from, sizeof(*from));
  351. }
  352. static void wpa_priv_l2_rx(void *ctx, const u8 *src_addr, const u8 *buf,
  353. size_t len)
  354. {
  355. struct wpa_priv_interface *iface = ctx;
  356. struct msghdr msg;
  357. struct iovec io[2];
  358. io[0].iov_base = (u8 *) src_addr;
  359. io[0].iov_len = ETH_ALEN;
  360. io[1].iov_base = (u8 *) buf;
  361. io[1].iov_len = len;
  362. os_memset(&msg, 0, sizeof(msg));
  363. msg.msg_iov = io;
  364. msg.msg_iovlen = 2;
  365. msg.msg_name = &iface->l2_addr;
  366. msg.msg_namelen = sizeof(iface->l2_addr);
  367. if (sendmsg(iface->fd, &msg, 0) < 0) {
  368. perror("sendmsg(l2 rx)");
  369. }
  370. }
  371. static void wpa_priv_cmd_l2_register(struct wpa_priv_interface *iface,
  372. struct sockaddr_un *from,
  373. void *buf, size_t len)
  374. {
  375. int *reg_cmd = buf;
  376. u8 own_addr[ETH_ALEN];
  377. int res;
  378. u16 proto;
  379. if (len != 2 * sizeof(int)) {
  380. wpa_printf(MSG_DEBUG, "Invalid l2_register length %lu",
  381. (unsigned long) len);
  382. return;
  383. }
  384. proto = reg_cmd[0];
  385. if (proto != ETH_P_EAPOL && proto != ETH_P_RSN_PREAUTH) {
  386. wpa_printf(MSG_DEBUG, "Refused l2_packet connection for "
  387. "ethertype 0x%x", proto);
  388. return;
  389. }
  390. if (iface->l2) {
  391. wpa_printf(MSG_DEBUG, "Cleaning up forgotten l2_packet "
  392. "instance");
  393. l2_packet_deinit(iface->l2);
  394. iface->l2 = NULL;
  395. }
  396. os_memcpy(&iface->l2_addr, from, sizeof(iface->l2_addr));
  397. iface->l2 = l2_packet_init(iface->ifname, NULL, proto,
  398. wpa_priv_l2_rx, iface, reg_cmd[1]);
  399. if (iface->l2 == NULL) {
  400. wpa_printf(MSG_DEBUG, "Failed to initialize l2_packet "
  401. "instance for protocol %d", proto);
  402. return;
  403. }
  404. if (l2_packet_get_own_addr(iface->l2, own_addr) < 0) {
  405. wpa_printf(MSG_DEBUG, "Failed to get own address from "
  406. "l2_packet");
  407. l2_packet_deinit(iface->l2);
  408. iface->l2 = NULL;
  409. return;
  410. }
  411. res = sendto(iface->fd, own_addr, ETH_ALEN, 0,
  412. (struct sockaddr *) from, sizeof(*from));
  413. wpa_printf(MSG_DEBUG, "L2 registration: res=%d", res);
  414. }
  415. static void wpa_priv_cmd_l2_unregister(struct wpa_priv_interface *iface,
  416. struct sockaddr_un *from)
  417. {
  418. if (iface->l2) {
  419. l2_packet_deinit(iface->l2);
  420. iface->l2 = NULL;
  421. }
  422. }
  423. static void wpa_priv_cmd_l2_notify_auth_start(struct wpa_priv_interface *iface,
  424. struct sockaddr_un *from)
  425. {
  426. if (iface->l2)
  427. l2_packet_notify_auth_start(iface->l2);
  428. }
  429. static void wpa_priv_cmd_l2_send(struct wpa_priv_interface *iface,
  430. struct sockaddr_un *from,
  431. void *buf, size_t len)
  432. {
  433. u8 *dst_addr;
  434. u16 proto;
  435. int res;
  436. if (iface->l2 == NULL)
  437. return;
  438. if (len < ETH_ALEN + 2) {
  439. wpa_printf(MSG_DEBUG, "Too short L2 send packet (len=%lu)",
  440. (unsigned long) len);
  441. return;
  442. }
  443. dst_addr = buf;
  444. os_memcpy(&proto, buf + ETH_ALEN, 2);
  445. if (proto != ETH_P_EAPOL && proto != ETH_P_RSN_PREAUTH) {
  446. wpa_printf(MSG_DEBUG, "Refused l2_packet send for ethertype "
  447. "0x%x", proto);
  448. return;
  449. }
  450. res = l2_packet_send(iface->l2, dst_addr, proto, buf + ETH_ALEN + 2,
  451. len - ETH_ALEN - 2);
  452. wpa_printf(MSG_DEBUG, "L2 send: res=%d", res);
  453. }
  454. static void wpa_priv_cmd_set_mode(struct wpa_priv_interface *iface,
  455. void *buf, size_t len)
  456. {
  457. if (iface->drv_priv == NULL || iface->driver->set_mode == NULL ||
  458. len != sizeof(int))
  459. return;
  460. iface->driver->set_mode(iface->drv_priv, *((int *) buf));
  461. }
  462. static void wpa_priv_cmd_set_country(struct wpa_priv_interface *iface,
  463. char *buf)
  464. {
  465. if (iface->drv_priv == NULL || iface->driver->set_country == NULL ||
  466. *buf == '\0')
  467. return;
  468. iface->driver->set_country(iface->drv_priv, buf);
  469. }
  470. static void wpa_priv_receive(int sock, void *eloop_ctx, void *sock_ctx)
  471. {
  472. struct wpa_priv_interface *iface = eloop_ctx;
  473. char buf[2000], *pos;
  474. void *cmd_buf;
  475. size_t cmd_len;
  476. int res, cmd;
  477. struct sockaddr_un from;
  478. socklen_t fromlen = sizeof(from);
  479. res = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *) &from,
  480. &fromlen);
  481. if (res < 0) {
  482. perror("recvfrom");
  483. return;
  484. }
  485. if (res < (int) sizeof(int)) {
  486. wpa_printf(MSG_DEBUG, "Too short command (len=%d)", res);
  487. return;
  488. }
  489. os_memcpy(&cmd, buf, sizeof(int));
  490. wpa_printf(MSG_DEBUG, "Command %d for interface %s",
  491. cmd, iface->ifname);
  492. cmd_buf = &buf[sizeof(int)];
  493. cmd_len = res - sizeof(int);
  494. switch (cmd) {
  495. case PRIVSEP_CMD_REGISTER:
  496. wpa_priv_cmd_register(iface, &from);
  497. break;
  498. case PRIVSEP_CMD_UNREGISTER:
  499. wpa_priv_cmd_unregister(iface, &from);
  500. break;
  501. case PRIVSEP_CMD_SET_WPA:
  502. wpa_priv_cmd_set_wpa(iface, cmd_buf, cmd_len);
  503. break;
  504. case PRIVSEP_CMD_SCAN:
  505. wpa_priv_cmd_scan(iface, cmd_buf, cmd_len);
  506. break;
  507. case PRIVSEP_CMD_GET_SCAN_RESULTS:
  508. wpa_priv_cmd_get_scan_results(iface, &from);
  509. break;
  510. case PRIVSEP_CMD_ASSOCIATE:
  511. wpa_priv_cmd_associate(iface, cmd_buf, cmd_len);
  512. break;
  513. case PRIVSEP_CMD_GET_BSSID:
  514. wpa_priv_cmd_get_bssid(iface, &from);
  515. break;
  516. case PRIVSEP_CMD_GET_SSID:
  517. wpa_priv_cmd_get_ssid(iface, &from);
  518. break;
  519. case PRIVSEP_CMD_SET_KEY:
  520. wpa_priv_cmd_set_key(iface, cmd_buf, cmd_len);
  521. break;
  522. case PRIVSEP_CMD_GET_CAPA:
  523. wpa_priv_cmd_get_capa(iface, &from);
  524. break;
  525. case PRIVSEP_CMD_L2_REGISTER:
  526. wpa_priv_cmd_l2_register(iface, &from, cmd_buf, cmd_len);
  527. break;
  528. case PRIVSEP_CMD_L2_UNREGISTER:
  529. wpa_priv_cmd_l2_unregister(iface, &from);
  530. break;
  531. case PRIVSEP_CMD_L2_NOTIFY_AUTH_START:
  532. wpa_priv_cmd_l2_notify_auth_start(iface, &from);
  533. break;
  534. case PRIVSEP_CMD_L2_SEND:
  535. wpa_priv_cmd_l2_send(iface, &from, cmd_buf, cmd_len);
  536. break;
  537. case PRIVSEP_CMD_SET_MODE:
  538. wpa_priv_cmd_set_mode(iface, cmd_buf, cmd_len);
  539. break;
  540. case PRIVSEP_CMD_SET_COUNTRY:
  541. pos = cmd_buf;
  542. if (pos + cmd_len >= buf + sizeof(buf))
  543. break;
  544. pos[cmd_len] = '\0';
  545. wpa_priv_cmd_set_country(iface, pos);
  546. break;
  547. }
  548. }
  549. static void wpa_priv_interface_deinit(struct wpa_priv_interface *iface)
  550. {
  551. if (iface->drv_priv && iface->driver->deinit)
  552. iface->driver->deinit(iface->drv_priv);
  553. if (iface->fd >= 0) {
  554. eloop_unregister_read_sock(iface->fd);
  555. close(iface->fd);
  556. unlink(iface->sock_name);
  557. }
  558. if (iface->l2)
  559. l2_packet_deinit(iface->l2);
  560. os_free(iface->ifname);
  561. os_free(iface->driver_name);
  562. os_free(iface->sock_name);
  563. os_free(iface);
  564. }
  565. extern struct wpa_driver_ops *wpa_drivers[];
  566. static struct wpa_priv_interface *
  567. wpa_priv_interface_init(const char *dir, const char *params)
  568. {
  569. struct wpa_priv_interface *iface;
  570. char *pos;
  571. size_t len;
  572. struct sockaddr_un addr;
  573. int i;
  574. pos = os_strchr(params, ':');
  575. if (pos == NULL)
  576. return NULL;
  577. iface = os_zalloc(sizeof(*iface));
  578. if (iface == NULL)
  579. return NULL;
  580. iface->fd = -1;
  581. len = pos - params;
  582. iface->driver_name = os_malloc(len + 1);
  583. if (iface->driver_name == NULL) {
  584. wpa_priv_interface_deinit(iface);
  585. return NULL;
  586. }
  587. os_memcpy(iface->driver_name, params, len);
  588. iface->driver_name[len] = '\0';
  589. for (i = 0; wpa_drivers[i]; i++) {
  590. if (os_strcmp(iface->driver_name,
  591. wpa_drivers[i]->name) == 0) {
  592. iface->driver = wpa_drivers[i];
  593. break;
  594. }
  595. }
  596. if (iface->driver == NULL) {
  597. wpa_printf(MSG_ERROR, "Unsupported driver '%s'",
  598. iface->driver_name);
  599. wpa_priv_interface_deinit(iface);
  600. return NULL;
  601. }
  602. pos++;
  603. iface->ifname = os_strdup(pos);
  604. if (iface->ifname == NULL) {
  605. wpa_priv_interface_deinit(iface);
  606. return NULL;
  607. }
  608. len = os_strlen(dir) + 1 + os_strlen(iface->ifname);
  609. iface->sock_name = os_malloc(len + 1);
  610. if (iface->sock_name == NULL) {
  611. wpa_priv_interface_deinit(iface);
  612. return NULL;
  613. }
  614. os_snprintf(iface->sock_name, len + 1, "%s/%s", dir, iface->ifname);
  615. if (os_strlen(iface->sock_name) >= sizeof(addr.sun_path)) {
  616. wpa_priv_interface_deinit(iface);
  617. return NULL;
  618. }
  619. iface->fd = socket(PF_UNIX, SOCK_DGRAM, 0);
  620. if (iface->fd < 0) {
  621. perror("socket(PF_UNIX)");
  622. wpa_priv_interface_deinit(iface);
  623. return NULL;
  624. }
  625. os_memset(&addr, 0, sizeof(addr));
  626. addr.sun_family = AF_UNIX;
  627. os_strlcpy(addr.sun_path, iface->sock_name, sizeof(addr.sun_path));
  628. if (bind(iface->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  629. wpa_printf(MSG_DEBUG, "bind(PF_UNIX) failed: %s",
  630. strerror(errno));
  631. if (connect(iface->fd, (struct sockaddr *) &addr,
  632. sizeof(addr)) < 0) {
  633. wpa_printf(MSG_DEBUG, "Socket exists, but does not "
  634. "allow connections - assuming it was "
  635. "leftover from forced program termination");
  636. if (unlink(iface->sock_name) < 0) {
  637. perror("unlink[ctrl_iface]");
  638. wpa_printf(MSG_ERROR, "Could not unlink "
  639. "existing ctrl_iface socket '%s'",
  640. iface->sock_name);
  641. goto fail;
  642. }
  643. if (bind(iface->fd, (struct sockaddr *) &addr,
  644. sizeof(addr)) < 0) {
  645. perror("bind(PF_UNIX)");
  646. goto fail;
  647. }
  648. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  649. "socket '%s'", iface->sock_name);
  650. } else {
  651. wpa_printf(MSG_INFO, "Socket exists and seems to be "
  652. "in use - cannot override it");
  653. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  654. "not used anymore", iface->sock_name);
  655. goto fail;
  656. }
  657. }
  658. if (chmod(iface->sock_name, S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
  659. perror("chmod");
  660. goto fail;
  661. }
  662. eloop_register_read_sock(iface->fd, wpa_priv_receive, iface, NULL);
  663. return iface;
  664. fail:
  665. wpa_priv_interface_deinit(iface);
  666. return NULL;
  667. }
  668. static int wpa_priv_send_event(struct wpa_priv_interface *iface, int event,
  669. const void *data, size_t data_len)
  670. {
  671. struct msghdr msg;
  672. struct iovec io[2];
  673. io[0].iov_base = &event;
  674. io[0].iov_len = sizeof(event);
  675. io[1].iov_base = (u8 *) data;
  676. io[1].iov_len = data_len;
  677. os_memset(&msg, 0, sizeof(msg));
  678. msg.msg_iov = io;
  679. msg.msg_iovlen = data ? 2 : 1;
  680. msg.msg_name = &iface->drv_addr;
  681. msg.msg_namelen = sizeof(iface->drv_addr);
  682. if (sendmsg(iface->fd, &msg, 0) < 0) {
  683. perror("sendmsg(wpas_socket)");
  684. return -1;
  685. }
  686. return 0;
  687. }
  688. static void wpa_priv_send_assoc(struct wpa_priv_interface *iface, int event,
  689. union wpa_event_data *data)
  690. {
  691. size_t buflen = 3 * sizeof(int);
  692. u8 *buf, *pos;
  693. int len;
  694. if (data) {
  695. buflen += data->assoc_info.req_ies_len +
  696. data->assoc_info.resp_ies_len +
  697. data->assoc_info.beacon_ies_len;
  698. }
  699. buf = os_malloc(buflen);
  700. if (buf == NULL)
  701. return;
  702. pos = buf;
  703. if (data && data->assoc_info.req_ies) {
  704. len = data->assoc_info.req_ies_len;
  705. os_memcpy(pos, &len, sizeof(int));
  706. pos += sizeof(int);
  707. os_memcpy(pos, data->assoc_info.req_ies, len);
  708. pos += len;
  709. } else {
  710. len = 0;
  711. os_memcpy(pos, &len, sizeof(int));
  712. pos += sizeof(int);
  713. }
  714. if (data && data->assoc_info.resp_ies) {
  715. len = data->assoc_info.resp_ies_len;
  716. os_memcpy(pos, &len, sizeof(int));
  717. pos += sizeof(int);
  718. os_memcpy(pos, data->assoc_info.resp_ies, len);
  719. pos += len;
  720. } else {
  721. len = 0;
  722. os_memcpy(pos, &len, sizeof(int));
  723. pos += sizeof(int);
  724. }
  725. if (data && data->assoc_info.beacon_ies) {
  726. len = data->assoc_info.beacon_ies_len;
  727. os_memcpy(pos, &len, sizeof(int));
  728. pos += sizeof(int);
  729. os_memcpy(pos, data->assoc_info.beacon_ies, len);
  730. pos += len;
  731. } else {
  732. len = 0;
  733. os_memcpy(pos, &len, sizeof(int));
  734. pos += sizeof(int);
  735. }
  736. wpa_priv_send_event(iface, event, buf, buflen);
  737. os_free(buf);
  738. }
  739. static void wpa_priv_send_interface_status(struct wpa_priv_interface *iface,
  740. union wpa_event_data *data)
  741. {
  742. int ievent;
  743. size_t len, maxlen;
  744. u8 *buf;
  745. char *ifname;
  746. if (data == NULL)
  747. return;
  748. ievent = data->interface_status.ievent;
  749. maxlen = sizeof(data->interface_status.ifname);
  750. ifname = data->interface_status.ifname;
  751. for (len = 0; len < maxlen && ifname[len]; len++)
  752. ;
  753. buf = os_malloc(sizeof(int) + len);
  754. if (buf == NULL)
  755. return;
  756. os_memcpy(buf, &ievent, sizeof(int));
  757. os_memcpy(buf + sizeof(int), ifname, len);
  758. wpa_priv_send_event(iface, PRIVSEP_EVENT_INTERFACE_STATUS,
  759. buf, sizeof(int) + len);
  760. os_free(buf);
  761. }
  762. static void wpa_priv_send_ft_response(struct wpa_priv_interface *iface,
  763. union wpa_event_data *data)
  764. {
  765. size_t len;
  766. u8 *buf, *pos;
  767. if (data == NULL || data->ft_ies.ies == NULL)
  768. return;
  769. len = sizeof(int) + ETH_ALEN + data->ft_ies.ies_len;
  770. buf = os_malloc(len);
  771. if (buf == NULL)
  772. return;
  773. pos = buf;
  774. os_memcpy(pos, &data->ft_ies.ft_action, sizeof(int));
  775. pos += sizeof(int);
  776. os_memcpy(pos, data->ft_ies.target_ap, ETH_ALEN);
  777. pos += ETH_ALEN;
  778. os_memcpy(pos, data->ft_ies.ies, data->ft_ies.ies_len);
  779. wpa_priv_send_event(iface, PRIVSEP_EVENT_FT_RESPONSE, buf, len);
  780. os_free(buf);
  781. }
  782. void wpa_supplicant_event(void *ctx, wpa_event_type event,
  783. union wpa_event_data *data)
  784. {
  785. struct wpa_priv_interface *iface = ctx;
  786. wpa_printf(MSG_DEBUG, "%s - event=%d", __func__, event);
  787. if (!iface->wpas_registered) {
  788. wpa_printf(MSG_DEBUG, "Driver event received, but "
  789. "wpa_supplicant not registered");
  790. return;
  791. }
  792. switch (event) {
  793. case EVENT_ASSOC:
  794. wpa_priv_send_assoc(iface, PRIVSEP_EVENT_ASSOC, data);
  795. break;
  796. case EVENT_DISASSOC:
  797. wpa_priv_send_event(iface, PRIVSEP_EVENT_DISASSOC, NULL, 0);
  798. break;
  799. case EVENT_ASSOCINFO:
  800. if (data == NULL)
  801. return;
  802. wpa_priv_send_assoc(iface, PRIVSEP_EVENT_ASSOCINFO, data);
  803. break;
  804. case EVENT_MICHAEL_MIC_FAILURE:
  805. if (data == NULL)
  806. return;
  807. wpa_priv_send_event(iface, PRIVSEP_EVENT_MICHAEL_MIC_FAILURE,
  808. &data->michael_mic_failure.unicast,
  809. sizeof(int));
  810. break;
  811. case EVENT_SCAN_RESULTS:
  812. wpa_priv_send_event(iface, PRIVSEP_EVENT_SCAN_RESULTS, NULL,
  813. 0);
  814. break;
  815. case EVENT_INTERFACE_STATUS:
  816. wpa_priv_send_interface_status(iface, data);
  817. break;
  818. case EVENT_PMKID_CANDIDATE:
  819. if (data == NULL)
  820. return;
  821. wpa_priv_send_event(iface, PRIVSEP_EVENT_PMKID_CANDIDATE,
  822. &data->pmkid_candidate,
  823. sizeof(struct pmkid_candidate));
  824. break;
  825. case EVENT_STKSTART:
  826. if (data == NULL)
  827. return;
  828. wpa_priv_send_event(iface, PRIVSEP_EVENT_STKSTART,
  829. &data->stkstart.peer, ETH_ALEN);
  830. break;
  831. case EVENT_FT_RESPONSE:
  832. wpa_priv_send_ft_response(iface, data);
  833. break;
  834. default:
  835. wpa_printf(MSG_DEBUG, "Unsupported driver event %d - TODO",
  836. event);
  837. break;
  838. }
  839. }
  840. void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
  841. const u8 *buf, size_t len)
  842. {
  843. struct wpa_priv_interface *iface = ctx;
  844. struct msghdr msg;
  845. struct iovec io[3];
  846. int event = PRIVSEP_EVENT_RX_EAPOL;
  847. wpa_printf(MSG_DEBUG, "RX EAPOL from driver");
  848. io[0].iov_base = &event;
  849. io[0].iov_len = sizeof(event);
  850. io[1].iov_base = (u8 *) src_addr;
  851. io[1].iov_len = ETH_ALEN;
  852. io[2].iov_base = (u8 *) buf;
  853. io[2].iov_len = len;
  854. os_memset(&msg, 0, sizeof(msg));
  855. msg.msg_iov = io;
  856. msg.msg_iovlen = 3;
  857. msg.msg_name = &iface->drv_addr;
  858. msg.msg_namelen = sizeof(iface->drv_addr);
  859. if (sendmsg(iface->fd, &msg, 0) < 0)
  860. perror("sendmsg(wpas_socket)");
  861. }
  862. #ifdef CONFIG_CLIENT_MLME
  863. void wpa_supplicant_sta_rx(void *ctx, const u8 *buf, size_t len,
  864. struct ieee80211_rx_status *rx_status)
  865. {
  866. struct wpa_priv_interface *iface = ctx;
  867. struct msghdr msg;
  868. struct iovec io[3];
  869. int event = PRIVSEP_EVENT_STA_RX;
  870. wpa_printf(MSG_DEBUG, "STA RX from driver");
  871. io[0].iov_base = &event;
  872. io[0].iov_len = sizeof(event);
  873. io[1].iov_base = (u8 *) rx_status;
  874. io[1].iov_len = sizeof(*rx_status);
  875. io[2].iov_base = (u8 *) buf;
  876. io[2].iov_len = len;
  877. os_memset(&msg, 0, sizeof(msg));
  878. msg.msg_iov = io;
  879. msg.msg_iovlen = 3;
  880. msg.msg_name = &iface->drv_addr;
  881. msg.msg_namelen = sizeof(iface->drv_addr);
  882. if (sendmsg(iface->fd, &msg, 0) < 0)
  883. perror("sendmsg(wpas_socket)");
  884. }
  885. #endif /* CONFIG_CLIENT_MLME */
  886. static void wpa_priv_terminate(int sig, void *eloop_ctx, void *signal_ctx)
  887. {
  888. wpa_printf(MSG_DEBUG, "wpa_priv termination requested");
  889. eloop_terminate();
  890. }
  891. static void wpa_priv_fd_workaround(void)
  892. {
  893. #ifdef __linux__
  894. int s, i;
  895. /* When started from pcmcia-cs scripts, wpa_supplicant might start with
  896. * fd 0, 1, and 2 closed. This will cause some issues because many
  897. * places in wpa_supplicant are still printing out to stdout. As a
  898. * workaround, make sure that fd's 0, 1, and 2 are not used for other
  899. * sockets. */
  900. for (i = 0; i < 3; i++) {
  901. s = open("/dev/null", O_RDWR);
  902. if (s > 2) {
  903. close(s);
  904. break;
  905. }
  906. }
  907. #endif /* __linux__ */
  908. }
  909. static void usage(void)
  910. {
  911. printf("wpa_priv v" VERSION_STR "\n"
  912. "Copyright (c) 2007-2009, Jouni Malinen <j@w1.fi> and "
  913. "contributors\n"
  914. "\n"
  915. "usage:\n"
  916. " wpa_priv [-Bdd] [-P<pid file>] <driver:ifname> "
  917. "[driver:ifname ...]\n");
  918. }
  919. extern int wpa_debug_level;
  920. int main(int argc, char *argv[])
  921. {
  922. int c, i;
  923. int ret = -1;
  924. char *pid_file = NULL;
  925. int daemonize = 0;
  926. char *ctrl_dir = "/var/run/wpa_priv";
  927. struct wpa_priv_interface *interfaces = NULL, *iface;
  928. if (os_program_init())
  929. return -1;
  930. wpa_priv_fd_workaround();
  931. for (;;) {
  932. c = getopt(argc, argv, "Bc:dP:");
  933. if (c < 0)
  934. break;
  935. switch (c) {
  936. case 'B':
  937. daemonize++;
  938. break;
  939. case 'c':
  940. ctrl_dir = optarg;
  941. break;
  942. case 'd':
  943. wpa_debug_level--;
  944. break;
  945. case 'P':
  946. pid_file = os_rel2abs_path(optarg);
  947. break;
  948. default:
  949. usage();
  950. goto out;
  951. }
  952. }
  953. if (optind >= argc) {
  954. usage();
  955. goto out;
  956. }
  957. wpa_printf(MSG_DEBUG, "wpa_priv control directory: '%s'", ctrl_dir);
  958. if (eloop_init(NULL)) {
  959. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  960. goto out;
  961. }
  962. for (i = optind; i < argc; i++) {
  963. wpa_printf(MSG_DEBUG, "Adding driver:interface %s", argv[i]);
  964. iface = wpa_priv_interface_init(ctrl_dir, argv[i]);
  965. if (iface == NULL)
  966. goto out;
  967. iface->next = interfaces;
  968. interfaces = iface;
  969. }
  970. if (daemonize && os_daemonize(pid_file))
  971. goto out;
  972. eloop_register_signal_terminate(wpa_priv_terminate, NULL);
  973. eloop_run();
  974. ret = 0;
  975. out:
  976. iface = interfaces;
  977. while (iface) {
  978. struct wpa_priv_interface *prev = iface;
  979. iface = iface->next;
  980. wpa_priv_interface_deinit(prev);
  981. }
  982. eloop_destroy();
  983. os_daemonize_terminate(pid_file);
  984. os_free(pid_file);
  985. os_program_deinit();
  986. return ret;
  987. }