mlme.c 79 KB

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