mlme.c 78 KB

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