wpa_priv.c 30 KB

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