mlme.c 86 KB

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