mlme.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050
  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)
  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 (!(rate->flags & WPA_RATE_SUPPORTED))
  735. continue;
  736. if (esupp_rates) {
  737. pos = buf + len;
  738. len++;
  739. esupp_rates[1]++;
  740. } else if (supp_rates[1] == 8) {
  741. esupp_rates = pos;
  742. esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
  743. esupp_rates[1] = 1;
  744. pos = &esupp_rates[2];
  745. } else {
  746. pos = buf + len;
  747. len++;
  748. supp_rates[1]++;
  749. }
  750. *pos++ = rate->rate / 5;
  751. }
  752. if (wpa_s->mlme.extra_probe_ie) {
  753. os_memcpy(pos, wpa_s->mlme.extra_probe_ie,
  754. wpa_s->mlme.extra_probe_ie_len);
  755. len += wpa_s->mlme.extra_probe_ie_len;
  756. }
  757. ieee80211_sta_tx(wpa_s, buf, len);
  758. os_free(buf);
  759. }
  760. static int ieee80211_sta_wep_configured(struct wpa_supplicant *wpa_s)
  761. {
  762. #if 0 /* FIX */
  763. if (sdata == NULL || sdata->default_key == NULL ||
  764. sdata->default_key->alg != ALG_WEP)
  765. return 0;
  766. return 1;
  767. #else
  768. return 0;
  769. #endif
  770. }
  771. static void ieee80211_auth_completed(struct wpa_supplicant *wpa_s)
  772. {
  773. wpa_printf(MSG_DEBUG, "MLME: authenticated");
  774. wpa_s->mlme.authenticated = 1;
  775. ieee80211_associate(wpa_s);
  776. }
  777. static void ieee80211_auth_challenge(struct wpa_supplicant *wpa_s,
  778. struct ieee80211_mgmt *mgmt,
  779. size_t len,
  780. struct ieee80211_rx_status *rx_status)
  781. {
  782. u8 *pos;
  783. struct ieee802_11_elems elems;
  784. wpa_printf(MSG_DEBUG, "MLME: replying to auth challenge");
  785. pos = mgmt->u.auth.variable;
  786. if (ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems)
  787. == ParseFailed) {
  788. wpa_printf(MSG_DEBUG, "MLME: failed to parse Auth(challenge)");
  789. return;
  790. }
  791. if (elems.challenge == NULL) {
  792. wpa_printf(MSG_DEBUG, "MLME: no challenge IE in shared key "
  793. "auth frame");
  794. return;
  795. }
  796. ieee80211_send_auth(wpa_s, 3, elems.challenge - 2,
  797. elems.challenge_len + 2, 1);
  798. }
  799. static void ieee80211_rx_mgmt_auth(struct wpa_supplicant *wpa_s,
  800. struct ieee80211_mgmt *mgmt,
  801. size_t len,
  802. struct ieee80211_rx_status *rx_status)
  803. {
  804. struct wpa_ssid *ssid = wpa_s->current_ssid;
  805. u16 auth_alg, auth_transaction, status_code;
  806. int adhoc;
  807. adhoc = ssid && ssid->mode == 1;
  808. if (wpa_s->mlme.state != IEEE80211_AUTHENTICATE && !adhoc) {
  809. wpa_printf(MSG_DEBUG, "MLME: authentication frame received "
  810. "from " MACSTR ", but not in authenticate state - "
  811. "ignored", MAC2STR(mgmt->sa));
  812. return;
  813. }
  814. if (len < 24 + 6) {
  815. wpa_printf(MSG_DEBUG, "MLME: too short (%lu) authentication "
  816. "frame received from " MACSTR " - ignored",
  817. (unsigned long) len, MAC2STR(mgmt->sa));
  818. return;
  819. }
  820. if (!adhoc && os_memcmp(wpa_s->bssid, mgmt->sa, ETH_ALEN) != 0) {
  821. wpa_printf(MSG_DEBUG, "MLME: authentication frame received "
  822. "from unknown AP (SA=" MACSTR " BSSID=" MACSTR
  823. ") - ignored",
  824. MAC2STR(mgmt->sa), MAC2STR(mgmt->bssid));
  825. return;
  826. }
  827. if (adhoc && os_memcmp(wpa_s->bssid, mgmt->bssid, ETH_ALEN) != 0) {
  828. wpa_printf(MSG_DEBUG, "MLME: authentication frame received "
  829. "from unknown BSSID (SA=" MACSTR " BSSID=" MACSTR
  830. ") - ignored",
  831. MAC2STR(mgmt->sa), MAC2STR(mgmt->bssid));
  832. return;
  833. }
  834. auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
  835. auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
  836. status_code = le_to_host16(mgmt->u.auth.status_code);
  837. wpa_printf(MSG_DEBUG, "MLME: RX authentication from " MACSTR
  838. " (alg=%d transaction=%d status=%d)",
  839. MAC2STR(mgmt->sa), auth_alg, auth_transaction, status_code);
  840. if (adhoc) {
  841. /* IEEE 802.11 standard does not require authentication in IBSS
  842. * networks and most implementations do not seem to use it.
  843. * However, try to reply to authentication attempts if someone
  844. * has actually implemented this.
  845. * TODO: Could implement shared key authentication. */
  846. if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1) {
  847. wpa_printf(MSG_DEBUG, "MLME: unexpected IBSS "
  848. "authentication frame (alg=%d "
  849. "transaction=%d)",
  850. auth_alg, auth_transaction);
  851. return;
  852. }
  853. ieee80211_send_auth(wpa_s, 2, NULL, 0, 0);
  854. }
  855. if (auth_alg != wpa_s->mlme.auth_alg ||
  856. auth_transaction != wpa_s->mlme.auth_transaction) {
  857. wpa_printf(MSG_DEBUG, "MLME: unexpected authentication frame "
  858. "(alg=%d transaction=%d)",
  859. auth_alg, auth_transaction);
  860. return;
  861. }
  862. if (status_code != WLAN_STATUS_SUCCESS) {
  863. wpa_printf(MSG_DEBUG, "MLME: AP denied authentication "
  864. "(auth_alg=%d code=%d)", wpa_s->mlme.auth_alg,
  865. status_code);
  866. if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
  867. const int num_algs = 3;
  868. u8 algs[num_algs];
  869. int i, pos;
  870. algs[0] = algs[1] = algs[2] = 0xff;
  871. if (wpa_s->mlme.auth_algs & IEEE80211_AUTH_ALG_OPEN)
  872. algs[0] = WLAN_AUTH_OPEN;
  873. if (wpa_s->mlme.auth_algs &
  874. IEEE80211_AUTH_ALG_SHARED_KEY)
  875. algs[1] = WLAN_AUTH_SHARED_KEY;
  876. if (wpa_s->mlme.auth_algs & IEEE80211_AUTH_ALG_LEAP)
  877. algs[2] = WLAN_AUTH_LEAP;
  878. if (wpa_s->mlme.auth_alg == WLAN_AUTH_OPEN)
  879. pos = 0;
  880. else if (wpa_s->mlme.auth_alg == WLAN_AUTH_SHARED_KEY)
  881. pos = 1;
  882. else
  883. pos = 2;
  884. for (i = 0; i < num_algs; i++) {
  885. pos++;
  886. if (pos >= num_algs)
  887. pos = 0;
  888. if (algs[pos] == wpa_s->mlme.auth_alg ||
  889. algs[pos] == 0xff)
  890. continue;
  891. if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
  892. !ieee80211_sta_wep_configured(wpa_s))
  893. continue;
  894. wpa_s->mlme.auth_alg = algs[pos];
  895. wpa_printf(MSG_DEBUG, "MLME: set auth_alg=%d "
  896. "for next try",
  897. wpa_s->mlme.auth_alg);
  898. break;
  899. }
  900. }
  901. return;
  902. }
  903. switch (wpa_s->mlme.auth_alg) {
  904. case WLAN_AUTH_OPEN:
  905. case WLAN_AUTH_LEAP:
  906. ieee80211_auth_completed(wpa_s);
  907. break;
  908. case WLAN_AUTH_SHARED_KEY:
  909. if (wpa_s->mlme.auth_transaction == 4)
  910. ieee80211_auth_completed(wpa_s);
  911. else
  912. ieee80211_auth_challenge(wpa_s, mgmt, len,
  913. rx_status);
  914. break;
  915. #ifdef CONFIG_IEEE80211R
  916. case WLAN_AUTH_FT:
  917. {
  918. union wpa_event_data data;
  919. os_memset(&data, 0, sizeof(data));
  920. data.ft_ies.ies = mgmt->u.auth.variable;
  921. data.ft_ies.ies_len = len -
  922. (mgmt->u.auth.variable - (u8 *) mgmt);
  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)) < 0) {
  1098. wpa_printf(MSG_DEBUG, "MLME: FT validation of Reassoc"
  1099. "Resp failed");
  1100. return;
  1101. }
  1102. } else if (ieee80211_ft_assoc_resp(wpa_s, &elems) < 0)
  1103. return;
  1104. wpa_printf(MSG_DEBUG, "MLME: associated");
  1105. wpa_s->mlme.aid = aid;
  1106. wpa_s->mlme.ap_capab = capab_info;
  1107. os_free(wpa_s->mlme.assocresp_ies);
  1108. wpa_s->mlme.assocresp_ies_len = len - (pos - (u8 *) mgmt);
  1109. wpa_s->mlme.assocresp_ies = os_malloc(wpa_s->mlme.assocresp_ies_len);
  1110. if (wpa_s->mlme.assocresp_ies) {
  1111. os_memcpy(wpa_s->mlme.assocresp_ies, pos,
  1112. wpa_s->mlme.assocresp_ies_len);
  1113. }
  1114. ieee80211_set_associated(wpa_s, 1);
  1115. rates_len = elems.supp_rates_len;
  1116. if (rates_len > sizeof(rates))
  1117. rates_len = sizeof(rates);
  1118. os_memcpy(rates, elems.supp_rates, rates_len);
  1119. if (elems.ext_supp_rates) {
  1120. size_t _len = elems.ext_supp_rates_len;
  1121. if (_len > sizeof(rates) - rates_len)
  1122. _len = sizeof(rates) - rates_len;
  1123. os_memcpy(rates + rates_len, elems.ext_supp_rates, _len);
  1124. rates_len += _len;
  1125. }
  1126. if (wpa_drv_set_bssid(wpa_s, wpa_s->bssid) < 0) {
  1127. wpa_printf(MSG_DEBUG, "MLME: failed to set BSSID for the "
  1128. "netstack");
  1129. }
  1130. if (wpa_drv_set_ssid(wpa_s, wpa_s->mlme.ssid, wpa_s->mlme.ssid_len) <
  1131. 0) {
  1132. wpa_printf(MSG_DEBUG, "MLME: failed to set SSID for the "
  1133. "netstack");
  1134. }
  1135. /* Remove STA entry before adding a new one just in case to avoid
  1136. * problems with existing configuration (e.g., keys). */
  1137. wpa_drv_mlme_remove_sta(wpa_s, wpa_s->bssid);
  1138. if (wpa_drv_mlme_add_sta(wpa_s, wpa_s->bssid, rates, rates_len) < 0) {
  1139. wpa_printf(MSG_DEBUG, "MLME: failed to add STA entry to the "
  1140. "netstack");
  1141. }
  1142. #if 0 /* FIX? */
  1143. sta->assoc_ap = 1;
  1144. if (elems.wmm_param && wpa_s->mlme.wmm_enabled) {
  1145. sta->flags |= WLAN_STA_WME;
  1146. ieee80211_sta_wmm_params(wpa_s, elems.wmm_param,
  1147. elems.wmm_param_len);
  1148. }
  1149. #endif
  1150. ieee80211_associated(wpa_s);
  1151. }
  1152. /* Caller must hold local->sta_bss_lock */
  1153. static void __ieee80211_bss_hash_add(struct wpa_supplicant *wpa_s,
  1154. struct ieee80211_sta_bss *bss)
  1155. {
  1156. bss->hnext = wpa_s->mlme.sta_bss_hash[STA_HASH(bss->bssid)];
  1157. wpa_s->mlme.sta_bss_hash[STA_HASH(bss->bssid)] = bss;
  1158. }
  1159. /* Caller must hold local->sta_bss_lock */
  1160. static void __ieee80211_bss_hash_del(struct wpa_supplicant *wpa_s,
  1161. struct ieee80211_sta_bss *bss)
  1162. {
  1163. struct ieee80211_sta_bss *b, *prev = NULL;
  1164. b = wpa_s->mlme.sta_bss_hash[STA_HASH(bss->bssid)];
  1165. while (b) {
  1166. if (b == bss) {
  1167. if (prev == NULL) {
  1168. wpa_s->mlme.sta_bss_hash[STA_HASH(bss->bssid)]
  1169. = bss->hnext;
  1170. } else {
  1171. prev->hnext = bss->hnext;
  1172. }
  1173. break;
  1174. }
  1175. prev = b;
  1176. b = b->hnext;
  1177. }
  1178. }
  1179. static struct ieee80211_sta_bss *
  1180. ieee80211_bss_add(struct wpa_supplicant *wpa_s, const u8 *bssid)
  1181. {
  1182. struct ieee80211_sta_bss *bss;
  1183. bss = os_zalloc(sizeof(*bss));
  1184. if (bss == NULL)
  1185. return NULL;
  1186. os_memcpy(bss->bssid, bssid, ETH_ALEN);
  1187. /* TODO: order by RSSI? */
  1188. bss->next = wpa_s->mlme.sta_bss_list;
  1189. wpa_s->mlme.sta_bss_list = bss;
  1190. __ieee80211_bss_hash_add(wpa_s, bss);
  1191. return bss;
  1192. }
  1193. static struct ieee80211_sta_bss *
  1194. ieee80211_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid)
  1195. {
  1196. struct ieee80211_sta_bss *bss;
  1197. bss = wpa_s->mlme.sta_bss_hash[STA_HASH(bssid)];
  1198. while (bss) {
  1199. if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
  1200. break;
  1201. bss = bss->hnext;
  1202. }
  1203. return bss;
  1204. }
  1205. static void ieee80211_bss_free(struct wpa_supplicant *wpa_s,
  1206. struct ieee80211_sta_bss *bss)
  1207. {
  1208. __ieee80211_bss_hash_del(wpa_s, bss);
  1209. os_free(bss->ie);
  1210. os_free(bss->wpa_ie);
  1211. os_free(bss->rsn_ie);
  1212. os_free(bss->wmm_ie);
  1213. os_free(bss->mdie);
  1214. os_free(bss);
  1215. }
  1216. static void ieee80211_bss_list_deinit(struct wpa_supplicant *wpa_s)
  1217. {
  1218. struct ieee80211_sta_bss *bss, *prev;
  1219. bss = wpa_s->mlme.sta_bss_list;
  1220. wpa_s->mlme.sta_bss_list = NULL;
  1221. while (bss) {
  1222. prev = bss;
  1223. bss = bss->next;
  1224. ieee80211_bss_free(wpa_s, prev);
  1225. }
  1226. }
  1227. static void ieee80211_bss_info(struct wpa_supplicant *wpa_s,
  1228. struct ieee80211_mgmt *mgmt,
  1229. size_t len,
  1230. struct ieee80211_rx_status *rx_status,
  1231. int beacon)
  1232. {
  1233. struct ieee802_11_elems elems;
  1234. size_t baselen;
  1235. int channel, invalid = 0, clen;
  1236. struct ieee80211_sta_bss *bss;
  1237. u64 timestamp;
  1238. u8 *pos, *ie_pos;
  1239. size_t ie_len;
  1240. if (!beacon && os_memcmp(mgmt->da, wpa_s->own_addr, ETH_ALEN))
  1241. return; /* ignore ProbeResp to foreign address */
  1242. #if 0
  1243. wpa_printf(MSG_MSGDUMP, "MLME: RX %s from " MACSTR " to " MACSTR,
  1244. beacon ? "Beacon" : "Probe Response",
  1245. MAC2STR(mgmt->sa), MAC2STR(mgmt->da));
  1246. #endif
  1247. baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
  1248. if (baselen > len)
  1249. return;
  1250. pos = mgmt->u.beacon.timestamp;
  1251. timestamp = WPA_GET_LE64(pos);
  1252. #if 0 /* FIX */
  1253. if (local->conf.mode == IW_MODE_ADHOC && beacon &&
  1254. os_memcmp(mgmt->bssid, local->bssid, ETH_ALEN) == 0) {
  1255. #ifdef IEEE80211_IBSS_DEBUG
  1256. static unsigned long last_tsf_debug = 0;
  1257. u64 tsf;
  1258. if (local->hw->get_tsf)
  1259. tsf = local->hw->get_tsf(local->mdev);
  1260. else
  1261. tsf = -1LLU;
  1262. if (time_after(jiffies, last_tsf_debug + 5 * HZ)) {
  1263. wpa_printf(MSG_DEBUG, "RX beacon SA=" MACSTR " BSSID="
  1264. MACSTR " TSF=0x%llx BCN=0x%llx diff=%lld "
  1265. "@%ld",
  1266. MAC2STR(mgmt->sa), MAC2STR(mgmt->bssid),
  1267. tsf, timestamp, tsf - timestamp, jiffies);
  1268. last_tsf_debug = jiffies;
  1269. }
  1270. #endif /* IEEE80211_IBSS_DEBUG */
  1271. }
  1272. #endif
  1273. ie_pos = mgmt->u.beacon.variable;
  1274. ie_len = len - baselen;
  1275. if (ieee802_11_parse_elems(ie_pos, ie_len, &elems) == ParseFailed)
  1276. invalid = 1;
  1277. #if 0 /* FIX */
  1278. if (local->conf.mode == IW_MODE_ADHOC && elems.supp_rates &&
  1279. os_memcmp(mgmt->bssid, local->bssid, ETH_ALEN) == 0 &&
  1280. (sta = sta_info_get(local, mgmt->sa))) {
  1281. struct ieee80211_rate *rates;
  1282. size_t num_rates;
  1283. u32 supp_rates, prev_rates;
  1284. int i, j, oper_mode;
  1285. rates = local->curr_rates;
  1286. num_rates = local->num_curr_rates;
  1287. oper_mode = wpa_s->mlme.sta_scanning ?
  1288. local->scan_oper_phymode : local->conf.phymode;
  1289. for (i = 0; i < local->hw->num_modes; i++) {
  1290. struct ieee80211_hw_modes *mode = &local->hw->modes[i];
  1291. if (oper_mode == mode->mode) {
  1292. rates = mode->rates;
  1293. num_rates = mode->num_rates;
  1294. break;
  1295. }
  1296. }
  1297. supp_rates = 0;
  1298. for (i = 0; i < elems.supp_rates_len +
  1299. elems.ext_supp_rates_len; i++) {
  1300. u8 rate = 0;
  1301. int own_rate;
  1302. if (i < elems.supp_rates_len)
  1303. rate = elems.supp_rates[i];
  1304. else if (elems.ext_supp_rates)
  1305. rate = elems.ext_supp_rates
  1306. [i - elems.supp_rates_len];
  1307. own_rate = 5 * (rate & 0x7f);
  1308. if (oper_mode == MODE_ATHEROS_TURBO)
  1309. own_rate *= 2;
  1310. for (j = 0; j < num_rates; j++)
  1311. if (rates[j].rate == own_rate)
  1312. supp_rates |= BIT(j);
  1313. }
  1314. prev_rates = sta->supp_rates;
  1315. sta->supp_rates &= supp_rates;
  1316. if (sta->supp_rates == 0) {
  1317. /* No matching rates - this should not really happen.
  1318. * Make sure that at least one rate is marked
  1319. * supported to avoid issues with TX rate ctrl. */
  1320. sta->supp_rates = wpa_s->mlme.supp_rates_bits;
  1321. }
  1322. if (sta->supp_rates != prev_rates) {
  1323. wpa_printf(MSG_DEBUG, "MLME: updated supp_rates set "
  1324. "for " MACSTR " based on beacon info "
  1325. "(0x%x & 0x%x -> 0x%x)",
  1326. MAC2STR(sta->addr), prev_rates,
  1327. supp_rates, sta->supp_rates);
  1328. }
  1329. sta_info_release(local, sta);
  1330. }
  1331. #endif
  1332. if (elems.ssid == NULL)
  1333. return;
  1334. if (elems.ds_params && elems.ds_params_len == 1)
  1335. channel = elems.ds_params[0];
  1336. else
  1337. channel = rx_status->channel;
  1338. bss = ieee80211_bss_get(wpa_s, mgmt->bssid);
  1339. if (bss == NULL) {
  1340. bss = ieee80211_bss_add(wpa_s, mgmt->bssid);
  1341. if (bss == NULL)
  1342. return;
  1343. } else {
  1344. #if 0
  1345. /* TODO: order by RSSI? */
  1346. spin_lock_bh(&local->sta_bss_lock);
  1347. list_move_tail(&bss->list, &local->sta_bss_list);
  1348. spin_unlock_bh(&local->sta_bss_lock);
  1349. #endif
  1350. }
  1351. if (bss->probe_resp && beacon) {
  1352. /* Do not allow beacon to override data from Probe Response. */
  1353. return;
  1354. }
  1355. bss->beacon_int = le_to_host16(mgmt->u.beacon.beacon_int);
  1356. bss->capability = le_to_host16(mgmt->u.beacon.capab_info);
  1357. if (bss->ie == NULL || bss->ie_len < ie_len) {
  1358. os_free(bss->ie);
  1359. bss->ie = os_malloc(ie_len);
  1360. }
  1361. if (bss->ie) {
  1362. os_memcpy(bss->ie, ie_pos, ie_len);
  1363. bss->ie_len = ie_len;
  1364. }
  1365. if (elems.ssid && elems.ssid_len <= MAX_SSID_LEN) {
  1366. os_memcpy(bss->ssid, elems.ssid, elems.ssid_len);
  1367. bss->ssid_len = elems.ssid_len;
  1368. }
  1369. bss->supp_rates_len = 0;
  1370. if (elems.supp_rates) {
  1371. clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
  1372. if (clen > elems.supp_rates_len)
  1373. clen = elems.supp_rates_len;
  1374. os_memcpy(&bss->supp_rates[bss->supp_rates_len],
  1375. elems.supp_rates, clen);
  1376. bss->supp_rates_len += clen;
  1377. }
  1378. if (elems.ext_supp_rates) {
  1379. clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
  1380. if (clen > elems.ext_supp_rates_len)
  1381. clen = elems.ext_supp_rates_len;
  1382. os_memcpy(&bss->supp_rates[bss->supp_rates_len],
  1383. elems.ext_supp_rates, clen);
  1384. bss->supp_rates_len += clen;
  1385. }
  1386. if (elems.wpa &&
  1387. (bss->wpa_ie == NULL || bss->wpa_ie_len != elems.wpa_len ||
  1388. os_memcmp(bss->wpa_ie, elems.wpa, elems.wpa_len))) {
  1389. os_free(bss->wpa_ie);
  1390. bss->wpa_ie = os_malloc(elems.wpa_len + 2);
  1391. if (bss->wpa_ie) {
  1392. os_memcpy(bss->wpa_ie, elems.wpa - 2,
  1393. elems.wpa_len + 2);
  1394. bss->wpa_ie_len = elems.wpa_len + 2;
  1395. } else
  1396. bss->wpa_ie_len = 0;
  1397. } else if (!elems.wpa && bss->wpa_ie) {
  1398. os_free(bss->wpa_ie);
  1399. bss->wpa_ie = NULL;
  1400. bss->wpa_ie_len = 0;
  1401. }
  1402. if (elems.rsn &&
  1403. (bss->rsn_ie == NULL || bss->rsn_ie_len != elems.rsn_len ||
  1404. os_memcmp(bss->rsn_ie, elems.rsn, elems.rsn_len))) {
  1405. os_free(bss->rsn_ie);
  1406. bss->rsn_ie = os_malloc(elems.rsn_len + 2);
  1407. if (bss->rsn_ie) {
  1408. os_memcpy(bss->rsn_ie, elems.rsn - 2,
  1409. elems.rsn_len + 2);
  1410. bss->rsn_ie_len = elems.rsn_len + 2;
  1411. } else
  1412. bss->rsn_ie_len = 0;
  1413. } else if (!elems.rsn && bss->rsn_ie) {
  1414. os_free(bss->rsn_ie);
  1415. bss->rsn_ie = NULL;
  1416. bss->rsn_ie_len = 0;
  1417. }
  1418. if (elems.wmm_param &&
  1419. (bss->wmm_ie == NULL || bss->wmm_ie_len != elems.wmm_param_len ||
  1420. os_memcmp(bss->wmm_ie, elems.wmm_param, elems.wmm_param_len))) {
  1421. os_free(bss->wmm_ie);
  1422. bss->wmm_ie = os_malloc(elems.wmm_param_len + 2);
  1423. if (bss->wmm_ie) {
  1424. os_memcpy(bss->wmm_ie, elems.wmm_param - 2,
  1425. elems.wmm_param_len + 2);
  1426. bss->wmm_ie_len = elems.wmm_param_len + 2;
  1427. } else
  1428. bss->wmm_ie_len = 0;
  1429. } else if (!elems.wmm_param && bss->wmm_ie) {
  1430. os_free(bss->wmm_ie);
  1431. bss->wmm_ie = NULL;
  1432. bss->wmm_ie_len = 0;
  1433. }
  1434. #ifdef CONFIG_IEEE80211R
  1435. if (elems.mdie &&
  1436. (bss->mdie == NULL || bss->mdie_len != elems.mdie_len ||
  1437. os_memcmp(bss->mdie, elems.mdie, elems.mdie_len))) {
  1438. os_free(bss->mdie);
  1439. bss->mdie = os_malloc(elems.mdie_len + 2);
  1440. if (bss->mdie) {
  1441. os_memcpy(bss->mdie, elems.mdie - 2,
  1442. elems.mdie_len + 2);
  1443. bss->mdie_len = elems.mdie_len + 2;
  1444. } else
  1445. bss->mdie_len = 0;
  1446. } else if (!elems.mdie && bss->mdie) {
  1447. os_free(bss->mdie);
  1448. bss->mdie = NULL;
  1449. bss->mdie_len = 0;
  1450. }
  1451. #endif /* CONFIG_IEEE80211R */
  1452. bss->hw_mode = wpa_s->mlme.phymode;
  1453. bss->channel = channel;
  1454. bss->freq = wpa_s->mlme.freq;
  1455. if (channel != wpa_s->mlme.channel &&
  1456. (wpa_s->mlme.phymode == WPA_MODE_IEEE80211G ||
  1457. wpa_s->mlme.phymode == WPA_MODE_IEEE80211B) &&
  1458. channel >= 1 && channel <= 14) {
  1459. static const int freq_list[] = {
  1460. 2412, 2417, 2422, 2427, 2432, 2437, 2442,
  1461. 2447, 2452, 2457, 2462, 2467, 2472, 2484
  1462. };
  1463. /* IEEE 802.11g/b mode can receive packets from neighboring
  1464. * channels, so map the channel into frequency. */
  1465. bss->freq = freq_list[channel - 1];
  1466. }
  1467. bss->timestamp = timestamp;
  1468. os_get_time(&bss->last_update);
  1469. bss->rssi = rx_status->ssi;
  1470. if (!beacon)
  1471. bss->probe_resp++;
  1472. }
  1473. static void ieee80211_rx_mgmt_probe_resp(struct wpa_supplicant *wpa_s,
  1474. struct ieee80211_mgmt *mgmt,
  1475. size_t len,
  1476. struct ieee80211_rx_status *rx_status)
  1477. {
  1478. ieee80211_bss_info(wpa_s, mgmt, len, rx_status, 0);
  1479. }
  1480. static void ieee80211_rx_mgmt_beacon(struct wpa_supplicant *wpa_s,
  1481. struct ieee80211_mgmt *mgmt,
  1482. size_t len,
  1483. struct ieee80211_rx_status *rx_status)
  1484. {
  1485. int use_protection;
  1486. size_t baselen;
  1487. struct ieee802_11_elems elems;
  1488. ieee80211_bss_info(wpa_s, mgmt, len, rx_status, 1);
  1489. if (!wpa_s->mlme.associated ||
  1490. os_memcmp(wpa_s->bssid, mgmt->bssid, ETH_ALEN) != 0)
  1491. return;
  1492. /* Process beacon from the current BSS */
  1493. baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
  1494. if (baselen > len)
  1495. return;
  1496. if (ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen,
  1497. &elems) == ParseFailed)
  1498. return;
  1499. use_protection = 0;
  1500. if (elems.erp_info && elems.erp_info_len >= 1) {
  1501. use_protection =
  1502. (elems.erp_info[0] & ERP_INFO_USE_PROTECTION) != 0;
  1503. }
  1504. if (use_protection != !!wpa_s->mlme.use_protection) {
  1505. wpa_printf(MSG_DEBUG, "MLME: CTS protection %s (BSSID=" MACSTR
  1506. ")",
  1507. use_protection ? "enabled" : "disabled",
  1508. MAC2STR(wpa_s->bssid));
  1509. wpa_s->mlme.use_protection = use_protection ? 1 : 0;
  1510. wpa_s->mlme.cts_protect_erp_frames = use_protection;
  1511. }
  1512. if (elems.wmm_param && wpa_s->mlme.wmm_enabled) {
  1513. ieee80211_sta_wmm_params(wpa_s, elems.wmm_param,
  1514. elems.wmm_param_len);
  1515. }
  1516. }
  1517. static void ieee80211_rx_mgmt_probe_req(struct wpa_supplicant *wpa_s,
  1518. struct ieee80211_mgmt *mgmt,
  1519. size_t len,
  1520. struct ieee80211_rx_status *rx_status)
  1521. {
  1522. int tx_last_beacon, adhoc;
  1523. #if 0 /* FIX */
  1524. struct ieee80211_mgmt *resp;
  1525. #endif
  1526. u8 *pos, *end;
  1527. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1528. adhoc = ssid && ssid->mode == 1;
  1529. if (!adhoc || wpa_s->mlme.state != IEEE80211_IBSS_JOINED ||
  1530. len < 24 + 2 || wpa_s->mlme.probe_resp == NULL)
  1531. return;
  1532. #if 0 /* FIX */
  1533. if (local->hw->tx_last_beacon)
  1534. tx_last_beacon = local->hw->tx_last_beacon(local->mdev);
  1535. else
  1536. #endif
  1537. tx_last_beacon = 1;
  1538. #ifdef IEEE80211_IBSS_DEBUG
  1539. wpa_printf(MSG_DEBUG, "MLME: RX ProbeReq SA=" MACSTR " DA=" MACSTR
  1540. " BSSID=" MACSTR " (tx_last_beacon=%d)",
  1541. MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
  1542. MAC2STR(mgmt->bssid), tx_last_beacon);
  1543. #endif /* IEEE80211_IBSS_DEBUG */
  1544. if (!tx_last_beacon)
  1545. return;
  1546. if (os_memcmp(mgmt->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
  1547. os_memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
  1548. return;
  1549. end = ((u8 *) mgmt) + len;
  1550. pos = mgmt->u.probe_req.variable;
  1551. if (pos[0] != WLAN_EID_SSID ||
  1552. pos + 2 + pos[1] > end) {
  1553. wpa_printf(MSG_DEBUG, "MLME: Invalid SSID IE in ProbeReq from "
  1554. MACSTR, MAC2STR(mgmt->sa));
  1555. return;
  1556. }
  1557. if (pos[1] != 0 &&
  1558. (pos[1] != wpa_s->mlme.ssid_len ||
  1559. os_memcmp(pos + 2, wpa_s->mlme.ssid, wpa_s->mlme.ssid_len) != 0))
  1560. {
  1561. /* Ignore ProbeReq for foreign SSID */
  1562. return;
  1563. }
  1564. #if 0 /* FIX */
  1565. /* Reply with ProbeResp */
  1566. skb = skb_copy(wpa_s->mlme.probe_resp, GFP_ATOMIC);
  1567. if (skb == NULL)
  1568. return;
  1569. resp = (struct ieee80211_mgmt *) skb->data;
  1570. os_memcpy(resp->da, mgmt->sa, ETH_ALEN);
  1571. #ifdef IEEE80211_IBSS_DEBUG
  1572. wpa_printf(MSG_DEBUG, "MLME: Sending ProbeResp to " MACSTR,
  1573. MAC2STR(resp->da));
  1574. #endif /* IEEE80211_IBSS_DEBUG */
  1575. ieee80211_sta_tx(wpa_s, skb, 0, 1);
  1576. #endif
  1577. }
  1578. static void ieee80211_rx_mgmt_ft_action(struct wpa_supplicant *wpa_s,
  1579. struct ieee80211_mgmt *mgmt,
  1580. size_t len,
  1581. struct ieee80211_rx_status *rx_status)
  1582. {
  1583. union wpa_event_data data;
  1584. u16 status;
  1585. u8 *sta_addr, *target_ap_addr;
  1586. if (len < 24 + 1 + sizeof(mgmt->u.action.u.ft_action_resp)) {
  1587. wpa_printf(MSG_DEBUG, "MLME: Too short FT Action frame");
  1588. return;
  1589. }
  1590. /*
  1591. * Only FT Action Response is needed for now since reservation
  1592. * protocol is not supported.
  1593. */
  1594. if (mgmt->u.action.u.ft_action_resp.action != 2) {
  1595. wpa_printf(MSG_DEBUG, "MLME: Unexpected FT Action %d",
  1596. mgmt->u.action.u.ft_action_resp.action);
  1597. return;
  1598. }
  1599. status = le_to_host16(mgmt->u.action.u.ft_action_resp.status_code);
  1600. sta_addr = mgmt->u.action.u.ft_action_resp.sta_addr;
  1601. target_ap_addr = mgmt->u.action.u.ft_action_resp.target_ap_addr;
  1602. wpa_printf(MSG_DEBUG, "MLME: Received FT Action Response: STA " MACSTR
  1603. " TargetAP " MACSTR " Status Code %d",
  1604. MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
  1605. if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
  1606. wpa_printf(MSG_DEBUG, "MLME: Foreign STA Address " MACSTR
  1607. " in FT Action Response", MAC2STR(sta_addr));
  1608. return;
  1609. }
  1610. if (status) {
  1611. wpa_printf(MSG_DEBUG, "MLME: FT Action Response indicates "
  1612. "failure (status code %d)", status);
  1613. /* TODO: report error to FT code(?) */
  1614. return;
  1615. }
  1616. os_memset(&data, 0, sizeof(data));
  1617. data.ft_ies.ies = mgmt->u.action.u.ft_action_resp.variable;
  1618. data.ft_ies.ies_len = len - (mgmt->u.action.u.ft_action_resp.variable -
  1619. (u8 *) mgmt);
  1620. data.ft_ies.ft_action = 1;
  1621. os_memcpy(data.ft_ies.target_ap, target_ap_addr, ETH_ALEN);
  1622. wpa_supplicant_event(wpa_s, EVENT_FT_RESPONSE, &data);
  1623. /* TODO: should only re-associate, if EVENT_FT_RESPONSE was processed
  1624. * successfully */
  1625. wpa_s->mlme.prev_bssid_set = 1;
  1626. wpa_s->mlme.auth_alg = WLAN_AUTH_FT;
  1627. os_memcpy(wpa_s->mlme.prev_bssid, wpa_s->bssid, ETH_ALEN);
  1628. os_memcpy(wpa_s->bssid, target_ap_addr, ETH_ALEN);
  1629. ieee80211_associate(wpa_s);
  1630. }
  1631. static void ieee80211_rx_mgmt_action(struct wpa_supplicant *wpa_s,
  1632. struct ieee80211_mgmt *mgmt,
  1633. size_t len,
  1634. struct ieee80211_rx_status *rx_status)
  1635. {
  1636. wpa_printf(MSG_DEBUG, "MLME: received Action frame");
  1637. if (len < 25)
  1638. return;
  1639. if (mgmt->u.action.category == WLAN_ACTION_FT)
  1640. ieee80211_rx_mgmt_ft_action(wpa_s, mgmt, len, rx_status);
  1641. else
  1642. wpa_printf(MSG_DEBUG, "MLME: unknown Action Category %d",
  1643. mgmt->u.action.category);
  1644. }
  1645. static void ieee80211_sta_rx_mgmt(struct wpa_supplicant *wpa_s,
  1646. const u8 *buf, size_t len,
  1647. struct ieee80211_rx_status *rx_status)
  1648. {
  1649. struct ieee80211_mgmt *mgmt;
  1650. u16 fc;
  1651. if (len < 24)
  1652. return;
  1653. mgmt = (struct ieee80211_mgmt *) buf;
  1654. fc = le_to_host16(mgmt->frame_control);
  1655. switch (WLAN_FC_GET_STYPE(fc)) {
  1656. case WLAN_FC_STYPE_PROBE_REQ:
  1657. ieee80211_rx_mgmt_probe_req(wpa_s, mgmt, len, rx_status);
  1658. break;
  1659. case WLAN_FC_STYPE_PROBE_RESP:
  1660. ieee80211_rx_mgmt_probe_resp(wpa_s, mgmt, len, rx_status);
  1661. break;
  1662. case WLAN_FC_STYPE_BEACON:
  1663. ieee80211_rx_mgmt_beacon(wpa_s, mgmt, len, rx_status);
  1664. break;
  1665. case WLAN_FC_STYPE_AUTH:
  1666. ieee80211_rx_mgmt_auth(wpa_s, mgmt, len, rx_status);
  1667. break;
  1668. case WLAN_FC_STYPE_ASSOC_RESP:
  1669. ieee80211_rx_mgmt_assoc_resp(wpa_s, mgmt, len, rx_status, 0);
  1670. break;
  1671. case WLAN_FC_STYPE_REASSOC_RESP:
  1672. ieee80211_rx_mgmt_assoc_resp(wpa_s, mgmt, len, rx_status, 1);
  1673. break;
  1674. case WLAN_FC_STYPE_DEAUTH:
  1675. ieee80211_rx_mgmt_deauth(wpa_s, mgmt, len, rx_status);
  1676. break;
  1677. case WLAN_FC_STYPE_DISASSOC:
  1678. ieee80211_rx_mgmt_disassoc(wpa_s, mgmt, len, rx_status);
  1679. break;
  1680. case WLAN_FC_STYPE_ACTION:
  1681. ieee80211_rx_mgmt_action(wpa_s, mgmt, len, rx_status);
  1682. break;
  1683. default:
  1684. wpa_printf(MSG_DEBUG, "MLME: received unknown management "
  1685. "frame - stype=%d", WLAN_FC_GET_STYPE(fc));
  1686. break;
  1687. }
  1688. }
  1689. static void ieee80211_sta_rx_scan(struct wpa_supplicant *wpa_s,
  1690. const u8 *buf, size_t len,
  1691. struct ieee80211_rx_status *rx_status)
  1692. {
  1693. struct ieee80211_mgmt *mgmt;
  1694. u16 fc;
  1695. if (len < 24)
  1696. return;
  1697. mgmt = (struct ieee80211_mgmt *) buf;
  1698. fc = le_to_host16(mgmt->frame_control);
  1699. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT) {
  1700. if (WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
  1701. ieee80211_rx_mgmt_probe_resp(wpa_s, mgmt,
  1702. len, rx_status);
  1703. } else if (WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON) {
  1704. ieee80211_rx_mgmt_beacon(wpa_s, mgmt, len, rx_status);
  1705. }
  1706. }
  1707. }
  1708. static int ieee80211_sta_active_ibss(struct wpa_supplicant *wpa_s)
  1709. {
  1710. int active = 0;
  1711. #if 0 /* FIX */
  1712. list_for_each(ptr, &local->sta_list) {
  1713. sta = list_entry(ptr, struct sta_info, list);
  1714. if (sta->dev == dev &&
  1715. time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
  1716. jiffies)) {
  1717. active++;
  1718. break;
  1719. }
  1720. }
  1721. #endif
  1722. return active;
  1723. }
  1724. static void ieee80211_sta_expire(struct wpa_supplicant *wpa_s)
  1725. {
  1726. #if 0 /* FIX */
  1727. list_for_each_safe(ptr, n, &local->sta_list) {
  1728. sta = list_entry(ptr, struct sta_info, list);
  1729. if (time_after(jiffies, sta->last_rx +
  1730. IEEE80211_IBSS_INACTIVITY_LIMIT)) {
  1731. wpa_printf(MSG_DEBUG, "MLME: expiring inactive STA "
  1732. MACSTR, MAC2STR(sta->addr));
  1733. sta_info_free(local, sta, 1);
  1734. }
  1735. }
  1736. #endif
  1737. }
  1738. static void ieee80211_sta_merge_ibss(struct wpa_supplicant *wpa_s)
  1739. {
  1740. ieee80211_reschedule_timer(wpa_s, IEEE80211_IBSS_MERGE_INTERVAL);
  1741. ieee80211_sta_expire(wpa_s);
  1742. if (ieee80211_sta_active_ibss(wpa_s))
  1743. return;
  1744. wpa_printf(MSG_DEBUG, "MLME: No active IBSS STAs - trying to scan for "
  1745. "other IBSS networks with same SSID (merge)");
  1746. ieee80211_sta_req_scan(wpa_s, wpa_s->mlme.ssid, wpa_s->mlme.ssid_len);
  1747. }
  1748. static void ieee80211_sta_timer(void *eloop_ctx, void *timeout_ctx)
  1749. {
  1750. struct wpa_supplicant *wpa_s = eloop_ctx;
  1751. switch (wpa_s->mlme.state) {
  1752. case IEEE80211_DISABLED:
  1753. break;
  1754. case IEEE80211_AUTHENTICATE:
  1755. ieee80211_authenticate(wpa_s);
  1756. break;
  1757. case IEEE80211_ASSOCIATE:
  1758. ieee80211_associate(wpa_s);
  1759. break;
  1760. case IEEE80211_ASSOCIATED:
  1761. ieee80211_associated(wpa_s);
  1762. break;
  1763. case IEEE80211_IBSS_SEARCH:
  1764. ieee80211_sta_find_ibss(wpa_s);
  1765. break;
  1766. case IEEE80211_IBSS_JOINED:
  1767. ieee80211_sta_merge_ibss(wpa_s);
  1768. break;
  1769. default:
  1770. wpa_printf(MSG_DEBUG, "ieee80211_sta_timer: Unknown state %d",
  1771. wpa_s->mlme.state);
  1772. break;
  1773. }
  1774. if (ieee80211_privacy_mismatch(wpa_s)) {
  1775. wpa_printf(MSG_DEBUG, "MLME: privacy configuration mismatch "
  1776. "and mixed-cell disabled - disassociate");
  1777. ieee80211_send_disassoc(wpa_s, WLAN_REASON_UNSPECIFIED);
  1778. ieee80211_set_associated(wpa_s, 0);
  1779. }
  1780. }
  1781. static void ieee80211_sta_new_auth(struct wpa_supplicant *wpa_s)
  1782. {
  1783. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1784. if (ssid && ssid->mode != 0)
  1785. return;
  1786. #if 0 /* FIX */
  1787. if (local->hw->reset_tsf) {
  1788. /* Reset own TSF to allow time synchronization work. */
  1789. local->hw->reset_tsf(local->mdev);
  1790. }
  1791. #endif
  1792. wpa_s->mlme.wmm_last_param_set = -1; /* allow any WMM update */
  1793. if (wpa_s->mlme.auth_algs & IEEE80211_AUTH_ALG_OPEN)
  1794. wpa_s->mlme.auth_alg = WLAN_AUTH_OPEN;
  1795. else if (wpa_s->mlme.auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
  1796. wpa_s->mlme.auth_alg = WLAN_AUTH_SHARED_KEY;
  1797. else if (wpa_s->mlme.auth_algs & IEEE80211_AUTH_ALG_LEAP)
  1798. wpa_s->mlme.auth_alg = WLAN_AUTH_LEAP;
  1799. else
  1800. wpa_s->mlme.auth_alg = WLAN_AUTH_OPEN;
  1801. wpa_printf(MSG_DEBUG, "MLME: Initial auth_alg=%d",
  1802. wpa_s->mlme.auth_alg);
  1803. wpa_s->mlme.auth_transaction = -1;
  1804. wpa_s->mlme.auth_tries = wpa_s->mlme.assoc_tries = 0;
  1805. ieee80211_authenticate(wpa_s);
  1806. }
  1807. static int ieee80211_ibss_allowed(struct wpa_supplicant *wpa_s)
  1808. {
  1809. #if 0 /* FIX */
  1810. int m, c;
  1811. for (m = 0; m < local->hw->num_modes; m++) {
  1812. struct ieee80211_hw_modes *mode = &local->hw->modes[m];
  1813. if (mode->mode != local->conf.phymode)
  1814. continue;
  1815. for (c = 0; c < mode->num_channels; c++) {
  1816. struct ieee80211_channel *chan = &mode->channels[c];
  1817. if (chan->flag & IEEE80211_CHAN_W_SCAN &&
  1818. chan->chan == local->conf.channel) {
  1819. if (chan->flag & IEEE80211_CHAN_W_IBSS)
  1820. return 1;
  1821. break;
  1822. }
  1823. }
  1824. }
  1825. #endif
  1826. return 0;
  1827. }
  1828. static int ieee80211_sta_join_ibss(struct wpa_supplicant *wpa_s,
  1829. struct ieee80211_sta_bss *bss)
  1830. {
  1831. int res = 0, rates, done = 0;
  1832. struct ieee80211_mgmt *mgmt;
  1833. #if 0 /* FIX */
  1834. struct ieee80211_tx_control control;
  1835. struct ieee80211_rate *rate;
  1836. struct rate_control_extra extra;
  1837. #endif
  1838. u8 *pos, *buf;
  1839. size_t len;
  1840. /* Remove possible STA entries from other IBSS networks. */
  1841. #if 0 /* FIX */
  1842. sta_info_flush(local, NULL);
  1843. if (local->hw->reset_tsf) {
  1844. /* Reset own TSF to allow time synchronization work. */
  1845. local->hw->reset_tsf(local->mdev);
  1846. }
  1847. #endif
  1848. os_memcpy(wpa_s->bssid, bss->bssid, ETH_ALEN);
  1849. #if 0 /* FIX */
  1850. local->conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
  1851. sdata->drop_unencrypted = bss->capability &
  1852. host_to_le16(WLAN_CAPABILITY_PRIVACY) ? 1 : 0;
  1853. #endif
  1854. #if 0 /* FIX */
  1855. os_memset(&rq, 0, sizeof(rq));
  1856. rq.m = bss->freq * 100000;
  1857. rq.e = 1;
  1858. res = ieee80211_ioctl_siwfreq(wpa_s, NULL, &rq, NULL);
  1859. #endif
  1860. if (!ieee80211_ibss_allowed(wpa_s)) {
  1861. #if 0 /* FIX */
  1862. wpa_printf(MSG_DEBUG, "MLME: IBSS not allowed on channel %d "
  1863. "(%d MHz)", local->conf.channel,
  1864. local->conf.freq);
  1865. #endif
  1866. return -1;
  1867. }
  1868. /* Set beacon template based on scan results */
  1869. buf = os_malloc(400);
  1870. len = 0;
  1871. do {
  1872. if (buf == NULL)
  1873. break;
  1874. mgmt = (struct ieee80211_mgmt *) buf;
  1875. len += 24 + sizeof(mgmt->u.beacon);
  1876. os_memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
  1877. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  1878. WLAN_FC_STYPE_BEACON);
  1879. os_memset(mgmt->da, 0xff, ETH_ALEN);
  1880. os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
  1881. os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
  1882. #if 0 /* FIX */
  1883. mgmt->u.beacon.beacon_int =
  1884. host_to_le16(local->conf.beacon_int);
  1885. #endif
  1886. mgmt->u.beacon.capab_info = host_to_le16(bss->capability);
  1887. pos = buf + len;
  1888. len += 2 + wpa_s->mlme.ssid_len;
  1889. *pos++ = WLAN_EID_SSID;
  1890. *pos++ = wpa_s->mlme.ssid_len;
  1891. os_memcpy(pos, wpa_s->mlme.ssid, wpa_s->mlme.ssid_len);
  1892. rates = bss->supp_rates_len;
  1893. if (rates > 8)
  1894. rates = 8;
  1895. pos = buf + len;
  1896. len += 2 + rates;
  1897. *pos++ = WLAN_EID_SUPP_RATES;
  1898. *pos++ = rates;
  1899. os_memcpy(pos, bss->supp_rates, rates);
  1900. pos = buf + len;
  1901. len += 2 + 1;
  1902. *pos++ = WLAN_EID_DS_PARAMS;
  1903. *pos++ = 1;
  1904. *pos++ = bss->channel;
  1905. pos = buf + len;
  1906. len += 2 + 2;
  1907. *pos++ = WLAN_EID_IBSS_PARAMS;
  1908. *pos++ = 2;
  1909. /* FIX: set ATIM window based on scan results */
  1910. *pos++ = 0;
  1911. *pos++ = 0;
  1912. if (bss->supp_rates_len > 8) {
  1913. rates = bss->supp_rates_len - 8;
  1914. pos = buf + len;
  1915. len += 2 + rates;
  1916. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  1917. *pos++ = rates;
  1918. os_memcpy(pos, &bss->supp_rates[8], rates);
  1919. }
  1920. #if 0 /* FIX */
  1921. os_memset(&control, 0, sizeof(control));
  1922. control.pkt_type = PKT_PROBE_RESP;
  1923. os_memset(&extra, 0, sizeof(extra));
  1924. extra.endidx = local->num_curr_rates;
  1925. rate = rate_control_get_rate(wpa_s, skb, &extra);
  1926. if (rate == NULL) {
  1927. wpa_printf(MSG_DEBUG, "MLME: Failed to determine TX "
  1928. "rate for IBSS beacon");
  1929. break;
  1930. }
  1931. control.tx_rate = (wpa_s->mlme.short_preamble &&
  1932. (rate->flags & IEEE80211_RATE_PREAMBLE2)) ?
  1933. rate->val2 : rate->val;
  1934. control.antenna_sel = local->conf.antenna_sel;
  1935. control.power_level = local->conf.power_level;
  1936. control.no_ack = 1;
  1937. control.retry_limit = 1;
  1938. control.rts_cts_duration = 0;
  1939. #endif
  1940. #if 0 /* FIX */
  1941. wpa_s->mlme.probe_resp = skb_copy(skb, GFP_ATOMIC);
  1942. if (wpa_s->mlme.probe_resp) {
  1943. mgmt = (struct ieee80211_mgmt *)
  1944. wpa_s->mlme.probe_resp->data;
  1945. mgmt->frame_control =
  1946. IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  1947. WLAN_FC_STYPE_PROBE_RESP);
  1948. } else {
  1949. wpa_printf(MSG_DEBUG, "MLME: Could not allocate "
  1950. "ProbeResp template for IBSS");
  1951. }
  1952. if (local->hw->beacon_update &&
  1953. local->hw->beacon_update(wpa_s, skb, &control) == 0) {
  1954. wpa_printf(MSG_DEBUG, "MLME: Configured IBSS beacon "
  1955. "template based on scan results");
  1956. skb = NULL;
  1957. }
  1958. rates = 0;
  1959. for (i = 0; i < bss->supp_rates_len; i++) {
  1960. int rate = (bss->supp_rates[i] & 0x7f) * 5;
  1961. if (local->conf.phymode == MODE_ATHEROS_TURBO)
  1962. rate *= 2;
  1963. for (j = 0; j < local->num_curr_rates; j++)
  1964. if (local->curr_rates[j].rate == rate)
  1965. rates |= BIT(j);
  1966. }
  1967. wpa_s->mlme.supp_rates_bits = rates;
  1968. #endif
  1969. done = 1;
  1970. } while (0);
  1971. os_free(buf);
  1972. if (!done) {
  1973. wpa_printf(MSG_DEBUG, "MLME: Failed to configure IBSS beacon "
  1974. "template");
  1975. }
  1976. wpa_s->mlme.state = IEEE80211_IBSS_JOINED;
  1977. ieee80211_reschedule_timer(wpa_s, IEEE80211_IBSS_MERGE_INTERVAL);
  1978. return res;
  1979. }
  1980. #if 0 /* FIX */
  1981. static int ieee80211_sta_create_ibss(struct wpa_supplicant *wpa_s)
  1982. {
  1983. struct ieee80211_sta_bss *bss;
  1984. u8 bssid[ETH_ALEN], *pos;
  1985. int i;
  1986. #if 0
  1987. /* Easier testing, use fixed BSSID. */
  1988. os_memset(bssid, 0xfe, ETH_ALEN);
  1989. #else
  1990. /* Generate random, not broadcast, locally administered BSSID. Mix in
  1991. * own MAC address to make sure that devices that do not have proper
  1992. * random number generator get different BSSID. */
  1993. os_get_random(bssid, ETH_ALEN);
  1994. for (i = 0; i < ETH_ALEN; i++)
  1995. bssid[i] ^= wpa_s->own_addr[i];
  1996. bssid[0] &= ~0x01;
  1997. bssid[0] |= 0x02;
  1998. #endif
  1999. wpa_printf(MSG_DEBUG, "MLME: Creating new IBSS network, BSSID "
  2000. MACSTR "", MAC2STR(bssid));
  2001. bss = ieee80211_bss_add(wpa_s, bssid);
  2002. if (bss == NULL)
  2003. return -ENOMEM;
  2004. #if 0 /* FIX */
  2005. if (local->conf.beacon_int == 0)
  2006. local->conf.beacon_int = 100;
  2007. bss->beacon_int = local->conf.beacon_int;
  2008. bss->hw_mode = local->conf.phymode;
  2009. bss->channel = local->conf.channel;
  2010. bss->freq = local->conf.freq;
  2011. #endif
  2012. os_get_time(&bss->last_update);
  2013. bss->capability = host_to_le16(WLAN_CAPABILITY_IBSS);
  2014. #if 0 /* FIX */
  2015. if (sdata->default_key) {
  2016. bss->capability |= host_to_le16(WLAN_CAPABILITY_PRIVACY);
  2017. } else
  2018. sdata->drop_unencrypted = 0;
  2019. bss->supp_rates_len = local->num_curr_rates;
  2020. #endif
  2021. pos = bss->supp_rates;
  2022. #if 0 /* FIX */
  2023. for (i = 0; i < local->num_curr_rates; i++) {
  2024. int rate = local->curr_rates[i].rate;
  2025. if (local->conf.phymode == MODE_ATHEROS_TURBO)
  2026. rate /= 2;
  2027. *pos++ = (u8) (rate / 5);
  2028. }
  2029. #endif
  2030. return ieee80211_sta_join_ibss(wpa_s, bss);
  2031. }
  2032. #endif
  2033. static int ieee80211_sta_find_ibss(struct wpa_supplicant *wpa_s)
  2034. {
  2035. struct ieee80211_sta_bss *bss;
  2036. int found = 0;
  2037. u8 bssid[ETH_ALEN];
  2038. int active_ibss;
  2039. struct os_time now;
  2040. if (wpa_s->mlme.ssid_len == 0)
  2041. return -EINVAL;
  2042. active_ibss = ieee80211_sta_active_ibss(wpa_s);
  2043. #ifdef IEEE80211_IBSS_DEBUG
  2044. wpa_printf(MSG_DEBUG, "MLME: sta_find_ibss (active_ibss=%d)",
  2045. active_ibss);
  2046. #endif /* IEEE80211_IBSS_DEBUG */
  2047. for (bss = wpa_s->mlme.sta_bss_list; bss; bss = bss->next) {
  2048. if (wpa_s->mlme.ssid_len != bss->ssid_len ||
  2049. os_memcmp(wpa_s->mlme.ssid, bss->ssid, bss->ssid_len) != 0
  2050. || !(bss->capability & WLAN_CAPABILITY_IBSS))
  2051. continue;
  2052. #ifdef IEEE80211_IBSS_DEBUG
  2053. wpa_printf(MSG_DEBUG, " bssid=" MACSTR " found",
  2054. MAC2STR(bss->bssid));
  2055. #endif /* IEEE80211_IBSS_DEBUG */
  2056. os_memcpy(bssid, bss->bssid, ETH_ALEN);
  2057. found = 1;
  2058. if (active_ibss ||
  2059. os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)
  2060. break;
  2061. }
  2062. #ifdef IEEE80211_IBSS_DEBUG
  2063. wpa_printf(MSG_DEBUG, " sta_find_ibss: selected " MACSTR " current "
  2064. MACSTR, MAC2STR(bssid), MAC2STR(wpa_s->bssid));
  2065. #endif /* IEEE80211_IBSS_DEBUG */
  2066. if (found && os_memcmp(wpa_s->bssid, bssid, ETH_ALEN) != 0 &&
  2067. (bss = ieee80211_bss_get(wpa_s, bssid))) {
  2068. wpa_printf(MSG_DEBUG, "MLME: Selected IBSS BSSID " MACSTR
  2069. " based on configured SSID",
  2070. MAC2STR(bssid));
  2071. return ieee80211_sta_join_ibss(wpa_s, bss);
  2072. }
  2073. #ifdef IEEE80211_IBSS_DEBUG
  2074. wpa_printf(MSG_DEBUG, " did not try to join ibss");
  2075. #endif /* IEEE80211_IBSS_DEBUG */
  2076. /* Selected IBSS not found in current scan results - try to scan */
  2077. os_get_time(&now);
  2078. #if 0 /* FIX */
  2079. if (wpa_s->mlme.state == IEEE80211_IBSS_JOINED &&
  2080. !ieee80211_sta_active_ibss(wpa_s)) {
  2081. ieee80211_reschedule_timer(wpa_s,
  2082. IEEE80211_IBSS_MERGE_INTERVAL);
  2083. } else if (time_after(jiffies, wpa_s->mlme.last_scan_completed +
  2084. IEEE80211_SCAN_INTERVAL)) {
  2085. wpa_printf(MSG_DEBUG, "MLME: Trigger new scan to find an IBSS "
  2086. "to join");
  2087. return ieee80211_sta_req_scan(wpa_s->mlme.ssid,
  2088. wpa_s->mlme.ssid_len);
  2089. } else if (wpa_s->mlme.state != IEEE80211_IBSS_JOINED) {
  2090. int interval = IEEE80211_SCAN_INTERVAL;
  2091. if (time_after(jiffies, wpa_s->mlme.ibss_join_req +
  2092. IEEE80211_IBSS_JOIN_TIMEOUT)) {
  2093. if (wpa_s->mlme.create_ibss &&
  2094. ieee80211_ibss_allowed(wpa_s))
  2095. return ieee80211_sta_create_ibss(wpa_s);
  2096. if (wpa_s->mlme.create_ibss) {
  2097. wpa_printf(MSG_DEBUG, "MLME: IBSS not allowed "
  2098. "on the configured channel %d "
  2099. "(%d MHz)",
  2100. local->conf.channel,
  2101. local->conf.freq);
  2102. }
  2103. /* No IBSS found - decrease scan interval and continue
  2104. * scanning. */
  2105. interval = IEEE80211_SCAN_INTERVAL_SLOW;
  2106. }
  2107. wpa_s->mlme.state = IEEE80211_IBSS_SEARCH;
  2108. ieee80211_reschedule_timer(wpa_s, interval);
  2109. return 0;
  2110. }
  2111. #endif
  2112. return 0;
  2113. }
  2114. int ieee80211_sta_get_ssid(struct wpa_supplicant *wpa_s, u8 *ssid,
  2115. size_t *len)
  2116. {
  2117. os_memcpy(ssid, wpa_s->mlme.ssid, wpa_s->mlme.ssid_len);
  2118. *len = wpa_s->mlme.ssid_len;
  2119. return 0;
  2120. }
  2121. int ieee80211_sta_associate(struct wpa_supplicant *wpa_s,
  2122. struct wpa_driver_associate_params *params)
  2123. {
  2124. struct ieee80211_sta_bss *bss;
  2125. wpa_s->mlme.bssid_set = 0;
  2126. wpa_s->mlme.freq = params->freq;
  2127. if (params->bssid) {
  2128. os_memcpy(wpa_s->bssid, params->bssid, ETH_ALEN);
  2129. if (os_memcmp(params->bssid, "\x00\x00\x00\x00\x00\x00",
  2130. ETH_ALEN))
  2131. wpa_s->mlme.bssid_set = 1;
  2132. bss = ieee80211_bss_get(wpa_s, wpa_s->bssid);
  2133. if (bss) {
  2134. wpa_s->mlme.phymode = bss->hw_mode;
  2135. wpa_s->mlme.channel = bss->channel;
  2136. wpa_s->mlme.freq = bss->freq;
  2137. }
  2138. }
  2139. #if 0 /* FIX */
  2140. /* TODO: This should always be done for IBSS, even if IEEE80211_QOS is
  2141. * not defined. */
  2142. if (local->hw->conf_tx) {
  2143. struct ieee80211_tx_queue_params qparam;
  2144. int i;
  2145. os_memset(&qparam, 0, sizeof(qparam));
  2146. /* TODO: are these ok defaults for all hw_modes? */
  2147. qparam.aifs = 2;
  2148. qparam.cw_min =
  2149. local->conf.phymode == MODE_IEEE80211B ? 31 : 15;
  2150. qparam.cw_max = 1023;
  2151. qparam.burst_time = 0;
  2152. for (i = IEEE80211_TX_QUEUE_DATA0; i < NUM_TX_DATA_QUEUES; i++)
  2153. {
  2154. local->hw->conf_tx(wpa_s, i + IEEE80211_TX_QUEUE_DATA0,
  2155. &qparam);
  2156. }
  2157. /* IBSS uses different parameters for Beacon sending */
  2158. qparam.cw_min++;
  2159. qparam.cw_min *= 2;
  2160. qparam.cw_min--;
  2161. local->hw->conf_tx(wpa_s, IEEE80211_TX_QUEUE_BEACON, &qparam);
  2162. }
  2163. #endif
  2164. if (wpa_s->mlme.ssid_len != params->ssid_len ||
  2165. os_memcmp(wpa_s->mlme.ssid, params->ssid, params->ssid_len) != 0)
  2166. wpa_s->mlme.prev_bssid_set = 0;
  2167. os_memcpy(wpa_s->mlme.ssid, params->ssid, params->ssid_len);
  2168. os_memset(wpa_s->mlme.ssid + params->ssid_len, 0,
  2169. MAX_SSID_LEN - params->ssid_len);
  2170. wpa_s->mlme.ssid_len = params->ssid_len;
  2171. wpa_s->mlme.ssid_set = 1;
  2172. os_free(wpa_s->mlme.extra_ie);
  2173. if (params->wpa_ie == NULL || params->wpa_ie_len == 0) {
  2174. wpa_s->mlme.extra_ie = NULL;
  2175. wpa_s->mlme.extra_ie_len = 0;
  2176. } else {
  2177. wpa_s->mlme.extra_ie = os_malloc(params->wpa_ie_len);
  2178. if (wpa_s->mlme.extra_ie == NULL) {
  2179. wpa_s->mlme.extra_ie_len = 0;
  2180. return -1;
  2181. }
  2182. os_memcpy(wpa_s->mlme.extra_ie, params->wpa_ie,
  2183. params->wpa_ie_len);
  2184. wpa_s->mlme.extra_ie_len = params->wpa_ie_len;
  2185. }
  2186. wpa_s->mlme.key_mgmt = params->key_mgmt_suite;
  2187. ieee80211_sta_set_channel(wpa_s, wpa_s->mlme.phymode,
  2188. wpa_s->mlme.channel, wpa_s->mlme.freq);
  2189. if (params->mode == 1 && !wpa_s->mlme.bssid_set) {
  2190. os_get_time(&wpa_s->mlme.ibss_join_req);
  2191. wpa_s->mlme.state = IEEE80211_IBSS_SEARCH;
  2192. return ieee80211_sta_find_ibss(wpa_s);
  2193. }
  2194. if (wpa_s->mlme.bssid_set)
  2195. ieee80211_sta_new_auth(wpa_s);
  2196. return 0;
  2197. }
  2198. static void ieee80211_sta_save_oper_chan(struct wpa_supplicant *wpa_s)
  2199. {
  2200. wpa_s->mlme.scan_oper_channel = wpa_s->mlme.channel;
  2201. wpa_s->mlme.scan_oper_freq = wpa_s->mlme.freq;
  2202. wpa_s->mlme.scan_oper_phymode = wpa_s->mlme.phymode;
  2203. }
  2204. static int ieee80211_sta_restore_oper_chan(struct wpa_supplicant *wpa_s)
  2205. {
  2206. wpa_s->mlme.channel = wpa_s->mlme.scan_oper_channel;
  2207. wpa_s->mlme.freq = wpa_s->mlme.scan_oper_freq;
  2208. wpa_s->mlme.phymode = wpa_s->mlme.scan_oper_phymode;
  2209. if (wpa_s->mlme.freq == 0)
  2210. return 0;
  2211. return ieee80211_sta_set_channel(wpa_s, wpa_s->mlme.phymode,
  2212. wpa_s->mlme.channel,
  2213. wpa_s->mlme.freq);
  2214. }
  2215. static int ieee80211_active_scan(struct wpa_supplicant *wpa_s)
  2216. {
  2217. size_t m;
  2218. int c;
  2219. for (m = 0; m < wpa_s->mlme.num_modes; m++) {
  2220. struct wpa_hw_modes *mode = &wpa_s->mlme.modes[m];
  2221. if ((int) mode->mode != (int) wpa_s->mlme.phymode)
  2222. continue;
  2223. for (c = 0; c < mode->num_channels; c++) {
  2224. struct wpa_channel_data *chan = &mode->channels[c];
  2225. if (chan->flag & WPA_CHAN_W_SCAN &&
  2226. chan->chan == wpa_s->mlme.channel) {
  2227. if (chan->flag & WPA_CHAN_W_ACTIVE_SCAN)
  2228. return 1;
  2229. break;
  2230. }
  2231. }
  2232. }
  2233. return 0;
  2234. }
  2235. static void ieee80211_sta_scan_timer(void *eloop_ctx, void *timeout_ctx)
  2236. {
  2237. struct wpa_supplicant *wpa_s = eloop_ctx;
  2238. struct wpa_hw_modes *mode;
  2239. struct wpa_channel_data *chan;
  2240. int skip = 0;
  2241. int timeout = 0;
  2242. struct wpa_ssid *ssid = wpa_s->current_ssid;
  2243. int adhoc;
  2244. if (!wpa_s->mlme.sta_scanning || wpa_s->mlme.modes == NULL)
  2245. return;
  2246. adhoc = ssid && ssid->mode == 1;
  2247. switch (wpa_s->mlme.scan_state) {
  2248. case SCAN_SET_CHANNEL:
  2249. mode = &wpa_s->mlme.modes[wpa_s->mlme.scan_hw_mode_idx];
  2250. if (wpa_s->mlme.scan_hw_mode_idx >=
  2251. (int) wpa_s->mlme.num_modes ||
  2252. (wpa_s->mlme.scan_hw_mode_idx + 1 ==
  2253. (int) wpa_s->mlme.num_modes
  2254. && wpa_s->mlme.scan_channel_idx >= mode->num_channels)) {
  2255. if (ieee80211_sta_restore_oper_chan(wpa_s)) {
  2256. wpa_printf(MSG_DEBUG, "MLME: failed to "
  2257. "restore operational channel after "
  2258. "scan");
  2259. }
  2260. wpa_printf(MSG_DEBUG, "MLME: scan completed");
  2261. wpa_s->mlme.sta_scanning = 0;
  2262. os_get_time(&wpa_s->mlme.last_scan_completed);
  2263. wpa_supplicant_event(wpa_s, EVENT_SCAN_RESULTS, NULL);
  2264. if (adhoc) {
  2265. if (!wpa_s->mlme.bssid_set ||
  2266. (wpa_s->mlme.state ==
  2267. IEEE80211_IBSS_JOINED &&
  2268. !ieee80211_sta_active_ibss(wpa_s)))
  2269. ieee80211_sta_find_ibss(wpa_s);
  2270. }
  2271. return;
  2272. }
  2273. skip = !(wpa_s->mlme.hw_modes & (1 << mode->mode));
  2274. chan = &mode->channels[wpa_s->mlme.scan_channel_idx];
  2275. if (!(chan->flag & WPA_CHAN_W_SCAN) ||
  2276. (adhoc && !(chan->flag & WPA_CHAN_W_IBSS)) ||
  2277. (wpa_s->mlme.hw_modes & (1 << WPA_MODE_IEEE80211G) &&
  2278. mode->mode == WPA_MODE_IEEE80211B &&
  2279. wpa_s->mlme.scan_skip_11b))
  2280. skip = 1;
  2281. if (!skip) {
  2282. wpa_printf(MSG_MSGDUMP,
  2283. "MLME: scan channel %d (%d MHz)",
  2284. chan->chan, chan->freq);
  2285. wpa_s->mlme.channel = chan->chan;
  2286. wpa_s->mlme.freq = chan->freq;
  2287. wpa_s->mlme.phymode = mode->mode;
  2288. if (ieee80211_sta_set_channel(wpa_s, mode->mode,
  2289. chan->chan, chan->freq))
  2290. {
  2291. wpa_printf(MSG_DEBUG, "MLME: failed to set "
  2292. "channel %d (%d MHz) for scan",
  2293. chan->chan, chan->freq);
  2294. skip = 1;
  2295. }
  2296. }
  2297. wpa_s->mlme.scan_channel_idx++;
  2298. if (wpa_s->mlme.scan_channel_idx >=
  2299. wpa_s->mlme.modes[wpa_s->mlme.scan_hw_mode_idx].
  2300. num_channels) {
  2301. wpa_s->mlme.scan_hw_mode_idx++;
  2302. wpa_s->mlme.scan_channel_idx = 0;
  2303. }
  2304. if (skip) {
  2305. timeout = 0;
  2306. break;
  2307. }
  2308. timeout = IEEE80211_PROBE_DELAY;
  2309. wpa_s->mlme.scan_state = SCAN_SEND_PROBE;
  2310. break;
  2311. case SCAN_SEND_PROBE:
  2312. if (ieee80211_active_scan(wpa_s)) {
  2313. ieee80211_send_probe_req(wpa_s, NULL,
  2314. wpa_s->mlme.scan_ssid,
  2315. wpa_s->mlme.scan_ssid_len);
  2316. timeout = IEEE80211_CHANNEL_TIME;
  2317. } else {
  2318. timeout = IEEE80211_PASSIVE_CHANNEL_TIME;
  2319. }
  2320. wpa_s->mlme.scan_state = SCAN_SET_CHANNEL;
  2321. break;
  2322. }
  2323. eloop_register_timeout(timeout / 1000, 1000 * (timeout % 1000),
  2324. ieee80211_sta_scan_timer, wpa_s, NULL);
  2325. }
  2326. int ieee80211_sta_req_scan(struct wpa_supplicant *wpa_s, const u8 *ssid,
  2327. size_t ssid_len)
  2328. {
  2329. if (ssid_len > MAX_SSID_LEN)
  2330. return -1;
  2331. /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
  2332. * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
  2333. * BSSID: MACAddress
  2334. * SSID
  2335. * ScanType: ACTIVE, PASSIVE
  2336. * ProbeDelay: delay (in microseconds) to be used prior to transmitting
  2337. * a Probe frame during active scanning
  2338. * ChannelList
  2339. * MinChannelTime (>= ProbeDelay), in TU
  2340. * MaxChannelTime: (>= MinChannelTime), in TU
  2341. */
  2342. /* MLME-SCAN.confirm
  2343. * BSSDescriptionSet
  2344. * ResultCode: SUCCESS, INVALID_PARAMETERS
  2345. */
  2346. /* TODO: if assoc, move to power save mode for the duration of the
  2347. * scan */
  2348. if (wpa_s->mlme.sta_scanning)
  2349. return -1;
  2350. wpa_printf(MSG_DEBUG, "MLME: starting scan");
  2351. ieee80211_sta_save_oper_chan(wpa_s);
  2352. wpa_s->mlme.sta_scanning = 1;
  2353. /* TODO: stop TX queue? */
  2354. if (ssid) {
  2355. wpa_s->mlme.scan_ssid_len = ssid_len;
  2356. os_memcpy(wpa_s->mlme.scan_ssid, ssid, ssid_len);
  2357. } else
  2358. wpa_s->mlme.scan_ssid_len = 0;
  2359. wpa_s->mlme.scan_skip_11b = 1; /* FIX: clear this is 11g is not
  2360. * supported */
  2361. wpa_s->mlme.scan_state = SCAN_SET_CHANNEL;
  2362. wpa_s->mlme.scan_hw_mode_idx = 0;
  2363. wpa_s->mlme.scan_channel_idx = 0;
  2364. eloop_register_timeout(0, 1, ieee80211_sta_scan_timer, wpa_s, NULL);
  2365. return 0;
  2366. }
  2367. struct wpa_scan_results *
  2368. ieee80211_sta_get_scan_results(struct wpa_supplicant *wpa_s)
  2369. {
  2370. size_t ap_num = 0;
  2371. struct wpa_scan_results *res;
  2372. struct wpa_scan_res *r;
  2373. struct ieee80211_sta_bss *bss;
  2374. res = os_zalloc(sizeof(*res));
  2375. for (bss = wpa_s->mlme.sta_bss_list; bss; bss = bss->next)
  2376. ap_num++;
  2377. res->res = os_zalloc(ap_num * sizeof(struct wpa_scan_res *));
  2378. if (res->res == NULL) {
  2379. os_free(res);
  2380. return NULL;
  2381. }
  2382. for (bss = wpa_s->mlme.sta_bss_list; bss; bss = bss->next) {
  2383. r = os_zalloc(sizeof(*r) + bss->ie_len);
  2384. if (r == NULL)
  2385. break;
  2386. os_memcpy(r->bssid, bss->bssid, ETH_ALEN);
  2387. r->freq = bss->freq;
  2388. r->beacon_int = bss->beacon_int;
  2389. r->caps = bss->capability;
  2390. r->level = bss->rssi;
  2391. r->tsf = bss->timestamp;
  2392. if (bss->ie) {
  2393. r->ie_len = bss->ie_len;
  2394. os_memcpy(r + 1, bss->ie, bss->ie_len);
  2395. }
  2396. res->res[res->num++] = r;
  2397. }
  2398. return res;
  2399. }
  2400. #if 0 /* FIX */
  2401. struct sta_info * ieee80211_ibss_add_sta(struct wpa_supplicant *wpa_s,
  2402. struct sk_buff *skb, u8 *bssid,
  2403. u8 *addr)
  2404. {
  2405. struct ieee80211_local *local = dev->priv;
  2406. struct list_head *ptr;
  2407. struct sta_info *sta;
  2408. struct wpa_supplicant *sta_dev = NULL;
  2409. /* TODO: Could consider removing the least recently used entry and
  2410. * allow new one to be added. */
  2411. if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
  2412. if (net_ratelimit()) {
  2413. wpa_printf(MSG_DEBUG, "MLME: No room for a new IBSS "
  2414. "STA entry " MACSTR, MAC2STR(addr));
  2415. }
  2416. return NULL;
  2417. }
  2418. spin_lock_bh(&local->sub_if_lock);
  2419. list_for_each(ptr, &local->sub_if_list) {
  2420. sdata = list_entry(ptr, struct ieee80211_sub_if_data, list);
  2421. if (sdata->type == IEEE80211_SUB_IF_TYPE_STA &&
  2422. os_memcmp(bssid, sdata->u.sta.bssid, ETH_ALEN) == 0) {
  2423. sta_dev = sdata->dev;
  2424. break;
  2425. }
  2426. }
  2427. spin_unlock_bh(&local->sub_if_lock);
  2428. if (sta_dev == NULL)
  2429. return NULL;
  2430. wpa_printf(MSG_DEBUG, "MLME: Adding new IBSS station " MACSTR
  2431. " (dev=%s)", MAC2STR(addr), sta_dev->name);
  2432. sta = sta_info_add(wpa_s, addr);
  2433. if (sta == NULL) {
  2434. return NULL;
  2435. }
  2436. sta->dev = sta_dev;
  2437. sta->supp_rates = wpa_s->mlme.supp_rates_bits;
  2438. rate_control_rate_init(local, sta);
  2439. return sta; /* caller will call sta_info_release() */
  2440. }
  2441. #endif
  2442. int ieee80211_sta_deauthenticate(struct wpa_supplicant *wpa_s, u16 reason)
  2443. {
  2444. wpa_printf(MSG_DEBUG, "MLME: deauthenticate(reason=%d)", reason);
  2445. ieee80211_send_deauth(wpa_s, reason);
  2446. ieee80211_set_associated(wpa_s, 0);
  2447. return 0;
  2448. }
  2449. int ieee80211_sta_disassociate(struct wpa_supplicant *wpa_s, u16 reason)
  2450. {
  2451. wpa_printf(MSG_DEBUG, "MLME: disassociate(reason=%d)", reason);
  2452. if (!wpa_s->mlme.associated)
  2453. return -1;
  2454. ieee80211_send_disassoc(wpa_s, reason);
  2455. ieee80211_set_associated(wpa_s, 0);
  2456. return 0;
  2457. }
  2458. void ieee80211_sta_rx(struct wpa_supplicant *wpa_s, const u8 *buf, size_t len,
  2459. struct ieee80211_rx_status *rx_status)
  2460. {
  2461. struct ieee80211_mgmt *mgmt;
  2462. u16 fc;
  2463. const u8 *pos;
  2464. /* wpa_hexdump(MSG_MSGDUMP, "MLME: Received frame", buf, len); */
  2465. if (wpa_s->mlme.sta_scanning) {
  2466. ieee80211_sta_rx_scan(wpa_s, buf, len, rx_status);
  2467. return;
  2468. }
  2469. if (len < 24)
  2470. return;
  2471. mgmt = (struct ieee80211_mgmt *) buf;
  2472. fc = le_to_host16(mgmt->frame_control);
  2473. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT)
  2474. ieee80211_sta_rx_mgmt(wpa_s, buf, len, rx_status);
  2475. else if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA) {
  2476. if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) !=
  2477. WLAN_FC_FROMDS)
  2478. return;
  2479. /* mgmt->sa is actually BSSID for FromDS data frames */
  2480. if (os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) != 0)
  2481. return;
  2482. /* Skip IEEE 802.11 and LLC headers */
  2483. pos = buf + 24 + 6;
  2484. if (WPA_GET_BE16(pos) != ETH_P_EAPOL)
  2485. return;
  2486. pos += 2;
  2487. /* mgmt->bssid is actually BSSID for SA data frames */
  2488. wpa_supplicant_rx_eapol(wpa_s, mgmt->bssid,
  2489. pos, buf + len - pos);
  2490. }
  2491. }
  2492. void ieee80211_sta_free_hw_features(struct wpa_hw_modes *hw_features,
  2493. size_t num_hw_features)
  2494. {
  2495. size_t i;
  2496. if (hw_features == NULL)
  2497. return;
  2498. for (i = 0; i < num_hw_features; i++) {
  2499. os_free(hw_features[i].channels);
  2500. os_free(hw_features[i].rates);
  2501. }
  2502. os_free(hw_features);
  2503. }
  2504. int ieee80211_sta_init(struct wpa_supplicant *wpa_s)
  2505. {
  2506. u16 num_modes, flags;
  2507. wpa_s->mlme.modes = wpa_drv_get_hw_feature_data(wpa_s, &num_modes,
  2508. &flags);
  2509. if (wpa_s->mlme.modes == NULL) {
  2510. wpa_printf(MSG_ERROR, "MLME: Failed to read supported "
  2511. "channels and rates from the driver");
  2512. return -1;
  2513. }
  2514. wpa_s->mlme.num_modes = num_modes;
  2515. wpa_s->mlme.hw_modes = 1 << WPA_MODE_IEEE80211A;
  2516. wpa_s->mlme.hw_modes |= 1 << WPA_MODE_IEEE80211B;
  2517. wpa_s->mlme.hw_modes |= 1 << WPA_MODE_IEEE80211G;
  2518. return 0;
  2519. }
  2520. void ieee80211_sta_deinit(struct wpa_supplicant *wpa_s)
  2521. {
  2522. eloop_cancel_timeout(ieee80211_sta_timer, wpa_s, NULL);
  2523. eloop_cancel_timeout(ieee80211_sta_scan_timer, wpa_s, NULL);
  2524. os_free(wpa_s->mlme.extra_ie);
  2525. wpa_s->mlme.extra_ie = NULL;
  2526. os_free(wpa_s->mlme.extra_probe_ie);
  2527. wpa_s->mlme.extra_probe_ie = NULL;
  2528. os_free(wpa_s->mlme.assocreq_ies);
  2529. wpa_s->mlme.assocreq_ies = NULL;
  2530. os_free(wpa_s->mlme.assocresp_ies);
  2531. wpa_s->mlme.assocresp_ies = NULL;
  2532. ieee80211_bss_list_deinit(wpa_s);
  2533. ieee80211_sta_free_hw_features(wpa_s->mlme.modes,
  2534. wpa_s->mlme.num_modes);
  2535. #ifdef CONFIG_IEEE80211R
  2536. os_free(wpa_s->mlme.ft_ies);
  2537. wpa_s->mlme.ft_ies = NULL;
  2538. wpa_s->mlme.ft_ies_len = 0;
  2539. #endif /* CONFIG_IEEE80211R */
  2540. }
  2541. #ifdef CONFIG_IEEE80211R
  2542. int ieee80211_sta_update_ft_ies(struct wpa_supplicant *wpa_s, const u8 *md,
  2543. const u8 *ies, size_t ies_len)
  2544. {
  2545. if (md == NULL) {
  2546. wpa_printf(MSG_DEBUG, "MLME: Clear FT mobility domain");
  2547. os_memset(wpa_s->mlme.current_md, 0, MOBILITY_DOMAIN_ID_LEN);
  2548. } else {
  2549. wpa_printf(MSG_DEBUG, "MLME: Update FT IEs for MD " MACSTR,
  2550. MAC2STR(md));
  2551. os_memcpy(wpa_s->mlme.current_md, md, MOBILITY_DOMAIN_ID_LEN);
  2552. }
  2553. wpa_hexdump(MSG_DEBUG, "MLME: FT IEs", ies, ies_len);
  2554. os_free(wpa_s->mlme.ft_ies);
  2555. wpa_s->mlme.ft_ies = os_malloc(ies_len);
  2556. if (wpa_s->mlme.ft_ies == NULL)
  2557. return -1;
  2558. os_memcpy(wpa_s->mlme.ft_ies, ies, ies_len);
  2559. wpa_s->mlme.ft_ies_len = ies_len;
  2560. return 0;
  2561. }
  2562. int ieee80211_sta_send_ft_action(struct wpa_supplicant *wpa_s, u8 action,
  2563. const u8 *target_ap,
  2564. const u8 *ies, size_t ies_len)
  2565. {
  2566. u8 *buf;
  2567. size_t len;
  2568. struct ieee80211_mgmt *mgmt;
  2569. int res;
  2570. /*
  2571. * Action frame payload:
  2572. * Category[1] = 6 (Fast BSS Transition)
  2573. * Action[1] = 1 (Fast BSS Transition Request)
  2574. * STA Address
  2575. * Target AP Address
  2576. * FT IEs
  2577. */
  2578. buf = os_zalloc(sizeof(*mgmt) + ies_len);
  2579. if (buf == NULL) {
  2580. wpa_printf(MSG_DEBUG, "MLME: Failed to allocate buffer for "
  2581. "FT action frame");
  2582. return -1;
  2583. }
  2584. mgmt = (struct ieee80211_mgmt *) buf;
  2585. len = 24;
  2586. os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
  2587. os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
  2588. os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
  2589. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  2590. WLAN_FC_STYPE_ACTION);
  2591. mgmt->u.action.category = WLAN_ACTION_FT;
  2592. mgmt->u.action.u.ft_action_req.action = action;
  2593. os_memcpy(mgmt->u.action.u.ft_action_req.sta_addr, wpa_s->own_addr,
  2594. ETH_ALEN);
  2595. os_memcpy(mgmt->u.action.u.ft_action_req.target_ap_addr, target_ap,
  2596. ETH_ALEN);
  2597. os_memcpy(mgmt->u.action.u.ft_action_req.variable, ies, ies_len);
  2598. len += 1 + sizeof(mgmt->u.action.u.ft_action_req) + ies_len;
  2599. wpa_printf(MSG_DEBUG, "MLME: Send FT Action Frame: Action=%d "
  2600. "Target AP=" MACSTR " body_len=%d",
  2601. action, MAC2STR(target_ap), ies_len);
  2602. res = ieee80211_sta_tx(wpa_s, buf, len);
  2603. os_free(buf);
  2604. return res;
  2605. }
  2606. #endif /* CONFIG_IEEE80211R */
  2607. int ieee80211_sta_set_probe_req_ie(struct wpa_supplicant *wpa_s, const u8 *ies,
  2608. size_t ies_len)
  2609. {
  2610. os_free(wpa_s->mlme.extra_probe_ie);
  2611. wpa_s->mlme.extra_probe_ie = NULL;
  2612. wpa_s->mlme.extra_probe_ie_len = 0;
  2613. if (ies == NULL)
  2614. return 0;
  2615. wpa_s->mlme.extra_probe_ie = os_malloc(ies_len);
  2616. if (wpa_s->mlme.extra_probe_ie == NULL)
  2617. return -1;
  2618. os_memcpy(wpa_s->mlme.extra_probe_ie, ies, ies_len);
  2619. wpa_s->mlme.extra_probe_ie_len = ies_len;
  2620. return 0;
  2621. }