wpa_priv.c 27 KB

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