mlme.c 79 KB

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