wpa_priv.c 29 KB

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