driver_wext.c 62 KB

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