mlme.c 82 KB

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