wpa_priv.c 24 KB

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