wpa_priv.c 30 KB

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