driver_wext.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324
  1. /*
  2. * WPA Supplicant - driver interaction with generic Linux Wireless Extensions
  3. * Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. *
  14. * This file implements a driver interface for the Linux Wireless Extensions.
  15. * When used with WE-18 or newer, this interface can be used as-is with number
  16. * of drivers. In addition to this, some of the common functions in this file
  17. * can be used by other driver interface implementations that use generic WE
  18. * ioctls, but require private ioctls for some of the functionality.
  19. */
  20. #include "includes.h"
  21. #include <sys/ioctl.h>
  22. #include <net/if_arp.h>
  23. #include "wireless_copy.h"
  24. #include "common.h"
  25. #include "driver.h"
  26. #include "eloop.h"
  27. #include "priv_netlink.h"
  28. #include "driver_wext.h"
  29. #include "ieee802_11_defs.h"
  30. #include "wpa_common.h"
  31. static int wpa_driver_wext_flush_pmkid(void *priv);
  32. static int wpa_driver_wext_get_range(void *priv);
  33. static void wpa_driver_wext_finish_drv_init(struct wpa_driver_wext_data *drv);
  34. static int wpa_driver_wext_send_oper_ifla(struct wpa_driver_wext_data *drv,
  35. int linkmode, int operstate)
  36. {
  37. struct {
  38. struct nlmsghdr hdr;
  39. struct ifinfomsg ifinfo;
  40. char opts[16];
  41. } req;
  42. struct rtattr *rta;
  43. static int nl_seq;
  44. ssize_t ret;
  45. os_memset(&req, 0, sizeof(req));
  46. req.hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
  47. req.hdr.nlmsg_type = RTM_SETLINK;
  48. req.hdr.nlmsg_flags = NLM_F_REQUEST;
  49. req.hdr.nlmsg_seq = ++nl_seq;
  50. req.hdr.nlmsg_pid = 0;
  51. req.ifinfo.ifi_family = AF_UNSPEC;
  52. req.ifinfo.ifi_type = 0;
  53. req.ifinfo.ifi_index = drv->ifindex;
  54. req.ifinfo.ifi_flags = 0;
  55. req.ifinfo.ifi_change = 0;
  56. if (linkmode != -1) {
  57. rta = (struct rtattr *)
  58. ((char *) &req + NLMSG_ALIGN(req.hdr.nlmsg_len));
  59. rta->rta_type = IFLA_LINKMODE;
  60. rta->rta_len = RTA_LENGTH(sizeof(char));
  61. *((char *) RTA_DATA(rta)) = linkmode;
  62. req.hdr.nlmsg_len = NLMSG_ALIGN(req.hdr.nlmsg_len) +
  63. RTA_LENGTH(sizeof(char));
  64. }
  65. if (operstate != -1) {
  66. rta = (struct rtattr *)
  67. ((char *) &req + NLMSG_ALIGN(req.hdr.nlmsg_len));
  68. rta->rta_type = IFLA_OPERSTATE;
  69. rta->rta_len = RTA_LENGTH(sizeof(char));
  70. *((char *) RTA_DATA(rta)) = operstate;
  71. req.hdr.nlmsg_len = NLMSG_ALIGN(req.hdr.nlmsg_len) +
  72. RTA_LENGTH(sizeof(char));
  73. }
  74. wpa_printf(MSG_DEBUG, "WEXT: Operstate: linkmode=%d, operstate=%d",
  75. linkmode, operstate);
  76. ret = send(drv->event_sock, &req, req.hdr.nlmsg_len, 0);
  77. if (ret < 0) {
  78. wpa_printf(MSG_DEBUG, "WEXT: Sending operstate IFLA failed: "
  79. "%s (assume operstate is not supported)",
  80. strerror(errno));
  81. }
  82. return ret < 0 ? -1 : 0;
  83. }
  84. int wpa_driver_wext_set_auth_param(struct wpa_driver_wext_data *drv,
  85. int idx, u32 value)
  86. {
  87. struct iwreq iwr;
  88. int ret = 0;
  89. os_memset(&iwr, 0, sizeof(iwr));
  90. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  91. iwr.u.param.flags = idx & IW_AUTH_INDEX;
  92. iwr.u.param.value = value;
  93. if (ioctl(drv->ioctl_sock, SIOCSIWAUTH, &iwr) < 0) {
  94. if (errno != EOPNOTSUPP) {
  95. wpa_printf(MSG_DEBUG, "WEXT: SIOCSIWAUTH(param %d "
  96. "value 0x%x) failed: %s)",
  97. idx, value, strerror(errno));
  98. }
  99. ret = errno == EOPNOTSUPP ? -2 : -1;
  100. }
  101. return ret;
  102. }
  103. /**
  104. * wpa_driver_wext_get_bssid - Get BSSID, SIOCGIWAP
  105. * @priv: Pointer to private wext data from wpa_driver_wext_init()
  106. * @bssid: Buffer for BSSID
  107. * Returns: 0 on success, -1 on failure
  108. */
  109. int wpa_driver_wext_get_bssid(void *priv, u8 *bssid)
  110. {
  111. struct wpa_driver_wext_data *drv = priv;
  112. struct iwreq iwr;
  113. int ret = 0;
  114. os_memset(&iwr, 0, sizeof(iwr));
  115. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  116. if (ioctl(drv->ioctl_sock, SIOCGIWAP, &iwr) < 0) {
  117. perror("ioctl[SIOCGIWAP]");
  118. ret = -1;
  119. }
  120. os_memcpy(bssid, iwr.u.ap_addr.sa_data, ETH_ALEN);
  121. return ret;
  122. }
  123. /**
  124. * wpa_driver_wext_set_bssid - Set BSSID, SIOCSIWAP
  125. * @priv: Pointer to private wext data from wpa_driver_wext_init()
  126. * @bssid: BSSID
  127. * Returns: 0 on success, -1 on failure
  128. */
  129. int wpa_driver_wext_set_bssid(void *priv, const u8 *bssid)
  130. {
  131. struct wpa_driver_wext_data *drv = priv;
  132. struct iwreq iwr;
  133. int ret = 0;
  134. os_memset(&iwr, 0, sizeof(iwr));
  135. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  136. iwr.u.ap_addr.sa_family = ARPHRD_ETHER;
  137. if (bssid)
  138. os_memcpy(iwr.u.ap_addr.sa_data, bssid, ETH_ALEN);
  139. else
  140. os_memset(iwr.u.ap_addr.sa_data, 0, ETH_ALEN);
  141. if (ioctl(drv->ioctl_sock, SIOCSIWAP, &iwr) < 0) {
  142. perror("ioctl[SIOCSIWAP]");
  143. ret = -1;
  144. }
  145. return ret;
  146. }
  147. /**
  148. * wpa_driver_wext_get_ssid - Get SSID, SIOCGIWESSID
  149. * @priv: Pointer to private wext data from wpa_driver_wext_init()
  150. * @ssid: Buffer for the SSID; must be at least 32 bytes long
  151. * Returns: SSID length on success, -1 on failure
  152. */
  153. int wpa_driver_wext_get_ssid(void *priv, u8 *ssid)
  154. {
  155. struct wpa_driver_wext_data *drv = priv;
  156. struct iwreq iwr;
  157. int ret = 0;
  158. os_memset(&iwr, 0, sizeof(iwr));
  159. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  160. iwr.u.essid.pointer = (caddr_t) ssid;
  161. iwr.u.essid.length = 32;
  162. if (ioctl(drv->ioctl_sock, SIOCGIWESSID, &iwr) < 0) {
  163. perror("ioctl[SIOCGIWESSID]");
  164. ret = -1;
  165. } else {
  166. ret = iwr.u.essid.length;
  167. if (ret > 32)
  168. ret = 32;
  169. /* Some drivers include nul termination in the SSID, so let's
  170. * remove it here before further processing. WE-21 changes this
  171. * to explicitly require the length _not_ to include nul
  172. * termination. */
  173. if (ret > 0 && ssid[ret - 1] == '\0' &&
  174. drv->we_version_compiled < 21)
  175. ret--;
  176. }
  177. return ret;
  178. }
  179. /**
  180. * wpa_driver_wext_set_ssid - Set SSID, SIOCSIWESSID
  181. * @priv: Pointer to private wext data from wpa_driver_wext_init()
  182. * @ssid: SSID
  183. * @ssid_len: Length of SSID (0..32)
  184. * Returns: 0 on success, -1 on failure
  185. */
  186. int wpa_driver_wext_set_ssid(void *priv, const u8 *ssid, size_t ssid_len)
  187. {
  188. struct wpa_driver_wext_data *drv = priv;
  189. struct iwreq iwr;
  190. int ret = 0;
  191. char buf[33];
  192. if (ssid_len > 32)
  193. return -1;
  194. os_memset(&iwr, 0, sizeof(iwr));
  195. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  196. /* flags: 1 = ESSID is active, 0 = not (promiscuous) */
  197. iwr.u.essid.flags = (ssid_len != 0);
  198. os_memset(buf, 0, sizeof(buf));
  199. os_memcpy(buf, ssid, ssid_len);
  200. iwr.u.essid.pointer = (caddr_t) buf;
  201. if (drv->we_version_compiled < 21) {
  202. /* For historic reasons, set SSID length to include one extra
  203. * character, C string nul termination, even though SSID is
  204. * really an octet string that should not be presented as a C
  205. * string. Some Linux drivers decrement the length by one and
  206. * can thus end up missing the last octet of the SSID if the
  207. * length is not incremented here. WE-21 changes this to
  208. * explicitly require the length _not_ to include nul
  209. * termination. */
  210. if (ssid_len)
  211. ssid_len++;
  212. }
  213. iwr.u.essid.length = ssid_len;
  214. if (ioctl(drv->ioctl_sock, SIOCSIWESSID, &iwr) < 0) {
  215. perror("ioctl[SIOCSIWESSID]");
  216. ret = -1;
  217. }
  218. return ret;
  219. }
  220. /**
  221. * wpa_driver_wext_set_freq - Set frequency/channel, SIOCSIWFREQ
  222. * @priv: Pointer to private wext data from wpa_driver_wext_init()
  223. * @freq: Frequency in MHz
  224. * Returns: 0 on success, -1 on failure
  225. */
  226. int wpa_driver_wext_set_freq(void *priv, int freq)
  227. {
  228. struct wpa_driver_wext_data *drv = priv;
  229. struct iwreq iwr;
  230. int ret = 0;
  231. os_memset(&iwr, 0, sizeof(iwr));
  232. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  233. iwr.u.freq.m = freq * 100000;
  234. iwr.u.freq.e = 1;
  235. if (ioctl(drv->ioctl_sock, SIOCSIWFREQ, &iwr) < 0) {
  236. perror("ioctl[SIOCSIWFREQ]");
  237. ret = -1;
  238. }
  239. return ret;
  240. }
  241. static void
  242. wpa_driver_wext_event_wireless_custom(void *ctx, char *custom)
  243. {
  244. union wpa_event_data data;
  245. wpa_printf(MSG_MSGDUMP, "WEXT: Custom wireless event: '%s'",
  246. custom);
  247. os_memset(&data, 0, sizeof(data));
  248. /* Host AP driver */
  249. if (os_strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
  250. data.michael_mic_failure.unicast =
  251. os_strstr(custom, " unicast ") != NULL;
  252. /* TODO: parse parameters(?) */
  253. wpa_supplicant_event(ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
  254. } else if (os_strncmp(custom, "ASSOCINFO(ReqIEs=", 17) == 0) {
  255. char *spos;
  256. int bytes;
  257. spos = custom + 17;
  258. bytes = strspn(spos, "0123456789abcdefABCDEF");
  259. if (!bytes || (bytes & 1))
  260. return;
  261. bytes /= 2;
  262. data.assoc_info.req_ies = os_malloc(bytes);
  263. if (data.assoc_info.req_ies == NULL)
  264. return;
  265. data.assoc_info.req_ies_len = bytes;
  266. hexstr2bin(spos, data.assoc_info.req_ies, bytes);
  267. spos += bytes * 2;
  268. data.assoc_info.resp_ies = NULL;
  269. data.assoc_info.resp_ies_len = 0;
  270. if (os_strncmp(spos, " RespIEs=", 9) == 0) {
  271. spos += 9;
  272. bytes = strspn(spos, "0123456789abcdefABCDEF");
  273. if (!bytes || (bytes & 1))
  274. goto done;
  275. bytes /= 2;
  276. data.assoc_info.resp_ies = os_malloc(bytes);
  277. if (data.assoc_info.resp_ies == NULL)
  278. goto done;
  279. data.assoc_info.resp_ies_len = bytes;
  280. hexstr2bin(spos, data.assoc_info.resp_ies, bytes);
  281. }
  282. wpa_supplicant_event(ctx, EVENT_ASSOCINFO, &data);
  283. done:
  284. os_free(data.assoc_info.resp_ies);
  285. os_free(data.assoc_info.req_ies);
  286. #ifdef CONFIG_PEERKEY
  287. } else if (os_strncmp(custom, "STKSTART.request=", 17) == 0) {
  288. if (hwaddr_aton(custom + 17, data.stkstart.peer)) {
  289. wpa_printf(MSG_DEBUG, "WEXT: unrecognized "
  290. "STKSTART.request '%s'", custom + 17);
  291. return;
  292. }
  293. wpa_supplicant_event(ctx, EVENT_STKSTART, &data);
  294. #endif /* CONFIG_PEERKEY */
  295. }
  296. }
  297. static int wpa_driver_wext_event_wireless_michaelmicfailure(
  298. void *ctx, const char *ev, size_t len)
  299. {
  300. const struct iw_michaelmicfailure *mic;
  301. union wpa_event_data data;
  302. if (len < sizeof(*mic))
  303. return -1;
  304. mic = (const struct iw_michaelmicfailure *) ev;
  305. wpa_printf(MSG_DEBUG, "Michael MIC failure wireless event: "
  306. "flags=0x%x src_addr=" MACSTR, mic->flags,
  307. MAC2STR(mic->src_addr.sa_data));
  308. os_memset(&data, 0, sizeof(data));
  309. data.michael_mic_failure.unicast = !(mic->flags & IW_MICFAILURE_GROUP);
  310. wpa_supplicant_event(ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
  311. return 0;
  312. }
  313. static int wpa_driver_wext_event_wireless_pmkidcand(
  314. struct wpa_driver_wext_data *drv, const char *ev, size_t len)
  315. {
  316. const struct iw_pmkid_cand *cand;
  317. union wpa_event_data data;
  318. const u8 *addr;
  319. if (len < sizeof(*cand))
  320. return -1;
  321. cand = (const struct iw_pmkid_cand *) ev;
  322. addr = (const u8 *) cand->bssid.sa_data;
  323. wpa_printf(MSG_DEBUG, "PMKID candidate wireless event: "
  324. "flags=0x%x index=%d bssid=" MACSTR, cand->flags,
  325. cand->index, MAC2STR(addr));
  326. os_memset(&data, 0, sizeof(data));
  327. os_memcpy(data.pmkid_candidate.bssid, addr, ETH_ALEN);
  328. data.pmkid_candidate.index = cand->index;
  329. data.pmkid_candidate.preauth = cand->flags & IW_PMKID_CAND_PREAUTH;
  330. wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data);
  331. return 0;
  332. }
  333. static int wpa_driver_wext_event_wireless_assocreqie(
  334. struct wpa_driver_wext_data *drv, const char *ev, int len)
  335. {
  336. if (len < 0)
  337. return -1;
  338. wpa_hexdump(MSG_DEBUG, "AssocReq IE wireless event", (const u8 *) ev,
  339. len);
  340. os_free(drv->assoc_req_ies);
  341. drv->assoc_req_ies = os_malloc(len);
  342. if (drv->assoc_req_ies == NULL) {
  343. drv->assoc_req_ies_len = 0;
  344. return -1;
  345. }
  346. os_memcpy(drv->assoc_req_ies, ev, len);
  347. drv->assoc_req_ies_len = len;
  348. return 0;
  349. }
  350. static int wpa_driver_wext_event_wireless_assocrespie(
  351. struct wpa_driver_wext_data *drv, const char *ev, int len)
  352. {
  353. if (len < 0)
  354. return -1;
  355. wpa_hexdump(MSG_DEBUG, "AssocResp IE wireless event", (const u8 *) ev,
  356. len);
  357. os_free(drv->assoc_resp_ies);
  358. drv->assoc_resp_ies = os_malloc(len);
  359. if (drv->assoc_resp_ies == NULL) {
  360. drv->assoc_resp_ies_len = 0;
  361. return -1;
  362. }
  363. os_memcpy(drv->assoc_resp_ies, ev, len);
  364. drv->assoc_resp_ies_len = len;
  365. return 0;
  366. }
  367. static void wpa_driver_wext_event_assoc_ies(struct wpa_driver_wext_data *drv)
  368. {
  369. union wpa_event_data data;
  370. if (drv->assoc_req_ies == NULL && drv->assoc_resp_ies == NULL)
  371. return;
  372. os_memset(&data, 0, sizeof(data));
  373. if (drv->assoc_req_ies) {
  374. data.assoc_info.req_ies = drv->assoc_req_ies;
  375. drv->assoc_req_ies = NULL;
  376. data.assoc_info.req_ies_len = drv->assoc_req_ies_len;
  377. }
  378. if (drv->assoc_resp_ies) {
  379. data.assoc_info.resp_ies = drv->assoc_resp_ies;
  380. drv->assoc_resp_ies = NULL;
  381. data.assoc_info.resp_ies_len = drv->assoc_resp_ies_len;
  382. }
  383. wpa_supplicant_event(drv->ctx, EVENT_ASSOCINFO, &data);
  384. os_free(data.assoc_info.req_ies);
  385. os_free(data.assoc_info.resp_ies);
  386. }
  387. static void wpa_driver_wext_event_wireless(struct wpa_driver_wext_data *drv,
  388. void *ctx, char *data, int len)
  389. {
  390. struct iw_event iwe_buf, *iwe = &iwe_buf;
  391. char *pos, *end, *custom, *buf;
  392. pos = data;
  393. end = data + len;
  394. while (pos + IW_EV_LCP_LEN <= end) {
  395. /* Event data may be unaligned, so make a local, aligned copy
  396. * before processing. */
  397. os_memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
  398. wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
  399. iwe->cmd, iwe->len);
  400. if (iwe->len <= IW_EV_LCP_LEN)
  401. return;
  402. custom = pos + IW_EV_POINT_LEN;
  403. if (drv->we_version_compiled > 18 &&
  404. (iwe->cmd == IWEVMICHAELMICFAILURE ||
  405. iwe->cmd == IWEVCUSTOM ||
  406. iwe->cmd == IWEVASSOCREQIE ||
  407. iwe->cmd == IWEVASSOCRESPIE ||
  408. iwe->cmd == IWEVPMKIDCAND)) {
  409. /* WE-19 removed the pointer from struct iw_point */
  410. char *dpos = (char *) &iwe_buf.u.data.length;
  411. int dlen = dpos - (char *) &iwe_buf;
  412. os_memcpy(dpos, pos + IW_EV_LCP_LEN,
  413. sizeof(struct iw_event) - dlen);
  414. } else {
  415. os_memcpy(&iwe_buf, pos, sizeof(struct iw_event));
  416. custom += IW_EV_POINT_OFF;
  417. }
  418. switch (iwe->cmd) {
  419. case SIOCGIWAP:
  420. wpa_printf(MSG_DEBUG, "Wireless event: new AP: "
  421. MACSTR,
  422. MAC2STR((u8 *) iwe->u.ap_addr.sa_data));
  423. if (is_zero_ether_addr(
  424. (const u8 *) iwe->u.ap_addr.sa_data) ||
  425. os_memcmp(iwe->u.ap_addr.sa_data,
  426. "\x44\x44\x44\x44\x44\x44", ETH_ALEN) ==
  427. 0) {
  428. os_free(drv->assoc_req_ies);
  429. drv->assoc_req_ies = NULL;
  430. os_free(drv->assoc_resp_ies);
  431. drv->assoc_resp_ies = NULL;
  432. wpa_supplicant_event(ctx, EVENT_DISASSOC,
  433. NULL);
  434. } else {
  435. wpa_driver_wext_event_assoc_ies(drv);
  436. wpa_supplicant_event(ctx, EVENT_ASSOC, NULL);
  437. }
  438. break;
  439. case IWEVMICHAELMICFAILURE:
  440. if (custom + iwe->u.data.length > end) {
  441. wpa_printf(MSG_DEBUG, "WEXT: Invalid "
  442. "IWEVMICHAELMICFAILURE length");
  443. return;
  444. }
  445. wpa_driver_wext_event_wireless_michaelmicfailure(
  446. ctx, custom, iwe->u.data.length);
  447. break;
  448. case IWEVCUSTOM:
  449. if (custom + iwe->u.data.length > end) {
  450. wpa_printf(MSG_DEBUG, "WEXT: Invalid "
  451. "IWEVCUSTOM length");
  452. return;
  453. }
  454. buf = os_malloc(iwe->u.data.length + 1);
  455. if (buf == NULL)
  456. return;
  457. os_memcpy(buf, custom, iwe->u.data.length);
  458. buf[iwe->u.data.length] = '\0';
  459. wpa_driver_wext_event_wireless_custom(ctx, buf);
  460. os_free(buf);
  461. break;
  462. case SIOCGIWSCAN:
  463. drv->scan_complete_events = 1;
  464. eloop_cancel_timeout(wpa_driver_wext_scan_timeout,
  465. drv, ctx);
  466. wpa_supplicant_event(ctx, EVENT_SCAN_RESULTS, NULL);
  467. break;
  468. case IWEVASSOCREQIE:
  469. if (custom + iwe->u.data.length > end) {
  470. wpa_printf(MSG_DEBUG, "WEXT: Invalid "
  471. "IWEVASSOCREQIE length");
  472. return;
  473. }
  474. wpa_driver_wext_event_wireless_assocreqie(
  475. drv, custom, iwe->u.data.length);
  476. break;
  477. case IWEVASSOCRESPIE:
  478. if (custom + iwe->u.data.length > end) {
  479. wpa_printf(MSG_DEBUG, "WEXT: Invalid "
  480. "IWEVASSOCRESPIE length");
  481. return;
  482. }
  483. wpa_driver_wext_event_wireless_assocrespie(
  484. drv, custom, iwe->u.data.length);
  485. break;
  486. case IWEVPMKIDCAND:
  487. if (custom + iwe->u.data.length > end) {
  488. wpa_printf(MSG_DEBUG, "WEXT: Invalid "
  489. "IWEVPMKIDCAND length");
  490. return;
  491. }
  492. wpa_driver_wext_event_wireless_pmkidcand(
  493. drv, custom, iwe->u.data.length);
  494. break;
  495. }
  496. pos += iwe->len;
  497. }
  498. }
  499. static void wpa_driver_wext_event_link(struct wpa_driver_wext_data *drv,
  500. void *ctx, char *buf, size_t len,
  501. int del)
  502. {
  503. union wpa_event_data event;
  504. os_memset(&event, 0, sizeof(event));
  505. if (len > sizeof(event.interface_status.ifname))
  506. len = sizeof(event.interface_status.ifname) - 1;
  507. os_memcpy(event.interface_status.ifname, buf, len);
  508. event.interface_status.ievent = del ? EVENT_INTERFACE_REMOVED :
  509. EVENT_INTERFACE_ADDED;
  510. wpa_printf(MSG_DEBUG, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s",
  511. del ? "DEL" : "NEW",
  512. event.interface_status.ifname,
  513. del ? "removed" : "added");
  514. if (os_strcmp(drv->ifname, event.interface_status.ifname) == 0) {
  515. if (del)
  516. drv->if_removed = 1;
  517. else
  518. drv->if_removed = 0;
  519. }
  520. wpa_supplicant_event(ctx, EVENT_INTERFACE_STATUS, &event);
  521. }
  522. static int wpa_driver_wext_own_ifname(struct wpa_driver_wext_data *drv,
  523. struct nlmsghdr *h)
  524. {
  525. struct ifinfomsg *ifi;
  526. int attrlen, nlmsg_len, rta_len;
  527. struct rtattr *attr;
  528. ifi = NLMSG_DATA(h);
  529. nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
  530. attrlen = h->nlmsg_len - nlmsg_len;
  531. if (attrlen < 0)
  532. return 0;
  533. attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
  534. rta_len = RTA_ALIGN(sizeof(struct rtattr));
  535. while (RTA_OK(attr, attrlen)) {
  536. if (attr->rta_type == IFLA_IFNAME) {
  537. if (os_strcmp(((char *) attr) + rta_len, drv->ifname)
  538. == 0)
  539. return 1;
  540. else
  541. break;
  542. }
  543. attr = RTA_NEXT(attr, attrlen);
  544. }
  545. return 0;
  546. }
  547. static int wpa_driver_wext_own_ifindex(struct wpa_driver_wext_data *drv,
  548. int ifindex, struct nlmsghdr *h)
  549. {
  550. if (drv->ifindex == ifindex || drv->ifindex2 == ifindex)
  551. return 1;
  552. if (drv->if_removed && wpa_driver_wext_own_ifname(drv, h)) {
  553. drv->ifindex = if_nametoindex(drv->ifname);
  554. wpa_printf(MSG_DEBUG, "WEXT: Update ifindex for a removed "
  555. "interface");
  556. wpa_driver_wext_finish_drv_init(drv);
  557. return 1;
  558. }
  559. return 0;
  560. }
  561. static void wpa_driver_wext_event_rtm_newlink(struct wpa_driver_wext_data *drv,
  562. void *ctx, struct nlmsghdr *h,
  563. size_t len)
  564. {
  565. struct ifinfomsg *ifi;
  566. int attrlen, nlmsg_len, rta_len;
  567. struct rtattr * attr;
  568. if (len < sizeof(*ifi))
  569. return;
  570. ifi = NLMSG_DATA(h);
  571. if (!wpa_driver_wext_own_ifindex(drv, ifi->ifi_index, h)) {
  572. wpa_printf(MSG_DEBUG, "Ignore event for foreign ifindex %d",
  573. ifi->ifi_index);
  574. return;
  575. }
  576. wpa_printf(MSG_DEBUG, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x "
  577. "(%s%s%s%s)",
  578. drv->operstate, ifi->ifi_flags,
  579. (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
  580. (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
  581. (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
  582. (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
  583. /*
  584. * Some drivers send the association event before the operup event--in
  585. * this case, lifting operstate in wpa_driver_wext_set_operstate()
  586. * fails. This will hit us when wpa_supplicant does not need to do
  587. * IEEE 802.1X authentication
  588. */
  589. if (drv->operstate == 1 &&
  590. (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
  591. !(ifi->ifi_flags & IFF_RUNNING))
  592. wpa_driver_wext_send_oper_ifla(drv, -1, IF_OPER_UP);
  593. nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
  594. attrlen = h->nlmsg_len - nlmsg_len;
  595. if (attrlen < 0)
  596. return;
  597. attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
  598. rta_len = RTA_ALIGN(sizeof(struct rtattr));
  599. while (RTA_OK(attr, attrlen)) {
  600. if (attr->rta_type == IFLA_WIRELESS) {
  601. wpa_driver_wext_event_wireless(
  602. drv, ctx, ((char *) attr) + rta_len,
  603. attr->rta_len - rta_len);
  604. } else if (attr->rta_type == IFLA_IFNAME) {
  605. wpa_driver_wext_event_link(drv, ctx,
  606. ((char *) attr) + rta_len,
  607. attr->rta_len - rta_len, 0);
  608. }
  609. attr = RTA_NEXT(attr, attrlen);
  610. }
  611. }
  612. static void wpa_driver_wext_event_rtm_dellink(struct wpa_driver_wext_data *drv,
  613. void *ctx, struct nlmsghdr *h,
  614. size_t len)
  615. {
  616. struct ifinfomsg *ifi;
  617. int attrlen, nlmsg_len, rta_len;
  618. struct rtattr * attr;
  619. if (len < sizeof(*ifi))
  620. return;
  621. ifi = NLMSG_DATA(h);
  622. nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
  623. attrlen = h->nlmsg_len - nlmsg_len;
  624. if (attrlen < 0)
  625. return;
  626. attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
  627. rta_len = RTA_ALIGN(sizeof(struct rtattr));
  628. while (RTA_OK(attr, attrlen)) {
  629. if (attr->rta_type == IFLA_IFNAME) {
  630. wpa_driver_wext_event_link(drv, ctx,
  631. ((char *) attr) + rta_len,
  632. attr->rta_len - rta_len, 1);
  633. }
  634. attr = RTA_NEXT(attr, attrlen);
  635. }
  636. }
  637. static void wpa_driver_wext_event_receive(int sock, void *eloop_ctx,
  638. void *sock_ctx)
  639. {
  640. char buf[8192];
  641. int left;
  642. struct sockaddr_nl from;
  643. socklen_t fromlen;
  644. struct nlmsghdr *h;
  645. int max_events = 10;
  646. try_again:
  647. fromlen = sizeof(from);
  648. left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
  649. (struct sockaddr *) &from, &fromlen);
  650. if (left < 0) {
  651. if (errno != EINTR && errno != EAGAIN)
  652. perror("recvfrom(netlink)");
  653. return;
  654. }
  655. h = (struct nlmsghdr *) buf;
  656. while (left >= (int) sizeof(*h)) {
  657. int len, plen;
  658. len = h->nlmsg_len;
  659. plen = len - sizeof(*h);
  660. if (len > left || plen < 0) {
  661. wpa_printf(MSG_DEBUG, "Malformed netlink message: "
  662. "len=%d left=%d plen=%d",
  663. len, left, plen);
  664. break;
  665. }
  666. switch (h->nlmsg_type) {
  667. case RTM_NEWLINK:
  668. wpa_driver_wext_event_rtm_newlink(eloop_ctx, sock_ctx,
  669. h, plen);
  670. break;
  671. case RTM_DELLINK:
  672. wpa_driver_wext_event_rtm_dellink(eloop_ctx, sock_ctx,
  673. h, plen);
  674. break;
  675. }
  676. len = NLMSG_ALIGN(len);
  677. left -= len;
  678. h = (struct nlmsghdr *) ((char *) h + len);
  679. }
  680. if (left > 0) {
  681. wpa_printf(MSG_DEBUG, "%d extra bytes in the end of netlink "
  682. "message", left);
  683. }
  684. if (--max_events > 0) {
  685. /*
  686. * Try to receive all events in one eloop call in order to
  687. * limit race condition on cases where AssocInfo event, Assoc
  688. * event, and EAPOL frames are received more or less at the
  689. * same time. We want to process the event messages first
  690. * before starting EAPOL processing.
  691. */
  692. goto try_again;
  693. }
  694. }
  695. static int wpa_driver_wext_get_ifflags_ifname(struct wpa_driver_wext_data *drv,
  696. const char *ifname, int *flags)
  697. {
  698. struct ifreq ifr;
  699. os_memset(&ifr, 0, sizeof(ifr));
  700. os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
  701. if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
  702. perror("ioctl[SIOCGIFFLAGS]");
  703. return -1;
  704. }
  705. *flags = ifr.ifr_flags & 0xffff;
  706. return 0;
  707. }
  708. /**
  709. * wpa_driver_wext_get_ifflags - Get interface flags (SIOCGIFFLAGS)
  710. * @drv: driver_wext private data
  711. * @flags: Pointer to returned flags value
  712. * Returns: 0 on success, -1 on failure
  713. */
  714. int wpa_driver_wext_get_ifflags(struct wpa_driver_wext_data *drv, int *flags)
  715. {
  716. return wpa_driver_wext_get_ifflags_ifname(drv, drv->ifname, flags);
  717. }
  718. static int wpa_driver_wext_set_ifflags_ifname(struct wpa_driver_wext_data *drv,
  719. const char *ifname, int flags)
  720. {
  721. struct ifreq ifr;
  722. os_memset(&ifr, 0, sizeof(ifr));
  723. os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
  724. ifr.ifr_flags = flags & 0xffff;
  725. if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
  726. perror("SIOCSIFFLAGS");
  727. return -1;
  728. }
  729. return 0;
  730. }
  731. /**
  732. * wpa_driver_wext_set_ifflags - Set interface flags (SIOCSIFFLAGS)
  733. * @drv: driver_wext private data
  734. * @flags: New value for flags
  735. * Returns: 0 on success, -1 on failure
  736. */
  737. int wpa_driver_wext_set_ifflags(struct wpa_driver_wext_data *drv, int flags)
  738. {
  739. return wpa_driver_wext_set_ifflags_ifname(drv, drv->ifname, flags);
  740. }
  741. /**
  742. * wpa_driver_wext_init - Initialize WE driver interface
  743. * @ctx: context to be used when calling wpa_supplicant functions,
  744. * e.g., wpa_supplicant_event()
  745. * @ifname: interface name, e.g., wlan0
  746. * Returns: Pointer to private data, %NULL on failure
  747. */
  748. void * wpa_driver_wext_init(void *ctx, const char *ifname)
  749. {
  750. int s;
  751. struct sockaddr_nl local;
  752. struct wpa_driver_wext_data *drv;
  753. drv = os_zalloc(sizeof(*drv));
  754. if (drv == NULL)
  755. return NULL;
  756. drv->ctx = ctx;
  757. os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname));
  758. drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
  759. if (drv->ioctl_sock < 0) {
  760. perror("socket(PF_INET,SOCK_DGRAM)");
  761. os_free(drv);
  762. return NULL;
  763. }
  764. s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  765. if (s < 0) {
  766. perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
  767. close(drv->ioctl_sock);
  768. os_free(drv);
  769. return NULL;
  770. }
  771. os_memset(&local, 0, sizeof(local));
  772. local.nl_family = AF_NETLINK;
  773. local.nl_groups = RTMGRP_LINK;
  774. if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
  775. perror("bind(netlink)");
  776. close(s);
  777. close(drv->ioctl_sock);
  778. os_free(drv);
  779. return NULL;
  780. }
  781. eloop_register_read_sock(s, wpa_driver_wext_event_receive, drv, ctx);
  782. drv->event_sock = s;
  783. drv->mlme_sock = -1;
  784. wpa_driver_wext_finish_drv_init(drv);
  785. return drv;
  786. }
  787. static void wpa_driver_wext_finish_drv_init(struct wpa_driver_wext_data *drv)
  788. {
  789. int flags;
  790. if (wpa_driver_wext_get_ifflags(drv, &flags) != 0)
  791. printf("Could not get interface '%s' flags\n", drv->ifname);
  792. else if (!(flags & IFF_UP)) {
  793. if (wpa_driver_wext_set_ifflags(drv, flags | IFF_UP) != 0) {
  794. printf("Could not set interface '%s' UP\n",
  795. drv->ifname);
  796. } else {
  797. /*
  798. * Wait some time to allow driver to initialize before
  799. * starting configuring the driver. This seems to be
  800. * needed at least some drivers that load firmware etc.
  801. * when the interface is set up.
  802. */
  803. wpa_printf(MSG_DEBUG, "Interface %s set UP - waiting "
  804. "a second for the driver to complete "
  805. "initialization", drv->ifname);
  806. sleep(1);
  807. }
  808. }
  809. /*
  810. * Make sure that the driver does not have any obsolete PMKID entries.
  811. */
  812. wpa_driver_wext_flush_pmkid(drv);
  813. if (wpa_driver_wext_set_mode(drv, 0) < 0) {
  814. printf("Could not configure driver to use managed mode\n");
  815. }
  816. wpa_driver_wext_get_range(drv);
  817. drv->ifindex = if_nametoindex(drv->ifname);
  818. if (os_strncmp(drv->ifname, "wlan", 4) == 0) {
  819. /*
  820. * Host AP driver may use both wlan# and wifi# interface in
  821. * wireless events. Since some of the versions included WE-18
  822. * support, let's add the alternative ifindex also from
  823. * driver_wext.c for the time being. This may be removed at
  824. * some point once it is believed that old versions of the
  825. * driver are not in use anymore.
  826. */
  827. char ifname2[IFNAMSIZ + 1];
  828. os_strlcpy(ifname2, drv->ifname, sizeof(ifname2));
  829. os_memcpy(ifname2, "wifi", 4);
  830. wpa_driver_wext_alternative_ifindex(drv, ifname2);
  831. }
  832. wpa_driver_wext_send_oper_ifla(drv, 1, IF_OPER_DORMANT);
  833. }
  834. /**
  835. * wpa_driver_wext_deinit - Deinitialize WE driver interface
  836. * @priv: Pointer to private wext data from wpa_driver_wext_init()
  837. *
  838. * Shut down driver interface and processing of driver events. Free
  839. * private data buffer if one was allocated in wpa_driver_wext_init().
  840. */
  841. void wpa_driver_wext_deinit(void *priv)
  842. {
  843. struct wpa_driver_wext_data *drv = priv;
  844. int flags;
  845. eloop_cancel_timeout(wpa_driver_wext_scan_timeout, drv, drv->ctx);
  846. /*
  847. * Clear possibly configured driver parameters in order to make it
  848. * easier to use the driver after wpa_supplicant has been terminated.
  849. */
  850. (void) wpa_driver_wext_set_bssid(drv,
  851. (u8 *) "\x00\x00\x00\x00\x00\x00");
  852. wpa_driver_wext_send_oper_ifla(priv, 0, IF_OPER_UP);
  853. eloop_unregister_read_sock(drv->event_sock);
  854. if (drv->mlme_sock >= 0)
  855. eloop_unregister_read_sock(drv->mlme_sock);
  856. if (wpa_driver_wext_get_ifflags(drv, &flags) == 0)
  857. (void) wpa_driver_wext_set_ifflags(drv, flags & ~IFF_UP);
  858. close(drv->event_sock);
  859. close(drv->ioctl_sock);
  860. if (drv->mlme_sock >= 0)
  861. close(drv->mlme_sock);
  862. os_free(drv->assoc_req_ies);
  863. os_free(drv->assoc_resp_ies);
  864. os_free(drv);
  865. }
  866. /**
  867. * wpa_driver_wext_scan_timeout - Scan timeout to report scan completion
  868. * @eloop_ctx: Unused
  869. * @timeout_ctx: ctx argument given to wpa_driver_wext_init()
  870. *
  871. * This function can be used as registered timeout when starting a scan to
  872. * generate a scan completed event if the driver does not report this.
  873. */
  874. void wpa_driver_wext_scan_timeout(void *eloop_ctx, void *timeout_ctx)
  875. {
  876. wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
  877. wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
  878. }
  879. /**
  880. * wpa_driver_wext_scan - Request the driver to initiate scan
  881. * @priv: Pointer to private wext data from wpa_driver_wext_init()
  882. * @ssid: Specific SSID to scan for (ProbeReq) or %NULL to scan for
  883. * all SSIDs (either active scan with broadcast SSID or passive
  884. * scan
  885. * @ssid_len: Length of the SSID
  886. * Returns: 0 on success, -1 on failure
  887. */
  888. int wpa_driver_wext_scan(void *priv, const u8 *ssid, size_t ssid_len)
  889. {
  890. struct wpa_driver_wext_data *drv = priv;
  891. struct iwreq iwr;
  892. int ret = 0, timeout;
  893. struct iw_scan_req req;
  894. if (ssid_len > IW_ESSID_MAX_SIZE) {
  895. wpa_printf(MSG_DEBUG, "%s: too long SSID (%lu)",
  896. __FUNCTION__, (unsigned long) ssid_len);
  897. return -1;
  898. }
  899. os_memset(&iwr, 0, sizeof(iwr));
  900. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  901. if (ssid && ssid_len) {
  902. os_memset(&req, 0, sizeof(req));
  903. req.essid_len = ssid_len;
  904. req.bssid.sa_family = ARPHRD_ETHER;
  905. os_memset(req.bssid.sa_data, 0xff, ETH_ALEN);
  906. os_memcpy(req.essid, ssid, ssid_len);
  907. iwr.u.data.pointer = (caddr_t) &req;
  908. iwr.u.data.length = sizeof(req);
  909. iwr.u.data.flags = IW_SCAN_THIS_ESSID;
  910. }
  911. if (ioctl(drv->ioctl_sock, SIOCSIWSCAN, &iwr) < 0) {
  912. perror("ioctl[SIOCSIWSCAN]");
  913. ret = -1;
  914. }
  915. /* Not all drivers generate "scan completed" wireless event, so try to
  916. * read results after a timeout. */
  917. timeout = 5;
  918. if (drv->scan_complete_events) {
  919. /*
  920. * The driver seems to deliver SIOCGIWSCAN events to notify
  921. * when scan is complete, so use longer timeout to avoid race
  922. * conditions with scanning and following association request.
  923. */
  924. timeout = 30;
  925. }
  926. wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
  927. "seconds", ret, timeout);
  928. eloop_cancel_timeout(wpa_driver_wext_scan_timeout, drv, drv->ctx);
  929. eloop_register_timeout(timeout, 0, wpa_driver_wext_scan_timeout, drv,
  930. drv->ctx);
  931. return ret;
  932. }
  933. static u8 * wpa_driver_wext_giwscan(struct wpa_driver_wext_data *drv,
  934. size_t *len)
  935. {
  936. struct iwreq iwr;
  937. u8 *res_buf;
  938. size_t res_buf_len;
  939. res_buf_len = IW_SCAN_MAX_DATA;
  940. for (;;) {
  941. res_buf = os_malloc(res_buf_len);
  942. if (res_buf == NULL)
  943. return NULL;
  944. os_memset(&iwr, 0, sizeof(iwr));
  945. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  946. iwr.u.data.pointer = res_buf;
  947. iwr.u.data.length = res_buf_len;
  948. if (ioctl(drv->ioctl_sock, SIOCGIWSCAN, &iwr) == 0)
  949. break;
  950. if (errno == E2BIG && res_buf_len < 65535) {
  951. os_free(res_buf);
  952. res_buf = NULL;
  953. res_buf_len *= 2;
  954. if (res_buf_len > 65535)
  955. res_buf_len = 65535; /* 16-bit length field */
  956. wpa_printf(MSG_DEBUG, "Scan results did not fit - "
  957. "trying larger buffer (%lu bytes)",
  958. (unsigned long) res_buf_len);
  959. } else {
  960. perror("ioctl[SIOCGIWSCAN]");
  961. os_free(res_buf);
  962. return NULL;
  963. }
  964. }
  965. if (iwr.u.data.length > res_buf_len) {
  966. os_free(res_buf);
  967. return NULL;
  968. }
  969. *len = iwr.u.data.length;
  970. return res_buf;
  971. }
  972. /*
  973. * Data structure for collecting WEXT scan results. This is needed to allow
  974. * the various methods of reporting IEs to be combined into a single IE buffer.
  975. */
  976. struct wext_scan_data {
  977. struct wpa_scan_res res;
  978. u8 *ie;
  979. size_t ie_len;
  980. u8 ssid[32];
  981. size_t ssid_len;
  982. int maxrate;
  983. };
  984. static void wext_get_scan_mode(struct iw_event *iwe,
  985. struct wext_scan_data *res)
  986. {
  987. if (iwe->u.mode == IW_MODE_ADHOC)
  988. res->res.caps |= IEEE80211_CAP_IBSS;
  989. else if (iwe->u.mode == IW_MODE_MASTER || iwe->u.mode == IW_MODE_INFRA)
  990. res->res.caps |= IEEE80211_CAP_ESS;
  991. }
  992. static void wext_get_scan_ssid(struct iw_event *iwe,
  993. struct wext_scan_data *res, char *custom,
  994. char *end)
  995. {
  996. int ssid_len = iwe->u.essid.length;
  997. if (custom + ssid_len > end)
  998. return;
  999. if (iwe->u.essid.flags &&
  1000. ssid_len > 0 &&
  1001. ssid_len <= IW_ESSID_MAX_SIZE) {
  1002. os_memcpy(res->ssid, custom, ssid_len);
  1003. res->ssid_len = ssid_len;
  1004. }
  1005. }
  1006. static void wext_get_scan_freq(struct iw_event *iwe,
  1007. struct wext_scan_data *res)
  1008. {
  1009. int divi = 1000000, i;
  1010. if (iwe->u.freq.e == 0) {
  1011. /*
  1012. * Some drivers do not report frequency, but a channel.
  1013. * Try to map this to frequency by assuming they are using
  1014. * IEEE 802.11b/g. But don't overwrite a previously parsed
  1015. * frequency if the driver sends both frequency and channel,
  1016. * since the driver may be sending an A-band channel that we
  1017. * don't handle here.
  1018. */
  1019. if (res->res.freq)
  1020. return;
  1021. if (iwe->u.freq.m >= 1 && iwe->u.freq.m <= 13) {
  1022. res->res.freq = 2407 + 5 * iwe->u.freq.m;
  1023. return;
  1024. } else if (iwe->u.freq.m == 14) {
  1025. res->res.freq = 2484;
  1026. return;
  1027. }
  1028. }
  1029. if (iwe->u.freq.e > 6) {
  1030. wpa_printf(MSG_DEBUG, "Invalid freq in scan results (BSSID="
  1031. MACSTR " m=%d e=%d)",
  1032. MAC2STR(res->res.bssid), iwe->u.freq.m,
  1033. iwe->u.freq.e);
  1034. return;
  1035. }
  1036. for (i = 0; i < iwe->u.freq.e; i++)
  1037. divi /= 10;
  1038. res->res.freq = iwe->u.freq.m / divi;
  1039. }
  1040. static void wext_get_scan_qual(struct iw_event *iwe,
  1041. struct wext_scan_data *res)
  1042. {
  1043. res->res.qual = iwe->u.qual.qual;
  1044. res->res.noise = iwe->u.qual.noise;
  1045. res->res.level = iwe->u.qual.level;
  1046. }
  1047. static void wext_get_scan_encode(struct iw_event *iwe,
  1048. struct wext_scan_data *res)
  1049. {
  1050. if (!(iwe->u.data.flags & IW_ENCODE_DISABLED))
  1051. res->res.caps |= IEEE80211_CAP_PRIVACY;
  1052. }
  1053. static void wext_get_scan_rate(struct iw_event *iwe,
  1054. struct wext_scan_data *res, char *pos,
  1055. char *end)
  1056. {
  1057. int maxrate;
  1058. char *custom = pos + IW_EV_LCP_LEN;
  1059. struct iw_param p;
  1060. size_t clen;
  1061. clen = iwe->len;
  1062. if (custom + clen > end)
  1063. return;
  1064. maxrate = 0;
  1065. while (((ssize_t) clen) >= (ssize_t) sizeof(struct iw_param)) {
  1066. /* Note: may be misaligned, make a local, aligned copy */
  1067. os_memcpy(&p, custom, sizeof(struct iw_param));
  1068. if (p.value > maxrate)
  1069. maxrate = p.value;
  1070. clen -= sizeof(struct iw_param);
  1071. custom += sizeof(struct iw_param);
  1072. }
  1073. /* Convert the maxrate from WE-style (b/s units) to
  1074. * 802.11 rates (500000 b/s units).
  1075. */
  1076. res->maxrate = maxrate / 500000;
  1077. }
  1078. static void wext_get_scan_iwevgenie(struct iw_event *iwe,
  1079. struct wext_scan_data *res, char *custom,
  1080. char *end)
  1081. {
  1082. char *genie, *gpos, *gend;
  1083. u8 *tmp;
  1084. if (iwe->u.data.length == 0)
  1085. return;
  1086. gpos = genie = custom;
  1087. gend = genie + iwe->u.data.length;
  1088. if (gend > end) {
  1089. wpa_printf(MSG_INFO, "IWEVGENIE overflow");
  1090. return;
  1091. }
  1092. tmp = os_realloc(res->ie, res->ie_len + gend - gpos);
  1093. if (tmp == NULL)
  1094. return;
  1095. os_memcpy(tmp + res->ie_len, gpos, gend - gpos);
  1096. res->ie = tmp;
  1097. res->ie_len += gend - gpos;
  1098. }
  1099. static void wext_get_scan_custom(struct iw_event *iwe,
  1100. struct wext_scan_data *res, char *custom,
  1101. char *end)
  1102. {
  1103. size_t clen;
  1104. u8 *tmp;
  1105. clen = iwe->u.data.length;
  1106. if (custom + clen > end)
  1107. return;
  1108. if (clen > 7 && os_strncmp(custom, "wpa_ie=", 7) == 0) {
  1109. char *spos;
  1110. int bytes;
  1111. spos = custom + 7;
  1112. bytes = custom + clen - spos;
  1113. if (bytes & 1 || bytes == 0)
  1114. return;
  1115. bytes /= 2;
  1116. tmp = os_realloc(res->ie, res->ie_len + bytes);
  1117. if (tmp == NULL)
  1118. return;
  1119. hexstr2bin(spos, tmp + res->ie_len, bytes);
  1120. res->ie = tmp;
  1121. res->ie_len += bytes;
  1122. } else if (clen > 7 && os_strncmp(custom, "rsn_ie=", 7) == 0) {
  1123. char *spos;
  1124. int bytes;
  1125. spos = custom + 7;
  1126. bytes = custom + clen - spos;
  1127. if (bytes & 1 || bytes == 0)
  1128. return;
  1129. bytes /= 2;
  1130. tmp = os_realloc(res->ie, res->ie_len + bytes);
  1131. if (tmp == NULL)
  1132. return;
  1133. hexstr2bin(spos, tmp + res->ie_len, bytes);
  1134. res->ie = tmp;
  1135. res->ie_len += bytes;
  1136. } else if (clen > 4 && os_strncmp(custom, "tsf=", 4) == 0) {
  1137. char *spos;
  1138. int bytes;
  1139. u8 bin[8];
  1140. spos = custom + 4;
  1141. bytes = custom + clen - spos;
  1142. if (bytes != 16) {
  1143. wpa_printf(MSG_INFO, "Invalid TSF length (%d)", bytes);
  1144. return;
  1145. }
  1146. bytes /= 2;
  1147. hexstr2bin(spos, bin, bytes);
  1148. res->res.tsf += WPA_GET_BE64(bin);
  1149. }
  1150. }
  1151. static int wext_19_iw_point(struct wpa_driver_wext_data *drv, u16 cmd)
  1152. {
  1153. return drv->we_version_compiled > 18 &&
  1154. (cmd == SIOCGIWESSID || cmd == SIOCGIWENCODE ||
  1155. cmd == IWEVGENIE || cmd == IWEVCUSTOM);
  1156. }
  1157. static void wpa_driver_wext_add_scan_entry(struct wpa_scan_results *res,
  1158. struct wext_scan_data *data)
  1159. {
  1160. struct wpa_scan_res **tmp;
  1161. struct wpa_scan_res *r;
  1162. size_t extra_len;
  1163. u8 *pos, *end, *ssid_ie = NULL, *rate_ie = NULL;
  1164. /* Figure out whether we need to fake any IEs */
  1165. pos = data->ie;
  1166. end = pos + data->ie_len;
  1167. while (pos && pos + 1 < end) {
  1168. if (pos + 2 + pos[1] > end)
  1169. break;
  1170. if (pos[0] == WLAN_EID_SSID)
  1171. ssid_ie = pos;
  1172. else if (pos[0] == WLAN_EID_SUPP_RATES)
  1173. rate_ie = pos;
  1174. else if (pos[0] == WLAN_EID_EXT_SUPP_RATES)
  1175. rate_ie = pos;
  1176. pos += 2 + pos[1];
  1177. }
  1178. extra_len = 0;
  1179. if (ssid_ie == NULL)
  1180. extra_len += 2 + data->ssid_len;
  1181. if (rate_ie == NULL && data->maxrate)
  1182. extra_len += 3;
  1183. r = os_zalloc(sizeof(*r) + extra_len + data->ie_len);
  1184. if (r == NULL)
  1185. return;
  1186. os_memcpy(r, &data->res, sizeof(*r));
  1187. r->ie_len = extra_len + data->ie_len;
  1188. pos = (u8 *) (r + 1);
  1189. if (ssid_ie == NULL) {
  1190. /*
  1191. * Generate a fake SSID IE since the driver did not report
  1192. * a full IE list.
  1193. */
  1194. *pos++ = WLAN_EID_SSID;
  1195. *pos++ = data->ssid_len;
  1196. os_memcpy(pos, data->ssid, data->ssid_len);
  1197. pos += data->ssid_len;
  1198. }
  1199. if (rate_ie == NULL && data->maxrate) {
  1200. /*
  1201. * Generate a fake Supported Rates IE since the driver did not
  1202. * report a full IE list.
  1203. */
  1204. *pos++ = WLAN_EID_SUPP_RATES;
  1205. *pos++ = 1;
  1206. *pos++ = data->maxrate;
  1207. }
  1208. if (data->ie)
  1209. os_memcpy(pos, data->ie, data->ie_len);
  1210. tmp = os_realloc(res->res,
  1211. (res->num + 1) * sizeof(struct wpa_scan_res *));
  1212. if (tmp == NULL) {
  1213. os_free(r);
  1214. return;
  1215. }
  1216. tmp[res->num++] = r;
  1217. res->res = tmp;
  1218. }
  1219. /**
  1220. * wpa_driver_wext_get_scan_results - Fetch the latest scan results
  1221. * @priv: Pointer to private wext data from wpa_driver_wext_init()
  1222. * Returns: Scan results on success, -1 on failure
  1223. */
  1224. struct wpa_scan_results * wpa_driver_wext_get_scan_results(void *priv)
  1225. {
  1226. struct wpa_driver_wext_data *drv = priv;
  1227. size_t ap_num = 0, len;
  1228. int first;
  1229. u8 *res_buf;
  1230. struct iw_event iwe_buf, *iwe = &iwe_buf;
  1231. char *pos, *end, *custom;
  1232. struct wpa_scan_results *res;
  1233. struct wext_scan_data data;
  1234. res_buf = wpa_driver_wext_giwscan(drv, &len);
  1235. if (res_buf == NULL)
  1236. return NULL;
  1237. ap_num = 0;
  1238. first = 1;
  1239. res = os_zalloc(sizeof(*res));
  1240. if (res == NULL) {
  1241. os_free(res_buf);
  1242. return NULL;
  1243. }
  1244. pos = (char *) res_buf;
  1245. end = (char *) res_buf + len;
  1246. os_memset(&data, 0, sizeof(data));
  1247. while (pos + IW_EV_LCP_LEN <= end) {
  1248. /* Event data may be unaligned, so make a local, aligned copy
  1249. * before processing. */
  1250. os_memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
  1251. if (iwe->len <= IW_EV_LCP_LEN)
  1252. break;
  1253. custom = pos + IW_EV_POINT_LEN;
  1254. if (wext_19_iw_point(drv, iwe->cmd)) {
  1255. /* WE-19 removed the pointer from struct iw_point */
  1256. char *dpos = (char *) &iwe_buf.u.data.length;
  1257. int dlen = dpos - (char *) &iwe_buf;
  1258. os_memcpy(dpos, pos + IW_EV_LCP_LEN,
  1259. sizeof(struct iw_event) - dlen);
  1260. } else {
  1261. os_memcpy(&iwe_buf, pos, sizeof(struct iw_event));
  1262. custom += IW_EV_POINT_OFF;
  1263. }
  1264. switch (iwe->cmd) {
  1265. case SIOCGIWAP:
  1266. if (!first)
  1267. wpa_driver_wext_add_scan_entry(res, &data);
  1268. first = 0;
  1269. os_free(data.ie);
  1270. os_memset(&data, 0, sizeof(data));
  1271. os_memcpy(data.res.bssid,
  1272. iwe->u.ap_addr.sa_data, ETH_ALEN);
  1273. break;
  1274. case SIOCGIWMODE:
  1275. wext_get_scan_mode(iwe, &data);
  1276. break;
  1277. case SIOCGIWESSID:
  1278. wext_get_scan_ssid(iwe, &data, custom, end);
  1279. break;
  1280. case SIOCGIWFREQ:
  1281. wext_get_scan_freq(iwe, &data);
  1282. break;
  1283. case IWEVQUAL:
  1284. wext_get_scan_qual(iwe, &data);
  1285. break;
  1286. case SIOCGIWENCODE:
  1287. wext_get_scan_encode(iwe, &data);
  1288. break;
  1289. case SIOCGIWRATE:
  1290. wext_get_scan_rate(iwe, &data, pos, end);
  1291. break;
  1292. case IWEVGENIE:
  1293. wext_get_scan_iwevgenie(iwe, &data, custom, end);
  1294. break;
  1295. case IWEVCUSTOM:
  1296. wext_get_scan_custom(iwe, &data, custom, end);
  1297. break;
  1298. }
  1299. pos += iwe->len;
  1300. }
  1301. os_free(res_buf);
  1302. res_buf = NULL;
  1303. if (!first)
  1304. wpa_driver_wext_add_scan_entry(res, &data);
  1305. os_free(data.ie);
  1306. wpa_printf(MSG_DEBUG, "Received %lu bytes of scan results (%lu BSSes)",
  1307. (unsigned long) len, (unsigned long) res->num);
  1308. return res;
  1309. }
  1310. static int wpa_driver_wext_get_range(void *priv)
  1311. {
  1312. struct wpa_driver_wext_data *drv = priv;
  1313. struct iw_range *range;
  1314. struct iwreq iwr;
  1315. int minlen;
  1316. size_t buflen;
  1317. /*
  1318. * Use larger buffer than struct iw_range in order to allow the
  1319. * structure to grow in the future.
  1320. */
  1321. buflen = sizeof(struct iw_range) + 500;
  1322. range = os_zalloc(buflen);
  1323. if (range == NULL)
  1324. return -1;
  1325. os_memset(&iwr, 0, sizeof(iwr));
  1326. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  1327. iwr.u.data.pointer = (caddr_t) range;
  1328. iwr.u.data.length = buflen;
  1329. minlen = ((char *) &range->enc_capa) - (char *) range +
  1330. sizeof(range->enc_capa);
  1331. if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
  1332. perror("ioctl[SIOCGIWRANGE]");
  1333. os_free(range);
  1334. return -1;
  1335. } else if (iwr.u.data.length >= minlen &&
  1336. range->we_version_compiled >= 18) {
  1337. wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
  1338. "WE(source)=%d enc_capa=0x%x",
  1339. range->we_version_compiled,
  1340. range->we_version_source,
  1341. range->enc_capa);
  1342. drv->has_capability = 1;
  1343. drv->we_version_compiled = range->we_version_compiled;
  1344. if (range->enc_capa & IW_ENC_CAPA_WPA) {
  1345. drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  1346. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK;
  1347. }
  1348. if (range->enc_capa & IW_ENC_CAPA_WPA2) {
  1349. drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  1350. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
  1351. }
  1352. drv->capa.enc |= WPA_DRIVER_CAPA_ENC_WEP40 |
  1353. WPA_DRIVER_CAPA_ENC_WEP104;
  1354. if (range->enc_capa & IW_ENC_CAPA_CIPHER_TKIP)
  1355. drv->capa.enc |= WPA_DRIVER_CAPA_ENC_TKIP;
  1356. if (range->enc_capa & IW_ENC_CAPA_CIPHER_CCMP)
  1357. drv->capa.enc |= WPA_DRIVER_CAPA_ENC_CCMP;
  1358. if (range->enc_capa & IW_ENC_CAPA_4WAY_HANDSHAKE)
  1359. drv->capa.flags |= WPA_DRIVER_FLAGS_4WAY_HANDSHAKE;
  1360. wpa_printf(MSG_DEBUG, " capabilities: key_mgmt 0x%x enc 0x%x "
  1361. "flags 0x%x",
  1362. drv->capa.key_mgmt, drv->capa.enc, drv->capa.flags);
  1363. } else {
  1364. wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: too old (short) data - "
  1365. "assuming WPA is not supported");
  1366. }
  1367. os_free(range);
  1368. return 0;
  1369. }
  1370. static int wpa_driver_wext_set_wpa(void *priv, int enabled)
  1371. {
  1372. struct wpa_driver_wext_data *drv = priv;
  1373. wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
  1374. return wpa_driver_wext_set_auth_param(drv, IW_AUTH_WPA_ENABLED,
  1375. enabled);
  1376. }
  1377. static int wpa_driver_wext_set_psk(struct wpa_driver_wext_data *drv,
  1378. const u8 *psk)
  1379. {
  1380. struct iw_encode_ext *ext;
  1381. struct iwreq iwr;
  1382. int ret;
  1383. wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
  1384. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
  1385. return 0;
  1386. if (!psk)
  1387. return 0;
  1388. os_memset(&iwr, 0, sizeof(iwr));
  1389. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  1390. ext = os_zalloc(sizeof(*ext) + PMK_LEN);
  1391. if (ext == NULL)
  1392. return -1;
  1393. iwr.u.encoding.pointer = (caddr_t) ext;
  1394. iwr.u.encoding.length = sizeof(*ext) + PMK_LEN;
  1395. ext->key_len = PMK_LEN;
  1396. os_memcpy(&ext->key, psk, ext->key_len);
  1397. ext->alg = IW_ENCODE_ALG_PMK;
  1398. ret = ioctl(drv->ioctl_sock, SIOCSIWENCODEEXT, &iwr);
  1399. if (ret < 0)
  1400. perror("ioctl[SIOCSIWENCODEEXT] PMK");
  1401. os_free(ext);
  1402. return ret;
  1403. }
  1404. static int wpa_driver_wext_set_key_ext(void *priv, wpa_alg alg,
  1405. const u8 *addr, int key_idx,
  1406. int set_tx, const u8 *seq,
  1407. size_t seq_len,
  1408. const u8 *key, size_t key_len)
  1409. {
  1410. struct wpa_driver_wext_data *drv = priv;
  1411. struct iwreq iwr;
  1412. int ret = 0;
  1413. struct iw_encode_ext *ext;
  1414. if (seq_len > IW_ENCODE_SEQ_MAX_SIZE) {
  1415. wpa_printf(MSG_DEBUG, "%s: Invalid seq_len %lu",
  1416. __FUNCTION__, (unsigned long) seq_len);
  1417. return -1;
  1418. }
  1419. ext = os_zalloc(sizeof(*ext) + key_len);
  1420. if (ext == NULL)
  1421. return -1;
  1422. os_memset(&iwr, 0, sizeof(iwr));
  1423. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  1424. iwr.u.encoding.flags = key_idx + 1;
  1425. iwr.u.encoding.flags |= IW_ENCODE_TEMP;
  1426. if (alg == WPA_ALG_NONE)
  1427. iwr.u.encoding.flags |= IW_ENCODE_DISABLED;
  1428. iwr.u.encoding.pointer = (caddr_t) ext;
  1429. iwr.u.encoding.length = sizeof(*ext) + key_len;
  1430. if (addr == NULL ||
  1431. os_memcmp(addr, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) == 0)
  1432. ext->ext_flags |= IW_ENCODE_EXT_GROUP_KEY;
  1433. if (set_tx)
  1434. ext->ext_flags |= IW_ENCODE_EXT_SET_TX_KEY;
  1435. ext->addr.sa_family = ARPHRD_ETHER;
  1436. if (addr)
  1437. os_memcpy(ext->addr.sa_data, addr, ETH_ALEN);
  1438. else
  1439. os_memset(ext->addr.sa_data, 0xff, ETH_ALEN);
  1440. if (key && key_len) {
  1441. os_memcpy(ext + 1, key, key_len);
  1442. ext->key_len = key_len;
  1443. }
  1444. switch (alg) {
  1445. case WPA_ALG_NONE:
  1446. ext->alg = IW_ENCODE_ALG_NONE;
  1447. break;
  1448. case WPA_ALG_WEP:
  1449. ext->alg = IW_ENCODE_ALG_WEP;
  1450. break;
  1451. case WPA_ALG_TKIP:
  1452. ext->alg = IW_ENCODE_ALG_TKIP;
  1453. break;
  1454. case WPA_ALG_CCMP:
  1455. ext->alg = IW_ENCODE_ALG_CCMP;
  1456. break;
  1457. case WPA_ALG_PMK:
  1458. ext->alg = IW_ENCODE_ALG_PMK;
  1459. break;
  1460. #ifdef CONFIG_IEEE80211W
  1461. case WPA_ALG_IGTK:
  1462. ext->alg = IW_ENCODE_ALG_AES_CMAC;
  1463. break;
  1464. #endif /* CONFIG_IEEE80211W */
  1465. default:
  1466. wpa_printf(MSG_DEBUG, "%s: Unknown algorithm %d",
  1467. __FUNCTION__, alg);
  1468. os_free(ext);
  1469. return -1;
  1470. }
  1471. if (seq && seq_len) {
  1472. ext->ext_flags |= IW_ENCODE_EXT_RX_SEQ_VALID;
  1473. os_memcpy(ext->rx_seq, seq, seq_len);
  1474. }
  1475. if (ioctl(drv->ioctl_sock, SIOCSIWENCODEEXT, &iwr) < 0) {
  1476. ret = errno == EOPNOTSUPP ? -2 : -1;
  1477. if (errno == ENODEV) {
  1478. /*
  1479. * ndiswrapper seems to be returning incorrect error
  1480. * code.. */
  1481. ret = -2;
  1482. }
  1483. perror("ioctl[SIOCSIWENCODEEXT]");
  1484. }
  1485. os_free(ext);
  1486. return ret;
  1487. }
  1488. /**
  1489. * wpa_driver_wext_set_key - Configure encryption key
  1490. * @priv: Pointer to private wext data from wpa_driver_wext_init()
  1491. * @priv: Private driver interface data
  1492. * @alg: Encryption algorithm (%WPA_ALG_NONE, %WPA_ALG_WEP,
  1493. * %WPA_ALG_TKIP, %WPA_ALG_CCMP); %WPA_ALG_NONE clears the key.
  1494. * @addr: Address of the peer STA or ff:ff:ff:ff:ff:ff for
  1495. * broadcast/default keys
  1496. * @key_idx: key index (0..3), usually 0 for unicast keys
  1497. * @set_tx: Configure this key as the default Tx key (only used when
  1498. * driver does not support separate unicast/individual key
  1499. * @seq: Sequence number/packet number, seq_len octets, the next
  1500. * packet number to be used for in replay protection; configured
  1501. * for Rx keys (in most cases, this is only used with broadcast
  1502. * keys and set to zero for unicast keys)
  1503. * @seq_len: Length of the seq, depends on the algorithm:
  1504. * TKIP: 6 octets, CCMP: 6 octets
  1505. * @key: Key buffer; TKIP: 16-byte temporal key, 8-byte Tx Mic key,
  1506. * 8-byte Rx Mic Key
  1507. * @key_len: Length of the key buffer in octets (WEP: 5 or 13,
  1508. * TKIP: 32, CCMP: 16)
  1509. * Returns: 0 on success, -1 on failure
  1510. *
  1511. * This function uses SIOCSIWENCODEEXT by default, but tries to use
  1512. * SIOCSIWENCODE if the extended ioctl fails when configuring a WEP key.
  1513. */
  1514. int wpa_driver_wext_set_key(void *priv, wpa_alg alg,
  1515. const u8 *addr, int key_idx,
  1516. int set_tx, const u8 *seq, size_t seq_len,
  1517. const u8 *key, size_t key_len)
  1518. {
  1519. struct wpa_driver_wext_data *drv = priv;
  1520. struct iwreq iwr;
  1521. int ret = 0;
  1522. wpa_printf(MSG_DEBUG, "%s: alg=%d key_idx=%d set_tx=%d seq_len=%lu "
  1523. "key_len=%lu",
  1524. __FUNCTION__, alg, key_idx, set_tx,
  1525. (unsigned long) seq_len, (unsigned long) key_len);
  1526. ret = wpa_driver_wext_set_key_ext(drv, alg, addr, key_idx, set_tx,
  1527. seq, seq_len, key, key_len);
  1528. if (ret == 0)
  1529. return 0;
  1530. if (ret == -2 &&
  1531. (alg == WPA_ALG_NONE || alg == WPA_ALG_WEP)) {
  1532. wpa_printf(MSG_DEBUG, "Driver did not support "
  1533. "SIOCSIWENCODEEXT, trying SIOCSIWENCODE");
  1534. ret = 0;
  1535. } else {
  1536. wpa_printf(MSG_DEBUG, "Driver did not support "
  1537. "SIOCSIWENCODEEXT");
  1538. return ret;
  1539. }
  1540. os_memset(&iwr, 0, sizeof(iwr));
  1541. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  1542. iwr.u.encoding.flags = key_idx + 1;
  1543. iwr.u.encoding.flags |= IW_ENCODE_TEMP;
  1544. if (alg == WPA_ALG_NONE)
  1545. iwr.u.encoding.flags |= IW_ENCODE_DISABLED;
  1546. iwr.u.encoding.pointer = (caddr_t) key;
  1547. iwr.u.encoding.length = key_len;
  1548. if (ioctl(drv->ioctl_sock, SIOCSIWENCODE, &iwr) < 0) {
  1549. perror("ioctl[SIOCSIWENCODE]");
  1550. ret = -1;
  1551. }
  1552. if (set_tx && alg != WPA_ALG_NONE) {
  1553. os_memset(&iwr, 0, sizeof(iwr));
  1554. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  1555. iwr.u.encoding.flags = key_idx + 1;
  1556. iwr.u.encoding.flags |= IW_ENCODE_TEMP;
  1557. iwr.u.encoding.pointer = (caddr_t) NULL;
  1558. iwr.u.encoding.length = 0;
  1559. if (ioctl(drv->ioctl_sock, SIOCSIWENCODE, &iwr) < 0) {
  1560. perror("ioctl[SIOCSIWENCODE] (set_tx)");
  1561. ret = -1;
  1562. }
  1563. }
  1564. return ret;
  1565. }
  1566. static int wpa_driver_wext_set_countermeasures(void *priv,
  1567. int enabled)
  1568. {
  1569. struct wpa_driver_wext_data *drv = priv;
  1570. wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
  1571. return wpa_driver_wext_set_auth_param(drv,
  1572. IW_AUTH_TKIP_COUNTERMEASURES,
  1573. enabled);
  1574. }
  1575. static int wpa_driver_wext_set_drop_unencrypted(void *priv,
  1576. int enabled)
  1577. {
  1578. struct wpa_driver_wext_data *drv = priv;
  1579. wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
  1580. drv->use_crypt = enabled;
  1581. return wpa_driver_wext_set_auth_param(drv, IW_AUTH_DROP_UNENCRYPTED,
  1582. enabled);
  1583. }
  1584. static int wpa_driver_wext_mlme(struct wpa_driver_wext_data *drv,
  1585. const u8 *addr, int cmd, int reason_code)
  1586. {
  1587. struct iwreq iwr;
  1588. struct iw_mlme mlme;
  1589. int ret = 0;
  1590. os_memset(&iwr, 0, sizeof(iwr));
  1591. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  1592. os_memset(&mlme, 0, sizeof(mlme));
  1593. mlme.cmd = cmd;
  1594. mlme.reason_code = reason_code;
  1595. mlme.addr.sa_family = ARPHRD_ETHER;
  1596. os_memcpy(mlme.addr.sa_data, addr, ETH_ALEN);
  1597. iwr.u.data.pointer = (caddr_t) &mlme;
  1598. iwr.u.data.length = sizeof(mlme);
  1599. if (ioctl(drv->ioctl_sock, SIOCSIWMLME, &iwr) < 0) {
  1600. perror("ioctl[SIOCSIWMLME]");
  1601. ret = -1;
  1602. }
  1603. return ret;
  1604. }
  1605. static int wpa_driver_wext_deauthenticate(void *priv, const u8 *addr,
  1606. int reason_code)
  1607. {
  1608. struct wpa_driver_wext_data *drv = priv;
  1609. wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
  1610. return wpa_driver_wext_mlme(drv, addr, IW_MLME_DEAUTH, reason_code);
  1611. }
  1612. static int wpa_driver_wext_disassociate(void *priv, const u8 *addr,
  1613. int reason_code)
  1614. {
  1615. struct wpa_driver_wext_data *drv = priv;
  1616. wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
  1617. return wpa_driver_wext_mlme(drv, addr, IW_MLME_DISASSOC,
  1618. reason_code);
  1619. }
  1620. static int wpa_driver_wext_set_gen_ie(void *priv, const u8 *ie,
  1621. size_t ie_len)
  1622. {
  1623. struct wpa_driver_wext_data *drv = priv;
  1624. struct iwreq iwr;
  1625. int ret = 0;
  1626. os_memset(&iwr, 0, sizeof(iwr));
  1627. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  1628. iwr.u.data.pointer = (caddr_t) ie;
  1629. iwr.u.data.length = ie_len;
  1630. if (ioctl(drv->ioctl_sock, SIOCSIWGENIE, &iwr) < 0) {
  1631. perror("ioctl[SIOCSIWGENIE]");
  1632. ret = -1;
  1633. }
  1634. return ret;
  1635. }
  1636. int wpa_driver_wext_cipher2wext(int cipher)
  1637. {
  1638. switch (cipher) {
  1639. case CIPHER_NONE:
  1640. return IW_AUTH_CIPHER_NONE;
  1641. case CIPHER_WEP40:
  1642. return IW_AUTH_CIPHER_WEP40;
  1643. case CIPHER_TKIP:
  1644. return IW_AUTH_CIPHER_TKIP;
  1645. case CIPHER_CCMP:
  1646. return IW_AUTH_CIPHER_CCMP;
  1647. case CIPHER_WEP104:
  1648. return IW_AUTH_CIPHER_WEP104;
  1649. default:
  1650. return 0;
  1651. }
  1652. }
  1653. int wpa_driver_wext_keymgmt2wext(int keymgmt)
  1654. {
  1655. switch (keymgmt) {
  1656. case KEY_MGMT_802_1X:
  1657. case KEY_MGMT_802_1X_NO_WPA:
  1658. return IW_AUTH_KEY_MGMT_802_1X;
  1659. case KEY_MGMT_PSK:
  1660. return IW_AUTH_KEY_MGMT_PSK;
  1661. default:
  1662. return 0;
  1663. }
  1664. }
  1665. static int
  1666. wpa_driver_wext_auth_alg_fallback(struct wpa_driver_wext_data *drv,
  1667. struct wpa_driver_associate_params *params)
  1668. {
  1669. struct iwreq iwr;
  1670. int ret = 0;
  1671. wpa_printf(MSG_DEBUG, "WEXT: Driver did not support "
  1672. "SIOCSIWAUTH for AUTH_ALG, trying SIOCSIWENCODE");
  1673. os_memset(&iwr, 0, sizeof(iwr));
  1674. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  1675. /* Just changing mode, not actual keys */
  1676. iwr.u.encoding.flags = 0;
  1677. iwr.u.encoding.pointer = (caddr_t) NULL;
  1678. iwr.u.encoding.length = 0;
  1679. /*
  1680. * Note: IW_ENCODE_{OPEN,RESTRICTED} can be interpreted to mean two
  1681. * different things. Here they are used to indicate Open System vs.
  1682. * Shared Key authentication algorithm. However, some drivers may use
  1683. * them to select between open/restricted WEP encrypted (open = allow
  1684. * both unencrypted and encrypted frames; restricted = only allow
  1685. * encrypted frames).
  1686. */
  1687. if (!drv->use_crypt) {
  1688. iwr.u.encoding.flags |= IW_ENCODE_DISABLED;
  1689. } else {
  1690. if (params->auth_alg & AUTH_ALG_OPEN_SYSTEM)
  1691. iwr.u.encoding.flags |= IW_ENCODE_OPEN;
  1692. if (params->auth_alg & AUTH_ALG_SHARED_KEY)
  1693. iwr.u.encoding.flags |= IW_ENCODE_RESTRICTED;
  1694. }
  1695. if (ioctl(drv->ioctl_sock, SIOCSIWENCODE, &iwr) < 0) {
  1696. perror("ioctl[SIOCSIWENCODE]");
  1697. ret = -1;
  1698. }
  1699. return ret;
  1700. }
  1701. int wpa_driver_wext_associate(void *priv,
  1702. struct wpa_driver_associate_params *params)
  1703. {
  1704. struct wpa_driver_wext_data *drv = priv;
  1705. int ret = 0;
  1706. int allow_unencrypted_eapol;
  1707. int value;
  1708. wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
  1709. /*
  1710. * If the driver did not support SIOCSIWAUTH, fallback to
  1711. * SIOCSIWENCODE here.
  1712. */
  1713. if (drv->auth_alg_fallback &&
  1714. wpa_driver_wext_auth_alg_fallback(drv, params) < 0)
  1715. ret = -1;
  1716. if (!params->bssid &&
  1717. wpa_driver_wext_set_bssid(drv, NULL) < 0)
  1718. ret = -1;
  1719. /* TODO: should consider getting wpa version and cipher/key_mgmt suites
  1720. * from configuration, not from here, where only the selected suite is
  1721. * available */
  1722. if (wpa_driver_wext_set_gen_ie(drv, params->wpa_ie, params->wpa_ie_len)
  1723. < 0)
  1724. ret = -1;
  1725. if (params->wpa_ie == NULL || params->wpa_ie_len == 0)
  1726. value = IW_AUTH_WPA_VERSION_DISABLED;
  1727. else if (params->wpa_ie[0] == WLAN_EID_RSN)
  1728. value = IW_AUTH_WPA_VERSION_WPA2;
  1729. else
  1730. value = IW_AUTH_WPA_VERSION_WPA;
  1731. if (wpa_driver_wext_set_auth_param(drv,
  1732. IW_AUTH_WPA_VERSION, value) < 0)
  1733. ret = -1;
  1734. value = wpa_driver_wext_cipher2wext(params->pairwise_suite);
  1735. if (wpa_driver_wext_set_auth_param(drv,
  1736. IW_AUTH_CIPHER_PAIRWISE, value) < 0)
  1737. ret = -1;
  1738. value = wpa_driver_wext_cipher2wext(params->group_suite);
  1739. if (wpa_driver_wext_set_auth_param(drv,
  1740. IW_AUTH_CIPHER_GROUP, value) < 0)
  1741. ret = -1;
  1742. value = wpa_driver_wext_keymgmt2wext(params->key_mgmt_suite);
  1743. if (wpa_driver_wext_set_auth_param(drv,
  1744. IW_AUTH_KEY_MGMT, value) < 0)
  1745. ret = -1;
  1746. value = params->key_mgmt_suite != KEY_MGMT_NONE ||
  1747. params->pairwise_suite != CIPHER_NONE ||
  1748. params->group_suite != CIPHER_NONE ||
  1749. params->wpa_ie_len;
  1750. if (wpa_driver_wext_set_auth_param(drv,
  1751. IW_AUTH_PRIVACY_INVOKED, value) < 0)
  1752. ret = -1;
  1753. /* Allow unencrypted EAPOL messages even if pairwise keys are set when
  1754. * not using WPA. IEEE 802.1X specifies that these frames are not
  1755. * encrypted, but WPA encrypts them when pairwise keys are in use. */
  1756. if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
  1757. params->key_mgmt_suite == KEY_MGMT_PSK)
  1758. allow_unencrypted_eapol = 0;
  1759. else
  1760. allow_unencrypted_eapol = 1;
  1761. if (wpa_driver_wext_set_psk(drv, params->psk) < 0)
  1762. ret = -1;
  1763. if (wpa_driver_wext_set_auth_param(drv,
  1764. IW_AUTH_RX_UNENCRYPTED_EAPOL,
  1765. allow_unencrypted_eapol) < 0)
  1766. ret = -1;
  1767. #ifdef CONFIG_IEEE80211W
  1768. switch (params->mgmt_frame_protection) {
  1769. case NO_MGMT_FRAME_PROTECTION:
  1770. value = IW_AUTH_MFP_DISABLED;
  1771. break;
  1772. case MGMT_FRAME_PROTECTION_OPTIONAL:
  1773. value = IW_AUTH_MFP_OPTIONAL;
  1774. break;
  1775. case MGMT_FRAME_PROTECTION_REQUIRED:
  1776. value = IW_AUTH_MFP_REQUIRED;
  1777. break;
  1778. };
  1779. if (wpa_driver_wext_set_auth_param(drv, IW_AUTH_MFP, value) < 0)
  1780. ret = -1;
  1781. #endif /* CONFIG_IEEE80211W */
  1782. if (params->freq && wpa_driver_wext_set_freq(drv, params->freq) < 0)
  1783. ret = -1;
  1784. if (wpa_driver_wext_set_ssid(drv, params->ssid, params->ssid_len) < 0)
  1785. ret = -1;
  1786. if (params->bssid &&
  1787. wpa_driver_wext_set_bssid(drv, params->bssid) < 0)
  1788. ret = -1;
  1789. return ret;
  1790. }
  1791. static int wpa_driver_wext_set_auth_alg(void *priv, int auth_alg)
  1792. {
  1793. struct wpa_driver_wext_data *drv = priv;
  1794. int algs = 0, res;
  1795. if (auth_alg & AUTH_ALG_OPEN_SYSTEM)
  1796. algs |= IW_AUTH_ALG_OPEN_SYSTEM;
  1797. if (auth_alg & AUTH_ALG_SHARED_KEY)
  1798. algs |= IW_AUTH_ALG_SHARED_KEY;
  1799. if (auth_alg & AUTH_ALG_LEAP)
  1800. algs |= IW_AUTH_ALG_LEAP;
  1801. if (algs == 0) {
  1802. /* at least one algorithm should be set */
  1803. algs = IW_AUTH_ALG_OPEN_SYSTEM;
  1804. }
  1805. res = wpa_driver_wext_set_auth_param(drv, IW_AUTH_80211_AUTH_ALG,
  1806. algs);
  1807. drv->auth_alg_fallback = res == -2;
  1808. return res;
  1809. }
  1810. /**
  1811. * wpa_driver_wext_set_mode - Set wireless mode (infra/adhoc), SIOCSIWMODE
  1812. * @priv: Pointer to private wext data from wpa_driver_wext_init()
  1813. * @mode: 0 = infra/BSS (associate with an AP), 1 = adhoc/IBSS
  1814. * Returns: 0 on success, -1 on failure
  1815. */
  1816. int wpa_driver_wext_set_mode(void *priv, int mode)
  1817. {
  1818. struct wpa_driver_wext_data *drv = priv;
  1819. struct iwreq iwr;
  1820. int ret = -1, flags;
  1821. unsigned int new_mode = mode ? IW_MODE_ADHOC : IW_MODE_INFRA;
  1822. os_memset(&iwr, 0, sizeof(iwr));
  1823. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  1824. iwr.u.mode = new_mode;
  1825. if (ioctl(drv->ioctl_sock, SIOCSIWMODE, &iwr) == 0) {
  1826. ret = 0;
  1827. goto done;
  1828. }
  1829. if (errno != EBUSY) {
  1830. perror("ioctl[SIOCSIWMODE]");
  1831. goto done;
  1832. }
  1833. /* mac80211 doesn't allow mode changes while the device is up, so if
  1834. * the device isn't in the mode we're about to change to, take device
  1835. * down, try to set the mode again, and bring it back up.
  1836. */
  1837. if (ioctl(drv->ioctl_sock, SIOCGIWMODE, &iwr) < 0) {
  1838. perror("ioctl[SIOCGIWMODE]");
  1839. goto done;
  1840. }
  1841. if (iwr.u.mode == new_mode) {
  1842. ret = 0;
  1843. goto done;
  1844. }
  1845. if (wpa_driver_wext_get_ifflags(drv, &flags) == 0) {
  1846. (void) wpa_driver_wext_set_ifflags(drv, flags & ~IFF_UP);
  1847. /* Try to set the mode again while the interface is down */
  1848. iwr.u.mode = new_mode;
  1849. if (ioctl(drv->ioctl_sock, SIOCSIWMODE, &iwr) < 0)
  1850. perror("ioctl[SIOCSIWMODE]");
  1851. else
  1852. ret = 0;
  1853. /* Ignore return value of get_ifflags to ensure that the device
  1854. * is always up like it was before this function was called.
  1855. */
  1856. (void) wpa_driver_wext_get_ifflags(drv, &flags);
  1857. (void) wpa_driver_wext_set_ifflags(drv, flags | IFF_UP);
  1858. }
  1859. done:
  1860. return ret;
  1861. }
  1862. static int wpa_driver_wext_pmksa(struct wpa_driver_wext_data *drv,
  1863. u32 cmd, const u8 *bssid, const u8 *pmkid)
  1864. {
  1865. struct iwreq iwr;
  1866. struct iw_pmksa pmksa;
  1867. int ret = 0;
  1868. os_memset(&iwr, 0, sizeof(iwr));
  1869. os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
  1870. os_memset(&pmksa, 0, sizeof(pmksa));
  1871. pmksa.cmd = cmd;
  1872. pmksa.bssid.sa_family = ARPHRD_ETHER;
  1873. if (bssid)
  1874. os_memcpy(pmksa.bssid.sa_data, bssid, ETH_ALEN);
  1875. if (pmkid)
  1876. os_memcpy(pmksa.pmkid, pmkid, IW_PMKID_LEN);
  1877. iwr.u.data.pointer = (caddr_t) &pmksa;
  1878. iwr.u.data.length = sizeof(pmksa);
  1879. if (ioctl(drv->ioctl_sock, SIOCSIWPMKSA, &iwr) < 0) {
  1880. if (errno != EOPNOTSUPP)
  1881. perror("ioctl[SIOCSIWPMKSA]");
  1882. ret = -1;
  1883. }
  1884. return ret;
  1885. }
  1886. static int wpa_driver_wext_add_pmkid(void *priv, const u8 *bssid,
  1887. const u8 *pmkid)
  1888. {
  1889. struct wpa_driver_wext_data *drv = priv;
  1890. return wpa_driver_wext_pmksa(drv, IW_PMKSA_ADD, bssid, pmkid);
  1891. }
  1892. static int wpa_driver_wext_remove_pmkid(void *priv, const u8 *bssid,
  1893. const u8 *pmkid)
  1894. {
  1895. struct wpa_driver_wext_data *drv = priv;
  1896. return wpa_driver_wext_pmksa(drv, IW_PMKSA_REMOVE, bssid, pmkid);
  1897. }
  1898. static int wpa_driver_wext_flush_pmkid(void *priv)
  1899. {
  1900. struct wpa_driver_wext_data *drv = priv;
  1901. return wpa_driver_wext_pmksa(drv, IW_PMKSA_FLUSH, NULL, NULL);
  1902. }
  1903. int wpa_driver_wext_get_capa(void *priv, struct wpa_driver_capa *capa)
  1904. {
  1905. struct wpa_driver_wext_data *drv = priv;
  1906. if (!drv->has_capability)
  1907. return -1;
  1908. os_memcpy(capa, &drv->capa, sizeof(*capa));
  1909. return 0;
  1910. }
  1911. int wpa_driver_wext_alternative_ifindex(struct wpa_driver_wext_data *drv,
  1912. const char *ifname)
  1913. {
  1914. if (ifname == NULL) {
  1915. drv->ifindex2 = -1;
  1916. return 0;
  1917. }
  1918. drv->ifindex2 = if_nametoindex(ifname);
  1919. if (drv->ifindex2 <= 0)
  1920. return -1;
  1921. wpa_printf(MSG_DEBUG, "Added alternative ifindex %d (%s) for "
  1922. "wireless events", drv->ifindex2, ifname);
  1923. return 0;
  1924. }
  1925. int wpa_driver_wext_set_operstate(void *priv, int state)
  1926. {
  1927. struct wpa_driver_wext_data *drv = priv;
  1928. wpa_printf(MSG_DEBUG, "%s: operstate %d->%d (%s)",
  1929. __func__, drv->operstate, state, state ? "UP" : "DORMANT");
  1930. drv->operstate = state;
  1931. return wpa_driver_wext_send_oper_ifla(
  1932. drv, -1, state ? IF_OPER_UP : IF_OPER_DORMANT);
  1933. }
  1934. int wpa_driver_wext_get_version(struct wpa_driver_wext_data *drv)
  1935. {
  1936. return drv->we_version_compiled;
  1937. }
  1938. const struct wpa_driver_ops wpa_driver_wext_ops = {
  1939. .name = "wext",
  1940. .desc = "Linux wireless extensions (generic)",
  1941. .get_bssid = wpa_driver_wext_get_bssid,
  1942. .get_ssid = wpa_driver_wext_get_ssid,
  1943. .set_wpa = wpa_driver_wext_set_wpa,
  1944. .set_key = wpa_driver_wext_set_key,
  1945. .set_countermeasures = wpa_driver_wext_set_countermeasures,
  1946. .set_drop_unencrypted = wpa_driver_wext_set_drop_unencrypted,
  1947. .scan = wpa_driver_wext_scan,
  1948. .get_scan_results2 = wpa_driver_wext_get_scan_results,
  1949. .deauthenticate = wpa_driver_wext_deauthenticate,
  1950. .disassociate = wpa_driver_wext_disassociate,
  1951. .set_mode = wpa_driver_wext_set_mode,
  1952. .associate = wpa_driver_wext_associate,
  1953. .set_auth_alg = wpa_driver_wext_set_auth_alg,
  1954. .init = wpa_driver_wext_init,
  1955. .deinit = wpa_driver_wext_deinit,
  1956. .add_pmkid = wpa_driver_wext_add_pmkid,
  1957. .remove_pmkid = wpa_driver_wext_remove_pmkid,
  1958. .flush_pmkid = wpa_driver_wext_flush_pmkid,
  1959. .get_capa = wpa_driver_wext_get_capa,
  1960. .set_operstate = wpa_driver_wext_set_operstate,
  1961. };