Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

wpa_priv.c 23KB

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