mlme.c 79 KB

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