mlme.c 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134
  1. /*
  2. * WPA Supplicant - Client mode MLME
  3. * Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi>
  4. * Copyright (c) 2004, Instant802 Networks, Inc.
  5. * Copyright (c) 2005-2006, Devicescape Software, Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * Alternatively, this software may be distributed under the terms of BSD
  12. * license.
  13. *
  14. * See README and COPYING for more details.
  15. */
  16. #include "includes.h"
  17. #include "common.h"
  18. #include "eloop.h"
  19. #include "config_ssid.h"
  20. #include "wpa_supplicant_i.h"
  21. #include "wpa.h"
  22. #include "drivers/driver.h"
  23. #include "ieee802_11_defs.h"
  24. #include "mlme.h"
  25. /* Timeouts and intervals in milliseconds */
  26. #define IEEE80211_AUTH_TIMEOUT (200)
  27. #define IEEE80211_AUTH_MAX_TRIES 3
  28. #define IEEE80211_ASSOC_TIMEOUT (200)
  29. #define IEEE80211_ASSOC_MAX_TRIES 3
  30. #define IEEE80211_MONITORING_INTERVAL (2000)
  31. #define IEEE80211_PROBE_INTERVAL (60000)
  32. #define IEEE80211_RETRY_AUTH_INTERVAL (1000)
  33. #define IEEE80211_SCAN_INTERVAL (2000)
  34. #define IEEE80211_SCAN_INTERVAL_SLOW (15000)
  35. #define IEEE80211_IBSS_JOIN_TIMEOUT (20000)
  36. #define IEEE80211_PROBE_DELAY (33)
  37. #define IEEE80211_CHANNEL_TIME (33)
  38. #define IEEE80211_PASSIVE_CHANNEL_TIME (200)
  39. #define IEEE80211_SCAN_RESULT_EXPIRE (10000)
  40. #define IEEE80211_IBSS_MERGE_INTERVAL (30000)
  41. #define IEEE80211_IBSS_INACTIVITY_LIMIT (60000)
  42. #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
  43. #define IEEE80211_FC(type, stype) host_to_le16((type << 2) | (stype << 4))
  44. struct ieee80211_sta_bss {
  45. struct ieee80211_sta_bss *next;
  46. struct ieee80211_sta_bss *hnext;
  47. u8 bssid[ETH_ALEN];
  48. u8 ssid[MAX_SSID_LEN];
  49. size_t ssid_len;
  50. u16 capability; /* host byte order */
  51. int hw_mode;
  52. int channel;
  53. int freq;
  54. int rssi;
  55. u8 *ie;
  56. size_t ie_len;
  57. u8 *wpa_ie;
  58. size_t wpa_ie_len;
  59. u8 *rsn_ie;
  60. size_t rsn_ie_len;
  61. u8 *wmm_ie;
  62. size_t wmm_ie_len;
  63. u8 *mdie;
  64. size_t mdie_len;
  65. #define IEEE80211_MAX_SUPP_RATES 32
  66. u8 supp_rates[IEEE80211_MAX_SUPP_RATES];
  67. size_t supp_rates_len;
  68. int beacon_int;
  69. u64 timestamp;
  70. int probe_resp;
  71. struct os_time last_update;
  72. };
  73. static void ieee80211_send_probe_req(struct wpa_supplicant *wpa_s,
  74. const u8 *dst,
  75. const u8 *ssid, size_t ssid_len);
  76. static struct ieee80211_sta_bss *
  77. ieee80211_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid);
  78. static int ieee80211_sta_find_ibss(struct wpa_supplicant *wpa_s);
  79. static int ieee80211_sta_wep_configured(struct wpa_supplicant *wpa_s);
  80. static void ieee80211_sta_timer(void *eloop_ctx, void *timeout_ctx);
  81. static void ieee80211_sta_scan_timer(void *eloop_ctx, void *timeout_ctx);
  82. /* Parsed Information Elements */
  83. struct ieee802_11_elems {
  84. u8 *ssid;
  85. u8 ssid_len;
  86. u8 *supp_rates;
  87. u8 supp_rates_len;
  88. u8 *fh_params;
  89. u8 fh_params_len;
  90. u8 *ds_params;
  91. u8 ds_params_len;
  92. u8 *cf_params;
  93. u8 cf_params_len;
  94. u8 *tim;
  95. u8 tim_len;
  96. u8 *ibss_params;
  97. u8 ibss_params_len;
  98. u8 *challenge;
  99. u8 challenge_len;
  100. u8 *wpa;
  101. u8 wpa_len;
  102. u8 *rsn;
  103. u8 rsn_len;
  104. u8 *erp_info;
  105. u8 erp_info_len;
  106. u8 *ext_supp_rates;
  107. u8 ext_supp_rates_len;
  108. u8 *wmm_info;
  109. u8 wmm_info_len;
  110. u8 *wmm_param;
  111. u8 wmm_param_len;
  112. u8 *mdie;
  113. u8 mdie_len;
  114. u8 *ftie;
  115. u8 ftie_len;
  116. };
  117. typedef enum { ParseOK = 0, ParseUnknown = 1, ParseFailed = -1 } ParseRes;
  118. static ParseRes ieee802_11_parse_elems(u8 *start, size_t len,
  119. struct ieee802_11_elems *elems)
  120. {
  121. size_t left = len;
  122. u8 *pos = start;
  123. int unknown = 0;
  124. os_memset(elems, 0, sizeof(*elems));
  125. while (left >= 2) {
  126. u8 id, elen;
  127. id = *pos++;
  128. elen = *pos++;
  129. left -= 2;
  130. if (elen > left) {
  131. #if 0
  132. wpa_printf(MSG_MSGDUMP, "MLME: IEEE 802.11 element "
  133. "parse failed (id=%d elen=%d left=%d)",
  134. id, elen, left);
  135. #endif
  136. return ParseFailed;
  137. }
  138. switch (id) {
  139. case WLAN_EID_SSID:
  140. elems->ssid = pos;
  141. elems->ssid_len = elen;
  142. break;
  143. case WLAN_EID_SUPP_RATES:
  144. elems->supp_rates = pos;
  145. elems->supp_rates_len = elen;
  146. break;
  147. case WLAN_EID_FH_PARAMS:
  148. elems->fh_params = pos;
  149. elems->fh_params_len = elen;
  150. break;
  151. case WLAN_EID_DS_PARAMS:
  152. elems->ds_params = pos;
  153. elems->ds_params_len = elen;
  154. break;
  155. case WLAN_EID_CF_PARAMS:
  156. elems->cf_params = pos;
  157. elems->cf_params_len = elen;
  158. break;
  159. case WLAN_EID_TIM:
  160. elems->tim = pos;
  161. elems->tim_len = elen;
  162. break;
  163. case WLAN_EID_IBSS_PARAMS:
  164. elems->ibss_params = pos;
  165. elems->ibss_params_len = elen;
  166. break;
  167. case WLAN_EID_CHALLENGE:
  168. elems->challenge = pos;
  169. elems->challenge_len = elen;
  170. break;
  171. case WLAN_EID_VENDOR_SPECIFIC:
  172. if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
  173. pos[2] == 0xf2) {
  174. /* Microsoft OUI (00:50:F2) */
  175. if (pos[3] == 1) {
  176. /* OUI Type 1 - WPA IE */
  177. elems->wpa = pos;
  178. elems->wpa_len = elen;
  179. } else if (elen >= 5 && pos[3] == 2) {
  180. if (pos[4] == 0) {
  181. elems->wmm_info = pos;
  182. elems->wmm_info_len = elen;
  183. } else if (pos[4] == 1) {
  184. elems->wmm_param = pos;
  185. elems->wmm_param_len = elen;
  186. }
  187. }
  188. }
  189. break;
  190. case WLAN_EID_RSN:
  191. elems->rsn = pos;
  192. elems->rsn_len = elen;
  193. break;
  194. case WLAN_EID_ERP_INFO:
  195. elems->erp_info = pos;
  196. elems->erp_info_len = elen;
  197. break;
  198. case WLAN_EID_EXT_SUPP_RATES:
  199. elems->ext_supp_rates = pos;
  200. elems->ext_supp_rates_len = elen;
  201. break;
  202. case WLAN_EID_MOBILITY_DOMAIN:
  203. elems->mdie = pos;
  204. elems->mdie_len = elen;
  205. break;
  206. case WLAN_EID_FAST_BSS_TRANSITION:
  207. elems->ftie = pos;
  208. elems->ftie_len = elen;
  209. break;
  210. default:
  211. #if 0
  212. wpa_printf(MSG_MSGDUMP "MLME: IEEE 802.11 element "
  213. "parse ignored unknown element (id=%d "
  214. "elen=%d)", id, elen);
  215. #endif
  216. unknown++;
  217. break;
  218. }
  219. left -= elen;
  220. pos += elen;
  221. }
  222. if (left)
  223. return ParseFailed;
  224. return unknown ? ParseUnknown : ParseOK;
  225. }
  226. static int ieee80211_sta_set_channel(struct wpa_supplicant *wpa_s,
  227. wpa_hw_mode phymode, int chan,
  228. int freq)
  229. {
  230. size_t i;
  231. struct wpa_hw_modes *mode;
  232. for (i = 0; i < wpa_s->mlme.num_modes; i++) {
  233. mode = &wpa_s->mlme.modes[i];
  234. if (mode->mode == phymode) {
  235. wpa_s->mlme.curr_rates = mode->rates;
  236. wpa_s->mlme.num_curr_rates = mode->num_rates;
  237. break;
  238. }
  239. }
  240. return wpa_drv_set_channel(wpa_s, phymode, chan, freq);
  241. }
  242. #if 0 /* FIX */
  243. static int ecw2cw(int ecw)
  244. {
  245. int cw = 1;
  246. while (ecw > 0) {
  247. cw <<= 1;
  248. ecw--;
  249. }
  250. return cw - 1;
  251. }
  252. #endif
  253. static void ieee80211_sta_wmm_params(struct wpa_supplicant *wpa_s,
  254. u8 *wmm_param, size_t wmm_param_len)
  255. {
  256. size_t left;
  257. int count;
  258. u8 *pos;
  259. if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
  260. return;
  261. count = wmm_param[6] & 0x0f;
  262. if (count == wpa_s->mlme.wmm_last_param_set)
  263. return;
  264. wpa_s->mlme.wmm_last_param_set = count;
  265. pos = wmm_param + 8;
  266. left = wmm_param_len - 8;
  267. #if 0 /* FIX */
  268. wmm_acm = 0;
  269. for (; left >= 4; left -= 4, pos += 4) {
  270. int aci = (pos[0] >> 5) & 0x03;
  271. int acm = (pos[0] >> 4) & 0x01;
  272. int queue;
  273. switch (aci) {
  274. case 1:
  275. queue = IEEE80211_TX_QUEUE_DATA3;
  276. if (acm)
  277. wmm_acm |= BIT(1) | BIT(2);
  278. break;
  279. case 2:
  280. queue = IEEE80211_TX_QUEUE_DATA1;
  281. if (acm)
  282. wmm_acm |= BIT(4) | BIT(5);
  283. break;
  284. case 3:
  285. queue = IEEE80211_TX_QUEUE_DATA0;
  286. if (acm)
  287. wmm_acm |= BIT(6) | BIT(7);
  288. break;
  289. case 0:
  290. default:
  291. queue = IEEE80211_TX_QUEUE_DATA2;
  292. if (acm)
  293. wpa_s->mlme.wmm_acm |= BIT(0) | BIT(3);
  294. break;
  295. }
  296. params.aifs = pos[0] & 0x0f;
  297. params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
  298. params.cw_min = ecw2cw(pos[1] & 0x0f);
  299. /* TXOP is in units of 32 usec; burst_time in 0.1 ms */
  300. params.burst_time = (pos[2] | (pos[3] << 8)) * 32 / 100;
  301. wpa_printf(MSG_DEBUG, "MLME: WMM queue=%d aci=%d acm=%d "
  302. "aifs=%d cWmin=%d cWmax=%d burst=%d",
  303. queue, aci, acm, params.aifs, params.cw_min,
  304. params.cw_max, params.burst_time);
  305. /* TODO: handle ACM (block TX, fallback to next lowest allowed
  306. * AC for now) */
  307. if (local->hw->conf_tx(local->mdev, queue, &params)) {
  308. wpa_printf(MSG_DEBUG, "MLME: failed to set TX queue "
  309. "parameters for queue %d", queue);
  310. }
  311. }
  312. #endif
  313. }
  314. static void ieee80211_set_associated(struct wpa_supplicant *wpa_s, int assoc)
  315. {
  316. if (wpa_s->mlme.associated == assoc && !assoc)
  317. return;
  318. wpa_s->mlme.associated = assoc;
  319. if (assoc) {
  320. union wpa_event_data data;
  321. os_memset(&data, 0, sizeof(data));
  322. wpa_s->mlme.prev_bssid_set = 1;
  323. os_memcpy(wpa_s->mlme.prev_bssid, wpa_s->bssid, ETH_ALEN);
  324. data.assoc_info.req_ies = wpa_s->mlme.assocreq_ies;
  325. data.assoc_info.req_ies_len = wpa_s->mlme.assocreq_ies_len;
  326. data.assoc_info.resp_ies = wpa_s->mlme.assocresp_ies;
  327. data.assoc_info.resp_ies_len = wpa_s->mlme.assocresp_ies_len;
  328. wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
  329. } else {
  330. wpa_supplicant_event(wpa_s, EVENT_DISASSOC, NULL);
  331. }
  332. os_get_time(&wpa_s->mlme.last_probe);
  333. }
  334. static int ieee80211_sta_tx(struct wpa_supplicant *wpa_s, const u8 *buf,
  335. size_t len)
  336. {
  337. return wpa_drv_send_mlme(wpa_s, buf, len);
  338. }
  339. static void ieee80211_send_auth(struct wpa_supplicant *wpa_s,
  340. int transaction, u8 *extra, size_t extra_len,
  341. int encrypt)
  342. {
  343. u8 *buf;
  344. size_t len;
  345. struct ieee80211_mgmt *mgmt;
  346. buf = os_malloc(sizeof(*mgmt) + 6 + extra_len);
  347. if (buf == NULL) {
  348. wpa_printf(MSG_DEBUG, "MLME: failed to allocate buffer for "
  349. "auth frame");
  350. return;
  351. }
  352. mgmt = (struct ieee80211_mgmt *) buf;
  353. len = 24 + 6;
  354. os_memset(mgmt, 0, 24 + 6);
  355. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  356. WLAN_FC_STYPE_AUTH);
  357. if (encrypt)
  358. mgmt->frame_control |= host_to_le16(WLAN_FC_ISWEP);
  359. os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
  360. os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
  361. os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
  362. mgmt->u.auth.auth_alg = host_to_le16(wpa_s->mlme.auth_alg);
  363. mgmt->u.auth.auth_transaction = host_to_le16(transaction);
  364. wpa_s->mlme.auth_transaction = transaction + 1;
  365. mgmt->u.auth.status_code = host_to_le16(0);
  366. if (extra) {
  367. os_memcpy(buf + len, extra, extra_len);
  368. len += extra_len;
  369. }
  370. ieee80211_sta_tx(wpa_s, buf, len);
  371. os_free(buf);
  372. }
  373. static void ieee80211_reschedule_timer(struct wpa_supplicant *wpa_s, int ms)
  374. {
  375. eloop_cancel_timeout(ieee80211_sta_timer, wpa_s, NULL);
  376. eloop_register_timeout(ms / 1000, 1000 * (ms % 1000),
  377. ieee80211_sta_timer, wpa_s, NULL);
  378. }
  379. static void ieee80211_authenticate(struct wpa_supplicant *wpa_s)
  380. {
  381. u8 *extra;
  382. size_t extra_len;
  383. wpa_s->mlme.auth_tries++;
  384. if (wpa_s->mlme.auth_tries > IEEE80211_AUTH_MAX_TRIES) {
  385. wpa_printf(MSG_DEBUG, "MLME: authentication with AP " MACSTR
  386. " timed out", MAC2STR(wpa_s->bssid));
  387. return;
  388. }
  389. wpa_s->mlme.state = IEEE80211_AUTHENTICATE;
  390. wpa_printf(MSG_DEBUG, "MLME: authenticate with AP " MACSTR,
  391. MAC2STR(wpa_s->bssid));
  392. extra = NULL;
  393. extra_len = 0;
  394. #ifdef CONFIG_IEEE80211R
  395. if ((wpa_s->mlme.key_mgmt == KEY_MGMT_FT_802_1X ||
  396. wpa_s->mlme.key_mgmt == KEY_MGMT_FT_PSK) &&
  397. wpa_s->mlme.ft_ies) {
  398. struct ieee80211_sta_bss *bss;
  399. struct rsn_mdie *mdie = NULL;
  400. bss = ieee80211_bss_get(wpa_s, wpa_s->bssid);
  401. if (bss && bss->mdie_len >= 2 + sizeof(*mdie))
  402. mdie = (struct rsn_mdie *) (bss->mdie + 2);
  403. if (mdie &&
  404. os_memcmp(mdie->mobility_domain, wpa_s->mlme.current_md,
  405. MOBILITY_DOMAIN_ID_LEN) == 0) {
  406. wpa_printf(MSG_DEBUG, "MLME: Trying to use FT "
  407. "over-the-air");
  408. wpa_s->mlme.auth_alg = WLAN_AUTH_FT;
  409. extra = wpa_s->mlme.ft_ies;
  410. extra_len = wpa_s->mlme.ft_ies_len;
  411. }
  412. }
  413. #endif /* CONFIG_IEEE80211R */
  414. ieee80211_send_auth(wpa_s, 1, extra, extra_len, 0);
  415. ieee80211_reschedule_timer(wpa_s, IEEE80211_AUTH_TIMEOUT);
  416. }
  417. static void ieee80211_send_assoc(struct wpa_supplicant *wpa_s)
  418. {
  419. struct ieee80211_mgmt *mgmt;
  420. u8 *pos, *ies, *buf;
  421. int i, len;
  422. u16 capab;
  423. struct ieee80211_sta_bss *bss;
  424. int wmm = 0;
  425. size_t blen, buflen;
  426. if (wpa_s->mlme.curr_rates == NULL) {
  427. wpa_printf(MSG_DEBUG, "MLME: curr_rates not set for assoc");
  428. return;
  429. }
  430. buflen = sizeof(*mgmt) + 200 + wpa_s->mlme.extra_ie_len +
  431. wpa_s->mlme.ssid_len;
  432. #ifdef CONFIG_IEEE80211R
  433. if (wpa_s->mlme.ft_ies)
  434. buflen += wpa_s->mlme.ft_ies_len;
  435. #endif /* CONFIG_IEEE80211R */
  436. buf = os_malloc(buflen);
  437. if (buf == NULL) {
  438. wpa_printf(MSG_DEBUG, "MLME: failed to allocate buffer for "
  439. "assoc frame");
  440. return;
  441. }
  442. blen = 0;
  443. capab = wpa_s->mlme.capab;
  444. if (wpa_s->mlme.phymode == WPA_MODE_IEEE80211G) {
  445. capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME |
  446. WLAN_CAPABILITY_SHORT_PREAMBLE;
  447. }
  448. bss = ieee80211_bss_get(wpa_s, wpa_s->bssid);
  449. if (bss) {
  450. if (bss->capability & WLAN_CAPABILITY_PRIVACY)
  451. capab |= WLAN_CAPABILITY_PRIVACY;
  452. if (bss->wmm_ie) {
  453. wmm = 1;
  454. }
  455. }
  456. mgmt = (struct ieee80211_mgmt *) buf;
  457. blen += 24;
  458. os_memset(mgmt, 0, 24);
  459. os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
  460. os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
  461. os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
  462. if (wpa_s->mlme.prev_bssid_set) {
  463. blen += 10;
  464. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  465. WLAN_FC_STYPE_REASSOC_REQ);
  466. mgmt->u.reassoc_req.capab_info = host_to_le16(capab);
  467. mgmt->u.reassoc_req.listen_interval = host_to_le16(1);
  468. os_memcpy(mgmt->u.reassoc_req.current_ap,
  469. wpa_s->mlme.prev_bssid,
  470. ETH_ALEN);
  471. } else {
  472. blen += 4;
  473. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  474. WLAN_FC_STYPE_ASSOC_REQ);
  475. mgmt->u.assoc_req.capab_info = host_to_le16(capab);
  476. mgmt->u.assoc_req.listen_interval = host_to_le16(1);
  477. }
  478. /* SSID */
  479. ies = pos = buf + blen;
  480. blen += 2 + wpa_s->mlme.ssid_len;
  481. *pos++ = WLAN_EID_SSID;
  482. *pos++ = wpa_s->mlme.ssid_len;
  483. os_memcpy(pos, wpa_s->mlme.ssid, wpa_s->mlme.ssid_len);
  484. len = wpa_s->mlme.num_curr_rates;
  485. if (len > 8)
  486. len = 8;
  487. pos = buf + blen;
  488. blen += len + 2;
  489. *pos++ = WLAN_EID_SUPP_RATES;
  490. *pos++ = len;
  491. for (i = 0; i < len; i++) {
  492. int rate = wpa_s->mlme.curr_rates[i].rate;
  493. *pos++ = (u8) (rate / 5);
  494. }
  495. if (wpa_s->mlme.num_curr_rates > len) {
  496. pos = buf + blen;
  497. blen += wpa_s->mlme.num_curr_rates - len + 2;
  498. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  499. *pos++ = wpa_s->mlme.num_curr_rates - len;
  500. for (i = len; i < wpa_s->mlme.num_curr_rates; i++) {
  501. int rate = wpa_s->mlme.curr_rates[i].rate;
  502. *pos++ = (u8) (rate / 5);
  503. }
  504. }
  505. if (wpa_s->mlme.extra_ie && wpa_s->mlme.auth_alg != WLAN_AUTH_FT) {
  506. pos = buf + blen;
  507. blen += wpa_s->mlme.extra_ie_len;
  508. os_memcpy(pos, wpa_s->mlme.extra_ie, wpa_s->mlme.extra_ie_len);
  509. }
  510. #ifdef CONFIG_IEEE80211R
  511. if ((wpa_s->mlme.key_mgmt == KEY_MGMT_FT_802_1X ||
  512. wpa_s->mlme.key_mgmt == KEY_MGMT_FT_PSK) &&
  513. wpa_s->mlme.auth_alg != WLAN_AUTH_FT &&
  514. bss && bss->mdie &&
  515. bss->mdie_len >= 2 + sizeof(struct rsn_mdie) &&
  516. bss->mdie[1] >= sizeof(struct rsn_mdie)) {
  517. pos = buf + blen;
  518. blen += 2 + sizeof(struct rsn_mdie);
  519. *pos++ = WLAN_EID_MOBILITY_DOMAIN;
  520. *pos++ = sizeof(struct rsn_mdie);
  521. os_memcpy(pos, bss->mdie + 2, MOBILITY_DOMAIN_ID_LEN);
  522. pos += MOBILITY_DOMAIN_ID_LEN;
  523. *pos++ = 0; /* FIX: copy from the target AP's MDIE */
  524. }
  525. if ((wpa_s->mlme.key_mgmt == KEY_MGMT_FT_802_1X ||
  526. wpa_s->mlme.key_mgmt == KEY_MGMT_FT_PSK) &&
  527. wpa_s->mlme.auth_alg == WLAN_AUTH_FT && wpa_s->mlme.ft_ies) {
  528. pos = buf + blen;
  529. os_memcpy(pos, wpa_s->mlme.ft_ies, wpa_s->mlme.ft_ies_len);
  530. pos += wpa_s->mlme.ft_ies_len;
  531. blen += wpa_s->mlme.ft_ies_len;
  532. }
  533. #endif /* CONFIG_IEEE80211R */
  534. if (wmm && wpa_s->mlme.wmm_enabled) {
  535. pos = buf + blen;
  536. blen += 9;
  537. *pos++ = WLAN_EID_VENDOR_SPECIFIC;
  538. *pos++ = 7; /* len */
  539. *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
  540. *pos++ = 0x50;
  541. *pos++ = 0xf2;
  542. *pos++ = 2; /* WME */
  543. *pos++ = 0; /* WME info */
  544. *pos++ = 1; /* WME ver */
  545. *pos++ = 0;
  546. }
  547. os_free(wpa_s->mlme.assocreq_ies);
  548. wpa_s->mlme.assocreq_ies_len = (buf + blen) - ies;
  549. wpa_s->mlme.assocreq_ies = os_malloc(wpa_s->mlme.assocreq_ies_len);
  550. if (wpa_s->mlme.assocreq_ies) {
  551. os_memcpy(wpa_s->mlme.assocreq_ies, ies,
  552. wpa_s->mlme.assocreq_ies_len);
  553. }
  554. ieee80211_sta_tx(wpa_s, buf, blen);
  555. os_free(buf);
  556. }
  557. static void ieee80211_send_deauth(struct wpa_supplicant *wpa_s, u16 reason)
  558. {
  559. u8 *buf;
  560. size_t len;
  561. struct ieee80211_mgmt *mgmt;
  562. buf = os_zalloc(sizeof(*mgmt));
  563. if (buf == NULL) {
  564. wpa_printf(MSG_DEBUG, "MLME: failed to allocate buffer for "
  565. "deauth frame");
  566. return;
  567. }
  568. mgmt = (struct ieee80211_mgmt *) buf;
  569. len = 24;
  570. os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
  571. os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
  572. os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
  573. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  574. WLAN_FC_STYPE_DEAUTH);
  575. len += 2;
  576. mgmt->u.deauth.reason_code = host_to_le16(reason);
  577. ieee80211_sta_tx(wpa_s, buf, len);
  578. os_free(buf);
  579. }
  580. static void ieee80211_send_disassoc(struct wpa_supplicant *wpa_s, u16 reason)
  581. {
  582. u8 *buf;
  583. size_t len;
  584. struct ieee80211_mgmt *mgmt;
  585. buf = os_zalloc(sizeof(*mgmt));
  586. if (buf == NULL) {
  587. wpa_printf(MSG_DEBUG, "MLME: failed to allocate buffer for "
  588. "disassoc frame");
  589. return;
  590. }
  591. mgmt = (struct ieee80211_mgmt *) buf;
  592. len = 24;
  593. os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
  594. os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
  595. os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
  596. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  597. WLAN_FC_STYPE_DISASSOC);
  598. len += 2;
  599. mgmt->u.disassoc.reason_code = host_to_le16(reason);
  600. ieee80211_sta_tx(wpa_s, buf, len);
  601. os_free(buf);
  602. }
  603. static int ieee80211_privacy_mismatch(struct wpa_supplicant *wpa_s)
  604. {
  605. struct ieee80211_sta_bss *bss;
  606. int res = 0;
  607. if (wpa_s->mlme.mixed_cell ||
  608. wpa_s->mlme.key_mgmt != KEY_MGMT_NONE)
  609. return 0;
  610. bss = ieee80211_bss_get(wpa_s, wpa_s->bssid);
  611. if (bss == NULL)
  612. return 0;
  613. if (ieee80211_sta_wep_configured(wpa_s) !=
  614. !!(bss->capability & WLAN_CAPABILITY_PRIVACY))
  615. res = 1;
  616. return res;
  617. }
  618. static void ieee80211_associate(struct wpa_supplicant *wpa_s)
  619. {
  620. wpa_s->mlme.assoc_tries++;
  621. if (wpa_s->mlme.assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
  622. wpa_printf(MSG_DEBUG, "MLME: association with AP " MACSTR
  623. " timed out", MAC2STR(wpa_s->bssid));
  624. return;
  625. }
  626. wpa_s->mlme.state = IEEE80211_ASSOCIATE;
  627. wpa_printf(MSG_DEBUG, "MLME: associate with AP " MACSTR,
  628. MAC2STR(wpa_s->bssid));
  629. if (ieee80211_privacy_mismatch(wpa_s)) {
  630. wpa_printf(MSG_DEBUG, "MLME: mismatch in privacy "
  631. "configuration and mixed-cell disabled - abort "
  632. "association");
  633. return;
  634. }
  635. ieee80211_send_assoc(wpa_s);
  636. ieee80211_reschedule_timer(wpa_s, IEEE80211_ASSOC_TIMEOUT);
  637. }
  638. static void ieee80211_associated(struct wpa_supplicant *wpa_s)
  639. {
  640. int disassoc;
  641. /* TODO: start monitoring current AP signal quality and number of
  642. * missed beacons. Scan other channels every now and then and search
  643. * for better APs. */
  644. /* TODO: remove expired BSSes */
  645. wpa_s->mlme.state = IEEE80211_ASSOCIATED;
  646. #if 0 /* FIX */
  647. sta = sta_info_get(local, wpa_s->bssid);
  648. if (sta == NULL) {
  649. wpa_printf(MSG_DEBUG "MLME: No STA entry for own AP " MACSTR,
  650. MAC2STR(wpa_s->bssid));
  651. disassoc = 1;
  652. } else {
  653. disassoc = 0;
  654. if (time_after(jiffies,
  655. sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
  656. if (wpa_s->mlme.probereq_poll) {
  657. wpa_printf(MSG_DEBUG "MLME: No ProbeResp from "
  658. "current AP " MACSTR " - assume "
  659. "out of range",
  660. MAC2STR(wpa_s->bssid));
  661. disassoc = 1;
  662. } else {
  663. ieee80211_send_probe_req(
  664. wpa_s->bssid,
  665. wpa_s->mlme.scan_ssid,
  666. wpa_s->mlme.scan_ssid_len);
  667. wpa_s->mlme.probereq_poll = 1;
  668. }
  669. } else {
  670. wpa_s->mlme.probereq_poll = 0;
  671. if (time_after(jiffies, wpa_s->mlme.last_probe +
  672. IEEE80211_PROBE_INTERVAL)) {
  673. wpa_s->mlme.last_probe = jiffies;
  674. ieee80211_send_probe_req(wpa_s->bssid,
  675. wpa_s->mlme.ssid,
  676. wpa_s->mlme.ssid_len);
  677. }
  678. }
  679. sta_info_release(local, sta);
  680. }
  681. #else
  682. disassoc = 0;
  683. #endif
  684. if (disassoc) {
  685. wpa_supplicant_event(wpa_s, EVENT_DISASSOC, NULL);
  686. ieee80211_reschedule_timer(wpa_s,
  687. IEEE80211_MONITORING_INTERVAL +
  688. 30000);
  689. } else {
  690. ieee80211_reschedule_timer(wpa_s,
  691. IEEE80211_MONITORING_INTERVAL);
  692. }
  693. }
  694. static void ieee80211_send_probe_req(struct wpa_supplicant *wpa_s,
  695. const u8 *dst,
  696. const u8 *ssid, size_t ssid_len)
  697. {
  698. u8 *buf;
  699. size_t len;
  700. struct ieee80211_mgmt *mgmt;
  701. u8 *pos, *supp_rates;
  702. u8 *esupp_rates = NULL;
  703. int i;
  704. buf = os_malloc(sizeof(*mgmt) + 200 + wpa_s->mlme.extra_probe_ie_len);
  705. if (buf == NULL) {
  706. wpa_printf(MSG_DEBUG, "MLME: failed to allocate buffer for "
  707. "probe request");
  708. return;
  709. }
  710. mgmt = (struct ieee80211_mgmt *) buf;
  711. len = 24;
  712. os_memset(mgmt, 0, 24);
  713. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  714. WLAN_FC_STYPE_PROBE_REQ);
  715. os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
  716. if (dst) {
  717. os_memcpy(mgmt->da, dst, ETH_ALEN);
  718. os_memcpy(mgmt->bssid, dst, ETH_ALEN);
  719. } else {
  720. os_memset(mgmt->da, 0xff, ETH_ALEN);
  721. os_memset(mgmt->bssid, 0xff, ETH_ALEN);
  722. }
  723. pos = buf + len;
  724. len += 2 + ssid_len;
  725. *pos++ = WLAN_EID_SSID;
  726. *pos++ = ssid_len;
  727. os_memcpy(pos, ssid, ssid_len);
  728. supp_rates = buf + len;
  729. len += 2;
  730. supp_rates[0] = WLAN_EID_SUPP_RATES;
  731. supp_rates[1] = 0;
  732. for (i = 0; i < wpa_s->mlme.num_curr_rates; i++) {
  733. struct wpa_rate_data *rate = &wpa_s->mlme.curr_rates[i];
  734. if (esupp_rates) {
  735. pos = buf + len;
  736. len++;
  737. esupp_rates[1]++;
  738. } else if (supp_rates[1] == 8) {
  739. esupp_rates = pos;
  740. esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
  741. esupp_rates[1] = 1;
  742. pos = &esupp_rates[2];
  743. len += 3;
  744. } else {
  745. pos = buf + len;
  746. len++;
  747. supp_rates[1]++;
  748. }
  749. *pos++ = rate->rate / 5;
  750. }
  751. if (wpa_s->mlme.extra_probe_ie) {
  752. os_memcpy(pos, wpa_s->mlme.extra_probe_ie,
  753. wpa_s->mlme.extra_probe_ie_len);
  754. len += wpa_s->mlme.extra_probe_ie_len;
  755. }
  756. ieee80211_sta_tx(wpa_s, buf, len);
  757. os_free(buf);
  758. }
  759. static int ieee80211_sta_wep_configured(struct wpa_supplicant *wpa_s)
  760. {
  761. #if 0 /* FIX */
  762. if (sdata == NULL || sdata->default_key == NULL ||
  763. sdata->default_key->alg != ALG_WEP)
  764. return 0;
  765. return 1;
  766. #else
  767. return 0;
  768. #endif
  769. }
  770. static void ieee80211_auth_completed(struct wpa_supplicant *wpa_s)
  771. {
  772. wpa_printf(MSG_DEBUG, "MLME: authenticated");
  773. wpa_s->mlme.authenticated = 1;
  774. ieee80211_associate(wpa_s);
  775. }
  776. static void ieee80211_auth_challenge(struct wpa_supplicant *wpa_s,
  777. struct ieee80211_mgmt *mgmt,
  778. size_t len,
  779. struct ieee80211_rx_status *rx_status)
  780. {
  781. u8 *pos;
  782. struct ieee802_11_elems elems;
  783. wpa_printf(MSG_DEBUG, "MLME: replying to auth challenge");
  784. pos = mgmt->u.auth.variable;
  785. if (ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems)
  786. == ParseFailed) {
  787. wpa_printf(MSG_DEBUG, "MLME: failed to parse Auth(challenge)");
  788. return;
  789. }
  790. if (elems.challenge == NULL) {
  791. wpa_printf(MSG_DEBUG, "MLME: no challenge IE in shared key "
  792. "auth frame");
  793. return;
  794. }
  795. ieee80211_send_auth(wpa_s, 3, elems.challenge - 2,
  796. elems.challenge_len + 2, 1);
  797. }
  798. static void ieee80211_rx_mgmt_auth(struct wpa_supplicant *wpa_s,
  799. struct ieee80211_mgmt *mgmt,
  800. size_t len,
  801. struct ieee80211_rx_status *rx_status)
  802. {
  803. struct wpa_ssid *ssid = wpa_s->current_ssid;
  804. u16 auth_alg, auth_transaction, status_code;
  805. int adhoc;
  806. adhoc = ssid && ssid->mode == 1;
  807. if (wpa_s->mlme.state != IEEE80211_AUTHENTICATE && !adhoc) {
  808. wpa_printf(MSG_DEBUG, "MLME: authentication frame received "
  809. "from " MACSTR ", but not in authenticate state - "
  810. "ignored", MAC2STR(mgmt->sa));
  811. return;
  812. }
  813. if (len < 24 + 6) {
  814. wpa_printf(MSG_DEBUG, "MLME: too short (%lu) authentication "
  815. "frame received from " MACSTR " - ignored",
  816. (unsigned long) len, MAC2STR(mgmt->sa));
  817. return;
  818. }
  819. if (!adhoc && os_memcmp(wpa_s->bssid, mgmt->sa, ETH_ALEN) != 0) {
  820. wpa_printf(MSG_DEBUG, "MLME: authentication frame received "
  821. "from unknown AP (SA=" MACSTR " BSSID=" MACSTR
  822. ") - ignored",
  823. MAC2STR(mgmt->sa), MAC2STR(mgmt->bssid));
  824. return;
  825. }
  826. if (adhoc && os_memcmp(wpa_s->bssid, mgmt->bssid, ETH_ALEN) != 0) {
  827. wpa_printf(MSG_DEBUG, "MLME: authentication frame received "
  828. "from unknown BSSID (SA=" MACSTR " BSSID=" MACSTR
  829. ") - ignored",
  830. MAC2STR(mgmt->sa), MAC2STR(mgmt->bssid));
  831. return;
  832. }
  833. auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
  834. auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
  835. status_code = le_to_host16(mgmt->u.auth.status_code);
  836. wpa_printf(MSG_DEBUG, "MLME: RX authentication from " MACSTR
  837. " (alg=%d transaction=%d status=%d)",
  838. MAC2STR(mgmt->sa), auth_alg, auth_transaction, status_code);
  839. if (adhoc) {
  840. /* IEEE 802.11 standard does not require authentication in IBSS
  841. * networks and most implementations do not seem to use it.
  842. * However, try to reply to authentication attempts if someone
  843. * has actually implemented this.
  844. * TODO: Could implement shared key authentication. */
  845. if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1) {
  846. wpa_printf(MSG_DEBUG, "MLME: unexpected IBSS "
  847. "authentication frame (alg=%d "
  848. "transaction=%d)",
  849. auth_alg, auth_transaction);
  850. return;
  851. }
  852. ieee80211_send_auth(wpa_s, 2, NULL, 0, 0);
  853. }
  854. if (auth_alg != wpa_s->mlme.auth_alg ||
  855. auth_transaction != wpa_s->mlme.auth_transaction) {
  856. wpa_printf(MSG_DEBUG, "MLME: unexpected authentication frame "
  857. "(alg=%d transaction=%d)",
  858. auth_alg, auth_transaction);
  859. return;
  860. }
  861. if (status_code != WLAN_STATUS_SUCCESS) {
  862. wpa_printf(MSG_DEBUG, "MLME: AP denied authentication "
  863. "(auth_alg=%d code=%d)", wpa_s->mlme.auth_alg,
  864. status_code);
  865. if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
  866. const int num_algs = 3;
  867. u8 algs[num_algs];
  868. int i, pos;
  869. algs[0] = algs[1] = algs[2] = 0xff;
  870. if (wpa_s->mlme.auth_algs & IEEE80211_AUTH_ALG_OPEN)
  871. algs[0] = WLAN_AUTH_OPEN;
  872. if (wpa_s->mlme.auth_algs &
  873. IEEE80211_AUTH_ALG_SHARED_KEY)
  874. algs[1] = WLAN_AUTH_SHARED_KEY;
  875. if (wpa_s->mlme.auth_algs & IEEE80211_AUTH_ALG_LEAP)
  876. algs[2] = WLAN_AUTH_LEAP;
  877. if (wpa_s->mlme.auth_alg == WLAN_AUTH_OPEN)
  878. pos = 0;
  879. else if (wpa_s->mlme.auth_alg == WLAN_AUTH_SHARED_KEY)
  880. pos = 1;
  881. else
  882. pos = 2;
  883. for (i = 0; i < num_algs; i++) {
  884. pos++;
  885. if (pos >= num_algs)
  886. pos = 0;
  887. if (algs[pos] == wpa_s->mlme.auth_alg ||
  888. algs[pos] == 0xff)
  889. continue;
  890. if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
  891. !ieee80211_sta_wep_configured(wpa_s))
  892. continue;
  893. wpa_s->mlme.auth_alg = algs[pos];
  894. wpa_printf(MSG_DEBUG, "MLME: set auth_alg=%d "
  895. "for next try",
  896. wpa_s->mlme.auth_alg);
  897. break;
  898. }
  899. }
  900. return;
  901. }
  902. switch (wpa_s->mlme.auth_alg) {
  903. case WLAN_AUTH_OPEN:
  904. case WLAN_AUTH_LEAP:
  905. ieee80211_auth_completed(wpa_s);
  906. break;
  907. case WLAN_AUTH_SHARED_KEY:
  908. if (wpa_s->mlme.auth_transaction == 4)
  909. ieee80211_auth_completed(wpa_s);
  910. else
  911. ieee80211_auth_challenge(wpa_s, mgmt, len,
  912. rx_status);
  913. break;
  914. #ifdef CONFIG_IEEE80211R
  915. case WLAN_AUTH_FT:
  916. {
  917. union wpa_event_data data;
  918. os_memset(&data, 0, sizeof(data));
  919. data.ft_ies.ies = mgmt->u.auth.variable;
  920. data.ft_ies.ies_len = len -
  921. (mgmt->u.auth.variable - (u8 *) mgmt);
  922. os_memcpy(data.ft_ies.target_ap, wpa_s->bssid, ETH_ALEN);
  923. wpa_supplicant_event(wpa_s, EVENT_FT_RESPONSE, &data);
  924. ieee80211_auth_completed(wpa_s);
  925. break;
  926. }
  927. #endif /* CONFIG_IEEE80211R */
  928. }
  929. }
  930. static void ieee80211_rx_mgmt_deauth(struct wpa_supplicant *wpa_s,
  931. struct ieee80211_mgmt *mgmt,
  932. size_t len,
  933. struct ieee80211_rx_status *rx_status)
  934. {
  935. u16 reason_code;
  936. if (len < 24 + 2) {
  937. wpa_printf(MSG_DEBUG, "MLME: too short (%lu) deauthentication "
  938. "frame received from " MACSTR " - ignored",
  939. (unsigned long) len, MAC2STR(mgmt->sa));
  940. return;
  941. }
  942. if (os_memcmp(wpa_s->bssid, mgmt->sa, ETH_ALEN) != 0) {
  943. wpa_printf(MSG_DEBUG, "MLME: deauthentication frame received "
  944. "from unknown AP (SA=" MACSTR " BSSID=" MACSTR
  945. ") - ignored",
  946. MAC2STR(mgmt->sa), MAC2STR(mgmt->bssid));
  947. return;
  948. }
  949. reason_code = le_to_host16(mgmt->u.deauth.reason_code);
  950. wpa_printf(MSG_DEBUG, "MLME: RX deauthentication from " MACSTR
  951. " (reason=%d)", MAC2STR(mgmt->sa), reason_code);
  952. if (wpa_s->mlme.authenticated)
  953. wpa_printf(MSG_DEBUG, "MLME: deauthenticated");
  954. if (wpa_s->mlme.state == IEEE80211_AUTHENTICATE ||
  955. wpa_s->mlme.state == IEEE80211_ASSOCIATE ||
  956. wpa_s->mlme.state == IEEE80211_ASSOCIATED) {
  957. wpa_s->mlme.state = IEEE80211_AUTHENTICATE;
  958. ieee80211_reschedule_timer(wpa_s,
  959. IEEE80211_RETRY_AUTH_INTERVAL);
  960. }
  961. ieee80211_set_associated(wpa_s, 0);
  962. wpa_s->mlme.authenticated = 0;
  963. }
  964. static void ieee80211_rx_mgmt_disassoc(struct wpa_supplicant *wpa_s,
  965. struct ieee80211_mgmt *mgmt,
  966. size_t len,
  967. struct ieee80211_rx_status *rx_status)
  968. {
  969. u16 reason_code;
  970. if (len < 24 + 2) {
  971. wpa_printf(MSG_DEBUG, "MLME: too short (%lu) disassociation "
  972. "frame received from " MACSTR " - ignored",
  973. (unsigned long) len, MAC2STR(mgmt->sa));
  974. return;
  975. }
  976. if (os_memcmp(wpa_s->bssid, mgmt->sa, ETH_ALEN) != 0) {
  977. wpa_printf(MSG_DEBUG, "MLME: disassociation frame received "
  978. "from unknown AP (SA=" MACSTR " BSSID=" MACSTR
  979. ") - ignored",
  980. MAC2STR(mgmt->sa), MAC2STR(mgmt->bssid));
  981. return;
  982. }
  983. reason_code = le_to_host16(mgmt->u.disassoc.reason_code);
  984. wpa_printf(MSG_DEBUG, "MLME: RX disassociation from " MACSTR
  985. " (reason=%d)", MAC2STR(mgmt->sa), reason_code);
  986. if (wpa_s->mlme.associated)
  987. wpa_printf(MSG_DEBUG, "MLME: disassociated");
  988. if (wpa_s->mlme.state == IEEE80211_ASSOCIATED) {
  989. wpa_s->mlme.state = IEEE80211_ASSOCIATE;
  990. ieee80211_reschedule_timer(wpa_s,
  991. IEEE80211_RETRY_AUTH_INTERVAL);
  992. }
  993. ieee80211_set_associated(wpa_s, 0);
  994. }
  995. static int ieee80211_ft_assoc_resp(struct wpa_supplicant *wpa_s,
  996. struct ieee802_11_elems *elems)
  997. {
  998. #ifdef CONFIG_IEEE80211R
  999. const u8 *mobility_domain = NULL;
  1000. const u8 *r0kh_id = NULL;
  1001. size_t r0kh_id_len = 0;
  1002. const u8 *r1kh_id = NULL;
  1003. struct rsn_ftie *hdr;
  1004. const u8 *pos, *end;
  1005. if (elems->mdie && elems->mdie_len >= MOBILITY_DOMAIN_ID_LEN)
  1006. mobility_domain = elems->mdie;
  1007. if (elems->ftie && elems->ftie_len >= sizeof(struct rsn_ftie)) {
  1008. end = elems->ftie + elems->ftie_len;
  1009. hdr = (struct rsn_ftie *) elems->ftie;
  1010. pos = (const u8 *) (hdr + 1);
  1011. while (pos + 1 < end) {
  1012. if (pos + 2 + pos[1] > end)
  1013. break;
  1014. if (pos[0] == FTIE_SUBELEM_R1KH_ID &&
  1015. pos[1] == FT_R1KH_ID_LEN)
  1016. r1kh_id = pos + 2;
  1017. else if (pos[0] == FTIE_SUBELEM_R0KH_ID &&
  1018. pos[1] >= 1 && pos[1] <= FT_R0KH_ID_MAX_LEN) {
  1019. r0kh_id = pos + 2;
  1020. r0kh_id_len = pos[1];
  1021. }
  1022. pos += 2 + pos[1];
  1023. }
  1024. }
  1025. return wpa_sm_set_ft_params(wpa_s->wpa, mobility_domain, r0kh_id,
  1026. r0kh_id_len, r1kh_id);
  1027. #else /* CONFIG_IEEE80211R */
  1028. return 0;
  1029. #endif /* CONFIG_IEEE80211R */
  1030. }
  1031. static void ieee80211_rx_mgmt_assoc_resp(struct wpa_supplicant *wpa_s,
  1032. struct ieee80211_mgmt *mgmt,
  1033. size_t len,
  1034. struct ieee80211_rx_status *rx_status,
  1035. int reassoc)
  1036. {
  1037. u8 rates[32];
  1038. size_t rates_len;
  1039. u16 capab_info, status_code, aid;
  1040. struct ieee802_11_elems elems;
  1041. u8 *pos;
  1042. /* AssocResp and ReassocResp have identical structure, so process both
  1043. * of them in this function. */
  1044. if (wpa_s->mlme.state != IEEE80211_ASSOCIATE) {
  1045. wpa_printf(MSG_DEBUG, "MLME: association frame received from "
  1046. MACSTR ", but not in associate state - ignored",
  1047. MAC2STR(mgmt->sa));
  1048. return;
  1049. }
  1050. if (len < 24 + 6) {
  1051. wpa_printf(MSG_DEBUG, "MLME: too short (%lu) association "
  1052. "frame received from " MACSTR " - ignored",
  1053. (unsigned long) len, MAC2STR(mgmt->sa));
  1054. return;
  1055. }
  1056. if (os_memcmp(wpa_s->bssid, mgmt->sa, ETH_ALEN) != 0) {
  1057. wpa_printf(MSG_DEBUG, "MLME: association frame received from "
  1058. "unknown AP (SA=" MACSTR " BSSID=" MACSTR ") - "
  1059. "ignored", MAC2STR(mgmt->sa), MAC2STR(mgmt->bssid));
  1060. return;
  1061. }
  1062. capab_info = le_to_host16(mgmt->u.assoc_resp.capab_info);
  1063. status_code = le_to_host16(mgmt->u.assoc_resp.status_code);
  1064. aid = le_to_host16(mgmt->u.assoc_resp.aid);
  1065. if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
  1066. wpa_printf(MSG_DEBUG, "MLME: invalid aid value %d; bits 15:14 "
  1067. "not set", aid);
  1068. aid &= ~(BIT(15) | BIT(14));
  1069. wpa_printf(MSG_DEBUG, "MLME: RX %sssocResp from " MACSTR
  1070. " (capab=0x%x status=%d aid=%d)",
  1071. reassoc ? "Rea" : "A", MAC2STR(mgmt->sa),
  1072. capab_info, status_code, aid);
  1073. if (status_code != WLAN_STATUS_SUCCESS) {
  1074. wpa_printf(MSG_DEBUG, "MLME: AP denied association (code=%d)",
  1075. status_code);
  1076. return;
  1077. }
  1078. pos = mgmt->u.assoc_resp.variable;
  1079. if (ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems)
  1080. == ParseFailed) {
  1081. wpa_printf(MSG_DEBUG, "MLME: failed to parse AssocResp");
  1082. return;
  1083. }
  1084. if (elems.supp_rates == NULL) {
  1085. wpa_printf(MSG_DEBUG, "MLME: no SuppRates element in "
  1086. "AssocResp");
  1087. return;
  1088. }
  1089. if (wpa_s->mlme.auth_alg == WLAN_AUTH_FT) {
  1090. if (!reassoc) {
  1091. wpa_printf(MSG_DEBUG, "MLME: AP tried to use "
  1092. "association, not reassociation, response "
  1093. "with FT");
  1094. return;
  1095. }
  1096. if (wpa_ft_validate_reassoc_resp(
  1097. wpa_s->wpa, pos, len - (pos - (u8 *) mgmt),
  1098. mgmt->sa) < 0) {
  1099. wpa_printf(MSG_DEBUG, "MLME: FT validation of Reassoc"
  1100. "Resp failed");
  1101. return;
  1102. }
  1103. } else if (ieee80211_ft_assoc_resp(wpa_s, &elems) < 0)
  1104. return;
  1105. wpa_printf(MSG_DEBUG, "MLME: associated");
  1106. wpa_s->mlme.aid = aid;
  1107. wpa_s->mlme.ap_capab = capab_info;
  1108. os_free(wpa_s->mlme.assocresp_ies);
  1109. wpa_s->mlme.assocresp_ies_len = len - (pos - (u8 *) mgmt);
  1110. wpa_s->mlme.assocresp_ies = os_malloc(wpa_s->mlme.assocresp_ies_len);
  1111. if (wpa_s->mlme.assocresp_ies) {
  1112. os_memcpy(wpa_s->mlme.assocresp_ies, pos,
  1113. wpa_s->mlme.assocresp_ies_len);
  1114. }
  1115. ieee80211_set_associated(wpa_s, 1);
  1116. rates_len = elems.supp_rates_len;
  1117. if (rates_len > sizeof(rates))
  1118. rates_len = sizeof(rates);
  1119. os_memcpy(rates, elems.supp_rates, rates_len);
  1120. if (elems.ext_supp_rates) {
  1121. size_t _len = elems.ext_supp_rates_len;
  1122. if (_len > sizeof(rates) - rates_len)
  1123. _len = sizeof(rates) - rates_len;
  1124. os_memcpy(rates + rates_len, elems.ext_supp_rates, _len);
  1125. rates_len += _len;
  1126. }
  1127. if (wpa_drv_set_bssid(wpa_s, wpa_s->bssid) < 0) {
  1128. wpa_printf(MSG_DEBUG, "MLME: failed to set BSSID for the "
  1129. "netstack");
  1130. }
  1131. if (wpa_drv_set_ssid(wpa_s, wpa_s->mlme.ssid, wpa_s->mlme.ssid_len) <
  1132. 0) {
  1133. wpa_printf(MSG_DEBUG, "MLME: failed to set SSID for the "
  1134. "netstack");
  1135. }
  1136. /* Remove STA entry before adding a new one just in case to avoid
  1137. * problems with existing configuration (e.g., keys). */
  1138. wpa_drv_mlme_remove_sta(wpa_s, wpa_s->bssid);
  1139. if (wpa_drv_mlme_add_sta(wpa_s, wpa_s->bssid, rates, rates_len) < 0) {
  1140. wpa_printf(MSG_DEBUG, "MLME: failed to add STA entry to the "
  1141. "netstack");
  1142. }
  1143. #if 0 /* FIX? */
  1144. sta->assoc_ap = 1;
  1145. if (elems.wmm_param && wpa_s->mlme.wmm_enabled) {
  1146. sta->flags |= WLAN_STA_WME;
  1147. ieee80211_sta_wmm_params(wpa_s, elems.wmm_param,
  1148. elems.wmm_param_len);
  1149. }
  1150. #endif
  1151. ieee80211_associated(wpa_s);
  1152. }
  1153. /* Caller must hold local->sta_bss_lock */
  1154. static void __ieee80211_bss_hash_add(struct wpa_supplicant *wpa_s,
  1155. struct ieee80211_sta_bss *bss)
  1156. {
  1157. bss->hnext = wpa_s->mlme.sta_bss_hash[STA_HASH(bss->bssid)];
  1158. wpa_s->mlme.sta_bss_hash[STA_HASH(bss->bssid)] = bss;
  1159. }
  1160. /* Caller must hold local->sta_bss_lock */
  1161. static void __ieee80211_bss_hash_del(struct wpa_supplicant *wpa_s,
  1162. struct ieee80211_sta_bss *bss)
  1163. {
  1164. struct ieee80211_sta_bss *b, *prev = NULL;
  1165. b = wpa_s->mlme.sta_bss_hash[STA_HASH(bss->bssid)];
  1166. while (b) {
  1167. if (b == bss) {
  1168. if (prev == NULL) {
  1169. wpa_s->mlme.sta_bss_hash[STA_HASH(bss->bssid)]
  1170. = bss->hnext;
  1171. } else {
  1172. prev->hnext = bss->hnext;
  1173. }
  1174. break;
  1175. }
  1176. prev = b;
  1177. b = b->hnext;
  1178. }
  1179. }
  1180. static struct ieee80211_sta_bss *
  1181. ieee80211_bss_add(struct wpa_supplicant *wpa_s, const u8 *bssid)
  1182. {
  1183. struct ieee80211_sta_bss *bss;
  1184. bss = os_zalloc(sizeof(*bss));
  1185. if (bss == NULL)
  1186. return NULL;
  1187. os_memcpy(bss->bssid, bssid, ETH_ALEN);
  1188. /* TODO: order by RSSI? */
  1189. bss->next = wpa_s->mlme.sta_bss_list;
  1190. wpa_s->mlme.sta_bss_list = bss;
  1191. __ieee80211_bss_hash_add(wpa_s, bss);
  1192. return bss;
  1193. }
  1194. static struct ieee80211_sta_bss *
  1195. ieee80211_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid)
  1196. {
  1197. struct ieee80211_sta_bss *bss;
  1198. bss = wpa_s->mlme.sta_bss_hash[STA_HASH(bssid)];
  1199. while (bss) {
  1200. if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
  1201. break;
  1202. bss = bss->hnext;
  1203. }
  1204. return bss;
  1205. }
  1206. static void ieee80211_bss_free(struct wpa_supplicant *wpa_s,
  1207. struct ieee80211_sta_bss *bss)
  1208. {
  1209. __ieee80211_bss_hash_del(wpa_s, bss);
  1210. os_free(bss->ie);
  1211. os_free(bss->wpa_ie);
  1212. os_free(bss->rsn_ie);
  1213. os_free(bss->wmm_ie);
  1214. os_free(bss->mdie);
  1215. os_free(bss);
  1216. }
  1217. static void ieee80211_bss_list_deinit(struct wpa_supplicant *wpa_s)
  1218. {
  1219. struct ieee80211_sta_bss *bss, *prev;
  1220. bss = wpa_s->mlme.sta_bss_list;
  1221. wpa_s->mlme.sta_bss_list = NULL;
  1222. while (bss) {
  1223. prev = bss;
  1224. bss = bss->next;
  1225. ieee80211_bss_free(wpa_s, prev);
  1226. }
  1227. }
  1228. static void ieee80211_bss_info(struct wpa_supplicant *wpa_s,
  1229. struct ieee80211_mgmt *mgmt,
  1230. size_t len,
  1231. struct ieee80211_rx_status *rx_status,
  1232. int beacon)
  1233. {
  1234. struct ieee802_11_elems elems;
  1235. size_t baselen;
  1236. int channel, invalid = 0, clen;
  1237. struct ieee80211_sta_bss *bss;
  1238. u64 timestamp;
  1239. u8 *pos, *ie_pos;
  1240. size_t ie_len;
  1241. if (!beacon && os_memcmp(mgmt->da, wpa_s->own_addr, ETH_ALEN))
  1242. return; /* ignore ProbeResp to foreign address */
  1243. #if 0
  1244. wpa_printf(MSG_MSGDUMP, "MLME: RX %s from " MACSTR " to " MACSTR,
  1245. beacon ? "Beacon" : "Probe Response",
  1246. MAC2STR(mgmt->sa), MAC2STR(mgmt->da));
  1247. #endif
  1248. baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
  1249. if (baselen > len)
  1250. return;
  1251. pos = mgmt->u.beacon.timestamp;
  1252. timestamp = WPA_GET_LE64(pos);
  1253. #if 0 /* FIX */
  1254. if (local->conf.mode == IW_MODE_ADHOC && beacon &&
  1255. os_memcmp(mgmt->bssid, local->bssid, ETH_ALEN) == 0) {
  1256. #ifdef IEEE80211_IBSS_DEBUG
  1257. static unsigned long last_tsf_debug = 0;
  1258. u64 tsf;
  1259. if (local->hw->get_tsf)
  1260. tsf = local->hw->get_tsf(local->mdev);
  1261. else
  1262. tsf = -1LLU;
  1263. if (time_after(jiffies, last_tsf_debug + 5 * HZ)) {
  1264. wpa_printf(MSG_DEBUG, "RX beacon SA=" MACSTR " BSSID="
  1265. MACSTR " TSF=0x%llx BCN=0x%llx diff=%lld "
  1266. "@%ld",
  1267. MAC2STR(mgmt->sa), MAC2STR(mgmt->bssid),
  1268. tsf, timestamp, tsf - timestamp, jiffies);
  1269. last_tsf_debug = jiffies;
  1270. }
  1271. #endif /* IEEE80211_IBSS_DEBUG */
  1272. }
  1273. #endif
  1274. ie_pos = mgmt->u.beacon.variable;
  1275. ie_len = len - baselen;
  1276. if (ieee802_11_parse_elems(ie_pos, ie_len, &elems) == ParseFailed)
  1277. invalid = 1;
  1278. #if 0 /* FIX */
  1279. if (local->conf.mode == IW_MODE_ADHOC && elems.supp_rates &&
  1280. os_memcmp(mgmt->bssid, local->bssid, ETH_ALEN) == 0 &&
  1281. (sta = sta_info_get(local, mgmt->sa))) {
  1282. struct ieee80211_rate *rates;
  1283. size_t num_rates;
  1284. u32 supp_rates, prev_rates;
  1285. int i, j, oper_mode;
  1286. rates = local->curr_rates;
  1287. num_rates = local->num_curr_rates;
  1288. oper_mode = wpa_s->mlme.sta_scanning ?
  1289. local->scan_oper_phymode : local->conf.phymode;
  1290. for (i = 0; i < local->hw->num_modes; i++) {
  1291. struct ieee80211_hw_modes *mode = &local->hw->modes[i];
  1292. if (oper_mode == mode->mode) {
  1293. rates = mode->rates;
  1294. num_rates = mode->num_rates;
  1295. break;
  1296. }
  1297. }
  1298. supp_rates = 0;
  1299. for (i = 0; i < elems.supp_rates_len +
  1300. elems.ext_supp_rates_len; i++) {
  1301. u8 rate = 0;
  1302. int own_rate;
  1303. if (i < elems.supp_rates_len)
  1304. rate = elems.supp_rates[i];
  1305. else if (elems.ext_supp_rates)
  1306. rate = elems.ext_supp_rates
  1307. [i - elems.supp_rates_len];
  1308. own_rate = 5 * (rate & 0x7f);
  1309. if (oper_mode == MODE_ATHEROS_TURBO)
  1310. own_rate *= 2;
  1311. for (j = 0; j < num_rates; j++)
  1312. if (rates[j].rate == own_rate)
  1313. supp_rates |= BIT(j);
  1314. }
  1315. prev_rates = sta->supp_rates;
  1316. sta->supp_rates &= supp_rates;
  1317. if (sta->supp_rates == 0) {
  1318. /* No matching rates - this should not really happen.
  1319. * Make sure that at least one rate is marked
  1320. * supported to avoid issues with TX rate ctrl. */
  1321. sta->supp_rates = wpa_s->mlme.supp_rates_bits;
  1322. }
  1323. if (sta->supp_rates != prev_rates) {
  1324. wpa_printf(MSG_DEBUG, "MLME: updated supp_rates set "
  1325. "for " MACSTR " based on beacon info "
  1326. "(0x%x & 0x%x -> 0x%x)",
  1327. MAC2STR(sta->addr), prev_rates,
  1328. supp_rates, sta->supp_rates);
  1329. }
  1330. sta_info_release(local, sta);
  1331. }
  1332. #endif
  1333. if (elems.ssid == NULL)
  1334. return;
  1335. if (elems.ds_params && elems.ds_params_len == 1)
  1336. channel = elems.ds_params[0];
  1337. else
  1338. channel = rx_status->channel;
  1339. bss = ieee80211_bss_get(wpa_s, mgmt->bssid);
  1340. if (bss == NULL) {
  1341. bss = ieee80211_bss_add(wpa_s, mgmt->bssid);
  1342. if (bss == NULL)
  1343. return;
  1344. } else {
  1345. #if 0
  1346. /* TODO: order by RSSI? */
  1347. spin_lock_bh(&local->sta_bss_lock);
  1348. list_move_tail(&bss->list, &local->sta_bss_list);
  1349. spin_unlock_bh(&local->sta_bss_lock);
  1350. #endif
  1351. }
  1352. if (bss->probe_resp && beacon) {
  1353. /* Do not allow beacon to override data from Probe Response. */
  1354. return;
  1355. }
  1356. bss->beacon_int = le_to_host16(mgmt->u.beacon.beacon_int);
  1357. bss->capability = le_to_host16(mgmt->u.beacon.capab_info);
  1358. if (bss->ie == NULL || bss->ie_len < ie_len) {
  1359. os_free(bss->ie);
  1360. bss->ie = os_malloc(ie_len);
  1361. }
  1362. if (bss->ie) {
  1363. os_memcpy(bss->ie, ie_pos, ie_len);
  1364. bss->ie_len = ie_len;
  1365. }
  1366. if (elems.ssid && elems.ssid_len <= MAX_SSID_LEN) {
  1367. os_memcpy(bss->ssid, elems.ssid, elems.ssid_len);
  1368. bss->ssid_len = elems.ssid_len;
  1369. }
  1370. bss->supp_rates_len = 0;
  1371. if (elems.supp_rates) {
  1372. clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
  1373. if (clen > elems.supp_rates_len)
  1374. clen = elems.supp_rates_len;
  1375. os_memcpy(&bss->supp_rates[bss->supp_rates_len],
  1376. elems.supp_rates, clen);
  1377. bss->supp_rates_len += clen;
  1378. }
  1379. if (elems.ext_supp_rates) {
  1380. clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
  1381. if (clen > elems.ext_supp_rates_len)
  1382. clen = elems.ext_supp_rates_len;
  1383. os_memcpy(&bss->supp_rates[bss->supp_rates_len],
  1384. elems.ext_supp_rates, clen);
  1385. bss->supp_rates_len += clen;
  1386. }
  1387. if (elems.wpa &&
  1388. (bss->wpa_ie == NULL || bss->wpa_ie_len != elems.wpa_len ||
  1389. os_memcmp(bss->wpa_ie, elems.wpa, elems.wpa_len))) {
  1390. os_free(bss->wpa_ie);
  1391. bss->wpa_ie = os_malloc(elems.wpa_len + 2);
  1392. if (bss->wpa_ie) {
  1393. os_memcpy(bss->wpa_ie, elems.wpa - 2,
  1394. elems.wpa_len + 2);
  1395. bss->wpa_ie_len = elems.wpa_len + 2;
  1396. } else
  1397. bss->wpa_ie_len = 0;
  1398. } else if (!elems.wpa && bss->wpa_ie) {
  1399. os_free(bss->wpa_ie);
  1400. bss->wpa_ie = NULL;
  1401. bss->wpa_ie_len = 0;
  1402. }
  1403. if (elems.rsn &&
  1404. (bss->rsn_ie == NULL || bss->rsn_ie_len != elems.rsn_len ||
  1405. os_memcmp(bss->rsn_ie, elems.rsn, elems.rsn_len))) {
  1406. os_free(bss->rsn_ie);
  1407. bss->rsn_ie = os_malloc(elems.rsn_len + 2);
  1408. if (bss->rsn_ie) {
  1409. os_memcpy(bss->rsn_ie, elems.rsn - 2,
  1410. elems.rsn_len + 2);
  1411. bss->rsn_ie_len = elems.rsn_len + 2;
  1412. } else
  1413. bss->rsn_ie_len = 0;
  1414. } else if (!elems.rsn && bss->rsn_ie) {
  1415. os_free(bss->rsn_ie);
  1416. bss->rsn_ie = NULL;
  1417. bss->rsn_ie_len = 0;
  1418. }
  1419. if (elems.wmm_param &&
  1420. (bss->wmm_ie == NULL || bss->wmm_ie_len != elems.wmm_param_len ||
  1421. os_memcmp(bss->wmm_ie, elems.wmm_param, elems.wmm_param_len))) {
  1422. os_free(bss->wmm_ie);
  1423. bss->wmm_ie = os_malloc(elems.wmm_param_len + 2);
  1424. if (bss->wmm_ie) {
  1425. os_memcpy(bss->wmm_ie, elems.wmm_param - 2,
  1426. elems.wmm_param_len + 2);
  1427. bss->wmm_ie_len = elems.wmm_param_len + 2;
  1428. } else
  1429. bss->wmm_ie_len = 0;
  1430. } else if (!elems.wmm_param && bss->wmm_ie) {
  1431. os_free(bss->wmm_ie);
  1432. bss->wmm_ie = NULL;
  1433. bss->wmm_ie_len = 0;
  1434. }
  1435. #ifdef CONFIG_IEEE80211R
  1436. if (elems.mdie &&
  1437. (bss->mdie == NULL || bss->mdie_len != elems.mdie_len ||
  1438. os_memcmp(bss->mdie, elems.mdie, elems.mdie_len))) {
  1439. os_free(bss->mdie);
  1440. bss->mdie = os_malloc(elems.mdie_len + 2);
  1441. if (bss->mdie) {
  1442. os_memcpy(bss->mdie, elems.mdie - 2,
  1443. elems.mdie_len + 2);
  1444. bss->mdie_len = elems.mdie_len + 2;
  1445. } else
  1446. bss->mdie_len = 0;
  1447. } else if (!elems.mdie && bss->mdie) {
  1448. os_free(bss->mdie);
  1449. bss->mdie = NULL;
  1450. bss->mdie_len = 0;
  1451. }
  1452. #endif /* CONFIG_IEEE80211R */
  1453. bss->hw_mode = wpa_s->mlme.phymode;
  1454. bss->channel = channel;
  1455. bss->freq = wpa_s->mlme.freq;
  1456. if (channel != wpa_s->mlme.channel &&
  1457. (wpa_s->mlme.phymode == WPA_MODE_IEEE80211G ||
  1458. wpa_s->mlme.phymode == WPA_MODE_IEEE80211B) &&
  1459. channel >= 1 && channel <= 14) {
  1460. static const int freq_list[] = {
  1461. 2412, 2417, 2422, 2427, 2432, 2437, 2442,
  1462. 2447, 2452, 2457, 2462, 2467, 2472, 2484
  1463. };
  1464. /* IEEE 802.11g/b mode can receive packets from neighboring
  1465. * channels, so map the channel into frequency. */
  1466. bss->freq = freq_list[channel - 1];
  1467. }
  1468. bss->timestamp = timestamp;
  1469. os_get_time(&bss->last_update);
  1470. bss->rssi = rx_status->ssi;
  1471. if (!beacon)
  1472. bss->probe_resp++;
  1473. }
  1474. static void ieee80211_rx_mgmt_probe_resp(struct wpa_supplicant *wpa_s,
  1475. struct ieee80211_mgmt *mgmt,
  1476. size_t len,
  1477. struct ieee80211_rx_status *rx_status)
  1478. {
  1479. ieee80211_bss_info(wpa_s, mgmt, len, rx_status, 0);
  1480. }
  1481. static void ieee80211_rx_mgmt_beacon(struct wpa_supplicant *wpa_s,
  1482. struct ieee80211_mgmt *mgmt,
  1483. size_t len,
  1484. struct ieee80211_rx_status *rx_status)
  1485. {
  1486. int use_protection;
  1487. size_t baselen;
  1488. struct ieee802_11_elems elems;
  1489. ieee80211_bss_info(wpa_s, mgmt, len, rx_status, 1);
  1490. if (!wpa_s->mlme.associated ||
  1491. os_memcmp(wpa_s->bssid, mgmt->bssid, ETH_ALEN) != 0)
  1492. return;
  1493. /* Process beacon from the current BSS */
  1494. baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
  1495. if (baselen > len)
  1496. return;
  1497. if (ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen,
  1498. &elems) == ParseFailed)
  1499. return;
  1500. use_protection = 0;
  1501. if (elems.erp_info && elems.erp_info_len >= 1) {
  1502. use_protection =
  1503. (elems.erp_info[0] & ERP_INFO_USE_PROTECTION) != 0;
  1504. }
  1505. if (use_protection != !!wpa_s->mlme.use_protection) {
  1506. wpa_printf(MSG_DEBUG, "MLME: CTS protection %s (BSSID=" MACSTR
  1507. ")",
  1508. use_protection ? "enabled" : "disabled",
  1509. MAC2STR(wpa_s->bssid));
  1510. wpa_s->mlme.use_protection = use_protection ? 1 : 0;
  1511. wpa_s->mlme.cts_protect_erp_frames = use_protection;
  1512. }
  1513. if (elems.wmm_param && wpa_s->mlme.wmm_enabled) {
  1514. ieee80211_sta_wmm_params(wpa_s, elems.wmm_param,
  1515. elems.wmm_param_len);
  1516. }
  1517. }
  1518. static void ieee80211_rx_mgmt_probe_req(struct wpa_supplicant *wpa_s,
  1519. struct ieee80211_mgmt *mgmt,
  1520. size_t len,
  1521. struct ieee80211_rx_status *rx_status)
  1522. {
  1523. int tx_last_beacon, adhoc;
  1524. #if 0 /* FIX */
  1525. struct ieee80211_mgmt *resp;
  1526. #endif
  1527. u8 *pos, *end;
  1528. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1529. adhoc = ssid && ssid->mode == 1;
  1530. if (!adhoc || wpa_s->mlme.state != IEEE80211_IBSS_JOINED ||
  1531. len < 24 + 2 || wpa_s->mlme.probe_resp == NULL)
  1532. return;
  1533. #if 0 /* FIX */
  1534. if (local->hw->tx_last_beacon)
  1535. tx_last_beacon = local->hw->tx_last_beacon(local->mdev);
  1536. else
  1537. #endif
  1538. tx_last_beacon = 1;
  1539. #ifdef IEEE80211_IBSS_DEBUG
  1540. wpa_printf(MSG_DEBUG, "MLME: RX ProbeReq SA=" MACSTR " DA=" MACSTR
  1541. " BSSID=" MACSTR " (tx_last_beacon=%d)",
  1542. MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
  1543. MAC2STR(mgmt->bssid), tx_last_beacon);
  1544. #endif /* IEEE80211_IBSS_DEBUG */
  1545. if (!tx_last_beacon)
  1546. return;
  1547. if (os_memcmp(mgmt->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
  1548. os_memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
  1549. return;
  1550. end = ((u8 *) mgmt) + len;
  1551. pos = mgmt->u.probe_req.variable;
  1552. if (pos[0] != WLAN_EID_SSID ||
  1553. pos + 2 + pos[1] > end) {
  1554. wpa_printf(MSG_DEBUG, "MLME: Invalid SSID IE in ProbeReq from "
  1555. MACSTR, MAC2STR(mgmt->sa));
  1556. return;
  1557. }
  1558. if (pos[1] != 0 &&
  1559. (pos[1] != wpa_s->mlme.ssid_len ||
  1560. os_memcmp(pos + 2, wpa_s->mlme.ssid, wpa_s->mlme.ssid_len) != 0))
  1561. {
  1562. /* Ignore ProbeReq for foreign SSID */
  1563. return;
  1564. }
  1565. #if 0 /* FIX */
  1566. /* Reply with ProbeResp */
  1567. skb = skb_copy(wpa_s->mlme.probe_resp, GFP_ATOMIC);
  1568. if (skb == NULL)
  1569. return;
  1570. resp = (struct ieee80211_mgmt *) skb->data;
  1571. os_memcpy(resp->da, mgmt->sa, ETH_ALEN);
  1572. #ifdef IEEE80211_IBSS_DEBUG
  1573. wpa_printf(MSG_DEBUG, "MLME: Sending ProbeResp to " MACSTR,
  1574. MAC2STR(resp->da));
  1575. #endif /* IEEE80211_IBSS_DEBUG */
  1576. ieee80211_sta_tx(wpa_s, skb, 0, 1);
  1577. #endif
  1578. }
  1579. #ifdef CONFIG_IEEE80211R
  1580. static void ieee80211_rx_mgmt_ft_action(struct wpa_supplicant *wpa_s,
  1581. struct ieee80211_mgmt *mgmt,
  1582. size_t len,
  1583. struct ieee80211_rx_status *rx_status)
  1584. {
  1585. union wpa_event_data data;
  1586. u16 status;
  1587. u8 *sta_addr, *target_ap_addr;
  1588. if (len < 24 + 1 + sizeof(mgmt->u.action.u.ft_action_resp)) {
  1589. wpa_printf(MSG_DEBUG, "MLME: Too short FT Action frame");
  1590. return;
  1591. }
  1592. /*
  1593. * Only FT Action Response is needed for now since reservation
  1594. * protocol is not supported.
  1595. */
  1596. if (mgmt->u.action.u.ft_action_resp.action != 2) {
  1597. wpa_printf(MSG_DEBUG, "MLME: Unexpected FT Action %d",
  1598. mgmt->u.action.u.ft_action_resp.action);
  1599. return;
  1600. }
  1601. status = le_to_host16(mgmt->u.action.u.ft_action_resp.status_code);
  1602. sta_addr = mgmt->u.action.u.ft_action_resp.sta_addr;
  1603. target_ap_addr = mgmt->u.action.u.ft_action_resp.target_ap_addr;
  1604. wpa_printf(MSG_DEBUG, "MLME: Received FT Action Response: STA " MACSTR
  1605. " TargetAP " MACSTR " Status Code %d",
  1606. MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
  1607. if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
  1608. wpa_printf(MSG_DEBUG, "MLME: Foreign STA Address " MACSTR
  1609. " in FT Action Response", MAC2STR(sta_addr));
  1610. return;
  1611. }
  1612. if (status) {
  1613. wpa_printf(MSG_DEBUG, "MLME: FT Action Response indicates "
  1614. "failure (status code %d)", status);
  1615. /* TODO: report error to FT code(?) */
  1616. return;
  1617. }
  1618. os_memset(&data, 0, sizeof(data));
  1619. data.ft_ies.ies = mgmt->u.action.u.ft_action_resp.variable;
  1620. data.ft_ies.ies_len = len - (mgmt->u.action.u.ft_action_resp.variable -
  1621. (u8 *) mgmt);
  1622. data.ft_ies.ft_action = 1;
  1623. os_memcpy(data.ft_ies.target_ap, target_ap_addr, ETH_ALEN);
  1624. wpa_supplicant_event(wpa_s, EVENT_FT_RESPONSE, &data);
  1625. /* TODO: should only re-associate, if EVENT_FT_RESPONSE was processed
  1626. * successfully */
  1627. wpa_s->mlme.prev_bssid_set = 1;
  1628. wpa_s->mlme.auth_alg = WLAN_AUTH_FT;
  1629. os_memcpy(wpa_s->mlme.prev_bssid, wpa_s->bssid, ETH_ALEN);
  1630. os_memcpy(wpa_s->bssid, target_ap_addr, ETH_ALEN);
  1631. ieee80211_associate(wpa_s);
  1632. }
  1633. #endif /* CONFIG_IEEE80211R */
  1634. #ifdef CONFIG_IEEE80211W
  1635. /* MLME-PING.response */
  1636. static int ieee80211_sta_send_ping_resp(struct wpa_supplicant *wpa_s,
  1637. const u8 *addr, const u8 *trans_id)
  1638. {
  1639. struct ieee80211_mgmt *mgmt;
  1640. int res;
  1641. size_t len;
  1642. mgmt = os_zalloc(sizeof(*mgmt));
  1643. if (mgmt == NULL) {
  1644. wpa_printf(MSG_DEBUG, "MLME: Failed to allocate buffer for "
  1645. "ping action frame");
  1646. return -1;
  1647. }
  1648. len = 24;
  1649. os_memcpy(mgmt->da, addr, ETH_ALEN);
  1650. os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
  1651. os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
  1652. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  1653. WLAN_FC_STYPE_ACTION);
  1654. mgmt->u.action.category = WLAN_ACTION_PING;
  1655. mgmt->u.action.u.ping_resp.action = WLAN_PING_RESPONSE;
  1656. os_memcpy(mgmt->u.action.u.ping_resp.trans_id, trans_id,
  1657. WLAN_PING_TRANS_ID_LEN);
  1658. len += 1 + sizeof(mgmt->u.action.u.ping_resp);
  1659. res = ieee80211_sta_tx(wpa_s, (u8 *) mgmt, len);
  1660. os_free(mgmt);
  1661. return res;
  1662. }
  1663. static void ieee80211_rx_mgmt_ping_action(
  1664. struct wpa_supplicant *wpa_s, struct ieee80211_mgmt *mgmt, size_t len,
  1665. struct ieee80211_rx_status *rx_status)
  1666. {
  1667. if (len < 24 + 1 + sizeof(mgmt->u.action.u.ping_req)) {
  1668. wpa_printf(MSG_DEBUG, "MLME: Too short Ping Action frame");
  1669. return;
  1670. }
  1671. if (mgmt->u.action.u.ping_req.action != WLAN_PING_REQUEST) {
  1672. wpa_printf(MSG_DEBUG, "MLME: Unexpected Ping Action %d",
  1673. mgmt->u.action.u.ping_req.action);
  1674. return;
  1675. }
  1676. if (os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) != 0) {
  1677. wpa_printf(MSG_DEBUG, "MLME: Ignore ping from unknown source "
  1678. MACSTR, MAC2STR(mgmt->sa));
  1679. return;
  1680. }
  1681. if (wpa_s->mlme.state == IEEE80211_ASSOCIATE) {
  1682. wpa_printf(MSG_DEBUG, "MLME: Ignore ping request during "
  1683. "association process");
  1684. return;
  1685. }
  1686. wpa_printf(MSG_DEBUG, "MLME: Replying to ping request");
  1687. ieee80211_sta_send_ping_resp(wpa_s, mgmt->sa,
  1688. mgmt->u.action.u.ping_req.trans_id);
  1689. }
  1690. #endif /* CONFIG_IEEE80211W */
  1691. static void ieee80211_rx_mgmt_action(struct wpa_supplicant *wpa_s,
  1692. struct ieee80211_mgmt *mgmt,
  1693. size_t len,
  1694. struct ieee80211_rx_status *rx_status)
  1695. {
  1696. wpa_printf(MSG_DEBUG, "MLME: received Action frame");
  1697. if (len < 25)
  1698. return;
  1699. switch (mgmt->u.action.category) {
  1700. #ifdef CONFIG_IEEE80211R
  1701. case WLAN_ACTION_FT:
  1702. ieee80211_rx_mgmt_ft_action(wpa_s, mgmt, len, rx_status);
  1703. break;
  1704. #endif /* CONFIG_IEEE80211R */
  1705. case WLAN_ACTION_PING:
  1706. ieee80211_rx_mgmt_ping_action(wpa_s, mgmt, len, rx_status);
  1707. break;
  1708. #ifdef CONFIG_IEEE80211W
  1709. #endif /* CONFIG_IEEE80211W */
  1710. default:
  1711. wpa_printf(MSG_DEBUG, "MLME: unknown Action Category %d",
  1712. mgmt->u.action.category);
  1713. break;
  1714. }
  1715. }
  1716. static void ieee80211_sta_rx_mgmt(struct wpa_supplicant *wpa_s,
  1717. const u8 *buf, size_t len,
  1718. struct ieee80211_rx_status *rx_status)
  1719. {
  1720. struct ieee80211_mgmt *mgmt;
  1721. u16 fc;
  1722. if (len < 24)
  1723. return;
  1724. mgmt = (struct ieee80211_mgmt *) buf;
  1725. fc = le_to_host16(mgmt->frame_control);
  1726. switch (WLAN_FC_GET_STYPE(fc)) {
  1727. case WLAN_FC_STYPE_PROBE_REQ:
  1728. ieee80211_rx_mgmt_probe_req(wpa_s, mgmt, len, rx_status);
  1729. break;
  1730. case WLAN_FC_STYPE_PROBE_RESP:
  1731. ieee80211_rx_mgmt_probe_resp(wpa_s, mgmt, len, rx_status);
  1732. break;
  1733. case WLAN_FC_STYPE_BEACON:
  1734. ieee80211_rx_mgmt_beacon(wpa_s, mgmt, len, rx_status);
  1735. break;
  1736. case WLAN_FC_STYPE_AUTH:
  1737. ieee80211_rx_mgmt_auth(wpa_s, mgmt, len, rx_status);
  1738. break;
  1739. case WLAN_FC_STYPE_ASSOC_RESP:
  1740. ieee80211_rx_mgmt_assoc_resp(wpa_s, mgmt, len, rx_status, 0);
  1741. break;
  1742. case WLAN_FC_STYPE_REASSOC_RESP:
  1743. ieee80211_rx_mgmt_assoc_resp(wpa_s, mgmt, len, rx_status, 1);
  1744. break;
  1745. case WLAN_FC_STYPE_DEAUTH:
  1746. ieee80211_rx_mgmt_deauth(wpa_s, mgmt, len, rx_status);
  1747. break;
  1748. case WLAN_FC_STYPE_DISASSOC:
  1749. ieee80211_rx_mgmt_disassoc(wpa_s, mgmt, len, rx_status);
  1750. break;
  1751. case WLAN_FC_STYPE_ACTION:
  1752. ieee80211_rx_mgmt_action(wpa_s, mgmt, len, rx_status);
  1753. break;
  1754. default:
  1755. wpa_printf(MSG_DEBUG, "MLME: received unknown management "
  1756. "frame - stype=%d", WLAN_FC_GET_STYPE(fc));
  1757. break;
  1758. }
  1759. }
  1760. static void ieee80211_sta_rx_scan(struct wpa_supplicant *wpa_s,
  1761. const u8 *buf, size_t len,
  1762. struct ieee80211_rx_status *rx_status)
  1763. {
  1764. struct ieee80211_mgmt *mgmt;
  1765. u16 fc;
  1766. if (len < 24)
  1767. return;
  1768. mgmt = (struct ieee80211_mgmt *) buf;
  1769. fc = le_to_host16(mgmt->frame_control);
  1770. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT) {
  1771. if (WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
  1772. ieee80211_rx_mgmt_probe_resp(wpa_s, mgmt,
  1773. len, rx_status);
  1774. } else if (WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON) {
  1775. ieee80211_rx_mgmt_beacon(wpa_s, mgmt, len, rx_status);
  1776. }
  1777. }
  1778. }
  1779. static int ieee80211_sta_active_ibss(struct wpa_supplicant *wpa_s)
  1780. {
  1781. int active = 0;
  1782. #if 0 /* FIX */
  1783. list_for_each(ptr, &local->sta_list) {
  1784. sta = list_entry(ptr, struct sta_info, list);
  1785. if (sta->dev == dev &&
  1786. time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
  1787. jiffies)) {
  1788. active++;
  1789. break;
  1790. }
  1791. }
  1792. #endif
  1793. return active;
  1794. }
  1795. static void ieee80211_sta_expire(struct wpa_supplicant *wpa_s)
  1796. {
  1797. #if 0 /* FIX */
  1798. list_for_each_safe(ptr, n, &local->sta_list) {
  1799. sta = list_entry(ptr, struct sta_info, list);
  1800. if (time_after(jiffies, sta->last_rx +
  1801. IEEE80211_IBSS_INACTIVITY_LIMIT)) {
  1802. wpa_printf(MSG_DEBUG, "MLME: expiring inactive STA "
  1803. MACSTR, MAC2STR(sta->addr));
  1804. sta_info_free(local, sta, 1);
  1805. }
  1806. }
  1807. #endif
  1808. }
  1809. static void ieee80211_sta_merge_ibss(struct wpa_supplicant *wpa_s)
  1810. {
  1811. ieee80211_reschedule_timer(wpa_s, IEEE80211_IBSS_MERGE_INTERVAL);
  1812. ieee80211_sta_expire(wpa_s);
  1813. if (ieee80211_sta_active_ibss(wpa_s))
  1814. return;
  1815. wpa_printf(MSG_DEBUG, "MLME: No active IBSS STAs - trying to scan for "
  1816. "other IBSS networks with same SSID (merge)");
  1817. ieee80211_sta_req_scan(wpa_s, wpa_s->mlme.ssid, wpa_s->mlme.ssid_len);
  1818. }
  1819. static void ieee80211_sta_timer(void *eloop_ctx, void *timeout_ctx)
  1820. {
  1821. struct wpa_supplicant *wpa_s = eloop_ctx;
  1822. switch (wpa_s->mlme.state) {
  1823. case IEEE80211_DISABLED:
  1824. break;
  1825. case IEEE80211_AUTHENTICATE:
  1826. ieee80211_authenticate(wpa_s);
  1827. break;
  1828. case IEEE80211_ASSOCIATE:
  1829. ieee80211_associate(wpa_s);
  1830. break;
  1831. case IEEE80211_ASSOCIATED:
  1832. ieee80211_associated(wpa_s);
  1833. break;
  1834. case IEEE80211_IBSS_SEARCH:
  1835. ieee80211_sta_find_ibss(wpa_s);
  1836. break;
  1837. case IEEE80211_IBSS_JOINED:
  1838. ieee80211_sta_merge_ibss(wpa_s);
  1839. break;
  1840. default:
  1841. wpa_printf(MSG_DEBUG, "ieee80211_sta_timer: Unknown state %d",
  1842. wpa_s->mlme.state);
  1843. break;
  1844. }
  1845. if (ieee80211_privacy_mismatch(wpa_s)) {
  1846. wpa_printf(MSG_DEBUG, "MLME: privacy configuration mismatch "
  1847. "and mixed-cell disabled - disassociate");
  1848. ieee80211_send_disassoc(wpa_s, WLAN_REASON_UNSPECIFIED);
  1849. ieee80211_set_associated(wpa_s, 0);
  1850. }
  1851. }
  1852. static void ieee80211_sta_new_auth(struct wpa_supplicant *wpa_s)
  1853. {
  1854. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1855. if (ssid && ssid->mode != 0)
  1856. return;
  1857. #if 0 /* FIX */
  1858. if (local->hw->reset_tsf) {
  1859. /* Reset own TSF to allow time synchronization work. */
  1860. local->hw->reset_tsf(local->mdev);
  1861. }
  1862. #endif
  1863. wpa_s->mlme.wmm_last_param_set = -1; /* allow any WMM update */
  1864. if (wpa_s->mlme.auth_algs & IEEE80211_AUTH_ALG_OPEN)
  1865. wpa_s->mlme.auth_alg = WLAN_AUTH_OPEN;
  1866. else if (wpa_s->mlme.auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
  1867. wpa_s->mlme.auth_alg = WLAN_AUTH_SHARED_KEY;
  1868. else if (wpa_s->mlme.auth_algs & IEEE80211_AUTH_ALG_LEAP)
  1869. wpa_s->mlme.auth_alg = WLAN_AUTH_LEAP;
  1870. else
  1871. wpa_s->mlme.auth_alg = WLAN_AUTH_OPEN;
  1872. wpa_printf(MSG_DEBUG, "MLME: Initial auth_alg=%d",
  1873. wpa_s->mlme.auth_alg);
  1874. wpa_s->mlme.auth_transaction = -1;
  1875. wpa_s->mlme.auth_tries = wpa_s->mlme.assoc_tries = 0;
  1876. ieee80211_authenticate(wpa_s);
  1877. }
  1878. static int ieee80211_ibss_allowed(struct wpa_supplicant *wpa_s)
  1879. {
  1880. #if 0 /* FIX */
  1881. int m, c;
  1882. for (m = 0; m < local->hw->num_modes; m++) {
  1883. struct ieee80211_hw_modes *mode = &local->hw->modes[m];
  1884. if (mode->mode != local->conf.phymode)
  1885. continue;
  1886. for (c = 0; c < mode->num_channels; c++) {
  1887. struct ieee80211_channel *chan = &mode->channels[c];
  1888. if (chan->flag & IEEE80211_CHAN_W_SCAN &&
  1889. chan->chan == local->conf.channel) {
  1890. if (chan->flag & IEEE80211_CHAN_W_IBSS)
  1891. return 1;
  1892. break;
  1893. }
  1894. }
  1895. }
  1896. #endif
  1897. return 0;
  1898. }
  1899. static int ieee80211_sta_join_ibss(struct wpa_supplicant *wpa_s,
  1900. struct ieee80211_sta_bss *bss)
  1901. {
  1902. int res = 0, rates, done = 0;
  1903. struct ieee80211_mgmt *mgmt;
  1904. #if 0 /* FIX */
  1905. struct ieee80211_tx_control control;
  1906. struct ieee80211_rate *rate;
  1907. struct rate_control_extra extra;
  1908. #endif
  1909. u8 *pos, *buf;
  1910. size_t len;
  1911. /* Remove possible STA entries from other IBSS networks. */
  1912. #if 0 /* FIX */
  1913. sta_info_flush(local, NULL);
  1914. if (local->hw->reset_tsf) {
  1915. /* Reset own TSF to allow time synchronization work. */
  1916. local->hw->reset_tsf(local->mdev);
  1917. }
  1918. #endif
  1919. os_memcpy(wpa_s->bssid, bss->bssid, ETH_ALEN);
  1920. #if 0 /* FIX */
  1921. local->conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
  1922. sdata->drop_unencrypted = bss->capability &
  1923. host_to_le16(WLAN_CAPABILITY_PRIVACY) ? 1 : 0;
  1924. #endif
  1925. #if 0 /* FIX */
  1926. os_memset(&rq, 0, sizeof(rq));
  1927. rq.m = bss->freq * 100000;
  1928. rq.e = 1;
  1929. res = ieee80211_ioctl_siwfreq(wpa_s, NULL, &rq, NULL);
  1930. #endif
  1931. if (!ieee80211_ibss_allowed(wpa_s)) {
  1932. #if 0 /* FIX */
  1933. wpa_printf(MSG_DEBUG, "MLME: IBSS not allowed on channel %d "
  1934. "(%d MHz)", local->conf.channel,
  1935. local->conf.freq);
  1936. #endif
  1937. return -1;
  1938. }
  1939. /* Set beacon template based on scan results */
  1940. buf = os_malloc(400);
  1941. len = 0;
  1942. do {
  1943. if (buf == NULL)
  1944. break;
  1945. mgmt = (struct ieee80211_mgmt *) buf;
  1946. len += 24 + sizeof(mgmt->u.beacon);
  1947. os_memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
  1948. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  1949. WLAN_FC_STYPE_BEACON);
  1950. os_memset(mgmt->da, 0xff, ETH_ALEN);
  1951. os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
  1952. os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
  1953. #if 0 /* FIX */
  1954. mgmt->u.beacon.beacon_int =
  1955. host_to_le16(local->conf.beacon_int);
  1956. #endif
  1957. mgmt->u.beacon.capab_info = host_to_le16(bss->capability);
  1958. pos = buf + len;
  1959. len += 2 + wpa_s->mlme.ssid_len;
  1960. *pos++ = WLAN_EID_SSID;
  1961. *pos++ = wpa_s->mlme.ssid_len;
  1962. os_memcpy(pos, wpa_s->mlme.ssid, wpa_s->mlme.ssid_len);
  1963. rates = bss->supp_rates_len;
  1964. if (rates > 8)
  1965. rates = 8;
  1966. pos = buf + len;
  1967. len += 2 + rates;
  1968. *pos++ = WLAN_EID_SUPP_RATES;
  1969. *pos++ = rates;
  1970. os_memcpy(pos, bss->supp_rates, rates);
  1971. pos = buf + len;
  1972. len += 2 + 1;
  1973. *pos++ = WLAN_EID_DS_PARAMS;
  1974. *pos++ = 1;
  1975. *pos++ = bss->channel;
  1976. pos = buf + len;
  1977. len += 2 + 2;
  1978. *pos++ = WLAN_EID_IBSS_PARAMS;
  1979. *pos++ = 2;
  1980. /* FIX: set ATIM window based on scan results */
  1981. *pos++ = 0;
  1982. *pos++ = 0;
  1983. if (bss->supp_rates_len > 8) {
  1984. rates = bss->supp_rates_len - 8;
  1985. pos = buf + len;
  1986. len += 2 + rates;
  1987. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  1988. *pos++ = rates;
  1989. os_memcpy(pos, &bss->supp_rates[8], rates);
  1990. }
  1991. #if 0 /* FIX */
  1992. os_memset(&control, 0, sizeof(control));
  1993. control.pkt_type = PKT_PROBE_RESP;
  1994. os_memset(&extra, 0, sizeof(extra));
  1995. extra.endidx = local->num_curr_rates;
  1996. rate = rate_control_get_rate(wpa_s, skb, &extra);
  1997. if (rate == NULL) {
  1998. wpa_printf(MSG_DEBUG, "MLME: Failed to determine TX "
  1999. "rate for IBSS beacon");
  2000. break;
  2001. }
  2002. control.tx_rate = (wpa_s->mlme.short_preamble &&
  2003. (rate->flags & IEEE80211_RATE_PREAMBLE2)) ?
  2004. rate->val2 : rate->val;
  2005. control.antenna_sel = local->conf.antenna_sel;
  2006. control.power_level = local->conf.power_level;
  2007. control.no_ack = 1;
  2008. control.retry_limit = 1;
  2009. control.rts_cts_duration = 0;
  2010. #endif
  2011. #if 0 /* FIX */
  2012. wpa_s->mlme.probe_resp = skb_copy(skb, GFP_ATOMIC);
  2013. if (wpa_s->mlme.probe_resp) {
  2014. mgmt = (struct ieee80211_mgmt *)
  2015. wpa_s->mlme.probe_resp->data;
  2016. mgmt->frame_control =
  2017. IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  2018. WLAN_FC_STYPE_PROBE_RESP);
  2019. } else {
  2020. wpa_printf(MSG_DEBUG, "MLME: Could not allocate "
  2021. "ProbeResp template for IBSS");
  2022. }
  2023. if (local->hw->beacon_update &&
  2024. local->hw->beacon_update(wpa_s, skb, &control) == 0) {
  2025. wpa_printf(MSG_DEBUG, "MLME: Configured IBSS beacon "
  2026. "template based on scan results");
  2027. skb = NULL;
  2028. }
  2029. rates = 0;
  2030. for (i = 0; i < bss->supp_rates_len; i++) {
  2031. int rate = (bss->supp_rates[i] & 0x7f) * 5;
  2032. if (local->conf.phymode == MODE_ATHEROS_TURBO)
  2033. rate *= 2;
  2034. for (j = 0; j < local->num_curr_rates; j++)
  2035. if (local->curr_rates[j].rate == rate)
  2036. rates |= BIT(j);
  2037. }
  2038. wpa_s->mlme.supp_rates_bits = rates;
  2039. #endif
  2040. done = 1;
  2041. } while (0);
  2042. os_free(buf);
  2043. if (!done) {
  2044. wpa_printf(MSG_DEBUG, "MLME: Failed to configure IBSS beacon "
  2045. "template");
  2046. }
  2047. wpa_s->mlme.state = IEEE80211_IBSS_JOINED;
  2048. ieee80211_reschedule_timer(wpa_s, IEEE80211_IBSS_MERGE_INTERVAL);
  2049. return res;
  2050. }
  2051. #if 0 /* FIX */
  2052. static int ieee80211_sta_create_ibss(struct wpa_supplicant *wpa_s)
  2053. {
  2054. struct ieee80211_sta_bss *bss;
  2055. u8 bssid[ETH_ALEN], *pos;
  2056. int i;
  2057. #if 0
  2058. /* Easier testing, use fixed BSSID. */
  2059. os_memset(bssid, 0xfe, ETH_ALEN);
  2060. #else
  2061. /* Generate random, not broadcast, locally administered BSSID. Mix in
  2062. * own MAC address to make sure that devices that do not have proper
  2063. * random number generator get different BSSID. */
  2064. os_get_random(bssid, ETH_ALEN);
  2065. for (i = 0; i < ETH_ALEN; i++)
  2066. bssid[i] ^= wpa_s->own_addr[i];
  2067. bssid[0] &= ~0x01;
  2068. bssid[0] |= 0x02;
  2069. #endif
  2070. wpa_printf(MSG_DEBUG, "MLME: Creating new IBSS network, BSSID "
  2071. MACSTR "", MAC2STR(bssid));
  2072. bss = ieee80211_bss_add(wpa_s, bssid);
  2073. if (bss == NULL)
  2074. return -ENOMEM;
  2075. #if 0 /* FIX */
  2076. if (local->conf.beacon_int == 0)
  2077. local->conf.beacon_int = 100;
  2078. bss->beacon_int = local->conf.beacon_int;
  2079. bss->hw_mode = local->conf.phymode;
  2080. bss->channel = local->conf.channel;
  2081. bss->freq = local->conf.freq;
  2082. #endif
  2083. os_get_time(&bss->last_update);
  2084. bss->capability = host_to_le16(WLAN_CAPABILITY_IBSS);
  2085. #if 0 /* FIX */
  2086. if (sdata->default_key) {
  2087. bss->capability |= host_to_le16(WLAN_CAPABILITY_PRIVACY);
  2088. } else
  2089. sdata->drop_unencrypted = 0;
  2090. bss->supp_rates_len = local->num_curr_rates;
  2091. #endif
  2092. pos = bss->supp_rates;
  2093. #if 0 /* FIX */
  2094. for (i = 0; i < local->num_curr_rates; i++) {
  2095. int rate = local->curr_rates[i].rate;
  2096. if (local->conf.phymode == MODE_ATHEROS_TURBO)
  2097. rate /= 2;
  2098. *pos++ = (u8) (rate / 5);
  2099. }
  2100. #endif
  2101. return ieee80211_sta_join_ibss(wpa_s, bss);
  2102. }
  2103. #endif
  2104. static int ieee80211_sta_find_ibss(struct wpa_supplicant *wpa_s)
  2105. {
  2106. struct ieee80211_sta_bss *bss;
  2107. int found = 0;
  2108. u8 bssid[ETH_ALEN];
  2109. int active_ibss;
  2110. struct os_time now;
  2111. if (wpa_s->mlme.ssid_len == 0)
  2112. return -EINVAL;
  2113. active_ibss = ieee80211_sta_active_ibss(wpa_s);
  2114. #ifdef IEEE80211_IBSS_DEBUG
  2115. wpa_printf(MSG_DEBUG, "MLME: sta_find_ibss (active_ibss=%d)",
  2116. active_ibss);
  2117. #endif /* IEEE80211_IBSS_DEBUG */
  2118. for (bss = wpa_s->mlme.sta_bss_list; bss; bss = bss->next) {
  2119. if (wpa_s->mlme.ssid_len != bss->ssid_len ||
  2120. os_memcmp(wpa_s->mlme.ssid, bss->ssid, bss->ssid_len) != 0
  2121. || !(bss->capability & WLAN_CAPABILITY_IBSS))
  2122. continue;
  2123. #ifdef IEEE80211_IBSS_DEBUG
  2124. wpa_printf(MSG_DEBUG, " bssid=" MACSTR " found",
  2125. MAC2STR(bss->bssid));
  2126. #endif /* IEEE80211_IBSS_DEBUG */
  2127. os_memcpy(bssid, bss->bssid, ETH_ALEN);
  2128. found = 1;
  2129. if (active_ibss ||
  2130. os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)
  2131. break;
  2132. }
  2133. #ifdef IEEE80211_IBSS_DEBUG
  2134. wpa_printf(MSG_DEBUG, " sta_find_ibss: selected " MACSTR " current "
  2135. MACSTR, MAC2STR(bssid), MAC2STR(wpa_s->bssid));
  2136. #endif /* IEEE80211_IBSS_DEBUG */
  2137. if (found && os_memcmp(wpa_s->bssid, bssid, ETH_ALEN) != 0 &&
  2138. (bss = ieee80211_bss_get(wpa_s, bssid))) {
  2139. wpa_printf(MSG_DEBUG, "MLME: Selected IBSS BSSID " MACSTR
  2140. " based on configured SSID",
  2141. MAC2STR(bssid));
  2142. return ieee80211_sta_join_ibss(wpa_s, bss);
  2143. }
  2144. #ifdef IEEE80211_IBSS_DEBUG
  2145. wpa_printf(MSG_DEBUG, " did not try to join ibss");
  2146. #endif /* IEEE80211_IBSS_DEBUG */
  2147. /* Selected IBSS not found in current scan results - try to scan */
  2148. os_get_time(&now);
  2149. #if 0 /* FIX */
  2150. if (wpa_s->mlme.state == IEEE80211_IBSS_JOINED &&
  2151. !ieee80211_sta_active_ibss(wpa_s)) {
  2152. ieee80211_reschedule_timer(wpa_s,
  2153. IEEE80211_IBSS_MERGE_INTERVAL);
  2154. } else if (time_after(jiffies, wpa_s->mlme.last_scan_completed +
  2155. IEEE80211_SCAN_INTERVAL)) {
  2156. wpa_printf(MSG_DEBUG, "MLME: Trigger new scan to find an IBSS "
  2157. "to join");
  2158. return ieee80211_sta_req_scan(wpa_s->mlme.ssid,
  2159. wpa_s->mlme.ssid_len);
  2160. } else if (wpa_s->mlme.state != IEEE80211_IBSS_JOINED) {
  2161. int interval = IEEE80211_SCAN_INTERVAL;
  2162. if (time_after(jiffies, wpa_s->mlme.ibss_join_req +
  2163. IEEE80211_IBSS_JOIN_TIMEOUT)) {
  2164. if (wpa_s->mlme.create_ibss &&
  2165. ieee80211_ibss_allowed(wpa_s))
  2166. return ieee80211_sta_create_ibss(wpa_s);
  2167. if (wpa_s->mlme.create_ibss) {
  2168. wpa_printf(MSG_DEBUG, "MLME: IBSS not allowed "
  2169. "on the configured channel %d "
  2170. "(%d MHz)",
  2171. local->conf.channel,
  2172. local->conf.freq);
  2173. }
  2174. /* No IBSS found - decrease scan interval and continue
  2175. * scanning. */
  2176. interval = IEEE80211_SCAN_INTERVAL_SLOW;
  2177. }
  2178. wpa_s->mlme.state = IEEE80211_IBSS_SEARCH;
  2179. ieee80211_reschedule_timer(wpa_s, interval);
  2180. return 0;
  2181. }
  2182. #endif
  2183. return 0;
  2184. }
  2185. int ieee80211_sta_get_ssid(struct wpa_supplicant *wpa_s, u8 *ssid,
  2186. size_t *len)
  2187. {
  2188. os_memcpy(ssid, wpa_s->mlme.ssid, wpa_s->mlme.ssid_len);
  2189. *len = wpa_s->mlme.ssid_len;
  2190. return 0;
  2191. }
  2192. int ieee80211_sta_associate(struct wpa_supplicant *wpa_s,
  2193. struct wpa_driver_associate_params *params)
  2194. {
  2195. struct ieee80211_sta_bss *bss;
  2196. wpa_s->mlme.bssid_set = 0;
  2197. wpa_s->mlme.freq = params->freq;
  2198. if (params->bssid) {
  2199. os_memcpy(wpa_s->bssid, params->bssid, ETH_ALEN);
  2200. if (!is_zero_ether_addr(params->bssid))
  2201. wpa_s->mlme.bssid_set = 1;
  2202. bss = ieee80211_bss_get(wpa_s, wpa_s->bssid);
  2203. if (bss) {
  2204. wpa_s->mlme.phymode = bss->hw_mode;
  2205. wpa_s->mlme.channel = bss->channel;
  2206. wpa_s->mlme.freq = bss->freq;
  2207. }
  2208. }
  2209. #if 0 /* FIX */
  2210. /* TODO: This should always be done for IBSS, even if IEEE80211_QOS is
  2211. * not defined. */
  2212. if (local->hw->conf_tx) {
  2213. struct ieee80211_tx_queue_params qparam;
  2214. int i;
  2215. os_memset(&qparam, 0, sizeof(qparam));
  2216. /* TODO: are these ok defaults for all hw_modes? */
  2217. qparam.aifs = 2;
  2218. qparam.cw_min =
  2219. local->conf.phymode == MODE_IEEE80211B ? 31 : 15;
  2220. qparam.cw_max = 1023;
  2221. qparam.burst_time = 0;
  2222. for (i = IEEE80211_TX_QUEUE_DATA0; i < NUM_TX_DATA_QUEUES; i++)
  2223. {
  2224. local->hw->conf_tx(wpa_s, i + IEEE80211_TX_QUEUE_DATA0,
  2225. &qparam);
  2226. }
  2227. /* IBSS uses different parameters for Beacon sending */
  2228. qparam.cw_min++;
  2229. qparam.cw_min *= 2;
  2230. qparam.cw_min--;
  2231. local->hw->conf_tx(wpa_s, IEEE80211_TX_QUEUE_BEACON, &qparam);
  2232. }
  2233. #endif
  2234. if (wpa_s->mlme.ssid_len != params->ssid_len ||
  2235. os_memcmp(wpa_s->mlme.ssid, params->ssid, params->ssid_len) != 0)
  2236. wpa_s->mlme.prev_bssid_set = 0;
  2237. os_memcpy(wpa_s->mlme.ssid, params->ssid, params->ssid_len);
  2238. os_memset(wpa_s->mlme.ssid + params->ssid_len, 0,
  2239. MAX_SSID_LEN - params->ssid_len);
  2240. wpa_s->mlme.ssid_len = params->ssid_len;
  2241. wpa_s->mlme.ssid_set = 1;
  2242. os_free(wpa_s->mlme.extra_ie);
  2243. if (params->wpa_ie == NULL || params->wpa_ie_len == 0) {
  2244. wpa_s->mlme.extra_ie = NULL;
  2245. wpa_s->mlme.extra_ie_len = 0;
  2246. } else {
  2247. wpa_s->mlme.extra_ie = os_malloc(params->wpa_ie_len);
  2248. if (wpa_s->mlme.extra_ie == NULL) {
  2249. wpa_s->mlme.extra_ie_len = 0;
  2250. return -1;
  2251. }
  2252. os_memcpy(wpa_s->mlme.extra_ie, params->wpa_ie,
  2253. params->wpa_ie_len);
  2254. wpa_s->mlme.extra_ie_len = params->wpa_ie_len;
  2255. }
  2256. wpa_s->mlme.key_mgmt = params->key_mgmt_suite;
  2257. ieee80211_sta_set_channel(wpa_s, wpa_s->mlme.phymode,
  2258. wpa_s->mlme.channel, wpa_s->mlme.freq);
  2259. if (params->mode == 1 && !wpa_s->mlme.bssid_set) {
  2260. os_get_time(&wpa_s->mlme.ibss_join_req);
  2261. wpa_s->mlme.state = IEEE80211_IBSS_SEARCH;
  2262. return ieee80211_sta_find_ibss(wpa_s);
  2263. }
  2264. if (wpa_s->mlme.bssid_set)
  2265. ieee80211_sta_new_auth(wpa_s);
  2266. return 0;
  2267. }
  2268. static void ieee80211_sta_save_oper_chan(struct wpa_supplicant *wpa_s)
  2269. {
  2270. wpa_s->mlme.scan_oper_channel = wpa_s->mlme.channel;
  2271. wpa_s->mlme.scan_oper_freq = wpa_s->mlme.freq;
  2272. wpa_s->mlme.scan_oper_phymode = wpa_s->mlme.phymode;
  2273. }
  2274. static int ieee80211_sta_restore_oper_chan(struct wpa_supplicant *wpa_s)
  2275. {
  2276. wpa_s->mlme.channel = wpa_s->mlme.scan_oper_channel;
  2277. wpa_s->mlme.freq = wpa_s->mlme.scan_oper_freq;
  2278. wpa_s->mlme.phymode = wpa_s->mlme.scan_oper_phymode;
  2279. if (wpa_s->mlme.freq == 0)
  2280. return 0;
  2281. return ieee80211_sta_set_channel(wpa_s, wpa_s->mlme.phymode,
  2282. wpa_s->mlme.channel,
  2283. wpa_s->mlme.freq);
  2284. }
  2285. static int ieee80211_active_scan(struct wpa_supplicant *wpa_s)
  2286. {
  2287. size_t m;
  2288. int c;
  2289. for (m = 0; m < wpa_s->mlme.num_modes; m++) {
  2290. struct wpa_hw_modes *mode = &wpa_s->mlme.modes[m];
  2291. if ((int) mode->mode != (int) wpa_s->mlme.phymode)
  2292. continue;
  2293. for (c = 0; c < mode->num_channels; c++) {
  2294. struct wpa_channel_data *chan = &mode->channels[c];
  2295. if (chan->flag & WPA_CHAN_W_SCAN &&
  2296. chan->chan == wpa_s->mlme.channel) {
  2297. if (chan->flag & WPA_CHAN_W_ACTIVE_SCAN)
  2298. return 1;
  2299. break;
  2300. }
  2301. }
  2302. }
  2303. return 0;
  2304. }
  2305. static void ieee80211_sta_scan_timer(void *eloop_ctx, void *timeout_ctx)
  2306. {
  2307. struct wpa_supplicant *wpa_s = eloop_ctx;
  2308. struct wpa_hw_modes *mode;
  2309. struct wpa_channel_data *chan;
  2310. int skip = 0;
  2311. int timeout = 0;
  2312. struct wpa_ssid *ssid = wpa_s->current_ssid;
  2313. int adhoc;
  2314. if (!wpa_s->mlme.sta_scanning || wpa_s->mlme.modes == NULL)
  2315. return;
  2316. adhoc = ssid && ssid->mode == 1;
  2317. switch (wpa_s->mlme.scan_state) {
  2318. case SCAN_SET_CHANNEL:
  2319. mode = &wpa_s->mlme.modes[wpa_s->mlme.scan_hw_mode_idx];
  2320. if (wpa_s->mlme.scan_hw_mode_idx >=
  2321. (int) wpa_s->mlme.num_modes ||
  2322. (wpa_s->mlme.scan_hw_mode_idx + 1 ==
  2323. (int) wpa_s->mlme.num_modes
  2324. && wpa_s->mlme.scan_channel_idx >= mode->num_channels)) {
  2325. if (ieee80211_sta_restore_oper_chan(wpa_s)) {
  2326. wpa_printf(MSG_DEBUG, "MLME: failed to "
  2327. "restore operational channel after "
  2328. "scan");
  2329. }
  2330. wpa_printf(MSG_DEBUG, "MLME: scan completed");
  2331. wpa_s->mlme.sta_scanning = 0;
  2332. os_get_time(&wpa_s->mlme.last_scan_completed);
  2333. wpa_supplicant_event(wpa_s, EVENT_SCAN_RESULTS, NULL);
  2334. if (adhoc) {
  2335. if (!wpa_s->mlme.bssid_set ||
  2336. (wpa_s->mlme.state ==
  2337. IEEE80211_IBSS_JOINED &&
  2338. !ieee80211_sta_active_ibss(wpa_s)))
  2339. ieee80211_sta_find_ibss(wpa_s);
  2340. }
  2341. return;
  2342. }
  2343. skip = !(wpa_s->mlme.hw_modes & (1 << mode->mode));
  2344. chan = &mode->channels[wpa_s->mlme.scan_channel_idx];
  2345. if (!(chan->flag & WPA_CHAN_W_SCAN) ||
  2346. (adhoc && !(chan->flag & WPA_CHAN_W_IBSS)) ||
  2347. (wpa_s->mlme.hw_modes & (1 << WPA_MODE_IEEE80211G) &&
  2348. mode->mode == WPA_MODE_IEEE80211B &&
  2349. wpa_s->mlme.scan_skip_11b))
  2350. skip = 1;
  2351. if (!skip) {
  2352. wpa_printf(MSG_MSGDUMP,
  2353. "MLME: scan channel %d (%d MHz)",
  2354. chan->chan, chan->freq);
  2355. wpa_s->mlme.channel = chan->chan;
  2356. wpa_s->mlme.freq = chan->freq;
  2357. wpa_s->mlme.phymode = mode->mode;
  2358. if (ieee80211_sta_set_channel(wpa_s, mode->mode,
  2359. chan->chan, chan->freq))
  2360. {
  2361. wpa_printf(MSG_DEBUG, "MLME: failed to set "
  2362. "channel %d (%d MHz) for scan",
  2363. chan->chan, chan->freq);
  2364. skip = 1;
  2365. }
  2366. }
  2367. wpa_s->mlme.scan_channel_idx++;
  2368. if (wpa_s->mlme.scan_channel_idx >=
  2369. wpa_s->mlme.modes[wpa_s->mlme.scan_hw_mode_idx].
  2370. num_channels) {
  2371. wpa_s->mlme.scan_hw_mode_idx++;
  2372. wpa_s->mlme.scan_channel_idx = 0;
  2373. }
  2374. if (skip) {
  2375. timeout = 0;
  2376. break;
  2377. }
  2378. timeout = IEEE80211_PROBE_DELAY;
  2379. wpa_s->mlme.scan_state = SCAN_SEND_PROBE;
  2380. break;
  2381. case SCAN_SEND_PROBE:
  2382. if (ieee80211_active_scan(wpa_s)) {
  2383. ieee80211_send_probe_req(wpa_s, NULL,
  2384. wpa_s->mlme.scan_ssid,
  2385. wpa_s->mlme.scan_ssid_len);
  2386. timeout = IEEE80211_CHANNEL_TIME;
  2387. } else {
  2388. timeout = IEEE80211_PASSIVE_CHANNEL_TIME;
  2389. }
  2390. wpa_s->mlme.scan_state = SCAN_SET_CHANNEL;
  2391. break;
  2392. }
  2393. eloop_register_timeout(timeout / 1000, 1000 * (timeout % 1000),
  2394. ieee80211_sta_scan_timer, wpa_s, NULL);
  2395. }
  2396. int ieee80211_sta_req_scan(struct wpa_supplicant *wpa_s, const u8 *ssid,
  2397. size_t ssid_len)
  2398. {
  2399. if (ssid_len > MAX_SSID_LEN)
  2400. return -1;
  2401. /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
  2402. * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
  2403. * BSSID: MACAddress
  2404. * SSID
  2405. * ScanType: ACTIVE, PASSIVE
  2406. * ProbeDelay: delay (in microseconds) to be used prior to transmitting
  2407. * a Probe frame during active scanning
  2408. * ChannelList
  2409. * MinChannelTime (>= ProbeDelay), in TU
  2410. * MaxChannelTime: (>= MinChannelTime), in TU
  2411. */
  2412. /* MLME-SCAN.confirm
  2413. * BSSDescriptionSet
  2414. * ResultCode: SUCCESS, INVALID_PARAMETERS
  2415. */
  2416. /* TODO: if assoc, move to power save mode for the duration of the
  2417. * scan */
  2418. if (wpa_s->mlme.sta_scanning)
  2419. return -1;
  2420. wpa_printf(MSG_DEBUG, "MLME: starting scan");
  2421. ieee80211_sta_save_oper_chan(wpa_s);
  2422. wpa_s->mlme.sta_scanning = 1;
  2423. /* TODO: stop TX queue? */
  2424. if (ssid) {
  2425. wpa_s->mlme.scan_ssid_len = ssid_len;
  2426. os_memcpy(wpa_s->mlme.scan_ssid, ssid, ssid_len);
  2427. } else
  2428. wpa_s->mlme.scan_ssid_len = 0;
  2429. wpa_s->mlme.scan_skip_11b = 1; /* FIX: clear this is 11g is not
  2430. * supported */
  2431. wpa_s->mlme.scan_state = SCAN_SET_CHANNEL;
  2432. wpa_s->mlme.scan_hw_mode_idx = 0;
  2433. wpa_s->mlme.scan_channel_idx = 0;
  2434. eloop_register_timeout(0, 1, ieee80211_sta_scan_timer, wpa_s, NULL);
  2435. return 0;
  2436. }
  2437. struct wpa_scan_results *
  2438. ieee80211_sta_get_scan_results(struct wpa_supplicant *wpa_s)
  2439. {
  2440. size_t ap_num = 0;
  2441. struct wpa_scan_results *res;
  2442. struct wpa_scan_res *r;
  2443. struct ieee80211_sta_bss *bss;
  2444. res = os_zalloc(sizeof(*res));
  2445. for (bss = wpa_s->mlme.sta_bss_list; bss; bss = bss->next)
  2446. ap_num++;
  2447. res->res = os_zalloc(ap_num * sizeof(struct wpa_scan_res *));
  2448. if (res->res == NULL) {
  2449. os_free(res);
  2450. return NULL;
  2451. }
  2452. for (bss = wpa_s->mlme.sta_bss_list; bss; bss = bss->next) {
  2453. r = os_zalloc(sizeof(*r) + bss->ie_len);
  2454. if (r == NULL)
  2455. break;
  2456. os_memcpy(r->bssid, bss->bssid, ETH_ALEN);
  2457. r->freq = bss->freq;
  2458. r->beacon_int = bss->beacon_int;
  2459. r->caps = bss->capability;
  2460. r->level = bss->rssi;
  2461. r->tsf = bss->timestamp;
  2462. if (bss->ie) {
  2463. r->ie_len = bss->ie_len;
  2464. os_memcpy(r + 1, bss->ie, bss->ie_len);
  2465. }
  2466. res->res[res->num++] = r;
  2467. }
  2468. return res;
  2469. }
  2470. #if 0 /* FIX */
  2471. struct sta_info * ieee80211_ibss_add_sta(struct wpa_supplicant *wpa_s,
  2472. struct sk_buff *skb, u8 *bssid,
  2473. u8 *addr)
  2474. {
  2475. struct ieee80211_local *local = dev->priv;
  2476. struct list_head *ptr;
  2477. struct sta_info *sta;
  2478. struct wpa_supplicant *sta_dev = NULL;
  2479. /* TODO: Could consider removing the least recently used entry and
  2480. * allow new one to be added. */
  2481. if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
  2482. if (net_ratelimit()) {
  2483. wpa_printf(MSG_DEBUG, "MLME: No room for a new IBSS "
  2484. "STA entry " MACSTR, MAC2STR(addr));
  2485. }
  2486. return NULL;
  2487. }
  2488. spin_lock_bh(&local->sub_if_lock);
  2489. list_for_each(ptr, &local->sub_if_list) {
  2490. sdata = list_entry(ptr, struct ieee80211_sub_if_data, list);
  2491. if (sdata->type == IEEE80211_SUB_IF_TYPE_STA &&
  2492. os_memcmp(bssid, sdata->u.sta.bssid, ETH_ALEN) == 0) {
  2493. sta_dev = sdata->dev;
  2494. break;
  2495. }
  2496. }
  2497. spin_unlock_bh(&local->sub_if_lock);
  2498. if (sta_dev == NULL)
  2499. return NULL;
  2500. wpa_printf(MSG_DEBUG, "MLME: Adding new IBSS station " MACSTR
  2501. " (dev=%s)", MAC2STR(addr), sta_dev->name);
  2502. sta = sta_info_add(wpa_s, addr);
  2503. if (sta == NULL) {
  2504. return NULL;
  2505. }
  2506. sta->dev = sta_dev;
  2507. sta->supp_rates = wpa_s->mlme.supp_rates_bits;
  2508. rate_control_rate_init(local, sta);
  2509. return sta; /* caller will call sta_info_release() */
  2510. }
  2511. #endif
  2512. int ieee80211_sta_deauthenticate(struct wpa_supplicant *wpa_s, u16 reason)
  2513. {
  2514. wpa_printf(MSG_DEBUG, "MLME: deauthenticate(reason=%d)", reason);
  2515. ieee80211_send_deauth(wpa_s, reason);
  2516. ieee80211_set_associated(wpa_s, 0);
  2517. return 0;
  2518. }
  2519. int ieee80211_sta_disassociate(struct wpa_supplicant *wpa_s, u16 reason)
  2520. {
  2521. wpa_printf(MSG_DEBUG, "MLME: disassociate(reason=%d)", reason);
  2522. if (!wpa_s->mlme.associated)
  2523. return -1;
  2524. ieee80211_send_disassoc(wpa_s, reason);
  2525. ieee80211_set_associated(wpa_s, 0);
  2526. return 0;
  2527. }
  2528. void ieee80211_sta_rx(struct wpa_supplicant *wpa_s, const u8 *buf, size_t len,
  2529. struct ieee80211_rx_status *rx_status)
  2530. {
  2531. struct ieee80211_mgmt *mgmt;
  2532. u16 fc;
  2533. const u8 *pos;
  2534. /* wpa_hexdump(MSG_MSGDUMP, "MLME: Received frame", buf, len); */
  2535. if (wpa_s->mlme.sta_scanning) {
  2536. ieee80211_sta_rx_scan(wpa_s, buf, len, rx_status);
  2537. return;
  2538. }
  2539. if (len < 24)
  2540. return;
  2541. mgmt = (struct ieee80211_mgmt *) buf;
  2542. fc = le_to_host16(mgmt->frame_control);
  2543. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT)
  2544. ieee80211_sta_rx_mgmt(wpa_s, buf, len, rx_status);
  2545. else if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA) {
  2546. if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) !=
  2547. WLAN_FC_FROMDS)
  2548. return;
  2549. /* mgmt->sa is actually BSSID for FromDS data frames */
  2550. if (os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) != 0)
  2551. return;
  2552. /* Skip IEEE 802.11 and LLC headers */
  2553. pos = buf + 24 + 6;
  2554. if (WPA_GET_BE16(pos) != ETH_P_EAPOL)
  2555. return;
  2556. pos += 2;
  2557. /* mgmt->bssid is actually BSSID for SA data frames */
  2558. wpa_supplicant_rx_eapol(wpa_s, mgmt->bssid,
  2559. pos, buf + len - pos);
  2560. }
  2561. }
  2562. void ieee80211_sta_free_hw_features(struct wpa_hw_modes *hw_features,
  2563. size_t num_hw_features)
  2564. {
  2565. size_t i;
  2566. if (hw_features == NULL)
  2567. return;
  2568. for (i = 0; i < num_hw_features; i++) {
  2569. os_free(hw_features[i].channels);
  2570. os_free(hw_features[i].rates);
  2571. }
  2572. os_free(hw_features);
  2573. }
  2574. int ieee80211_sta_init(struct wpa_supplicant *wpa_s)
  2575. {
  2576. u16 num_modes, flags;
  2577. wpa_s->mlme.modes = wpa_drv_get_hw_feature_data(wpa_s, &num_modes,
  2578. &flags);
  2579. if (wpa_s->mlme.modes == NULL) {
  2580. wpa_printf(MSG_ERROR, "MLME: Failed to read supported "
  2581. "channels and rates from the driver");
  2582. return -1;
  2583. }
  2584. wpa_s->mlme.num_modes = num_modes;
  2585. wpa_s->mlme.hw_modes = 1 << WPA_MODE_IEEE80211A;
  2586. wpa_s->mlme.hw_modes |= 1 << WPA_MODE_IEEE80211B;
  2587. wpa_s->mlme.hw_modes |= 1 << WPA_MODE_IEEE80211G;
  2588. return 0;
  2589. }
  2590. void ieee80211_sta_deinit(struct wpa_supplicant *wpa_s)
  2591. {
  2592. eloop_cancel_timeout(ieee80211_sta_timer, wpa_s, NULL);
  2593. eloop_cancel_timeout(ieee80211_sta_scan_timer, wpa_s, NULL);
  2594. os_free(wpa_s->mlme.extra_ie);
  2595. wpa_s->mlme.extra_ie = NULL;
  2596. os_free(wpa_s->mlme.extra_probe_ie);
  2597. wpa_s->mlme.extra_probe_ie = NULL;
  2598. os_free(wpa_s->mlme.assocreq_ies);
  2599. wpa_s->mlme.assocreq_ies = NULL;
  2600. os_free(wpa_s->mlme.assocresp_ies);
  2601. wpa_s->mlme.assocresp_ies = NULL;
  2602. ieee80211_bss_list_deinit(wpa_s);
  2603. ieee80211_sta_free_hw_features(wpa_s->mlme.modes,
  2604. wpa_s->mlme.num_modes);
  2605. #ifdef CONFIG_IEEE80211R
  2606. os_free(wpa_s->mlme.ft_ies);
  2607. wpa_s->mlme.ft_ies = NULL;
  2608. wpa_s->mlme.ft_ies_len = 0;
  2609. #endif /* CONFIG_IEEE80211R */
  2610. }
  2611. #ifdef CONFIG_IEEE80211R
  2612. int ieee80211_sta_update_ft_ies(struct wpa_supplicant *wpa_s, const u8 *md,
  2613. const u8 *ies, size_t ies_len)
  2614. {
  2615. if (md == NULL) {
  2616. wpa_printf(MSG_DEBUG, "MLME: Clear FT mobility domain");
  2617. os_memset(wpa_s->mlme.current_md, 0, MOBILITY_DOMAIN_ID_LEN);
  2618. } else {
  2619. wpa_printf(MSG_DEBUG, "MLME: Update FT IEs for MD " MACSTR,
  2620. MAC2STR(md));
  2621. os_memcpy(wpa_s->mlme.current_md, md, MOBILITY_DOMAIN_ID_LEN);
  2622. }
  2623. wpa_hexdump(MSG_DEBUG, "MLME: FT IEs", ies, ies_len);
  2624. os_free(wpa_s->mlme.ft_ies);
  2625. wpa_s->mlme.ft_ies = os_malloc(ies_len);
  2626. if (wpa_s->mlme.ft_ies == NULL)
  2627. return -1;
  2628. os_memcpy(wpa_s->mlme.ft_ies, ies, ies_len);
  2629. wpa_s->mlme.ft_ies_len = ies_len;
  2630. return 0;
  2631. }
  2632. int ieee80211_sta_send_ft_action(struct wpa_supplicant *wpa_s, u8 action,
  2633. const u8 *target_ap,
  2634. const u8 *ies, size_t ies_len)
  2635. {
  2636. u8 *buf;
  2637. size_t len;
  2638. struct ieee80211_mgmt *mgmt;
  2639. int res;
  2640. /*
  2641. * Action frame payload:
  2642. * Category[1] = 6 (Fast BSS Transition)
  2643. * Action[1] = 1 (Fast BSS Transition Request)
  2644. * STA Address
  2645. * Target AP Address
  2646. * FT IEs
  2647. */
  2648. buf = os_zalloc(sizeof(*mgmt) + ies_len);
  2649. if (buf == NULL) {
  2650. wpa_printf(MSG_DEBUG, "MLME: Failed to allocate buffer for "
  2651. "FT action frame");
  2652. return -1;
  2653. }
  2654. mgmt = (struct ieee80211_mgmt *) buf;
  2655. len = 24;
  2656. os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
  2657. os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
  2658. os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
  2659. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  2660. WLAN_FC_STYPE_ACTION);
  2661. mgmt->u.action.category = WLAN_ACTION_FT;
  2662. mgmt->u.action.u.ft_action_req.action = action;
  2663. os_memcpy(mgmt->u.action.u.ft_action_req.sta_addr, wpa_s->own_addr,
  2664. ETH_ALEN);
  2665. os_memcpy(mgmt->u.action.u.ft_action_req.target_ap_addr, target_ap,
  2666. ETH_ALEN);
  2667. os_memcpy(mgmt->u.action.u.ft_action_req.variable, ies, ies_len);
  2668. len += 1 + sizeof(mgmt->u.action.u.ft_action_req) + ies_len;
  2669. wpa_printf(MSG_DEBUG, "MLME: Send FT Action Frame: Action=%d "
  2670. "Target AP=" MACSTR " body_len=%d",
  2671. action, MAC2STR(target_ap), ies_len);
  2672. res = ieee80211_sta_tx(wpa_s, buf, len);
  2673. os_free(buf);
  2674. return res;
  2675. }
  2676. #endif /* CONFIG_IEEE80211R */
  2677. int ieee80211_sta_set_probe_req_ie(struct wpa_supplicant *wpa_s, const u8 *ies,
  2678. size_t ies_len)
  2679. {
  2680. os_free(wpa_s->mlme.extra_probe_ie);
  2681. wpa_s->mlme.extra_probe_ie = NULL;
  2682. wpa_s->mlme.extra_probe_ie_len = 0;
  2683. if (ies == NULL)
  2684. return 0;
  2685. wpa_s->mlme.extra_probe_ie = os_malloc(ies_len);
  2686. if (wpa_s->mlme.extra_probe_ie == NULL)
  2687. return -1;
  2688. os_memcpy(wpa_s->mlme.extra_probe_ie, ies, ies_len);
  2689. wpa_s->mlme.extra_probe_ie_len = ies_len;
  2690. return 0;
  2691. }