wpa_priv.c 30 KB

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