wpa_priv.c 29 KB

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