mlme.c 86 KB

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