mlme.c 79 KB

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