driver_ralink.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509
  1. /*
  2. * WPA Supplicant - driver interaction with Ralink Wireless Client
  3. * Copyright (c) 2003-2006, Jouni Malinen <j@w1.fi>
  4. * Copyright (c) 2007, Snowpin Lee <snowpin_lee@ralinktech.com.tw>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Alternatively, this software may be distributed under the terms of BSD
  11. * license.
  12. *
  13. * See README and COPYING for more details.
  14. *
  15. */
  16. #include "includes.h"
  17. #include <sys/ioctl.h>
  18. #include "wireless_copy.h"
  19. #include "common.h"
  20. #include "driver.h"
  21. #include "l2_packet/l2_packet.h"
  22. #include "eloop.h"
  23. #include "ieee802_11_defs.h"
  24. #include "priv_netlink.h"
  25. #include "driver_ralink.h"
  26. static void wpa_driver_ralink_scan_timeout(void *eloop_ctx, void *timeout_ctx);
  27. #define MAX_SSID_LEN 32
  28. struct wpa_driver_ralink_data {
  29. void *ctx;
  30. int ioctl_sock;
  31. int event_sock;
  32. char ifname[IFNAMSIZ + 1];
  33. u8 *assoc_req_ies;
  34. size_t assoc_req_ies_len;
  35. u8 *assoc_resp_ies;
  36. size_t assoc_resp_ies_len;
  37. int no_of_pmkid;
  38. struct ndis_pmkid_entry *pmkid;
  39. int we_version_compiled;
  40. int ap_scan;
  41. int scanning_done;
  42. u8 g_driver_down;
  43. };
  44. static int ralink_set_oid(struct wpa_driver_ralink_data *drv,
  45. unsigned short oid, char *data, int len)
  46. {
  47. char *buf;
  48. struct iwreq iwr;
  49. buf = os_zalloc(len);
  50. if (buf == NULL)
  51. return -1;
  52. os_memset(&iwr, 0, sizeof(iwr));
  53. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  54. iwr.u.data.flags = oid;
  55. iwr.u.data.flags |= OID_GET_SET_TOGGLE;
  56. if (data)
  57. os_memcpy(buf, data, len);
  58. iwr.u.data.pointer = (caddr_t) buf;
  59. iwr.u.data.length = len;
  60. if (ioctl(drv->ioctl_sock, RT_PRIV_IOCTL, &iwr) < 0) {
  61. wpa_printf(MSG_DEBUG, "%s: oid=0x%x len (%d) failed",
  62. __func__, oid, len);
  63. os_free(buf);
  64. return -1;
  65. }
  66. os_free(buf);
  67. return 0;
  68. }
  69. static int
  70. ralink_get_new_driver_flag(struct wpa_driver_ralink_data *drv)
  71. {
  72. struct iwreq iwr;
  73. UCHAR enabled = 0;
  74. os_memset(&iwr, 0, sizeof(iwr));
  75. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  76. iwr.u.data.pointer = (UCHAR*) &enabled;
  77. iwr.u.data.flags = RT_OID_NEW_DRIVER;
  78. if (ioctl(drv->ioctl_sock, RT_PRIV_IOCTL, &iwr) < 0) {
  79. wpa_printf(MSG_DEBUG, "%s: failed", __func__);
  80. return 0;
  81. }
  82. return (enabled == 1) ? 1 : 0;
  83. }
  84. static int wpa_driver_ralink_get_bssid(void *priv, u8 *bssid)
  85. {
  86. struct wpa_driver_ralink_data *drv = priv;
  87. struct iwreq iwr;
  88. int ret = 0;
  89. if (drv->g_driver_down == 1)
  90. return -1;
  91. wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
  92. os_memset(&iwr, 0, sizeof(iwr));
  93. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  94. if (ioctl(drv->ioctl_sock, SIOCGIWAP, &iwr) < 0) {
  95. perror("ioctl[SIOCGIWAP]");
  96. ret = -1;
  97. }
  98. os_memcpy(bssid, iwr.u.ap_addr.sa_data, ETH_ALEN);
  99. return ret;
  100. }
  101. static int wpa_driver_ralink_get_ssid(void *priv, u8 *ssid)
  102. {
  103. struct wpa_driver_ralink_data *drv = priv;
  104. #if 0
  105. struct wpa_supplicant *wpa_s = drv->ctx;
  106. struct wpa_ssid *entry;
  107. #endif
  108. int ssid_len;
  109. u8 bssid[ETH_ALEN];
  110. u8 ssid_str[MAX_SSID_LEN];
  111. struct iwreq iwr;
  112. #if 0
  113. int result = 0;
  114. #endif
  115. int ret = 0;
  116. #if 0
  117. BOOLEAN ieee8021x_mode = FALSE;
  118. BOOLEAN ieee8021x_required_key = FALSE;
  119. #endif
  120. if (drv->g_driver_down == 1)
  121. return -1;
  122. wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
  123. os_memset(&iwr, 0, sizeof(iwr));
  124. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  125. iwr.u.essid.pointer = (caddr_t) ssid;
  126. iwr.u.essid.length = 32;
  127. if (ioctl(drv->ioctl_sock, SIOCGIWESSID, &iwr) < 0) {
  128. perror("ioctl[SIOCGIWESSID]");
  129. ret = -1;
  130. } else
  131. ret = iwr.u.essid.length;
  132. if (ret <= 0)
  133. return ret;
  134. ssid_len = ret;
  135. os_memset(ssid_str, 0, MAX_SSID_LEN);
  136. os_memcpy(ssid_str, ssid, ssid_len);
  137. if (drv->ap_scan == 0) {
  138. /* Read BSSID form driver */
  139. if (wpa_driver_ralink_get_bssid(priv, bssid) < 0) {
  140. wpa_printf(MSG_WARNING, "Could not read BSSID from "
  141. "driver.");
  142. return ret;
  143. }
  144. #if 0
  145. entry = wpa_s->conf->ssid;
  146. while (entry) {
  147. if (!entry->disabled && ssid_len == entry->ssid_len &&
  148. os_memcmp(ssid_str, entry->ssid, ssid_len) == 0 &&
  149. (!entry->bssid_set ||
  150. os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0)) {
  151. /* match the config of driver */
  152. result = 1;
  153. break;
  154. }
  155. entry = entry->next;
  156. }
  157. if (result) {
  158. wpa_printf(MSG_DEBUG, "Ready to set 802.1x mode and "
  159. "ieee_required_keys parameters to driver");
  160. /* set 802.1x mode and ieee_required_keys parameter */
  161. if (entry->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  162. if ((entry->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST | EAPOL_FLAG_REQUIRE_KEY_BROADCAST)))
  163. ieee8021x_required_key = TRUE;
  164. ieee8021x_mode = TRUE;
  165. }
  166. if (ralink_set_oid(drv, OID_802_11_SET_IEEE8021X, (char *) &ieee8021x_mode, sizeof(BOOLEAN)) < 0)
  167. {
  168. wpa_printf(MSG_DEBUG, "RALINK: Failed to set OID_802_11_SET_IEEE8021X(%d)", (int) ieee8021x_mode);
  169. }
  170. else
  171. {
  172. wpa_printf(MSG_DEBUG, "ieee8021x_mode is %s", ieee8021x_mode ? "TRUE" : "FALSE");
  173. }
  174. if (ralink_set_oid(drv, OID_802_11_SET_IEEE8021X_REQUIRE_KEY, (char *) &ieee8021x_required_key, sizeof(BOOLEAN)) < 0)
  175. {
  176. wpa_printf(MSG_DEBUG, "ERROR: Failed to set OID_802_11_SET_IEEE8021X_REQUIRE_KEY(%d)", (int) ieee8021x_required_key);
  177. }
  178. else
  179. {
  180. wpa_printf(MSG_DEBUG, "ieee8021x_required_key is %s and eapol_flag(%d)", ieee8021x_required_key ? "TRUE" : "FALSE",
  181. entry->eapol_flags);
  182. }
  183. }
  184. #endif
  185. }
  186. return ret;
  187. }
  188. static int wpa_driver_ralink_set_ssid(struct wpa_driver_ralink_data *drv,
  189. const u8 *ssid, size_t ssid_len)
  190. {
  191. NDIS_802_11_SSID *buf;
  192. int ret = 0;
  193. struct iwreq iwr;
  194. wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
  195. buf = os_zalloc(sizeof(NDIS_802_11_SSID));
  196. if (buf == NULL)
  197. return -1;
  198. os_memset(buf, 0, sizeof(buf));
  199. buf->SsidLength = ssid_len;
  200. os_memcpy(buf->Ssid, ssid, ssid_len);
  201. os_memset(&iwr, 0, sizeof(iwr));
  202. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  203. iwr.u.data.flags = OID_802_11_SSID;
  204. iwr.u.data.flags |= OID_GET_SET_TOGGLE;
  205. iwr.u.data.pointer = (caddr_t) buf;
  206. iwr.u.data.length = sizeof(NDIS_802_11_SSID);
  207. if (ioctl(drv->ioctl_sock, RT_PRIV_IOCTL, &iwr) < 0) {
  208. perror("ioctl[RT_PRIV_IOCTL] -- OID_802_11_SSID");
  209. ret = -1;
  210. }
  211. os_free(buf);
  212. return ret;
  213. }
  214. static void wpa_driver_ralink_event_pmkid(struct wpa_driver_ralink_data *drv,
  215. const u8 *data, size_t data_len)
  216. {
  217. NDIS_802_11_PMKID_CANDIDATE_LIST *pmkid;
  218. size_t i;
  219. union wpa_event_data event;
  220. wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
  221. if (data_len < 8) {
  222. wpa_printf(MSG_DEBUG, "RALINK: Too short PMKID Candidate List "
  223. "Event (len=%lu)", (unsigned long) data_len);
  224. return;
  225. }
  226. pmkid = (NDIS_802_11_PMKID_CANDIDATE_LIST *) data;
  227. wpa_printf(MSG_DEBUG, "RALINK: PMKID Candidate List Event - Version %d"
  228. " NumCandidates %d",
  229. (int) pmkid->Version, (int) pmkid->NumCandidates);
  230. if (pmkid->Version != 1) {
  231. wpa_printf(MSG_DEBUG, "RALINK: Unsupported PMKID Candidate "
  232. "List Version %d", (int) pmkid->Version);
  233. return;
  234. }
  235. if (data_len < 8 + pmkid->NumCandidates * sizeof(PMKID_CANDIDATE)) {
  236. wpa_printf(MSG_DEBUG, "RALINK: PMKID Candidate List "
  237. "underflow");
  238. return;
  239. }
  240. os_memset(&event, 0, sizeof(event));
  241. for (i = 0; i < pmkid->NumCandidates; i++) {
  242. PMKID_CANDIDATE *p = &pmkid->CandidateList[i];
  243. wpa_printf(MSG_DEBUG, "RALINK: %lu: " MACSTR " Flags 0x%x",
  244. (unsigned long) i, MAC2STR(p->BSSID),
  245. (int) p->Flags);
  246. os_memcpy(event.pmkid_candidate.bssid, p->BSSID, ETH_ALEN);
  247. event.pmkid_candidate.index = i;
  248. event.pmkid_candidate.preauth =
  249. p->Flags & NDIS_802_11_PMKID_CANDIDATE_PREAUTH_ENABLED;
  250. wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE,
  251. &event);
  252. }
  253. }
  254. static int wpa_driver_ralink_set_pmkid(struct wpa_driver_ralink_data *drv)
  255. {
  256. int len, count, i, ret;
  257. struct ndis_pmkid_entry *entry;
  258. NDIS_802_11_PMKID *p;
  259. wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
  260. count = 0;
  261. entry = drv->pmkid;
  262. while (entry) {
  263. count++;
  264. if (count >= drv->no_of_pmkid)
  265. break;
  266. entry = entry->next;
  267. }
  268. len = 8 + count * sizeof(BSSID_INFO);
  269. p = os_zalloc(len);
  270. if (p == NULL)
  271. return -1;
  272. p->Length = len;
  273. p->BSSIDInfoCount = count;
  274. entry = drv->pmkid;
  275. for (i = 0; i < count; i++) {
  276. os_memcpy(&p->BSSIDInfo[i].BSSID, entry->bssid, ETH_ALEN);
  277. os_memcpy(&p->BSSIDInfo[i].PMKID, entry->pmkid, 16);
  278. entry = entry->next;
  279. }
  280. wpa_hexdump(MSG_MSGDUMP, "NDIS: OID_802_11_PMKID",
  281. (const u8 *) p, len);
  282. ret = ralink_set_oid(drv, OID_802_11_PMKID, (char *) p, len);
  283. os_free(p);
  284. return ret;
  285. }
  286. static int wpa_driver_ralink_add_pmkid(void *priv, const u8 *bssid,
  287. const u8 *pmkid)
  288. {
  289. struct wpa_driver_ralink_data *drv = priv;
  290. struct ndis_pmkid_entry *entry, *prev;
  291. if (drv->g_driver_down == 1)
  292. return -1;
  293. wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
  294. if (drv->no_of_pmkid == 0)
  295. return 0;
  296. prev = NULL;
  297. entry = drv->pmkid;
  298. while (entry) {
  299. if (os_memcmp(entry->bssid, bssid, ETH_ALEN) == 0)
  300. break;
  301. prev = entry;
  302. entry = entry->next;
  303. }
  304. if (entry) {
  305. /* Replace existing entry for this BSSID and move it into the
  306. * beginning of the list. */
  307. os_memcpy(entry->pmkid, pmkid, 16);
  308. if (prev) {
  309. prev->next = entry->next;
  310. entry->next = drv->pmkid;
  311. drv->pmkid = entry;
  312. }
  313. } else {
  314. entry = os_malloc(sizeof(*entry));
  315. if (entry) {
  316. os_memcpy(entry->bssid, bssid, ETH_ALEN);
  317. os_memcpy(entry->pmkid, pmkid, 16);
  318. entry->next = drv->pmkid;
  319. drv->pmkid = entry;
  320. }
  321. }
  322. return wpa_driver_ralink_set_pmkid(drv);
  323. }
  324. static int wpa_driver_ralink_remove_pmkid(void *priv, const u8 *bssid,
  325. const u8 *pmkid)
  326. {
  327. struct wpa_driver_ralink_data *drv = priv;
  328. struct ndis_pmkid_entry *entry, *prev;
  329. if (drv->g_driver_down == 1)
  330. return -1;
  331. wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
  332. if (drv->no_of_pmkid == 0)
  333. return 0;
  334. entry = drv->pmkid;
  335. prev = NULL;
  336. drv->pmkid = NULL;
  337. while (entry) {
  338. if (os_memcmp(entry->bssid, bssid, ETH_ALEN) == 0 &&
  339. os_memcmp(entry->pmkid, pmkid, 16) == 0) {
  340. if (prev)
  341. prev->next = entry->next;
  342. else
  343. drv->pmkid = entry->next;
  344. os_free(entry);
  345. break;
  346. }
  347. prev = entry;
  348. entry = entry->next;
  349. }
  350. return wpa_driver_ralink_set_pmkid(drv);
  351. }
  352. static int wpa_driver_ralink_flush_pmkid(void *priv)
  353. {
  354. struct wpa_driver_ralink_data *drv = priv;
  355. NDIS_802_11_PMKID p;
  356. struct ndis_pmkid_entry *pmkid, *prev;
  357. if (drv->g_driver_down == 1)
  358. return -1;
  359. wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
  360. if (drv->no_of_pmkid == 0)
  361. return 0;
  362. pmkid = drv->pmkid;
  363. drv->pmkid = NULL;
  364. while (pmkid) {
  365. prev = pmkid;
  366. pmkid = pmkid->next;
  367. os_free(prev);
  368. }
  369. os_memset(&p, 0, sizeof(p));
  370. p.Length = 8;
  371. p.BSSIDInfoCount = 0;
  372. wpa_hexdump(MSG_MSGDUMP, "NDIS: OID_802_11_PMKID (flush)",
  373. (const u8 *) &p, 8);
  374. return ralink_set_oid(drv, OID_802_11_PMKID, (char *) &p, 8);
  375. }
  376. static void
  377. wpa_driver_ralink_event_wireless_custom(struct wpa_driver_ralink_data *drv,
  378. void *ctx, char *custom)
  379. {
  380. union wpa_event_data data;
  381. wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
  382. wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
  383. os_memset(&data, 0, sizeof(data));
  384. /* Host AP driver */
  385. if (os_strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
  386. /* receive a MICFAILURE report */
  387. data.michael_mic_failure.unicast =
  388. os_strstr(custom, " unicast") != NULL;
  389. /* TODO: parse parameters(?) */
  390. wpa_supplicant_event(ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
  391. } else if (os_strncmp(custom, "ASSOCINFO_ReqIEs=", 17) == 0) {
  392. /* receive assoc. req. IEs */
  393. char *spos;
  394. int bytes;
  395. spos = custom + 17;
  396. /*get IE's length */
  397. /*
  398. * bytes = strlen(spos); ==> bug, bytes may less than original
  399. * size by using this way to get size. snowpin 20070312
  400. * if (!bytes)
  401. * return;
  402. */
  403. bytes = drv->assoc_req_ies_len;
  404. data.assoc_info.req_ies = os_malloc(bytes);
  405. if (data.assoc_info.req_ies == NULL)
  406. return;
  407. data.assoc_info.req_ies_len = bytes;
  408. os_memcpy(data.assoc_info.req_ies, spos, bytes);
  409. /* skip the '\0' byte */
  410. spos += bytes + 1;
  411. data.assoc_info.resp_ies = NULL;
  412. data.assoc_info.resp_ies_len = 0;
  413. if (os_strncmp(spos, " RespIEs=", 9) == 0) {
  414. /* receive assoc. resp. IEs */
  415. spos += 9;
  416. /* get IE's length */
  417. bytes = os_strlen(spos);
  418. if (!bytes)
  419. goto done;
  420. data.assoc_info.resp_ies = os_malloc(bytes);
  421. if (data.assoc_info.resp_ies == NULL)
  422. goto done;
  423. data.assoc_info.resp_ies_len = bytes;
  424. os_memcpy(data.assoc_info.resp_ies, spos, bytes);
  425. }
  426. wpa_supplicant_event(ctx, EVENT_ASSOCINFO, &data);
  427. /* free allocated memory */
  428. done:
  429. os_free(data.assoc_info.resp_ies);
  430. os_free(data.assoc_info.req_ies);
  431. }
  432. }
  433. static void
  434. wpa_driver_ralink_event_wireless(struct wpa_driver_ralink_data *drv,
  435. void *ctx, char *data, int len)
  436. {
  437. struct iw_event iwe_buf, *iwe = &iwe_buf;
  438. char *pos, *end, *custom, *buf, *assoc_info_buf, *info_pos;
  439. #if 0
  440. BOOLEAN ieee8021x_required_key = FALSE;
  441. #endif
  442. wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
  443. assoc_info_buf = info_pos = NULL;
  444. pos = data;
  445. end = data + len;
  446. while (pos + IW_EV_LCP_LEN <= end) {
  447. /* Event data may be unaligned, so make a local, aligned copy
  448. * before processing. */
  449. os_memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
  450. wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
  451. iwe->cmd, iwe->len);
  452. if (iwe->len <= IW_EV_LCP_LEN)
  453. return;
  454. custom = pos + IW_EV_POINT_LEN;
  455. if (drv->we_version_compiled > 18 && iwe->cmd == IWEVCUSTOM) {
  456. /* WE-19 removed the pointer from struct iw_point */
  457. char *dpos = (char *) &iwe_buf.u.data.length;
  458. int dlen = dpos - (char *) &iwe_buf;
  459. os_memcpy(dpos, pos + IW_EV_LCP_LEN,
  460. sizeof(struct iw_event) - dlen);
  461. } else {
  462. os_memcpy(&iwe_buf, pos, sizeof(struct iw_event));
  463. custom += IW_EV_POINT_OFF;
  464. }
  465. switch (iwe->cmd) {
  466. case IWEVCUSTOM:
  467. if (custom + iwe->u.data.length > end)
  468. return;
  469. buf = os_malloc(iwe->u.data.length + 1);
  470. if (buf == NULL)
  471. return;
  472. os_memcpy(buf, custom, iwe->u.data.length);
  473. buf[iwe->u.data.length] = '\0';
  474. if (drv->ap_scan == 1) {
  475. if ((iwe->u.data.flags == RT_ASSOC_EVENT_FLAG)
  476. || (iwe->u.data.flags ==
  477. RT_REQIE_EVENT_FLAG) ||
  478. (iwe->u.data.flags == RT_RESPIE_EVENT_FLAG)
  479. || (iwe->u.data.flags ==
  480. RT_ASSOCINFO_EVENT_FLAG)) {
  481. if (drv->scanning_done == 0) {
  482. os_free(buf);
  483. return;
  484. }
  485. }
  486. }
  487. if (iwe->u.data.flags == RT_ASSOC_EVENT_FLAG) {
  488. wpa_printf(MSG_DEBUG, "Custom wireless event: "
  489. "receive ASSOCIATED_EVENT !!!");
  490. /* determine whether the dynamic-WEP is used or
  491. * not */
  492. #if 0
  493. if (wpa_s && wpa_s->current_ssid &&
  494. wpa_s->current_ssid->key_mgmt ==
  495. WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  496. if ((wpa_s->current_ssid->eapol_flags &
  497. (EAPOL_FLAG_REQUIRE_KEY_UNICAST | EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
  498. //wpa_printf(MSG_DEBUG, "The current ssid - (%s), eapol_flag = %d.\n",
  499. // wpa_ssid_txt(wpa_s->current_ssid->ssid, wpa_s->current_ssid->ssid_len),wpa_s->current_ssid->eapol_flags);
  500. ieee8021x_required_key = TRUE;
  501. }
  502. if (ralink_set_oid(drv, OID_802_11_SET_IEEE8021X_REQUIRE_KEY, (char *) &ieee8021x_required_key, sizeof(BOOLEAN)) < 0)
  503. {
  504. wpa_printf(MSG_DEBUG, "ERROR: Failed to set OID_802_11_SET_IEEE8021X_REQUIRE_KEY(%d)",
  505. (int) ieee8021x_required_key);
  506. }
  507. wpa_printf(MSG_DEBUG, "ieee8021x_required_key is %s and eapol_flag(%d).\n", ieee8021x_required_key ? "TRUE" : "FALSE",
  508. wpa_s->current_ssid->eapol_flags);
  509. }
  510. #endif
  511. wpa_supplicant_event(ctx, EVENT_ASSOC, NULL);
  512. } else if (iwe->u.data.flags == RT_REQIE_EVENT_FLAG) {
  513. wpa_printf(MSG_DEBUG, "Custom wireless event: "
  514. "receive ReqIEs !!!");
  515. drv->assoc_req_ies =
  516. os_malloc(iwe->u.data.length);
  517. if (drv->assoc_req_ies == NULL) {
  518. os_free(buf);
  519. return;
  520. }
  521. drv->assoc_req_ies_len = iwe->u.data.length;
  522. os_memcpy(drv->assoc_req_ies, custom,
  523. iwe->u.data.length);
  524. } else if (iwe->u.data.flags == RT_RESPIE_EVENT_FLAG) {
  525. wpa_printf(MSG_DEBUG, "Custom wireless event: "
  526. "receive RespIEs !!!");
  527. drv->assoc_resp_ies =
  528. os_malloc(iwe->u.data.length);
  529. if (drv->assoc_resp_ies == NULL) {
  530. os_free(drv->assoc_req_ies);
  531. drv->assoc_req_ies = NULL;
  532. os_free(buf);
  533. return;
  534. }
  535. drv->assoc_resp_ies_len = iwe->u.data.length;
  536. os_memcpy(drv->assoc_resp_ies, custom,
  537. iwe->u.data.length);
  538. } else if (iwe->u.data.flags ==
  539. RT_ASSOCINFO_EVENT_FLAG) {
  540. wpa_printf(MSG_DEBUG, "Custom wireless event: "
  541. "receive ASSOCINFO_EVENT !!!");
  542. assoc_info_buf =
  543. os_zalloc(drv->assoc_req_ies_len +
  544. drv->assoc_resp_ies_len + 1);
  545. if (assoc_info_buf == NULL) {
  546. os_free(drv->assoc_req_ies);
  547. drv->assoc_req_ies = NULL;
  548. os_free(drv->assoc_resp_ies);
  549. drv->assoc_resp_ies = NULL;
  550. os_free(buf);
  551. return;
  552. }
  553. if (drv->assoc_req_ies) {
  554. os_memcpy(assoc_info_buf,
  555. drv->assoc_req_ies,
  556. drv->assoc_req_ies_len);
  557. }
  558. info_pos = assoc_info_buf +
  559. drv->assoc_req_ies_len;
  560. if (drv->assoc_resp_ies) {
  561. os_memcpy(info_pos,
  562. drv->assoc_resp_ies,
  563. drv->assoc_resp_ies_len);
  564. }
  565. assoc_info_buf[drv->assoc_req_ies_len +
  566. drv->assoc_resp_ies_len] = '\0';
  567. wpa_driver_ralink_event_wireless_custom(
  568. drv, ctx, assoc_info_buf);
  569. os_free(drv->assoc_req_ies);
  570. drv->assoc_req_ies = NULL;
  571. os_free(drv->assoc_resp_ies);
  572. drv->assoc_resp_ies = NULL;
  573. os_free(assoc_info_buf);
  574. } else if (iwe->u.data.flags == RT_DISASSOC_EVENT_FLAG)
  575. {
  576. wpa_printf(MSG_DEBUG, "Custom wireless event: "
  577. "receive DISASSOCIATED_EVENT !!!");
  578. wpa_supplicant_event(ctx, EVENT_DISASSOC,
  579. NULL);
  580. } else if (iwe->u.data.flags == RT_PMKIDCAND_FLAG) {
  581. wpa_printf(MSG_DEBUG, "Custom wireless event: "
  582. "receive PMKIDCAND_EVENT !!!");
  583. wpa_driver_ralink_event_pmkid(
  584. drv, (const u8 *) custom,
  585. iwe->u.data.length);
  586. } else if (iwe->u.data.flags == RT_INTERFACE_DOWN) {
  587. drv->g_driver_down = 1;
  588. eloop_terminate();
  589. } else if (iwe->u.data.flags == RT_REPORT_AP_INFO) {
  590. if (drv->ap_scan != 1) {
  591. typedef struct PACKED {
  592. UCHAR bssid[MAC_ADDR_LEN];
  593. UCHAR ssid[MAX_LEN_OF_SSID];
  594. INT ssid_len;
  595. UCHAR wpa_ie[40];
  596. INT wpa_ie_len;
  597. UCHAR rsn_ie[40];
  598. INT rsn_ie_len;
  599. INT freq;
  600. USHORT caps;
  601. } *PAPINFO;
  602. wpa_printf(MSG_DEBUG, "Custom wireless"
  603. " event: receive "
  604. "RT_REPORT_AP_INFO !!!");
  605. //printf("iwe->u.data.length = %d\n", iwe->u.data.length);
  606. //wpa_hexdump(MSG_DEBUG, "AP_Info: ", buf, iwe->u.data.length);
  607. #if 0
  608. wpa_s->num_scan_results = 1;
  609. if (wpa_s->scan_results)
  610. os_free(wpa_s->scan_results);
  611. wpa_s->scan_results = os_malloc(sizeof(struct wpa_scan_result) + 1);
  612. if (wpa_s->scan_results) {
  613. PAPINFO pApInfo = (PAPINFO)buf;
  614. os_memcpy(wpa_s->scan_results[0].bssid, pApInfo->bssid, ETH_ALEN);
  615. os_memcpy(wpa_s->scan_results[0].ssid, pApInfo->ssid, pApInfo->ssid_len);
  616. wpa_s->scan_results[0].ssid_len = pApInfo->ssid_len;
  617. if (pApInfo->wpa_ie_len > 0) {
  618. os_memcpy(wpa_s->scan_results[0].wpa_ie, pApInfo->wpa_ie, pApInfo->wpa_ie_len);
  619. wpa_s->scan_results[0].wpa_ie_len = pApInfo->wpa_ie_len;
  620. } else if (pApInfo->rsn_ie_len > 0) {
  621. os_memcpy(wpa_s->scan_results[0].rsn_ie, pApInfo->rsn_ie, pApInfo->rsn_ie_len);
  622. wpa_s->scan_results[0].rsn_ie_len = pApInfo->rsn_ie_len;
  623. }
  624. wpa_s->scan_results[0].caps = pApInfo->caps;
  625. wpa_s->scan_results[0].freq = pApInfo->freq;
  626. } else {
  627. wpa_printf("wpa_s->scan_"
  628. "results fail to "
  629. "os_malloc!!\n");
  630. }
  631. #endif
  632. }
  633. } else {
  634. wpa_driver_ralink_event_wireless_custom(
  635. drv, ctx, buf);
  636. }
  637. os_free(buf);
  638. break;
  639. }
  640. pos += iwe->len;
  641. }
  642. }
  643. static void
  644. wpa_driver_ralink_event_rtm_newlink(struct wpa_driver_ralink_data *drv,
  645. void *ctx, struct nlmsghdr *h, int len)
  646. {
  647. struct ifinfomsg *ifi;
  648. int attrlen, nlmsg_len, rta_len;
  649. struct rtattr * attr;
  650. wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
  651. if (len < (int) sizeof(*ifi))
  652. return;
  653. ifi = NLMSG_DATA(h);
  654. wpa_hexdump(MSG_DEBUG, "ifi: ", (u8 *) ifi, sizeof(struct ifinfomsg));
  655. nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
  656. attrlen = h->nlmsg_len - nlmsg_len;
  657. wpa_printf(MSG_DEBUG, "attrlen=%d", attrlen);
  658. if (attrlen < 0)
  659. return;
  660. attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
  661. wpa_hexdump(MSG_DEBUG, "attr1: ", (u8 *) attr, sizeof(struct rtattr));
  662. rta_len = RTA_ALIGN(sizeof(struct rtattr));
  663. wpa_hexdump(MSG_DEBUG, "attr2: ", (u8 *)attr,rta_len);
  664. while (RTA_OK(attr, attrlen)) {
  665. wpa_printf(MSG_DEBUG, "rta_type=%02x\n", attr->rta_type);
  666. if (attr->rta_type == IFLA_WIRELESS) {
  667. wpa_driver_ralink_event_wireless(
  668. drv, ctx,
  669. ((char *) attr) + rta_len,
  670. attr->rta_len - rta_len);
  671. }
  672. attr = RTA_NEXT(attr, attrlen);
  673. wpa_hexdump(MSG_DEBUG, "attr3: ",
  674. (u8 *) attr, sizeof(struct rtattr));
  675. }
  676. }
  677. static void wpa_driver_ralink_event_receive(int sock, void *ctx,
  678. void *sock_ctx)
  679. {
  680. char buf[8192];
  681. int left;
  682. struct sockaddr_nl from;
  683. socklen_t fromlen;
  684. struct nlmsghdr *h;
  685. wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
  686. fromlen = sizeof(from);
  687. left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
  688. (struct sockaddr *) &from, &fromlen);
  689. if (left < 0) {
  690. if (errno != EINTR && errno != EAGAIN)
  691. perror("recvfrom(netlink)");
  692. return;
  693. }
  694. h = (struct nlmsghdr *) buf;
  695. wpa_hexdump(MSG_DEBUG, "h: ", (u8 *)h, h->nlmsg_len);
  696. while (left >= (int) sizeof(*h)) {
  697. int len, plen;
  698. len = h->nlmsg_len;
  699. plen = len - sizeof(*h);
  700. if (len > left || plen < 0) {
  701. wpa_printf(MSG_DEBUG, "Malformed netlink message: "
  702. "len=%d left=%d plen=%d", len, left, plen);
  703. break;
  704. }
  705. switch (h->nlmsg_type) {
  706. case RTM_NEWLINK:
  707. wpa_driver_ralink_event_rtm_newlink(ctx, sock_ctx, h,
  708. plen);
  709. break;
  710. }
  711. len = NLMSG_ALIGN(len);
  712. left -= len;
  713. h = (struct nlmsghdr *) ((char *) h + len);
  714. }
  715. if (left > 0) {
  716. wpa_printf(MSG_DEBUG, "%d extra bytes in the end of netlink "
  717. "message", left);
  718. }
  719. }
  720. static int
  721. ralink_get_we_version_compiled(struct wpa_driver_ralink_data *drv)
  722. {
  723. struct iwreq iwr;
  724. UINT we_version_compiled = 0;
  725. os_memset(&iwr, 0, sizeof(iwr));
  726. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  727. iwr.u.data.pointer = (caddr_t) &we_version_compiled;
  728. iwr.u.data.flags = RT_OID_WE_VERSION_COMPILED;
  729. if (ioctl(drv->ioctl_sock, RT_PRIV_IOCTL, &iwr) < 0) {
  730. wpa_printf(MSG_DEBUG, "%s: failed", __func__);
  731. return -1;
  732. }
  733. drv->we_version_compiled = we_version_compiled;
  734. return 0;
  735. }
  736. static int
  737. ralink_set_iface_flags(void *priv, int dev_up)
  738. {
  739. struct wpa_driver_ralink_data *drv = priv;
  740. struct ifreq ifr;
  741. wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
  742. if (drv->ioctl_sock < 0)
  743. return -1;
  744. os_memset(&ifr, 0, sizeof(ifr));
  745. os_snprintf(ifr.ifr_name, IFNAMSIZ, "%s", drv->ifname);
  746. if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, &ifr) != 0) {
  747. perror("ioctl[SIOCGIFFLAGS]");
  748. return -1;
  749. }
  750. if (dev_up)
  751. ifr.ifr_flags |= IFF_UP;
  752. else
  753. ifr.ifr_flags &= ~IFF_UP;
  754. if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, &ifr) != 0) {
  755. perror("ioctl[SIOCSIFFLAGS]");
  756. return -1;
  757. }
  758. return 0;
  759. }
  760. static void * wpa_driver_ralink_init(void *ctx, const char *ifname)
  761. {
  762. int s;
  763. struct wpa_driver_ralink_data *drv;
  764. struct ifreq ifr;
  765. struct sockaddr_nl local;
  766. UCHAR enable_wpa_supplicant = 0;
  767. wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
  768. /* open socket to kernel */
  769. if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  770. perror("socket");
  771. return NULL;
  772. }
  773. /* do it */
  774. os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
  775. if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
  776. perror(ifr.ifr_name);
  777. return NULL;
  778. }
  779. drv = os_zalloc(sizeof(*drv));
  780. if (drv == NULL)
  781. return NULL;
  782. drv->scanning_done = 1;
  783. drv->ap_scan = 1; /* for now - let's assume ap_scan=1 is used */
  784. drv->ctx = ctx;
  785. os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname));
  786. drv->ioctl_sock = s;
  787. drv->g_driver_down = 0;
  788. s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  789. if (s < 0) {
  790. perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
  791. close(drv->ioctl_sock);
  792. os_free(drv);
  793. return NULL;
  794. }
  795. os_memset(&local, 0, sizeof(local));
  796. local.nl_family = AF_NETLINK;
  797. local.nl_groups = RTMGRP_LINK;
  798. if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
  799. perror("bind(netlink)");
  800. close(s);
  801. close(drv->ioctl_sock);
  802. os_free(drv);
  803. return NULL;
  804. }
  805. eloop_register_read_sock(s, wpa_driver_ralink_event_receive, drv, ctx);
  806. drv->event_sock = s;
  807. drv->no_of_pmkid = 4; /* Number of PMKID saved supported */
  808. ralink_set_iface_flags(drv, 1); /* mark up during setup */
  809. ralink_get_we_version_compiled(drv);
  810. wpa_driver_ralink_flush_pmkid(drv);
  811. if (drv->ap_scan == 1)
  812. enable_wpa_supplicant = 1;
  813. else
  814. enable_wpa_supplicant = 2;
  815. /* trigger driver support wpa_supplicant */
  816. if (ralink_set_oid(drv, RT_OID_WPA_SUPPLICANT_SUPPORT,
  817. (PCHAR) &enable_wpa_supplicant, sizeof(UCHAR)) < 0)
  818. {
  819. wpa_printf(MSG_DEBUG, "RALINK: Failed to set "
  820. "RT_OID_WPA_SUPPLICANT_SUPPORT(%d)",
  821. (int) enable_wpa_supplicant);
  822. wpa_printf(MSG_ERROR, "RALINK: Driver does not support "
  823. "wpa_supplicant");
  824. close(s);
  825. close(drv->ioctl_sock);
  826. os_free(drv);
  827. return NULL;
  828. }
  829. if (drv->ap_scan == 1)
  830. drv->scanning_done = 0;
  831. return drv;
  832. }
  833. static void wpa_driver_ralink_deinit(void *priv)
  834. {
  835. struct wpa_driver_ralink_data *drv = priv;
  836. UCHAR enable_wpa_supplicant;
  837. wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
  838. enable_wpa_supplicant = 0;
  839. if (drv->g_driver_down == 0) {
  840. /* trigger driver disable wpa_supplicant support */
  841. if (ralink_set_oid(drv, RT_OID_WPA_SUPPLICANT_SUPPORT,
  842. (char *) &enable_wpa_supplicant,
  843. sizeof(BOOLEAN)) < 0) {
  844. wpa_printf(MSG_DEBUG, "RALINK: Failed to set "
  845. "RT_OID_WPA_SUPPLICANT_SUPPORT(%d)",
  846. (int) enable_wpa_supplicant);
  847. }
  848. wpa_driver_ralink_flush_pmkid(drv);
  849. sleep(1);
  850. ralink_set_iface_flags(drv, 0);
  851. }
  852. eloop_cancel_timeout(wpa_driver_ralink_scan_timeout, drv, drv->ctx);
  853. eloop_unregister_read_sock(drv->event_sock);
  854. close(drv->event_sock);
  855. close(drv->ioctl_sock);
  856. os_free(drv);
  857. }
  858. static void wpa_driver_ralink_scan_timeout(void *eloop_ctx, void *timeout_ctx)
  859. {
  860. struct wpa_driver_ralink_data *drv = eloop_ctx;
  861. wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
  862. wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
  863. wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
  864. drv->scanning_done = 1;
  865. }
  866. static int wpa_driver_ralink_scan(void *priv,
  867. struct wpa_driver_scan_params *params)
  868. {
  869. struct wpa_driver_ralink_data *drv = priv;
  870. struct iwreq iwr;
  871. int ret = 0;
  872. if (drv->g_driver_down == 1)
  873. return -1;
  874. wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
  875. #if 0
  876. if (ssid_len > IW_ESSID_MAX_SIZE) {
  877. wpa_printf(MSG_DEBUG, "%s: too long SSID (%lu)",
  878. __FUNCTION__, (unsigned long) ssid_len);
  879. return -1;
  880. }
  881. /* wpa_driver_ralink_set_ssid(drv, ssid, ssid_len); */
  882. #endif
  883. os_memset(&iwr, 0, sizeof(iwr));
  884. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  885. if (ioctl(drv->ioctl_sock, SIOCSIWSCAN, &iwr) < 0) {
  886. perror("ioctl[SIOCSIWSCAN]");
  887. ret = -1;
  888. }
  889. /* Not all drivers generate "scan completed" wireless event, so try to
  890. * read results after a timeout. */
  891. eloop_cancel_timeout(wpa_driver_ralink_scan_timeout, drv, drv->ctx);
  892. eloop_register_timeout(4, 0, wpa_driver_ralink_scan_timeout, drv,
  893. drv->ctx);
  894. drv->scanning_done = 0;
  895. return ret;
  896. }
  897. static int
  898. wpa_driver_ralink_get_scan_results(void *priv,
  899. struct wpa_scan_result *results,
  900. size_t max_size)
  901. {
  902. struct wpa_driver_ralink_data *drv = priv;
  903. UCHAR *buf = NULL;
  904. NDIS_802_11_BSSID_LIST_EX *wsr;
  905. NDIS_WLAN_BSSID_EX *wbi;
  906. struct iwreq iwr;
  907. int rv = 0;
  908. size_t ap_num;
  909. u8 *pos, *end;
  910. if (drv->g_driver_down == 1)
  911. return -1;
  912. wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
  913. if (drv->we_version_compiled >= 17) {
  914. buf = os_zalloc(8192);
  915. iwr.u.data.length = 8192;
  916. } else {
  917. buf = os_zalloc(4096);
  918. iwr.u.data.length = 4096;
  919. }
  920. if (buf == NULL)
  921. return -1;
  922. wsr = (NDIS_802_11_BSSID_LIST_EX *) buf;
  923. wsr->NumberOfItems = 0;
  924. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  925. iwr.u.data.pointer = (void *) buf;
  926. iwr.u.data.flags = OID_802_11_BSSID_LIST;
  927. if ((rv = ioctl(drv->ioctl_sock, RT_PRIV_IOCTL, &iwr)) < 0) {
  928. wpa_printf(MSG_DEBUG, "ioctl fail: rv = %d", rv);
  929. os_free(buf);
  930. return -1;
  931. }
  932. os_memset(results, 0, max_size * sizeof(struct wpa_scan_result));
  933. for (ap_num = 0, wbi = wsr->Bssid; ap_num < wsr->NumberOfItems;
  934. ++ap_num) {
  935. os_memcpy(results[ap_num].bssid, &wbi->MacAddress, ETH_ALEN);
  936. os_memcpy(results[ap_num].ssid, wbi->Ssid.Ssid,
  937. wbi->Ssid.SsidLength);
  938. results[ap_num].ssid_len = wbi->Ssid.SsidLength;
  939. results[ap_num].freq = (wbi->Configuration.DSConfig / 1000);
  940. /* get ie's */
  941. wpa_hexdump(MSG_DEBUG, "RALINK: AP IEs",
  942. (u8 *) wbi + sizeof(*wbi) - 1, wbi->IELength);
  943. pos = (u8 *) wbi + sizeof(*wbi) - 1;
  944. end = (u8 *) wbi + sizeof(*wbi) + wbi->IELength;
  945. if (wbi->IELength < sizeof(NDIS_802_11_FIXED_IEs))
  946. break;
  947. pos += sizeof(NDIS_802_11_FIXED_IEs) - 2;
  948. os_memcpy(&results[ap_num].caps, pos, 2);
  949. pos += 2;
  950. while (pos + 1 < end && pos + 2 + pos[1] <= end) {
  951. u8 ielen = 2 + pos[1];
  952. if (ielen > SSID_MAX_WPA_IE_LEN) {
  953. pos += ielen;
  954. continue;
  955. }
  956. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC &&
  957. pos[1] >= 4 &&
  958. os_memcmp(pos + 2, "\x00\x50\xf2\x01", 4) == 0) {
  959. os_memcpy(results[ap_num].wpa_ie, pos, ielen);
  960. results[ap_num].wpa_ie_len = ielen;
  961. } else if (pos[0] == WLAN_EID_RSN) {
  962. os_memcpy(results[ap_num].rsn_ie, pos, ielen);
  963. results[ap_num].rsn_ie_len = ielen;
  964. }
  965. pos += ielen;
  966. }
  967. wbi = (NDIS_WLAN_BSSID_EX *) ((u8 *) wbi + wbi->Length);
  968. }
  969. os_free(buf);
  970. return ap_num;
  971. }
  972. static int ralink_set_auth_mode(struct wpa_driver_ralink_data *drv,
  973. NDIS_802_11_AUTHENTICATION_MODE mode)
  974. {
  975. NDIS_802_11_AUTHENTICATION_MODE auth_mode = mode;
  976. wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
  977. if (ralink_set_oid(drv, OID_802_11_AUTHENTICATION_MODE,
  978. (char *) &auth_mode, sizeof(auth_mode)) < 0) {
  979. wpa_printf(MSG_DEBUG, "RALINK: Failed to set "
  980. "OID_802_11_AUTHENTICATION_MODE (%d)",
  981. (int) auth_mode);
  982. return -1;
  983. }
  984. return 0;
  985. }
  986. static int wpa_driver_ralink_remove_key(struct wpa_driver_ralink_data *drv,
  987. int key_idx, const u8 *addr,
  988. const u8 *bssid, int pairwise)
  989. {
  990. NDIS_802_11_REMOVE_KEY rkey;
  991. NDIS_802_11_KEY_INDEX _index;
  992. int res, res2;
  993. wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
  994. os_memset(&rkey, 0, sizeof(rkey));
  995. rkey.Length = sizeof(rkey);
  996. rkey.KeyIndex = key_idx;
  997. if (pairwise)
  998. rkey.KeyIndex |= 1 << 30;
  999. os_memcpy(rkey.BSSID, bssid, ETH_ALEN);
  1000. res = ralink_set_oid(drv, OID_802_11_REMOVE_KEY, (char *) &rkey,
  1001. sizeof(rkey));
  1002. /* AlbertY@20060210 removed it */
  1003. if (0 /* !pairwise */) {
  1004. res2 = ralink_set_oid(drv, OID_802_11_REMOVE_WEP,
  1005. (char *) &_index, sizeof(_index));
  1006. } else
  1007. res2 = 0;
  1008. if (res < 0 && res2 < 0)
  1009. return res;
  1010. return 0;
  1011. }
  1012. static int wpa_driver_ralink_add_wep(struct wpa_driver_ralink_data *drv,
  1013. int pairwise, int key_idx, int set_tx,
  1014. const u8 *key, size_t key_len)
  1015. {
  1016. NDIS_802_11_WEP *wep;
  1017. size_t len;
  1018. int res;
  1019. wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
  1020. len = 12 + key_len;
  1021. wep = os_zalloc(len);
  1022. if (wep == NULL)
  1023. return -1;
  1024. wep->Length = len;
  1025. wep->KeyIndex = key_idx;
  1026. if (set_tx)
  1027. wep->KeyIndex |= 0x80000000;
  1028. wep->KeyLength = key_len;
  1029. os_memcpy(wep->KeyMaterial, key, key_len);
  1030. wpa_hexdump_key(MSG_MSGDUMP, "RALINK: OID_802_11_ADD_WEP",
  1031. (const u8 *) wep, len);
  1032. res = ralink_set_oid(drv, OID_802_11_ADD_WEP, (char *) wep, len);
  1033. os_free(wep);
  1034. return res;
  1035. }
  1036. static int wpa_driver_ralink_set_key(const char *ifname, void *priv,
  1037. wpa_alg alg, const u8 *addr,
  1038. int key_idx, int set_tx,
  1039. const u8 *seq, size_t seq_len,
  1040. const u8 *key, size_t key_len)
  1041. {
  1042. struct wpa_driver_ralink_data *drv = priv;
  1043. size_t len, i;
  1044. NDIS_802_11_KEY *nkey;
  1045. int res, pairwise;
  1046. u8 bssid[ETH_ALEN];
  1047. if (drv->g_driver_down == 1)
  1048. return -1;
  1049. wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
  1050. if (addr == NULL || os_memcmp(addr, "\xff\xff\xff\xff\xff\xff",
  1051. ETH_ALEN) == 0) {
  1052. /* Group Key */
  1053. pairwise = 0;
  1054. wpa_driver_ralink_get_bssid(drv, bssid);
  1055. } else {
  1056. /* Pairwise Key */
  1057. pairwise = 1;
  1058. os_memcpy(bssid, addr, ETH_ALEN);
  1059. }
  1060. if (alg == WPA_ALG_NONE || key_len == 0) {
  1061. return wpa_driver_ralink_remove_key(drv, key_idx, addr, bssid,
  1062. pairwise);
  1063. }
  1064. if (alg == WPA_ALG_WEP) {
  1065. return wpa_driver_ralink_add_wep(drv, pairwise, key_idx,
  1066. set_tx, key, key_len);
  1067. }
  1068. len = 12 + 6 + 6 + 8 + key_len;
  1069. nkey = os_zalloc(len);
  1070. if (nkey == NULL)
  1071. return -1;
  1072. nkey->Length = len;
  1073. nkey->KeyIndex = key_idx;
  1074. if (set_tx)
  1075. nkey->KeyIndex |= 1 << 31;
  1076. if (pairwise)
  1077. nkey->KeyIndex |= 1 << 30;
  1078. if (seq && seq_len)
  1079. nkey->KeyIndex |= 1 << 29;
  1080. nkey->KeyLength = key_len;
  1081. os_memcpy(nkey->BSSID, bssid, ETH_ALEN);
  1082. if (seq && seq_len) {
  1083. for (i = 0; i < seq_len; i++)
  1084. nkey->KeyRSC |= seq[i] << (i * 8);
  1085. }
  1086. if (alg == WPA_ALG_TKIP && key_len == 32) {
  1087. os_memcpy(nkey->KeyMaterial, key, 16);
  1088. os_memcpy(nkey->KeyMaterial + 16, key + 24, 8);
  1089. os_memcpy(nkey->KeyMaterial + 24, key + 16, 8);
  1090. } else {
  1091. os_memcpy(nkey->KeyMaterial, key, key_len);
  1092. }
  1093. wpa_printf(MSG_DEBUG, "%s: alg=%d key_idx=%d set_tx=%d seq_len=%lu "
  1094. "key_len=%lu", __FUNCTION__, alg, key_idx, set_tx,
  1095. (unsigned long) seq_len, (unsigned long) key_len);
  1096. wpa_hexdump_key(MSG_MSGDUMP, "RALINK: OID_802_11_ADD_KEY",
  1097. (const u8 *) nkey, len);
  1098. res = ralink_set_oid(drv, OID_802_11_ADD_KEY, (char *) nkey, len);
  1099. os_free(nkey);
  1100. return res;
  1101. }
  1102. static int wpa_driver_ralink_disassociate(void *priv, const u8 *addr,
  1103. int reason_code)
  1104. {
  1105. struct wpa_driver_ralink_data *drv = priv;
  1106. if (drv->g_driver_down == 1)
  1107. return -1;
  1108. wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
  1109. if (ralink_set_oid(drv, OID_802_11_DISASSOCIATE, " ", 4) < 0) {
  1110. wpa_printf(MSG_DEBUG, "RALINK: Failed to set "
  1111. "OID_802_11_DISASSOCIATE");
  1112. }
  1113. return 0;
  1114. }
  1115. static int wpa_driver_ralink_deauthenticate(void *priv, const u8 *addr,
  1116. int reason_code)
  1117. {
  1118. struct wpa_driver_ralink_data *drv = priv;
  1119. wpa_printf(MSG_DEBUG, "g_driver_down = %d", drv->g_driver_down);
  1120. if (drv->g_driver_down == 1)
  1121. return -1;
  1122. wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
  1123. if (ralink_get_new_driver_flag(drv) == 0) {
  1124. return wpa_driver_ralink_disassociate(priv, addr, reason_code);
  1125. } else {
  1126. MLME_DEAUTH_REQ_STRUCT mlme;
  1127. os_memset(&mlme, 0, sizeof(MLME_DEAUTH_REQ_STRUCT));
  1128. mlme.Reason = reason_code;
  1129. os_memcpy(mlme.Addr, addr, MAC_ADDR_LEN);
  1130. return ralink_set_oid(drv, OID_802_11_DEAUTHENTICATION,
  1131. (char *) &mlme,
  1132. sizeof(MLME_DEAUTH_REQ_STRUCT));
  1133. }
  1134. }
  1135. static int
  1136. wpa_driver_ralink_associate(void *priv,
  1137. struct wpa_driver_associate_params *params)
  1138. {
  1139. struct wpa_driver_ralink_data *drv = priv;
  1140. NDIS_802_11_NETWORK_INFRASTRUCTURE mode;
  1141. NDIS_802_11_AUTHENTICATION_MODE auth_mode;
  1142. NDIS_802_11_WEP_STATUS encr;
  1143. BOOLEAN ieee8021xMode;
  1144. if (drv->g_driver_down == 1)
  1145. return -1;
  1146. wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
  1147. if (params->mode == IEEE80211_MODE_IBSS)
  1148. mode = Ndis802_11IBSS;
  1149. else
  1150. mode = Ndis802_11Infrastructure;
  1151. if (ralink_set_oid(drv, OID_802_11_INFRASTRUCTURE_MODE,
  1152. (char *) &mode, sizeof(mode)) < 0) {
  1153. wpa_printf(MSG_DEBUG, "RALINK: Failed to set "
  1154. "OID_802_11_INFRASTRUCTURE_MODE (%d)",
  1155. (int) mode);
  1156. /* Try to continue anyway */
  1157. }
  1158. if (params->wpa_ie == NULL || params->wpa_ie_len == 0) {
  1159. if (params->auth_alg & AUTH_ALG_SHARED_KEY) {
  1160. if (params->auth_alg & AUTH_ALG_OPEN_SYSTEM)
  1161. auth_mode = Ndis802_11AuthModeAutoSwitch;
  1162. else
  1163. auth_mode = Ndis802_11AuthModeShared;
  1164. } else
  1165. auth_mode = Ndis802_11AuthModeOpen;
  1166. } else if (params->wpa_ie[0] == WLAN_EID_RSN) {
  1167. if (params->key_mgmt_suite == KEY_MGMT_PSK)
  1168. auth_mode = Ndis802_11AuthModeWPA2PSK;
  1169. else
  1170. auth_mode = Ndis802_11AuthModeWPA2;
  1171. } else {
  1172. if (params->key_mgmt_suite == KEY_MGMT_WPA_NONE)
  1173. auth_mode = Ndis802_11AuthModeWPANone;
  1174. else if (params->key_mgmt_suite == KEY_MGMT_PSK)
  1175. auth_mode = Ndis802_11AuthModeWPAPSK;
  1176. else
  1177. auth_mode = Ndis802_11AuthModeWPA;
  1178. }
  1179. switch (params->pairwise_suite) {
  1180. case CIPHER_CCMP:
  1181. encr = Ndis802_11Encryption3Enabled;
  1182. break;
  1183. case CIPHER_TKIP:
  1184. encr = Ndis802_11Encryption2Enabled;
  1185. break;
  1186. case CIPHER_WEP40:
  1187. case CIPHER_WEP104:
  1188. encr = Ndis802_11Encryption1Enabled;
  1189. break;
  1190. case CIPHER_NONE:
  1191. if (params->group_suite == CIPHER_CCMP)
  1192. encr = Ndis802_11Encryption3Enabled;
  1193. else if (params->group_suite == CIPHER_TKIP)
  1194. encr = Ndis802_11Encryption2Enabled;
  1195. else
  1196. encr = Ndis802_11EncryptionDisabled;
  1197. break;
  1198. default:
  1199. encr = Ndis802_11EncryptionDisabled;
  1200. break;
  1201. }
  1202. ralink_set_auth_mode(drv, auth_mode);
  1203. /* notify driver that IEEE8021x mode is enabled */
  1204. if (params->key_mgmt_suite == KEY_MGMT_802_1X_NO_WPA)
  1205. ieee8021xMode = TRUE;
  1206. else
  1207. ieee8021xMode = FALSE;
  1208. if (ralink_set_oid(drv, OID_802_11_SET_IEEE8021X,
  1209. (char *) &ieee8021xMode, sizeof(BOOLEAN)) < 0) {
  1210. wpa_printf(MSG_DEBUG, "RALINK: Failed to set "
  1211. "OID_802_11_SET_IEEE8021X(%d)",
  1212. (int) ieee8021xMode);
  1213. }
  1214. if (ralink_set_oid(drv, OID_802_11_WEP_STATUS,
  1215. (char *) &encr, sizeof(encr)) < 0) {
  1216. wpa_printf(MSG_DEBUG, "RALINK: Failed to set "
  1217. "OID_802_11_WEP_STATUS(%d)",
  1218. (int) encr);
  1219. }
  1220. if ((ieee8021xMode == FALSE) &&
  1221. (encr == Ndis802_11Encryption1Enabled)) {
  1222. /* static WEP */
  1223. int enabled = 0;
  1224. if (ralink_set_oid(drv, OID_802_11_DROP_UNENCRYPTED,
  1225. (char *) &enabled, sizeof(enabled)) < 0) {
  1226. wpa_printf(MSG_DEBUG, "RALINK: Failed to set "
  1227. "OID_802_11_DROP_UNENCRYPTED(%d)",
  1228. (int) encr);
  1229. }
  1230. }
  1231. return wpa_driver_ralink_set_ssid(drv, params->ssid, params->ssid_len);
  1232. }
  1233. static int
  1234. wpa_driver_ralink_set_countermeasures(void *priv, int enabled)
  1235. {
  1236. struct wpa_driver_ralink_data *drv = priv;
  1237. if (drv->g_driver_down == 1)
  1238. return -1;
  1239. wpa_printf(MSG_DEBUG, "%s: enabled=%d", __FUNCTION__, enabled);
  1240. return ralink_set_oid(drv, OID_SET_COUNTERMEASURES, (char *) &enabled,
  1241. sizeof(int));
  1242. }
  1243. const struct wpa_driver_ops wpa_driver_ralink_ops = {
  1244. .name = "ralink",
  1245. .desc = "Ralink Wireless Client driver",
  1246. .get_bssid = wpa_driver_ralink_get_bssid,
  1247. .get_ssid = wpa_driver_ralink_get_ssid,
  1248. .set_key = wpa_driver_ralink_set_key,
  1249. .init = wpa_driver_ralink_init,
  1250. .deinit = wpa_driver_ralink_deinit,
  1251. .set_countermeasures = wpa_driver_ralink_set_countermeasures,
  1252. .scan2 = wpa_driver_ralink_scan,
  1253. .get_scan_results = wpa_driver_ralink_get_scan_results,
  1254. .deauthenticate = wpa_driver_ralink_deauthenticate,
  1255. .disassociate = wpa_driver_ralink_disassociate,
  1256. .associate = wpa_driver_ralink_associate,
  1257. .add_pmkid = wpa_driver_ralink_add_pmkid,
  1258. .remove_pmkid = wpa_driver_ralink_remove_pmkid,
  1259. .flush_pmkid = wpa_driver_ralink_flush_pmkid,
  1260. };