driver_privsep.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. /*
  2. * WPA Supplicant - privilege separated driver interface
  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. #include <sys/un.h>
  10. #include "common.h"
  11. #include "driver.h"
  12. #include "eloop.h"
  13. #include "common/privsep_commands.h"
  14. struct wpa_driver_privsep_data {
  15. void *ctx;
  16. u8 own_addr[ETH_ALEN];
  17. int priv_socket;
  18. char *own_socket_path;
  19. int cmd_socket;
  20. char *own_cmd_path;
  21. struct sockaddr_un priv_addr;
  22. char ifname[16];
  23. };
  24. static int wpa_priv_reg_cmd(struct wpa_driver_privsep_data *drv, int cmd)
  25. {
  26. int res;
  27. res = sendto(drv->priv_socket, &cmd, sizeof(cmd), 0,
  28. (struct sockaddr *) &drv->priv_addr,
  29. sizeof(drv->priv_addr));
  30. if (res < 0)
  31. wpa_printf(MSG_ERROR, "sendto: %s", strerror(errno));
  32. return res < 0 ? -1 : 0;
  33. }
  34. static int wpa_priv_cmd(struct wpa_driver_privsep_data *drv, int cmd,
  35. const void *data, size_t data_len,
  36. void *reply, size_t *reply_len)
  37. {
  38. struct msghdr msg;
  39. struct iovec io[2];
  40. io[0].iov_base = &cmd;
  41. io[0].iov_len = sizeof(cmd);
  42. io[1].iov_base = (u8 *) data;
  43. io[1].iov_len = data_len;
  44. os_memset(&msg, 0, sizeof(msg));
  45. msg.msg_iov = io;
  46. msg.msg_iovlen = data ? 2 : 1;
  47. msg.msg_name = &drv->priv_addr;
  48. msg.msg_namelen = sizeof(drv->priv_addr);
  49. if (sendmsg(drv->cmd_socket, &msg, 0) < 0) {
  50. wpa_printf(MSG_ERROR, "sendmsg(cmd_socket): %s",
  51. strerror(errno));
  52. return -1;
  53. }
  54. if (reply) {
  55. fd_set rfds;
  56. struct timeval tv;
  57. int res;
  58. FD_ZERO(&rfds);
  59. FD_SET(drv->cmd_socket, &rfds);
  60. tv.tv_sec = 5;
  61. tv.tv_usec = 0;
  62. res = select(drv->cmd_socket + 1, &rfds, NULL, NULL, &tv);
  63. if (res < 0 && errno != EINTR) {
  64. wpa_printf(MSG_ERROR, "select: %s", strerror(errno));
  65. return -1;
  66. }
  67. if (FD_ISSET(drv->cmd_socket, &rfds)) {
  68. res = recv(drv->cmd_socket, reply, *reply_len, 0);
  69. if (res < 0) {
  70. wpa_printf(MSG_ERROR, "recv: %s",
  71. strerror(errno));
  72. return -1;
  73. }
  74. *reply_len = res;
  75. } else {
  76. wpa_printf(MSG_DEBUG, "PRIVSEP: Timeout while waiting "
  77. "for reply (cmd=%d)", cmd);
  78. return -1;
  79. }
  80. }
  81. return 0;
  82. }
  83. static int wpa_driver_privsep_scan(void *priv,
  84. struct wpa_driver_scan_params *params)
  85. {
  86. struct wpa_driver_privsep_data *drv = priv;
  87. const u8 *ssid = params->ssids[0].ssid;
  88. size_t ssid_len = params->ssids[0].ssid_len;
  89. wpa_printf(MSG_DEBUG, "%s: priv=%p", __func__, priv);
  90. return wpa_priv_cmd(drv, PRIVSEP_CMD_SCAN, ssid, ssid_len,
  91. NULL, NULL);
  92. }
  93. static struct wpa_scan_results *
  94. wpa_driver_privsep_get_scan_results2(void *priv)
  95. {
  96. struct wpa_driver_privsep_data *drv = priv;
  97. int res, num;
  98. u8 *buf, *pos, *end;
  99. size_t reply_len = 60000;
  100. struct wpa_scan_results *results;
  101. struct wpa_scan_res *r;
  102. buf = os_malloc(reply_len);
  103. if (buf == NULL)
  104. return NULL;
  105. res = wpa_priv_cmd(drv, PRIVSEP_CMD_GET_SCAN_RESULTS,
  106. NULL, 0, buf, &reply_len);
  107. if (res < 0) {
  108. os_free(buf);
  109. return NULL;
  110. }
  111. wpa_printf(MSG_DEBUG, "privsep: Received %lu bytes of scan results",
  112. (unsigned long) reply_len);
  113. if (reply_len < sizeof(int)) {
  114. wpa_printf(MSG_DEBUG, "privsep: Invalid scan result len %lu",
  115. (unsigned long) reply_len);
  116. os_free(buf);
  117. return NULL;
  118. }
  119. pos = buf;
  120. end = buf + reply_len;
  121. os_memcpy(&num, pos, sizeof(int));
  122. if (num < 0 || num > 1000) {
  123. os_free(buf);
  124. return NULL;
  125. }
  126. pos += sizeof(int);
  127. results = os_zalloc(sizeof(*results));
  128. if (results == NULL) {
  129. os_free(buf);
  130. return NULL;
  131. }
  132. results->res = os_calloc(num, sizeof(struct wpa_scan_res *));
  133. if (results->res == NULL) {
  134. os_free(results);
  135. os_free(buf);
  136. return NULL;
  137. }
  138. while (results->num < (size_t) num && pos + sizeof(int) < end) {
  139. int len;
  140. os_memcpy(&len, pos, sizeof(int));
  141. pos += sizeof(int);
  142. if (len < 0 || len > 10000 || pos + len > end)
  143. break;
  144. r = os_malloc(len);
  145. if (r == NULL)
  146. break;
  147. os_memcpy(r, pos, len);
  148. pos += len;
  149. if (sizeof(*r) + r->ie_len > (size_t) len) {
  150. os_free(r);
  151. break;
  152. }
  153. results->res[results->num++] = r;
  154. }
  155. os_free(buf);
  156. return results;
  157. }
  158. static int wpa_driver_privsep_set_key(const char *ifname, void *priv,
  159. enum wpa_alg alg, const u8 *addr,
  160. int key_idx, int set_tx,
  161. const u8 *seq, size_t seq_len,
  162. const u8 *key, size_t key_len)
  163. {
  164. struct wpa_driver_privsep_data *drv = priv;
  165. struct privsep_cmd_set_key cmd;
  166. wpa_printf(MSG_DEBUG, "%s: priv=%p alg=%d key_idx=%d set_tx=%d",
  167. __func__, priv, alg, key_idx, set_tx);
  168. os_memset(&cmd, 0, sizeof(cmd));
  169. cmd.alg = alg;
  170. if (addr)
  171. os_memcpy(cmd.addr, addr, ETH_ALEN);
  172. else
  173. os_memset(cmd.addr, 0xff, ETH_ALEN);
  174. cmd.key_idx = key_idx;
  175. cmd.set_tx = set_tx;
  176. if (seq && seq_len > 0 && seq_len < sizeof(cmd.seq)) {
  177. os_memcpy(cmd.seq, seq, seq_len);
  178. cmd.seq_len = seq_len;
  179. }
  180. if (key && key_len > 0 && key_len < sizeof(cmd.key)) {
  181. os_memcpy(cmd.key, key, key_len);
  182. cmd.key_len = key_len;
  183. }
  184. return wpa_priv_cmd(drv, PRIVSEP_CMD_SET_KEY, &cmd, sizeof(cmd),
  185. NULL, NULL);
  186. }
  187. static int wpa_driver_privsep_associate(
  188. void *priv, struct wpa_driver_associate_params *params)
  189. {
  190. struct wpa_driver_privsep_data *drv = priv;
  191. struct privsep_cmd_associate *data;
  192. int res;
  193. size_t buflen;
  194. wpa_printf(MSG_DEBUG, "%s: priv=%p freq=%d pairwise_suite=%d "
  195. "group_suite=%d key_mgmt_suite=%d auth_alg=%d mode=%d",
  196. __func__, priv, params->freq.freq, params->pairwise_suite,
  197. params->group_suite, params->key_mgmt_suite,
  198. params->auth_alg, params->mode);
  199. buflen = sizeof(*data) + params->wpa_ie_len;
  200. data = os_zalloc(buflen);
  201. if (data == NULL)
  202. return -1;
  203. if (params->bssid)
  204. os_memcpy(data->bssid, params->bssid, ETH_ALEN);
  205. os_memcpy(data->ssid, params->ssid, params->ssid_len);
  206. data->ssid_len = params->ssid_len;
  207. data->hwmode = params->freq.mode;
  208. data->freq = params->freq.freq;
  209. data->channel = params->freq.channel;
  210. data->pairwise_suite = params->pairwise_suite;
  211. data->group_suite = params->group_suite;
  212. data->key_mgmt_suite = params->key_mgmt_suite;
  213. data->auth_alg = params->auth_alg;
  214. data->mode = params->mode;
  215. data->wpa_ie_len = params->wpa_ie_len;
  216. if (params->wpa_ie)
  217. os_memcpy(data + 1, params->wpa_ie, params->wpa_ie_len);
  218. /* TODO: add support for other assoc parameters */
  219. res = wpa_priv_cmd(drv, PRIVSEP_CMD_ASSOCIATE, data, buflen,
  220. NULL, NULL);
  221. os_free(data);
  222. return res;
  223. }
  224. static int wpa_driver_privsep_get_bssid(void *priv, u8 *bssid)
  225. {
  226. struct wpa_driver_privsep_data *drv = priv;
  227. int res;
  228. size_t len = ETH_ALEN;
  229. res = wpa_priv_cmd(drv, PRIVSEP_CMD_GET_BSSID, NULL, 0, bssid, &len);
  230. if (res < 0 || len != ETH_ALEN)
  231. return -1;
  232. return 0;
  233. }
  234. static int wpa_driver_privsep_get_ssid(void *priv, u8 *ssid)
  235. {
  236. struct wpa_driver_privsep_data *drv = priv;
  237. int res, ssid_len;
  238. u8 reply[sizeof(int) + SSID_MAX_LEN];
  239. size_t len = sizeof(reply);
  240. res = wpa_priv_cmd(drv, PRIVSEP_CMD_GET_SSID, NULL, 0, reply, &len);
  241. if (res < 0 || len < sizeof(int))
  242. return -1;
  243. os_memcpy(&ssid_len, reply, sizeof(int));
  244. if (ssid_len < 0 || ssid_len > SSID_MAX_LEN ||
  245. sizeof(int) + ssid_len > len) {
  246. wpa_printf(MSG_DEBUG, "privsep: Invalid get SSID reply");
  247. return -1;
  248. }
  249. os_memcpy(ssid, &reply[sizeof(int)], ssid_len);
  250. return ssid_len;
  251. }
  252. static int wpa_driver_privsep_deauthenticate(void *priv, const u8 *addr,
  253. int reason_code)
  254. {
  255. //struct wpa_driver_privsep_data *drv = priv;
  256. wpa_printf(MSG_DEBUG, "%s addr=" MACSTR " reason_code=%d",
  257. __func__, MAC2STR(addr), reason_code);
  258. wpa_printf(MSG_DEBUG, "%s - TODO", __func__);
  259. return 0;
  260. }
  261. static void wpa_driver_privsep_event_assoc(void *ctx,
  262. enum wpa_event_type event,
  263. u8 *buf, size_t len)
  264. {
  265. union wpa_event_data data;
  266. int inc_data = 0;
  267. u8 *pos, *end;
  268. int ie_len;
  269. os_memset(&data, 0, sizeof(data));
  270. pos = buf;
  271. end = buf + len;
  272. if (end - pos < (int) sizeof(int))
  273. return;
  274. os_memcpy(&ie_len, pos, sizeof(int));
  275. pos += sizeof(int);
  276. if (ie_len < 0 || ie_len > end - pos)
  277. return;
  278. if (ie_len) {
  279. data.assoc_info.req_ies = pos;
  280. data.assoc_info.req_ies_len = ie_len;
  281. pos += ie_len;
  282. inc_data = 1;
  283. }
  284. wpa_supplicant_event(ctx, event, inc_data ? &data : NULL);
  285. }
  286. static void wpa_driver_privsep_event_interface_status(void *ctx, u8 *buf,
  287. size_t len)
  288. {
  289. union wpa_event_data data;
  290. int ievent;
  291. if (len < sizeof(int) ||
  292. len - sizeof(int) > sizeof(data.interface_status.ifname))
  293. return;
  294. os_memcpy(&ievent, buf, sizeof(int));
  295. os_memset(&data, 0, sizeof(data));
  296. data.interface_status.ievent = ievent;
  297. os_memcpy(data.interface_status.ifname, buf + sizeof(int),
  298. len - sizeof(int));
  299. wpa_supplicant_event(ctx, EVENT_INTERFACE_STATUS, &data);
  300. }
  301. static void wpa_driver_privsep_event_michael_mic_failure(
  302. void *ctx, u8 *buf, size_t len)
  303. {
  304. union wpa_event_data data;
  305. if (len != sizeof(int))
  306. return;
  307. os_memset(&data, 0, sizeof(data));
  308. os_memcpy(&data.michael_mic_failure.unicast, buf, sizeof(int));
  309. wpa_supplicant_event(ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
  310. }
  311. static void wpa_driver_privsep_event_pmkid_candidate(void *ctx, u8 *buf,
  312. size_t len)
  313. {
  314. union wpa_event_data data;
  315. if (len != sizeof(struct pmkid_candidate))
  316. return;
  317. os_memset(&data, 0, sizeof(data));
  318. os_memcpy(&data.pmkid_candidate, buf, len);
  319. wpa_supplicant_event(ctx, EVENT_PMKID_CANDIDATE, &data);
  320. }
  321. static void wpa_driver_privsep_event_stkstart(void *ctx, u8 *buf, size_t len)
  322. {
  323. union wpa_event_data data;
  324. if (len != ETH_ALEN)
  325. return;
  326. os_memset(&data, 0, sizeof(data));
  327. os_memcpy(data.stkstart.peer, buf, ETH_ALEN);
  328. wpa_supplicant_event(ctx, EVENT_STKSTART, &data);
  329. }
  330. static void wpa_driver_privsep_event_ft_response(void *ctx, u8 *buf,
  331. size_t len)
  332. {
  333. union wpa_event_data data;
  334. if (len < sizeof(int) + ETH_ALEN)
  335. return;
  336. os_memset(&data, 0, sizeof(data));
  337. os_memcpy(&data.ft_ies.ft_action, buf, sizeof(int));
  338. os_memcpy(data.ft_ies.target_ap, buf + sizeof(int), ETH_ALEN);
  339. data.ft_ies.ies = buf + sizeof(int) + ETH_ALEN;
  340. data.ft_ies.ies_len = len - sizeof(int) - ETH_ALEN;
  341. wpa_supplicant_event(ctx, EVENT_FT_RESPONSE, &data);
  342. }
  343. static void wpa_driver_privsep_event_rx_eapol(void *ctx, u8 *buf, size_t len)
  344. {
  345. if (len < ETH_ALEN)
  346. return;
  347. drv_event_eapol_rx(ctx, buf, buf + ETH_ALEN, len - ETH_ALEN);
  348. }
  349. static void wpa_driver_privsep_receive(int sock, void *eloop_ctx,
  350. void *sock_ctx)
  351. {
  352. struct wpa_driver_privsep_data *drv = eloop_ctx;
  353. u8 *buf, *event_buf;
  354. size_t event_len;
  355. int res, event;
  356. enum privsep_event e;
  357. struct sockaddr_un from;
  358. socklen_t fromlen = sizeof(from);
  359. const size_t buflen = 2000;
  360. buf = os_malloc(buflen);
  361. if (buf == NULL)
  362. return;
  363. res = recvfrom(sock, buf, buflen, 0,
  364. (struct sockaddr *) &from, &fromlen);
  365. if (res < 0) {
  366. wpa_printf(MSG_ERROR, "recvfrom(priv_socket): %s",
  367. strerror(errno));
  368. os_free(buf);
  369. return;
  370. }
  371. wpa_printf(MSG_DEBUG, "privsep_driver: received %u bytes", res);
  372. if (res < (int) sizeof(int)) {
  373. wpa_printf(MSG_DEBUG, "Too short event message (len=%d)", res);
  374. return;
  375. }
  376. os_memcpy(&event, buf, sizeof(int));
  377. event_buf = &buf[sizeof(int)];
  378. event_len = res - sizeof(int);
  379. wpa_printf(MSG_DEBUG, "privsep: Event %d received (len=%lu)",
  380. event, (unsigned long) event_len);
  381. e = event;
  382. switch (e) {
  383. case PRIVSEP_EVENT_SCAN_RESULTS:
  384. wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, NULL);
  385. break;
  386. case PRIVSEP_EVENT_SCAN_STARTED:
  387. wpa_supplicant_event(drv->ctx, EVENT_SCAN_STARTED, NULL);
  388. break;
  389. case PRIVSEP_EVENT_ASSOC:
  390. wpa_driver_privsep_event_assoc(drv->ctx, EVENT_ASSOC,
  391. event_buf, event_len);
  392. break;
  393. case PRIVSEP_EVENT_DISASSOC:
  394. wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
  395. break;
  396. case PRIVSEP_EVENT_ASSOCINFO:
  397. wpa_driver_privsep_event_assoc(drv->ctx, EVENT_ASSOCINFO,
  398. event_buf, event_len);
  399. break;
  400. case PRIVSEP_EVENT_MICHAEL_MIC_FAILURE:
  401. wpa_driver_privsep_event_michael_mic_failure(
  402. drv->ctx, event_buf, event_len);
  403. break;
  404. case PRIVSEP_EVENT_INTERFACE_STATUS:
  405. wpa_driver_privsep_event_interface_status(drv->ctx, event_buf,
  406. event_len);
  407. break;
  408. case PRIVSEP_EVENT_PMKID_CANDIDATE:
  409. wpa_driver_privsep_event_pmkid_candidate(drv->ctx, event_buf,
  410. event_len);
  411. break;
  412. case PRIVSEP_EVENT_STKSTART:
  413. wpa_driver_privsep_event_stkstart(drv->ctx, event_buf,
  414. event_len);
  415. break;
  416. case PRIVSEP_EVENT_FT_RESPONSE:
  417. wpa_driver_privsep_event_ft_response(drv->ctx, event_buf,
  418. event_len);
  419. break;
  420. case PRIVSEP_EVENT_RX_EAPOL:
  421. wpa_driver_privsep_event_rx_eapol(drv->ctx, event_buf,
  422. event_len);
  423. break;
  424. }
  425. os_free(buf);
  426. }
  427. static void * wpa_driver_privsep_init(void *ctx, const char *ifname)
  428. {
  429. struct wpa_driver_privsep_data *drv;
  430. drv = os_zalloc(sizeof(*drv));
  431. if (drv == NULL)
  432. return NULL;
  433. drv->ctx = ctx;
  434. drv->priv_socket = -1;
  435. drv->cmd_socket = -1;
  436. os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname));
  437. return drv;
  438. }
  439. static void wpa_driver_privsep_deinit(void *priv)
  440. {
  441. struct wpa_driver_privsep_data *drv = priv;
  442. if (drv->priv_socket >= 0) {
  443. wpa_priv_reg_cmd(drv, PRIVSEP_CMD_UNREGISTER);
  444. eloop_unregister_read_sock(drv->priv_socket);
  445. close(drv->priv_socket);
  446. }
  447. if (drv->own_socket_path) {
  448. unlink(drv->own_socket_path);
  449. os_free(drv->own_socket_path);
  450. }
  451. if (drv->cmd_socket >= 0) {
  452. eloop_unregister_read_sock(drv->cmd_socket);
  453. close(drv->cmd_socket);
  454. }
  455. if (drv->own_cmd_path) {
  456. unlink(drv->own_cmd_path);
  457. os_free(drv->own_cmd_path);
  458. }
  459. os_free(drv);
  460. }
  461. static int wpa_driver_privsep_set_param(void *priv, const char *param)
  462. {
  463. struct wpa_driver_privsep_data *drv = priv;
  464. const char *pos;
  465. char *own_dir, *priv_dir;
  466. static unsigned int counter = 0;
  467. size_t len;
  468. struct sockaddr_un addr;
  469. wpa_printf(MSG_DEBUG, "%s: param='%s'", __func__, param);
  470. if (param == NULL)
  471. pos = NULL;
  472. else
  473. pos = os_strstr(param, "own_dir=");
  474. if (pos) {
  475. char *end;
  476. own_dir = os_strdup(pos + 8);
  477. if (own_dir == NULL)
  478. return -1;
  479. end = os_strchr(own_dir, ' ');
  480. if (end)
  481. *end = '\0';
  482. } else {
  483. own_dir = os_strdup("/tmp");
  484. if (own_dir == NULL)
  485. return -1;
  486. }
  487. if (param == NULL)
  488. pos = NULL;
  489. else
  490. pos = os_strstr(param, "priv_dir=");
  491. if (pos) {
  492. char *end;
  493. priv_dir = os_strdup(pos + 9);
  494. if (priv_dir == NULL) {
  495. os_free(own_dir);
  496. return -1;
  497. }
  498. end = os_strchr(priv_dir, ' ');
  499. if (end)
  500. *end = '\0';
  501. } else {
  502. priv_dir = os_strdup("/var/run/wpa_priv");
  503. if (priv_dir == NULL) {
  504. os_free(own_dir);
  505. return -1;
  506. }
  507. }
  508. len = os_strlen(own_dir) + 50;
  509. drv->own_socket_path = os_malloc(len);
  510. if (drv->own_socket_path == NULL) {
  511. os_free(priv_dir);
  512. os_free(own_dir);
  513. return -1;
  514. }
  515. os_snprintf(drv->own_socket_path, len, "%s/wpa_privsep-%d-%d",
  516. own_dir, getpid(), counter++);
  517. len = os_strlen(own_dir) + 50;
  518. drv->own_cmd_path = os_malloc(len);
  519. if (drv->own_cmd_path == NULL) {
  520. os_free(drv->own_socket_path);
  521. drv->own_socket_path = NULL;
  522. os_free(priv_dir);
  523. os_free(own_dir);
  524. return -1;
  525. }
  526. os_snprintf(drv->own_cmd_path, len, "%s/wpa_privsep-%d-%d",
  527. own_dir, getpid(), counter++);
  528. os_free(own_dir);
  529. drv->priv_addr.sun_family = AF_UNIX;
  530. os_snprintf(drv->priv_addr.sun_path, sizeof(drv->priv_addr.sun_path),
  531. "%s/%s", priv_dir, drv->ifname);
  532. os_free(priv_dir);
  533. drv->priv_socket = socket(PF_UNIX, SOCK_DGRAM, 0);
  534. if (drv->priv_socket < 0) {
  535. wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
  536. os_free(drv->own_socket_path);
  537. drv->own_socket_path = NULL;
  538. return -1;
  539. }
  540. os_memset(&addr, 0, sizeof(addr));
  541. addr.sun_family = AF_UNIX;
  542. os_strlcpy(addr.sun_path, drv->own_socket_path, sizeof(addr.sun_path));
  543. if (bind(drv->priv_socket, (struct sockaddr *) &addr, sizeof(addr)) <
  544. 0) {
  545. wpa_printf(MSG_ERROR,
  546. "privsep-set-params priv-sock: bind(PF_UNIX): %s",
  547. strerror(errno));
  548. close(drv->priv_socket);
  549. drv->priv_socket = -1;
  550. unlink(drv->own_socket_path);
  551. os_free(drv->own_socket_path);
  552. drv->own_socket_path = NULL;
  553. return -1;
  554. }
  555. eloop_register_read_sock(drv->priv_socket, wpa_driver_privsep_receive,
  556. drv, NULL);
  557. drv->cmd_socket = socket(PF_UNIX, SOCK_DGRAM, 0);
  558. if (drv->cmd_socket < 0) {
  559. wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
  560. os_free(drv->own_cmd_path);
  561. drv->own_cmd_path = NULL;
  562. return -1;
  563. }
  564. os_memset(&addr, 0, sizeof(addr));
  565. addr.sun_family = AF_UNIX;
  566. os_strlcpy(addr.sun_path, drv->own_cmd_path, sizeof(addr.sun_path));
  567. if (bind(drv->cmd_socket, (struct sockaddr *) &addr, sizeof(addr)) < 0)
  568. {
  569. wpa_printf(MSG_ERROR,
  570. "privsep-set-params cmd-sock: bind(PF_UNIX): %s",
  571. strerror(errno));
  572. close(drv->cmd_socket);
  573. drv->cmd_socket = -1;
  574. unlink(drv->own_cmd_path);
  575. os_free(drv->own_cmd_path);
  576. drv->own_cmd_path = NULL;
  577. return -1;
  578. }
  579. if (wpa_priv_reg_cmd(drv, PRIVSEP_CMD_REGISTER) < 0) {
  580. wpa_printf(MSG_ERROR, "Failed to register with wpa_priv");
  581. return -1;
  582. }
  583. return 0;
  584. }
  585. static int wpa_driver_privsep_get_capa(void *priv,
  586. struct wpa_driver_capa *capa)
  587. {
  588. struct wpa_driver_privsep_data *drv = priv;
  589. int res;
  590. size_t len = sizeof(*capa);
  591. res = wpa_priv_cmd(drv, PRIVSEP_CMD_GET_CAPA, NULL, 0, capa, &len);
  592. if (res < 0 || len != sizeof(*capa))
  593. return -1;
  594. /* For now, no support for passing extended_capa pointers */
  595. capa->extended_capa = NULL;
  596. capa->extended_capa_mask = NULL;
  597. capa->extended_capa_len = 0;
  598. return 0;
  599. }
  600. static const u8 * wpa_driver_privsep_get_mac_addr(void *priv)
  601. {
  602. struct wpa_driver_privsep_data *drv = priv;
  603. wpa_printf(MSG_DEBUG, "%s", __func__);
  604. return drv->own_addr;
  605. }
  606. static int wpa_driver_privsep_set_country(void *priv, const char *alpha2)
  607. {
  608. struct wpa_driver_privsep_data *drv = priv;
  609. wpa_printf(MSG_DEBUG, "%s country='%s'", __func__, alpha2);
  610. return wpa_priv_cmd(drv, PRIVSEP_CMD_SET_COUNTRY, alpha2,
  611. os_strlen(alpha2), NULL, NULL);
  612. }
  613. struct wpa_driver_ops wpa_driver_privsep_ops = {
  614. "privsep",
  615. "wpa_supplicant privilege separated driver",
  616. .get_bssid = wpa_driver_privsep_get_bssid,
  617. .get_ssid = wpa_driver_privsep_get_ssid,
  618. .set_key = wpa_driver_privsep_set_key,
  619. .init = wpa_driver_privsep_init,
  620. .deinit = wpa_driver_privsep_deinit,
  621. .set_param = wpa_driver_privsep_set_param,
  622. .scan2 = wpa_driver_privsep_scan,
  623. .deauthenticate = wpa_driver_privsep_deauthenticate,
  624. .associate = wpa_driver_privsep_associate,
  625. .get_capa = wpa_driver_privsep_get_capa,
  626. .get_mac_addr = wpa_driver_privsep_get_mac_addr,
  627. .get_scan_results2 = wpa_driver_privsep_get_scan_results2,
  628. .set_country = wpa_driver_privsep_set_country,
  629. };
  630. const struct wpa_driver_ops *const wpa_drivers[] =
  631. {
  632. &wpa_driver_privsep_ops,
  633. NULL
  634. };