mlme.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799
  1. /*
  2. * BSS client mode implementation
  3. * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
  4. * Copyright 2004, Instant802 Networks, Inc.
  5. * Copyright 2005, Devicescape Software, Inc.
  6. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  7. * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/delay.h>
  14. #include <linux/if_ether.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/if_arp.h>
  17. #include <linux/wireless.h>
  18. #include <linux/random.h>
  19. #include <linux/etherdevice.h>
  20. #include <linux/rtnetlink.h>
  21. #include <net/iw_handler.h>
  22. #include <net/mac80211.h>
  23. #include <asm/unaligned.h>
  24. #include "ieee80211_i.h"
  25. #include "rate.h"
  26. #include "led.h"
  27. #define IEEE80211_ASSOC_SCANS_MAX_TRIES 2
  28. #define IEEE80211_AUTH_TIMEOUT (HZ / 5)
  29. #define IEEE80211_AUTH_MAX_TRIES 3
  30. #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
  31. #define IEEE80211_ASSOC_MAX_TRIES 3
  32. #define IEEE80211_MONITORING_INTERVAL (2 * HZ)
  33. #define IEEE80211_PROBE_INTERVAL (60 * HZ)
  34. #define IEEE80211_RETRY_AUTH_INTERVAL (1 * HZ)
  35. #define IEEE80211_SCAN_INTERVAL (2 * HZ)
  36. #define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
  37. #define IEEE80211_IBSS_JOIN_TIMEOUT (7 * HZ)
  38. #define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
  39. #define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
  40. #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
  41. /* utils */
  42. static int ecw2cw(int ecw)
  43. {
  44. return (1 << ecw) - 1;
  45. }
  46. static u8 *ieee80211_bss_get_ie(struct ieee80211_bss *bss, u8 ie)
  47. {
  48. u8 *end, *pos;
  49. pos = bss->ies;
  50. if (pos == NULL)
  51. return NULL;
  52. end = pos + bss->ies_len;
  53. while (pos + 1 < end) {
  54. if (pos + 2 + pos[1] > end)
  55. break;
  56. if (pos[0] == ie)
  57. return pos;
  58. pos += 2 + pos[1];
  59. }
  60. return NULL;
  61. }
  62. static int ieee80211_compatible_rates(struct ieee80211_bss *bss,
  63. struct ieee80211_supported_band *sband,
  64. u32 *rates)
  65. {
  66. int i, j, count;
  67. *rates = 0;
  68. count = 0;
  69. for (i = 0; i < bss->supp_rates_len; i++) {
  70. int rate = (bss->supp_rates[i] & 0x7F) * 5;
  71. for (j = 0; j < sband->n_bitrates; j++)
  72. if (sband->bitrates[j].bitrate == rate) {
  73. *rates |= BIT(j);
  74. count++;
  75. break;
  76. }
  77. }
  78. return count;
  79. }
  80. /* also used by mesh code */
  81. u32 ieee80211_sta_get_rates(struct ieee80211_local *local,
  82. struct ieee802_11_elems *elems,
  83. enum ieee80211_band band)
  84. {
  85. struct ieee80211_supported_band *sband;
  86. struct ieee80211_rate *bitrates;
  87. size_t num_rates;
  88. u32 supp_rates;
  89. int i, j;
  90. sband = local->hw.wiphy->bands[band];
  91. if (!sband) {
  92. WARN_ON(1);
  93. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  94. }
  95. bitrates = sband->bitrates;
  96. num_rates = sband->n_bitrates;
  97. supp_rates = 0;
  98. for (i = 0; i < elems->supp_rates_len +
  99. elems->ext_supp_rates_len; i++) {
  100. u8 rate = 0;
  101. int own_rate;
  102. if (i < elems->supp_rates_len)
  103. rate = elems->supp_rates[i];
  104. else if (elems->ext_supp_rates)
  105. rate = elems->ext_supp_rates
  106. [i - elems->supp_rates_len];
  107. own_rate = 5 * (rate & 0x7f);
  108. for (j = 0; j < num_rates; j++)
  109. if (bitrates[j].bitrate == own_rate)
  110. supp_rates |= BIT(j);
  111. }
  112. return supp_rates;
  113. }
  114. /* frame sending functions */
  115. static void add_extra_ies(struct sk_buff *skb, u8 *ies, size_t ies_len)
  116. {
  117. if (ies)
  118. memcpy(skb_put(skb, ies_len), ies, ies_len);
  119. }
  120. /* also used by scanning code */
  121. void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
  122. u8 *ssid, size_t ssid_len)
  123. {
  124. struct ieee80211_local *local = sdata->local;
  125. struct ieee80211_supported_band *sband;
  126. struct sk_buff *skb;
  127. struct ieee80211_mgmt *mgmt;
  128. u8 *pos, *supp_rates, *esupp_rates = NULL;
  129. int i;
  130. skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200 +
  131. sdata->u.sta.ie_probereq_len);
  132. if (!skb) {
  133. printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
  134. "request\n", sdata->dev->name);
  135. return;
  136. }
  137. skb_reserve(skb, local->hw.extra_tx_headroom);
  138. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
  139. memset(mgmt, 0, 24);
  140. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  141. IEEE80211_STYPE_PROBE_REQ);
  142. memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
  143. if (dst) {
  144. memcpy(mgmt->da, dst, ETH_ALEN);
  145. memcpy(mgmt->bssid, dst, ETH_ALEN);
  146. } else {
  147. memset(mgmt->da, 0xff, ETH_ALEN);
  148. memset(mgmt->bssid, 0xff, ETH_ALEN);
  149. }
  150. pos = skb_put(skb, 2 + ssid_len);
  151. *pos++ = WLAN_EID_SSID;
  152. *pos++ = ssid_len;
  153. memcpy(pos, ssid, ssid_len);
  154. supp_rates = skb_put(skb, 2);
  155. supp_rates[0] = WLAN_EID_SUPP_RATES;
  156. supp_rates[1] = 0;
  157. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  158. for (i = 0; i < sband->n_bitrates; i++) {
  159. struct ieee80211_rate *rate = &sband->bitrates[i];
  160. if (esupp_rates) {
  161. pos = skb_put(skb, 1);
  162. esupp_rates[1]++;
  163. } else if (supp_rates[1] == 8) {
  164. esupp_rates = skb_put(skb, 3);
  165. esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
  166. esupp_rates[1] = 1;
  167. pos = &esupp_rates[2];
  168. } else {
  169. pos = skb_put(skb, 1);
  170. supp_rates[1]++;
  171. }
  172. *pos = rate->bitrate / 5;
  173. }
  174. add_extra_ies(skb, sdata->u.sta.ie_probereq,
  175. sdata->u.sta.ie_probereq_len);
  176. ieee80211_tx_skb(sdata, skb, 0);
  177. }
  178. static void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
  179. struct ieee80211_if_sta *ifsta,
  180. int transaction, u8 *extra, size_t extra_len,
  181. int encrypt)
  182. {
  183. struct ieee80211_local *local = sdata->local;
  184. struct sk_buff *skb;
  185. struct ieee80211_mgmt *mgmt;
  186. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  187. sizeof(*mgmt) + 6 + extra_len +
  188. sdata->u.sta.ie_auth_len);
  189. if (!skb) {
  190. printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
  191. "frame\n", sdata->dev->name);
  192. return;
  193. }
  194. skb_reserve(skb, local->hw.extra_tx_headroom);
  195. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
  196. memset(mgmt, 0, 24 + 6);
  197. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  198. IEEE80211_STYPE_AUTH);
  199. if (encrypt)
  200. mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  201. memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
  202. memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
  203. memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
  204. mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
  205. mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
  206. ifsta->auth_transaction = transaction + 1;
  207. mgmt->u.auth.status_code = cpu_to_le16(0);
  208. if (extra)
  209. memcpy(skb_put(skb, extra_len), extra, extra_len);
  210. add_extra_ies(skb, sdata->u.sta.ie_auth, sdata->u.sta.ie_auth_len);
  211. ieee80211_tx_skb(sdata, skb, encrypt);
  212. }
  213. static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata,
  214. struct ieee80211_if_sta *ifsta)
  215. {
  216. struct ieee80211_local *local = sdata->local;
  217. struct sk_buff *skb;
  218. struct ieee80211_mgmt *mgmt;
  219. u8 *pos, *ies, *ht_ie, *e_ies;
  220. int i, len, count, rates_len, supp_rates_len;
  221. u16 capab;
  222. struct ieee80211_bss *bss;
  223. int wmm = 0;
  224. struct ieee80211_supported_band *sband;
  225. u32 rates = 0;
  226. size_t e_ies_len;
  227. if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
  228. e_ies = sdata->u.sta.ie_reassocreq;
  229. e_ies_len = sdata->u.sta.ie_reassocreq_len;
  230. } else {
  231. e_ies = sdata->u.sta.ie_assocreq;
  232. e_ies_len = sdata->u.sta.ie_assocreq_len;
  233. }
  234. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  235. sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
  236. ifsta->ssid_len + e_ies_len);
  237. if (!skb) {
  238. printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
  239. "frame\n", sdata->dev->name);
  240. return;
  241. }
  242. skb_reserve(skb, local->hw.extra_tx_headroom);
  243. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  244. capab = ifsta->capab;
  245. if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
  246. if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
  247. capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
  248. if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
  249. capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
  250. }
  251. bss = ieee80211_rx_bss_get(local, ifsta->bssid,
  252. local->hw.conf.channel->center_freq,
  253. ifsta->ssid, ifsta->ssid_len);
  254. if (bss) {
  255. if (bss->capability & WLAN_CAPABILITY_PRIVACY)
  256. capab |= WLAN_CAPABILITY_PRIVACY;
  257. if (bss->wmm_used)
  258. wmm = 1;
  259. /* get all rates supported by the device and the AP as
  260. * some APs don't like getting a superset of their rates
  261. * in the association request (e.g. D-Link DAP 1353 in
  262. * b-only mode) */
  263. rates_len = ieee80211_compatible_rates(bss, sband, &rates);
  264. if ((bss->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
  265. (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
  266. capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
  267. ieee80211_rx_bss_put(local, bss);
  268. } else {
  269. rates = ~0;
  270. rates_len = sband->n_bitrates;
  271. }
  272. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
  273. memset(mgmt, 0, 24);
  274. memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
  275. memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
  276. memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
  277. if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
  278. skb_put(skb, 10);
  279. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  280. IEEE80211_STYPE_REASSOC_REQ);
  281. mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
  282. mgmt->u.reassoc_req.listen_interval =
  283. cpu_to_le16(local->hw.conf.listen_interval);
  284. memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
  285. ETH_ALEN);
  286. } else {
  287. skb_put(skb, 4);
  288. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  289. IEEE80211_STYPE_ASSOC_REQ);
  290. mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
  291. mgmt->u.assoc_req.listen_interval =
  292. cpu_to_le16(local->hw.conf.listen_interval);
  293. }
  294. /* SSID */
  295. ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
  296. *pos++ = WLAN_EID_SSID;
  297. *pos++ = ifsta->ssid_len;
  298. memcpy(pos, ifsta->ssid, ifsta->ssid_len);
  299. /* add all rates which were marked to be used above */
  300. supp_rates_len = rates_len;
  301. if (supp_rates_len > 8)
  302. supp_rates_len = 8;
  303. len = sband->n_bitrates;
  304. pos = skb_put(skb, supp_rates_len + 2);
  305. *pos++ = WLAN_EID_SUPP_RATES;
  306. *pos++ = supp_rates_len;
  307. count = 0;
  308. for (i = 0; i < sband->n_bitrates; i++) {
  309. if (BIT(i) & rates) {
  310. int rate = sband->bitrates[i].bitrate;
  311. *pos++ = (u8) (rate / 5);
  312. if (++count == 8)
  313. break;
  314. }
  315. }
  316. if (rates_len > count) {
  317. pos = skb_put(skb, rates_len - count + 2);
  318. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  319. *pos++ = rates_len - count;
  320. for (i++; i < sband->n_bitrates; i++) {
  321. if (BIT(i) & rates) {
  322. int rate = sband->bitrates[i].bitrate;
  323. *pos++ = (u8) (rate / 5);
  324. }
  325. }
  326. }
  327. if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
  328. /* 1. power capabilities */
  329. pos = skb_put(skb, 4);
  330. *pos++ = WLAN_EID_PWR_CAPABILITY;
  331. *pos++ = 2;
  332. *pos++ = 0; /* min tx power */
  333. *pos++ = local->hw.conf.channel->max_power; /* max tx power */
  334. /* 2. supported channels */
  335. /* TODO: get this in reg domain format */
  336. pos = skb_put(skb, 2 * sband->n_channels + 2);
  337. *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
  338. *pos++ = 2 * sband->n_channels;
  339. for (i = 0; i < sband->n_channels; i++) {
  340. *pos++ = ieee80211_frequency_to_channel(
  341. sband->channels[i].center_freq);
  342. *pos++ = 1; /* one channel in the subband*/
  343. }
  344. }
  345. if (ifsta->extra_ie) {
  346. pos = skb_put(skb, ifsta->extra_ie_len);
  347. memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
  348. }
  349. if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
  350. pos = skb_put(skb, 9);
  351. *pos++ = WLAN_EID_VENDOR_SPECIFIC;
  352. *pos++ = 7; /* len */
  353. *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
  354. *pos++ = 0x50;
  355. *pos++ = 0xf2;
  356. *pos++ = 2; /* WME */
  357. *pos++ = 0; /* WME info */
  358. *pos++ = 1; /* WME ver */
  359. *pos++ = 0;
  360. }
  361. /* wmm support is a must to HT */
  362. /*
  363. * IEEE802.11n does not allow TKIP/WEP as pairwise
  364. * ciphers in HT mode. We still associate in non-ht
  365. * mode (11a/b/g) if any one of these ciphers is
  366. * configured as pairwise.
  367. */
  368. if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED) &&
  369. sband->ht_cap.ht_supported &&
  370. (ht_ie = ieee80211_bss_get_ie(bss, WLAN_EID_HT_INFORMATION)) &&
  371. ht_ie[1] >= sizeof(struct ieee80211_ht_info) &&
  372. (!(ifsta->flags & IEEE80211_STA_TKIP_WEP_USED))) {
  373. struct ieee80211_ht_info *ht_info =
  374. (struct ieee80211_ht_info *)(ht_ie + 2);
  375. u16 cap = sband->ht_cap.cap;
  376. __le16 tmp;
  377. u32 flags = local->hw.conf.channel->flags;
  378. switch (ht_info->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  379. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  380. if (flags & IEEE80211_CHAN_NO_FAT_ABOVE) {
  381. cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  382. cap &= ~IEEE80211_HT_CAP_SGI_40;
  383. }
  384. break;
  385. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  386. if (flags & IEEE80211_CHAN_NO_FAT_BELOW) {
  387. cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  388. cap &= ~IEEE80211_HT_CAP_SGI_40;
  389. }
  390. break;
  391. }
  392. tmp = cpu_to_le16(cap);
  393. pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
  394. *pos++ = WLAN_EID_HT_CAPABILITY;
  395. *pos++ = sizeof(struct ieee80211_ht_cap);
  396. memset(pos, 0, sizeof(struct ieee80211_ht_cap));
  397. memcpy(pos, &tmp, sizeof(u16));
  398. pos += sizeof(u16);
  399. /* TODO: needs a define here for << 2 */
  400. *pos++ = sband->ht_cap.ampdu_factor |
  401. (sband->ht_cap.ampdu_density << 2);
  402. memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
  403. }
  404. add_extra_ies(skb, e_ies, e_ies_len);
  405. kfree(ifsta->assocreq_ies);
  406. ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
  407. ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
  408. if (ifsta->assocreq_ies)
  409. memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
  410. ieee80211_tx_skb(sdata, skb, 0);
  411. }
  412. static void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
  413. u16 stype, u16 reason)
  414. {
  415. struct ieee80211_local *local = sdata->local;
  416. struct ieee80211_if_sta *ifsta = &sdata->u.sta;
  417. struct sk_buff *skb;
  418. struct ieee80211_mgmt *mgmt;
  419. u8 *ies;
  420. size_t ies_len;
  421. if (stype == IEEE80211_STYPE_DEAUTH) {
  422. ies = sdata->u.sta.ie_deauth;
  423. ies_len = sdata->u.sta.ie_deauth_len;
  424. } else {
  425. ies = sdata->u.sta.ie_disassoc;
  426. ies_len = sdata->u.sta.ie_disassoc_len;
  427. }
  428. skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) +
  429. ies_len);
  430. if (!skb) {
  431. printk(KERN_DEBUG "%s: failed to allocate buffer for "
  432. "deauth/disassoc frame\n", sdata->dev->name);
  433. return;
  434. }
  435. skb_reserve(skb, local->hw.extra_tx_headroom);
  436. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
  437. memset(mgmt, 0, 24);
  438. memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
  439. memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
  440. memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
  441. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
  442. skb_put(skb, 2);
  443. /* u.deauth.reason_code == u.disassoc.reason_code */
  444. mgmt->u.deauth.reason_code = cpu_to_le16(reason);
  445. add_extra_ies(skb, ies, ies_len);
  446. ieee80211_tx_skb(sdata, skb, ifsta->flags & IEEE80211_STA_MFP_ENABLED);
  447. }
  448. /* MLME */
  449. static void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
  450. struct ieee80211_bss *bss)
  451. {
  452. struct ieee80211_local *local = sdata->local;
  453. int i, have_higher_than_11mbit = 0;
  454. /* cf. IEEE 802.11 9.2.12 */
  455. for (i = 0; i < bss->supp_rates_len; i++)
  456. if ((bss->supp_rates[i] & 0x7f) * 5 > 110)
  457. have_higher_than_11mbit = 1;
  458. if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
  459. have_higher_than_11mbit)
  460. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  461. else
  462. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  463. ieee80211_set_wmm_default(sdata);
  464. }
  465. static void ieee80211_sta_wmm_params(struct ieee80211_local *local,
  466. struct ieee80211_if_sta *ifsta,
  467. u8 *wmm_param, size_t wmm_param_len)
  468. {
  469. struct ieee80211_tx_queue_params params;
  470. size_t left;
  471. int count;
  472. u8 *pos;
  473. if (!(ifsta->flags & IEEE80211_STA_WMM_ENABLED))
  474. return;
  475. if (!wmm_param)
  476. return;
  477. if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
  478. return;
  479. count = wmm_param[6] & 0x0f;
  480. if (count == ifsta->wmm_last_param_set)
  481. return;
  482. ifsta->wmm_last_param_set = count;
  483. pos = wmm_param + 8;
  484. left = wmm_param_len - 8;
  485. memset(&params, 0, sizeof(params));
  486. if (!local->ops->conf_tx)
  487. return;
  488. local->wmm_acm = 0;
  489. for (; left >= 4; left -= 4, pos += 4) {
  490. int aci = (pos[0] >> 5) & 0x03;
  491. int acm = (pos[0] >> 4) & 0x01;
  492. int queue;
  493. switch (aci) {
  494. case 1:
  495. queue = 3;
  496. if (acm)
  497. local->wmm_acm |= BIT(0) | BIT(3);
  498. break;
  499. case 2:
  500. queue = 1;
  501. if (acm)
  502. local->wmm_acm |= BIT(4) | BIT(5);
  503. break;
  504. case 3:
  505. queue = 0;
  506. if (acm)
  507. local->wmm_acm |= BIT(6) | BIT(7);
  508. break;
  509. case 0:
  510. default:
  511. queue = 2;
  512. if (acm)
  513. local->wmm_acm |= BIT(1) | BIT(2);
  514. break;
  515. }
  516. params.aifs = pos[0] & 0x0f;
  517. params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
  518. params.cw_min = ecw2cw(pos[1] & 0x0f);
  519. params.txop = get_unaligned_le16(pos + 2);
  520. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  521. printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
  522. "cWmin=%d cWmax=%d txop=%d\n",
  523. local->mdev->name, queue, aci, acm, params.aifs, params.cw_min,
  524. params.cw_max, params.txop);
  525. #endif
  526. /* TODO: handle ACM (block TX, fallback to next lowest allowed
  527. * AC for now) */
  528. if (local->ops->conf_tx(local_to_hw(local), queue, &params)) {
  529. printk(KERN_DEBUG "%s: failed to set TX queue "
  530. "parameters for queue %d\n", local->mdev->name, queue);
  531. }
  532. }
  533. }
  534. static bool check_tim(struct ieee802_11_elems *elems, u16 aid, bool *is_mc)
  535. {
  536. u8 mask;
  537. u8 index, indexn1, indexn2;
  538. struct ieee80211_tim_ie *tim = (struct ieee80211_tim_ie *) elems->tim;
  539. aid &= 0x3fff;
  540. index = aid / 8;
  541. mask = 1 << (aid & 7);
  542. if (tim->bitmap_ctrl & 0x01)
  543. *is_mc = true;
  544. indexn1 = tim->bitmap_ctrl & 0xfe;
  545. indexn2 = elems->tim_len + indexn1 - 4;
  546. if (index < indexn1 || index > indexn2)
  547. return false;
  548. index -= indexn1;
  549. return !!(tim->virtual_map[index] & mask);
  550. }
  551. static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
  552. u16 capab, bool erp_valid, u8 erp)
  553. {
  554. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  555. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  556. struct ieee80211_if_sta *ifsta = &sdata->u.sta;
  557. #endif
  558. u32 changed = 0;
  559. bool use_protection;
  560. bool use_short_preamble;
  561. bool use_short_slot;
  562. if (erp_valid) {
  563. use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
  564. use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
  565. } else {
  566. use_protection = false;
  567. use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
  568. }
  569. use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
  570. if (use_protection != bss_conf->use_cts_prot) {
  571. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  572. if (net_ratelimit()) {
  573. printk(KERN_DEBUG "%s: CTS protection %s (BSSID=%pM)\n",
  574. sdata->dev->name,
  575. use_protection ? "enabled" : "disabled",
  576. ifsta->bssid);
  577. }
  578. #endif
  579. bss_conf->use_cts_prot = use_protection;
  580. changed |= BSS_CHANGED_ERP_CTS_PROT;
  581. }
  582. if (use_short_preamble != bss_conf->use_short_preamble) {
  583. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  584. if (net_ratelimit()) {
  585. printk(KERN_DEBUG "%s: switched to %s barker preamble"
  586. " (BSSID=%pM)\n",
  587. sdata->dev->name,
  588. use_short_preamble ? "short" : "long",
  589. ifsta->bssid);
  590. }
  591. #endif
  592. bss_conf->use_short_preamble = use_short_preamble;
  593. changed |= BSS_CHANGED_ERP_PREAMBLE;
  594. }
  595. if (use_short_slot != bss_conf->use_short_slot) {
  596. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  597. if (net_ratelimit()) {
  598. printk(KERN_DEBUG "%s: switched to %s slot time"
  599. " (BSSID=%pM)\n",
  600. sdata->dev->name,
  601. use_short_slot ? "short" : "long",
  602. ifsta->bssid);
  603. }
  604. #endif
  605. bss_conf->use_short_slot = use_short_slot;
  606. changed |= BSS_CHANGED_ERP_SLOT;
  607. }
  608. return changed;
  609. }
  610. static void ieee80211_sta_send_apinfo(struct ieee80211_sub_if_data *sdata,
  611. struct ieee80211_if_sta *ifsta)
  612. {
  613. union iwreq_data wrqu;
  614. memset(&wrqu, 0, sizeof(wrqu));
  615. if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
  616. memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
  617. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  618. wireless_send_event(sdata->dev, SIOCGIWAP, &wrqu, NULL);
  619. }
  620. static void ieee80211_sta_send_associnfo(struct ieee80211_sub_if_data *sdata,
  621. struct ieee80211_if_sta *ifsta)
  622. {
  623. char *buf;
  624. size_t len;
  625. int i;
  626. union iwreq_data wrqu;
  627. if (!ifsta->assocreq_ies && !ifsta->assocresp_ies)
  628. return;
  629. buf = kmalloc(50 + 2 * (ifsta->assocreq_ies_len +
  630. ifsta->assocresp_ies_len), GFP_KERNEL);
  631. if (!buf)
  632. return;
  633. len = sprintf(buf, "ASSOCINFO(");
  634. if (ifsta->assocreq_ies) {
  635. len += sprintf(buf + len, "ReqIEs=");
  636. for (i = 0; i < ifsta->assocreq_ies_len; i++) {
  637. len += sprintf(buf + len, "%02x",
  638. ifsta->assocreq_ies[i]);
  639. }
  640. }
  641. if (ifsta->assocresp_ies) {
  642. if (ifsta->assocreq_ies)
  643. len += sprintf(buf + len, " ");
  644. len += sprintf(buf + len, "RespIEs=");
  645. for (i = 0; i < ifsta->assocresp_ies_len; i++) {
  646. len += sprintf(buf + len, "%02x",
  647. ifsta->assocresp_ies[i]);
  648. }
  649. }
  650. len += sprintf(buf + len, ")");
  651. if (len > IW_CUSTOM_MAX) {
  652. len = sprintf(buf, "ASSOCRESPIE=");
  653. for (i = 0; i < ifsta->assocresp_ies_len; i++) {
  654. len += sprintf(buf + len, "%02x",
  655. ifsta->assocresp_ies[i]);
  656. }
  657. }
  658. if (len <= IW_CUSTOM_MAX) {
  659. memset(&wrqu, 0, sizeof(wrqu));
  660. wrqu.data.length = len;
  661. wireless_send_event(sdata->dev, IWEVCUSTOM, &wrqu, buf);
  662. }
  663. kfree(buf);
  664. }
  665. static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
  666. struct ieee80211_if_sta *ifsta,
  667. u32 bss_info_changed)
  668. {
  669. struct ieee80211_local *local = sdata->local;
  670. struct ieee80211_conf *conf = &local_to_hw(local)->conf;
  671. struct ieee80211_bss *bss;
  672. bss_info_changed |= BSS_CHANGED_ASSOC;
  673. ifsta->flags |= IEEE80211_STA_ASSOCIATED;
  674. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  675. return;
  676. bss = ieee80211_rx_bss_get(local, ifsta->bssid,
  677. conf->channel->center_freq,
  678. ifsta->ssid, ifsta->ssid_len);
  679. if (bss) {
  680. /* set timing information */
  681. sdata->vif.bss_conf.beacon_int = bss->beacon_int;
  682. sdata->vif.bss_conf.timestamp = bss->timestamp;
  683. sdata->vif.bss_conf.dtim_period = bss->dtim_period;
  684. bss_info_changed |= ieee80211_handle_bss_capability(sdata,
  685. bss->capability, bss->has_erp_value, bss->erp_value);
  686. ieee80211_rx_bss_put(local, bss);
  687. }
  688. ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
  689. memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
  690. ieee80211_sta_send_associnfo(sdata, ifsta);
  691. ifsta->last_probe = jiffies;
  692. ieee80211_led_assoc(local, 1);
  693. sdata->vif.bss_conf.assoc = 1;
  694. /*
  695. * For now just always ask the driver to update the basic rateset
  696. * when we have associated, we aren't checking whether it actually
  697. * changed or not.
  698. */
  699. bss_info_changed |= BSS_CHANGED_BASIC_RATES;
  700. ieee80211_bss_info_change_notify(sdata, bss_info_changed);
  701. if (local->powersave) {
  702. if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS) &&
  703. local->hw.conf.dynamic_ps_timeout > 0) {
  704. mod_timer(&local->dynamic_ps_timer, jiffies +
  705. msecs_to_jiffies(
  706. local->hw.conf.dynamic_ps_timeout));
  707. } else {
  708. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
  709. ieee80211_send_nullfunc(local, sdata, 1);
  710. conf->flags |= IEEE80211_CONF_PS;
  711. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  712. }
  713. }
  714. netif_tx_start_all_queues(sdata->dev);
  715. netif_carrier_on(sdata->dev);
  716. ieee80211_sta_send_apinfo(sdata, ifsta);
  717. }
  718. static void ieee80211_direct_probe(struct ieee80211_sub_if_data *sdata,
  719. struct ieee80211_if_sta *ifsta)
  720. {
  721. ifsta->direct_probe_tries++;
  722. if (ifsta->direct_probe_tries > IEEE80211_AUTH_MAX_TRIES) {
  723. printk(KERN_DEBUG "%s: direct probe to AP %pM timed out\n",
  724. sdata->dev->name, ifsta->bssid);
  725. ifsta->state = IEEE80211_STA_MLME_DISABLED;
  726. ieee80211_sta_send_apinfo(sdata, ifsta);
  727. /*
  728. * Most likely AP is not in the range so remove the
  729. * bss information associated to the AP
  730. */
  731. ieee80211_rx_bss_remove(sdata, ifsta->bssid,
  732. sdata->local->hw.conf.channel->center_freq,
  733. ifsta->ssid, ifsta->ssid_len);
  734. return;
  735. }
  736. printk(KERN_DEBUG "%s: direct probe to AP %pM try %d\n",
  737. sdata->dev->name, ifsta->bssid,
  738. ifsta->direct_probe_tries);
  739. ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
  740. set_bit(IEEE80211_STA_REQ_DIRECT_PROBE, &ifsta->request);
  741. /* Direct probe is sent to broadcast address as some APs
  742. * will not answer to direct packet in unassociated state.
  743. */
  744. ieee80211_send_probe_req(sdata, NULL,
  745. ifsta->ssid, ifsta->ssid_len);
  746. mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
  747. }
  748. static void ieee80211_authenticate(struct ieee80211_sub_if_data *sdata,
  749. struct ieee80211_if_sta *ifsta)
  750. {
  751. ifsta->auth_tries++;
  752. if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
  753. printk(KERN_DEBUG "%s: authentication with AP %pM"
  754. " timed out\n",
  755. sdata->dev->name, ifsta->bssid);
  756. ifsta->state = IEEE80211_STA_MLME_DISABLED;
  757. ieee80211_sta_send_apinfo(sdata, ifsta);
  758. ieee80211_rx_bss_remove(sdata, ifsta->bssid,
  759. sdata->local->hw.conf.channel->center_freq,
  760. ifsta->ssid, ifsta->ssid_len);
  761. return;
  762. }
  763. ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
  764. printk(KERN_DEBUG "%s: authenticate with AP %pM\n",
  765. sdata->dev->name, ifsta->bssid);
  766. ieee80211_send_auth(sdata, ifsta, 1, NULL, 0, 0);
  767. mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
  768. }
  769. /*
  770. * The disassoc 'reason' argument can be either our own reason
  771. * if self disconnected or a reason code from the AP.
  772. */
  773. static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
  774. struct ieee80211_if_sta *ifsta, bool deauth,
  775. bool self_disconnected, u16 reason)
  776. {
  777. struct ieee80211_local *local = sdata->local;
  778. struct sta_info *sta;
  779. u32 changed = 0, config_changed = 0;
  780. rcu_read_lock();
  781. sta = sta_info_get(local, ifsta->bssid);
  782. if (!sta) {
  783. rcu_read_unlock();
  784. return;
  785. }
  786. if (deauth) {
  787. ifsta->direct_probe_tries = 0;
  788. ifsta->auth_tries = 0;
  789. }
  790. ifsta->assoc_scan_tries = 0;
  791. ifsta->assoc_tries = 0;
  792. netif_tx_stop_all_queues(sdata->dev);
  793. netif_carrier_off(sdata->dev);
  794. ieee80211_sta_tear_down_BA_sessions(sdata, sta->sta.addr);
  795. if (self_disconnected) {
  796. if (deauth)
  797. ieee80211_send_deauth_disassoc(sdata,
  798. IEEE80211_STYPE_DEAUTH, reason);
  799. else
  800. ieee80211_send_deauth_disassoc(sdata,
  801. IEEE80211_STYPE_DISASSOC, reason);
  802. }
  803. ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
  804. changed |= ieee80211_reset_erp_info(sdata);
  805. ieee80211_led_assoc(local, 0);
  806. changed |= BSS_CHANGED_ASSOC;
  807. sdata->vif.bss_conf.assoc = false;
  808. ieee80211_sta_send_apinfo(sdata, ifsta);
  809. if (self_disconnected || reason == WLAN_REASON_DISASSOC_STA_HAS_LEFT) {
  810. ifsta->state = IEEE80211_STA_MLME_DISABLED;
  811. ieee80211_rx_bss_remove(sdata, ifsta->bssid,
  812. sdata->local->hw.conf.channel->center_freq,
  813. ifsta->ssid, ifsta->ssid_len);
  814. }
  815. rcu_read_unlock();
  816. /* channel(_type) changes are handled by ieee80211_hw_config */
  817. local->oper_channel_type = NL80211_CHAN_NO_HT;
  818. local->power_constr_level = 0;
  819. del_timer_sync(&local->dynamic_ps_timer);
  820. cancel_work_sync(&local->dynamic_ps_enable_work);
  821. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  822. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  823. config_changed |= IEEE80211_CONF_CHANGE_PS;
  824. }
  825. ieee80211_hw_config(local, config_changed);
  826. ieee80211_bss_info_change_notify(sdata, changed);
  827. rcu_read_lock();
  828. sta = sta_info_get(local, ifsta->bssid);
  829. if (!sta) {
  830. rcu_read_unlock();
  831. return;
  832. }
  833. sta_info_unlink(&sta);
  834. rcu_read_unlock();
  835. sta_info_destroy(sta);
  836. }
  837. static int ieee80211_sta_wep_configured(struct ieee80211_sub_if_data *sdata)
  838. {
  839. if (!sdata || !sdata->default_key ||
  840. sdata->default_key->conf.alg != ALG_WEP)
  841. return 0;
  842. return 1;
  843. }
  844. static int ieee80211_privacy_mismatch(struct ieee80211_sub_if_data *sdata,
  845. struct ieee80211_if_sta *ifsta)
  846. {
  847. struct ieee80211_local *local = sdata->local;
  848. struct ieee80211_bss *bss;
  849. int bss_privacy;
  850. int wep_privacy;
  851. int privacy_invoked;
  852. if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
  853. return 0;
  854. bss = ieee80211_rx_bss_get(local, ifsta->bssid,
  855. local->hw.conf.channel->center_freq,
  856. ifsta->ssid, ifsta->ssid_len);
  857. if (!bss)
  858. return 0;
  859. bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
  860. wep_privacy = !!ieee80211_sta_wep_configured(sdata);
  861. privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
  862. ieee80211_rx_bss_put(local, bss);
  863. if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
  864. return 0;
  865. return 1;
  866. }
  867. static void ieee80211_associate(struct ieee80211_sub_if_data *sdata,
  868. struct ieee80211_if_sta *ifsta)
  869. {
  870. ifsta->assoc_tries++;
  871. if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
  872. printk(KERN_DEBUG "%s: association with AP %pM"
  873. " timed out\n",
  874. sdata->dev->name, ifsta->bssid);
  875. ifsta->state = IEEE80211_STA_MLME_DISABLED;
  876. ieee80211_sta_send_apinfo(sdata, ifsta);
  877. ieee80211_rx_bss_remove(sdata, ifsta->bssid,
  878. sdata->local->hw.conf.channel->center_freq,
  879. ifsta->ssid, ifsta->ssid_len);
  880. return;
  881. }
  882. ifsta->state = IEEE80211_STA_MLME_ASSOCIATE;
  883. printk(KERN_DEBUG "%s: associate with AP %pM\n",
  884. sdata->dev->name, ifsta->bssid);
  885. if (ieee80211_privacy_mismatch(sdata, ifsta)) {
  886. printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
  887. "mixed-cell disabled - abort association\n", sdata->dev->name);
  888. ifsta->state = IEEE80211_STA_MLME_DISABLED;
  889. return;
  890. }
  891. ieee80211_send_assoc(sdata, ifsta);
  892. mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
  893. }
  894. static void ieee80211_associated(struct ieee80211_sub_if_data *sdata,
  895. struct ieee80211_if_sta *ifsta)
  896. {
  897. struct ieee80211_local *local = sdata->local;
  898. struct sta_info *sta;
  899. int disassoc;
  900. /* TODO: start monitoring current AP signal quality and number of
  901. * missed beacons. Scan other channels every now and then and search
  902. * for better APs. */
  903. /* TODO: remove expired BSSes */
  904. ifsta->state = IEEE80211_STA_MLME_ASSOCIATED;
  905. rcu_read_lock();
  906. sta = sta_info_get(local, ifsta->bssid);
  907. if (!sta) {
  908. printk(KERN_DEBUG "%s: No STA entry for own AP %pM\n",
  909. sdata->dev->name, ifsta->bssid);
  910. disassoc = 1;
  911. } else {
  912. disassoc = 0;
  913. if (time_after(jiffies,
  914. sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
  915. if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
  916. printk(KERN_DEBUG "%s: No ProbeResp from "
  917. "current AP %pM - assume out of "
  918. "range\n",
  919. sdata->dev->name, ifsta->bssid);
  920. disassoc = 1;
  921. } else
  922. ieee80211_send_probe_req(sdata, ifsta->bssid,
  923. ifsta->ssid,
  924. ifsta->ssid_len);
  925. ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
  926. } else {
  927. ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
  928. if (time_after(jiffies, ifsta->last_probe +
  929. IEEE80211_PROBE_INTERVAL)) {
  930. ifsta->last_probe = jiffies;
  931. ieee80211_send_probe_req(sdata, ifsta->bssid,
  932. ifsta->ssid,
  933. ifsta->ssid_len);
  934. }
  935. }
  936. }
  937. rcu_read_unlock();
  938. if (disassoc)
  939. ieee80211_set_disassoc(sdata, ifsta, true, true,
  940. WLAN_REASON_PREV_AUTH_NOT_VALID);
  941. else
  942. mod_timer(&ifsta->timer, jiffies +
  943. IEEE80211_MONITORING_INTERVAL);
  944. }
  945. static void ieee80211_auth_completed(struct ieee80211_sub_if_data *sdata,
  946. struct ieee80211_if_sta *ifsta)
  947. {
  948. printk(KERN_DEBUG "%s: authenticated\n", sdata->dev->name);
  949. ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
  950. ieee80211_associate(sdata, ifsta);
  951. }
  952. static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
  953. struct ieee80211_if_sta *ifsta,
  954. struct ieee80211_mgmt *mgmt,
  955. size_t len)
  956. {
  957. u8 *pos;
  958. struct ieee802_11_elems elems;
  959. pos = mgmt->u.auth.variable;
  960. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  961. if (!elems.challenge)
  962. return;
  963. ieee80211_send_auth(sdata, ifsta, 3, elems.challenge - 2,
  964. elems.challenge_len + 2, 1);
  965. }
  966. static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
  967. struct ieee80211_if_sta *ifsta,
  968. struct ieee80211_mgmt *mgmt,
  969. size_t len)
  970. {
  971. u16 auth_alg, auth_transaction, status_code;
  972. if (ifsta->state != IEEE80211_STA_MLME_AUTHENTICATE &&
  973. sdata->vif.type != NL80211_IFTYPE_ADHOC)
  974. return;
  975. if (len < 24 + 6)
  976. return;
  977. if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  978. memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
  979. return;
  980. if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  981. memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
  982. return;
  983. auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
  984. auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
  985. status_code = le16_to_cpu(mgmt->u.auth.status_code);
  986. if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  987. /*
  988. * IEEE 802.11 standard does not require authentication in IBSS
  989. * networks and most implementations do not seem to use it.
  990. * However, try to reply to authentication attempts if someone
  991. * has actually implemented this.
  992. */
  993. if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1)
  994. return;
  995. ieee80211_send_auth(sdata, ifsta, 2, NULL, 0, 0);
  996. }
  997. if (auth_alg != ifsta->auth_alg ||
  998. auth_transaction != ifsta->auth_transaction)
  999. return;
  1000. if (status_code != WLAN_STATUS_SUCCESS) {
  1001. if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
  1002. u8 algs[3];
  1003. const int num_algs = ARRAY_SIZE(algs);
  1004. int i, pos;
  1005. algs[0] = algs[1] = algs[2] = 0xff;
  1006. if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
  1007. algs[0] = WLAN_AUTH_OPEN;
  1008. if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
  1009. algs[1] = WLAN_AUTH_SHARED_KEY;
  1010. if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
  1011. algs[2] = WLAN_AUTH_LEAP;
  1012. if (ifsta->auth_alg == WLAN_AUTH_OPEN)
  1013. pos = 0;
  1014. else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
  1015. pos = 1;
  1016. else
  1017. pos = 2;
  1018. for (i = 0; i < num_algs; i++) {
  1019. pos++;
  1020. if (pos >= num_algs)
  1021. pos = 0;
  1022. if (algs[pos] == ifsta->auth_alg ||
  1023. algs[pos] == 0xff)
  1024. continue;
  1025. if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
  1026. !ieee80211_sta_wep_configured(sdata))
  1027. continue;
  1028. ifsta->auth_alg = algs[pos];
  1029. break;
  1030. }
  1031. }
  1032. return;
  1033. }
  1034. switch (ifsta->auth_alg) {
  1035. case WLAN_AUTH_OPEN:
  1036. case WLAN_AUTH_LEAP:
  1037. ieee80211_auth_completed(sdata, ifsta);
  1038. break;
  1039. case WLAN_AUTH_SHARED_KEY:
  1040. if (ifsta->auth_transaction == 4)
  1041. ieee80211_auth_completed(sdata, ifsta);
  1042. else
  1043. ieee80211_auth_challenge(sdata, ifsta, mgmt, len);
  1044. break;
  1045. }
  1046. }
  1047. static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
  1048. struct ieee80211_if_sta *ifsta,
  1049. struct ieee80211_mgmt *mgmt,
  1050. size_t len)
  1051. {
  1052. u16 reason_code;
  1053. if (len < 24 + 2)
  1054. return;
  1055. if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
  1056. return;
  1057. reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
  1058. if (ifsta->flags & IEEE80211_STA_AUTHENTICATED)
  1059. printk(KERN_DEBUG "%s: deauthenticated (Reason: %u)\n",
  1060. sdata->dev->name, reason_code);
  1061. if (ifsta->state == IEEE80211_STA_MLME_AUTHENTICATE ||
  1062. ifsta->state == IEEE80211_STA_MLME_ASSOCIATE ||
  1063. ifsta->state == IEEE80211_STA_MLME_ASSOCIATED) {
  1064. ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
  1065. mod_timer(&ifsta->timer, jiffies +
  1066. IEEE80211_RETRY_AUTH_INTERVAL);
  1067. }
  1068. ieee80211_set_disassoc(sdata, ifsta, true, false, 0);
  1069. ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
  1070. }
  1071. static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
  1072. struct ieee80211_if_sta *ifsta,
  1073. struct ieee80211_mgmt *mgmt,
  1074. size_t len)
  1075. {
  1076. u16 reason_code;
  1077. if (len < 24 + 2)
  1078. return;
  1079. if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
  1080. return;
  1081. reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
  1082. if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
  1083. printk(KERN_DEBUG "%s: disassociated (Reason: %u)\n",
  1084. sdata->dev->name, reason_code);
  1085. if (ifsta->state == IEEE80211_STA_MLME_ASSOCIATED) {
  1086. ifsta->state = IEEE80211_STA_MLME_ASSOCIATE;
  1087. mod_timer(&ifsta->timer, jiffies +
  1088. IEEE80211_RETRY_AUTH_INTERVAL);
  1089. }
  1090. ieee80211_set_disassoc(sdata, ifsta, false, false, reason_code);
  1091. }
  1092. static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
  1093. struct ieee80211_if_sta *ifsta,
  1094. struct ieee80211_mgmt *mgmt,
  1095. size_t len,
  1096. int reassoc)
  1097. {
  1098. struct ieee80211_local *local = sdata->local;
  1099. struct ieee80211_supported_band *sband;
  1100. struct sta_info *sta;
  1101. u32 rates, basic_rates;
  1102. u16 capab_info, status_code, aid;
  1103. struct ieee802_11_elems elems;
  1104. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  1105. u8 *pos;
  1106. u32 changed = 0;
  1107. int i, j;
  1108. bool have_higher_than_11mbit = false, newsta = false;
  1109. u16 ap_ht_cap_flags;
  1110. /* AssocResp and ReassocResp have identical structure, so process both
  1111. * of them in this function. */
  1112. if (ifsta->state != IEEE80211_STA_MLME_ASSOCIATE)
  1113. return;
  1114. if (len < 24 + 6)
  1115. return;
  1116. if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
  1117. return;
  1118. capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
  1119. status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
  1120. aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
  1121. printk(KERN_DEBUG "%s: RX %sssocResp from %pM (capab=0x%x "
  1122. "status=%d aid=%d)\n",
  1123. sdata->dev->name, reassoc ? "Rea" : "A", mgmt->sa,
  1124. capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
  1125. pos = mgmt->u.assoc_resp.variable;
  1126. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  1127. if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
  1128. elems.timeout_int && elems.timeout_int_len == 5 &&
  1129. elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
  1130. u32 tu, ms;
  1131. tu = get_unaligned_le32(elems.timeout_int + 1);
  1132. ms = tu * 1024 / 1000;
  1133. printk(KERN_DEBUG "%s: AP rejected association temporarily; "
  1134. "comeback duration %u TU (%u ms)\n",
  1135. sdata->dev->name, tu, ms);
  1136. if (ms > IEEE80211_ASSOC_TIMEOUT)
  1137. mod_timer(&ifsta->timer,
  1138. jiffies + msecs_to_jiffies(ms));
  1139. return;
  1140. }
  1141. if (status_code != WLAN_STATUS_SUCCESS) {
  1142. printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
  1143. sdata->dev->name, status_code);
  1144. /* if this was a reassociation, ensure we try a "full"
  1145. * association next time. This works around some broken APs
  1146. * which do not correctly reject reassociation requests. */
  1147. ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
  1148. return;
  1149. }
  1150. if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
  1151. printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
  1152. "set\n", sdata->dev->name, aid);
  1153. aid &= ~(BIT(15) | BIT(14));
  1154. if (!elems.supp_rates) {
  1155. printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
  1156. sdata->dev->name);
  1157. return;
  1158. }
  1159. printk(KERN_DEBUG "%s: associated\n", sdata->dev->name);
  1160. ifsta->aid = aid;
  1161. ifsta->ap_capab = capab_info;
  1162. kfree(ifsta->assocresp_ies);
  1163. ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
  1164. ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
  1165. if (ifsta->assocresp_ies)
  1166. memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
  1167. rcu_read_lock();
  1168. /* Add STA entry for the AP */
  1169. sta = sta_info_get(local, ifsta->bssid);
  1170. if (!sta) {
  1171. struct ieee80211_bss *bss;
  1172. newsta = true;
  1173. sta = sta_info_alloc(sdata, ifsta->bssid, GFP_ATOMIC);
  1174. if (!sta) {
  1175. printk(KERN_DEBUG "%s: failed to alloc STA entry for"
  1176. " the AP\n", sdata->dev->name);
  1177. rcu_read_unlock();
  1178. return;
  1179. }
  1180. bss = ieee80211_rx_bss_get(local, ifsta->bssid,
  1181. local->hw.conf.channel->center_freq,
  1182. ifsta->ssid, ifsta->ssid_len);
  1183. if (bss) {
  1184. sta->last_signal = bss->signal;
  1185. sta->last_qual = bss->qual;
  1186. sta->last_noise = bss->noise;
  1187. ieee80211_rx_bss_put(local, bss);
  1188. }
  1189. /* update new sta with its last rx activity */
  1190. sta->last_rx = jiffies;
  1191. }
  1192. /*
  1193. * FIXME: Do we really need to update the sta_info's information here?
  1194. * We already know about the AP (we found it in our list) so it
  1195. * should already be filled with the right info, no?
  1196. * As is stands, all this is racy because typically we assume
  1197. * the information that is filled in here (except flags) doesn't
  1198. * change while a STA structure is alive. As such, it should move
  1199. * to between the sta_info_alloc() and sta_info_insert() above.
  1200. */
  1201. set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
  1202. WLAN_STA_AUTHORIZED);
  1203. rates = 0;
  1204. basic_rates = 0;
  1205. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  1206. for (i = 0; i < elems.supp_rates_len; i++) {
  1207. int rate = (elems.supp_rates[i] & 0x7f) * 5;
  1208. bool is_basic = !!(elems.supp_rates[i] & 0x80);
  1209. if (rate > 110)
  1210. have_higher_than_11mbit = true;
  1211. for (j = 0; j < sband->n_bitrates; j++) {
  1212. if (sband->bitrates[j].bitrate == rate) {
  1213. rates |= BIT(j);
  1214. if (is_basic)
  1215. basic_rates |= BIT(j);
  1216. break;
  1217. }
  1218. }
  1219. }
  1220. for (i = 0; i < elems.ext_supp_rates_len; i++) {
  1221. int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
  1222. bool is_basic = !!(elems.supp_rates[i] & 0x80);
  1223. if (rate > 110)
  1224. have_higher_than_11mbit = true;
  1225. for (j = 0; j < sband->n_bitrates; j++) {
  1226. if (sband->bitrates[j].bitrate == rate) {
  1227. rates |= BIT(j);
  1228. if (is_basic)
  1229. basic_rates |= BIT(j);
  1230. break;
  1231. }
  1232. }
  1233. }
  1234. sta->sta.supp_rates[local->hw.conf.channel->band] = rates;
  1235. sdata->vif.bss_conf.basic_rates = basic_rates;
  1236. /* cf. IEEE 802.11 9.2.12 */
  1237. if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
  1238. have_higher_than_11mbit)
  1239. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  1240. else
  1241. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  1242. if (elems.ht_cap_elem)
  1243. ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
  1244. elems.ht_cap_elem, &sta->sta.ht_cap);
  1245. ap_ht_cap_flags = sta->sta.ht_cap.cap;
  1246. rate_control_rate_init(sta);
  1247. if (ifsta->flags & IEEE80211_STA_MFP_ENABLED)
  1248. set_sta_flags(sta, WLAN_STA_MFP);
  1249. if (elems.wmm_param)
  1250. set_sta_flags(sta, WLAN_STA_WME);
  1251. if (newsta) {
  1252. int err = sta_info_insert(sta);
  1253. if (err) {
  1254. printk(KERN_DEBUG "%s: failed to insert STA entry for"
  1255. " the AP (error %d)\n", sdata->dev->name, err);
  1256. rcu_read_unlock();
  1257. return;
  1258. }
  1259. }
  1260. rcu_read_unlock();
  1261. if (elems.wmm_param)
  1262. ieee80211_sta_wmm_params(local, ifsta, elems.wmm_param,
  1263. elems.wmm_param_len);
  1264. if (elems.ht_info_elem && elems.wmm_param &&
  1265. (ifsta->flags & IEEE80211_STA_WMM_ENABLED))
  1266. changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
  1267. ap_ht_cap_flags);
  1268. /* set AID and assoc capability,
  1269. * ieee80211_set_associated() will tell the driver */
  1270. bss_conf->aid = aid;
  1271. bss_conf->assoc_capability = capab_info;
  1272. ieee80211_set_associated(sdata, ifsta, changed);
  1273. ieee80211_associated(sdata, ifsta);
  1274. }
  1275. static int ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
  1276. struct ieee80211_if_sta *ifsta,
  1277. struct ieee80211_bss *bss)
  1278. {
  1279. struct ieee80211_local *local = sdata->local;
  1280. int res = 0, rates, i, j;
  1281. struct sk_buff *skb;
  1282. struct ieee80211_mgmt *mgmt;
  1283. u8 *pos;
  1284. struct ieee80211_supported_band *sband;
  1285. union iwreq_data wrqu;
  1286. if (local->ops->reset_tsf) {
  1287. /* Reset own TSF to allow time synchronization work. */
  1288. local->ops->reset_tsf(local_to_hw(local));
  1289. }
  1290. if ((ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) &&
  1291. memcmp(ifsta->bssid, bss->bssid, ETH_ALEN) == 0)
  1292. return res;
  1293. skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400 +
  1294. sdata->u.sta.ie_proberesp_len);
  1295. if (!skb) {
  1296. printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
  1297. "response\n", sdata->dev->name);
  1298. return -ENOMEM;
  1299. }
  1300. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  1301. if (!(ifsta->flags & IEEE80211_STA_PREV_BSSID_SET)) {
  1302. /* Remove possible STA entries from other IBSS networks. */
  1303. sta_info_flush_delayed(sdata);
  1304. }
  1305. memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
  1306. res = ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
  1307. if (res)
  1308. return res;
  1309. local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
  1310. sdata->drop_unencrypted = bss->capability &
  1311. WLAN_CAPABILITY_PRIVACY ? 1 : 0;
  1312. res = ieee80211_set_freq(sdata, bss->freq);
  1313. if (res)
  1314. return res;
  1315. /* Build IBSS probe response */
  1316. skb_reserve(skb, local->hw.extra_tx_headroom);
  1317. mgmt = (struct ieee80211_mgmt *)
  1318. skb_put(skb, 24 + sizeof(mgmt->u.beacon));
  1319. memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
  1320. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  1321. IEEE80211_STYPE_PROBE_RESP);
  1322. memset(mgmt->da, 0xff, ETH_ALEN);
  1323. memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
  1324. memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
  1325. mgmt->u.beacon.beacon_int =
  1326. cpu_to_le16(local->hw.conf.beacon_int);
  1327. mgmt->u.beacon.timestamp = cpu_to_le64(bss->timestamp);
  1328. mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
  1329. pos = skb_put(skb, 2 + ifsta->ssid_len);
  1330. *pos++ = WLAN_EID_SSID;
  1331. *pos++ = ifsta->ssid_len;
  1332. memcpy(pos, ifsta->ssid, ifsta->ssid_len);
  1333. rates = bss->supp_rates_len;
  1334. if (rates > 8)
  1335. rates = 8;
  1336. pos = skb_put(skb, 2 + rates);
  1337. *pos++ = WLAN_EID_SUPP_RATES;
  1338. *pos++ = rates;
  1339. memcpy(pos, bss->supp_rates, rates);
  1340. if (bss->band == IEEE80211_BAND_2GHZ) {
  1341. pos = skb_put(skb, 2 + 1);
  1342. *pos++ = WLAN_EID_DS_PARAMS;
  1343. *pos++ = 1;
  1344. *pos++ = ieee80211_frequency_to_channel(bss->freq);
  1345. }
  1346. pos = skb_put(skb, 2 + 2);
  1347. *pos++ = WLAN_EID_IBSS_PARAMS;
  1348. *pos++ = 2;
  1349. /* FIX: set ATIM window based on scan results */
  1350. *pos++ = 0;
  1351. *pos++ = 0;
  1352. if (bss->supp_rates_len > 8) {
  1353. rates = bss->supp_rates_len - 8;
  1354. pos = skb_put(skb, 2 + rates);
  1355. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  1356. *pos++ = rates;
  1357. memcpy(pos, &bss->supp_rates[8], rates);
  1358. }
  1359. add_extra_ies(skb, sdata->u.sta.ie_proberesp,
  1360. sdata->u.sta.ie_proberesp_len);
  1361. ifsta->probe_resp = skb;
  1362. ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON |
  1363. IEEE80211_IFCC_BEACON_ENABLED);
  1364. rates = 0;
  1365. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  1366. for (i = 0; i < bss->supp_rates_len; i++) {
  1367. int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
  1368. for (j = 0; j < sband->n_bitrates; j++)
  1369. if (sband->bitrates[j].bitrate == bitrate)
  1370. rates |= BIT(j);
  1371. }
  1372. ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
  1373. ieee80211_sta_def_wmm_params(sdata, bss);
  1374. ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
  1375. ifsta->state = IEEE80211_STA_MLME_IBSS_JOINED;
  1376. mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
  1377. ieee80211_led_assoc(local, true);
  1378. memset(&wrqu, 0, sizeof(wrqu));
  1379. memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
  1380. wireless_send_event(sdata->dev, SIOCGIWAP, &wrqu, NULL);
  1381. return res;
  1382. }
  1383. static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
  1384. struct ieee80211_mgmt *mgmt,
  1385. size_t len,
  1386. struct ieee80211_rx_status *rx_status,
  1387. struct ieee802_11_elems *elems,
  1388. bool beacon)
  1389. {
  1390. struct ieee80211_local *local = sdata->local;
  1391. int freq;
  1392. struct ieee80211_bss *bss;
  1393. struct sta_info *sta;
  1394. struct ieee80211_channel *channel;
  1395. u64 beacon_timestamp, rx_timestamp;
  1396. u32 supp_rates = 0;
  1397. enum ieee80211_band band = rx_status->band;
  1398. if (elems->ds_params && elems->ds_params_len == 1)
  1399. freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
  1400. else
  1401. freq = rx_status->freq;
  1402. channel = ieee80211_get_channel(local->hw.wiphy, freq);
  1403. if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
  1404. return;
  1405. if (sdata->vif.type == NL80211_IFTYPE_ADHOC && elems->supp_rates &&
  1406. memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0) {
  1407. supp_rates = ieee80211_sta_get_rates(local, elems, band);
  1408. rcu_read_lock();
  1409. sta = sta_info_get(local, mgmt->sa);
  1410. if (sta) {
  1411. u32 prev_rates;
  1412. prev_rates = sta->sta.supp_rates[band];
  1413. /* make sure mandatory rates are always added */
  1414. sta->sta.supp_rates[band] = supp_rates |
  1415. ieee80211_mandatory_rates(local, band);
  1416. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  1417. if (sta->sta.supp_rates[band] != prev_rates)
  1418. printk(KERN_DEBUG "%s: updated supp_rates set "
  1419. "for %pM based on beacon info (0x%llx | "
  1420. "0x%llx -> 0x%llx)\n",
  1421. sdata->dev->name,
  1422. sta->sta.addr,
  1423. (unsigned long long) prev_rates,
  1424. (unsigned long long) supp_rates,
  1425. (unsigned long long) sta->sta.supp_rates[band]);
  1426. #endif
  1427. } else {
  1428. ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa, supp_rates);
  1429. }
  1430. rcu_read_unlock();
  1431. }
  1432. bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
  1433. freq, beacon);
  1434. if (!bss)
  1435. return;
  1436. if (elems->ch_switch_elem && (elems->ch_switch_elem_len == 3) &&
  1437. (memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0)) {
  1438. struct ieee80211_channel_sw_ie *sw_elem =
  1439. (struct ieee80211_channel_sw_ie *)elems->ch_switch_elem;
  1440. ieee80211_process_chanswitch(sdata, sw_elem, bss);
  1441. }
  1442. /* was just updated in ieee80211_bss_info_update */
  1443. beacon_timestamp = bss->timestamp;
  1444. /*
  1445. * In STA mode, the remaining parameters should not be overridden
  1446. * by beacons because they're not necessarily accurate there.
  1447. */
  1448. if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  1449. bss->last_probe_resp && beacon) {
  1450. ieee80211_rx_bss_put(local, bss);
  1451. return;
  1452. }
  1453. /* check if we need to merge IBSS */
  1454. if (sdata->vif.type == NL80211_IFTYPE_ADHOC && beacon &&
  1455. (!(sdata->u.sta.flags & IEEE80211_STA_BSSID_SET)) &&
  1456. bss->capability & WLAN_CAPABILITY_IBSS &&
  1457. bss->freq == local->oper_channel->center_freq &&
  1458. elems->ssid_len == sdata->u.sta.ssid_len &&
  1459. memcmp(elems->ssid, sdata->u.sta.ssid,
  1460. sdata->u.sta.ssid_len) == 0) {
  1461. if (rx_status->flag & RX_FLAG_TSFT) {
  1462. /* in order for correct IBSS merging we need mactime
  1463. *
  1464. * since mactime is defined as the time the first data
  1465. * symbol of the frame hits the PHY, and the timestamp
  1466. * of the beacon is defined as "the time that the data
  1467. * symbol containing the first bit of the timestamp is
  1468. * transmitted to the PHY plus the transmitting STA’s
  1469. * delays through its local PHY from the MAC-PHY
  1470. * interface to its interface with the WM"
  1471. * (802.11 11.1.2) - equals the time this bit arrives at
  1472. * the receiver - we have to take into account the
  1473. * offset between the two.
  1474. * e.g: at 1 MBit that means mactime is 192 usec earlier
  1475. * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
  1476. */
  1477. int rate;
  1478. if (rx_status->flag & RX_FLAG_HT) {
  1479. rate = 65; /* TODO: HT rates */
  1480. } else {
  1481. rate = local->hw.wiphy->bands[band]->
  1482. bitrates[rx_status->rate_idx].bitrate;
  1483. }
  1484. rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
  1485. } else if (local && local->ops && local->ops->get_tsf)
  1486. /* second best option: get current TSF */
  1487. rx_timestamp = local->ops->get_tsf(local_to_hw(local));
  1488. else
  1489. /* can't merge without knowing the TSF */
  1490. rx_timestamp = -1LLU;
  1491. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  1492. printk(KERN_DEBUG "RX beacon SA=%pM BSSID="
  1493. "%pM TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
  1494. mgmt->sa, mgmt->bssid,
  1495. (unsigned long long)rx_timestamp,
  1496. (unsigned long long)beacon_timestamp,
  1497. (unsigned long long)(rx_timestamp - beacon_timestamp),
  1498. jiffies);
  1499. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  1500. if (beacon_timestamp > rx_timestamp) {
  1501. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  1502. printk(KERN_DEBUG "%s: beacon TSF higher than "
  1503. "local TSF - IBSS merge with BSSID %pM\n",
  1504. sdata->dev->name, mgmt->bssid);
  1505. #endif
  1506. ieee80211_sta_join_ibss(sdata, &sdata->u.sta, bss);
  1507. ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa, supp_rates);
  1508. }
  1509. }
  1510. ieee80211_rx_bss_put(local, bss);
  1511. }
  1512. static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
  1513. struct ieee80211_mgmt *mgmt,
  1514. size_t len,
  1515. struct ieee80211_rx_status *rx_status)
  1516. {
  1517. size_t baselen;
  1518. struct ieee802_11_elems elems;
  1519. struct ieee80211_if_sta *ifsta = &sdata->u.sta;
  1520. if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
  1521. return; /* ignore ProbeResp to foreign address */
  1522. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  1523. if (baselen > len)
  1524. return;
  1525. ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
  1526. &elems);
  1527. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
  1528. /* direct probe may be part of the association flow */
  1529. if (test_and_clear_bit(IEEE80211_STA_REQ_DIRECT_PROBE,
  1530. &ifsta->request)) {
  1531. printk(KERN_DEBUG "%s direct probe responded\n",
  1532. sdata->dev->name);
  1533. ieee80211_authenticate(sdata, ifsta);
  1534. }
  1535. }
  1536. static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
  1537. struct ieee80211_mgmt *mgmt,
  1538. size_t len,
  1539. struct ieee80211_rx_status *rx_status)
  1540. {
  1541. struct ieee80211_if_sta *ifsta;
  1542. size_t baselen;
  1543. struct ieee802_11_elems elems;
  1544. struct ieee80211_local *local = sdata->local;
  1545. u32 changed = 0;
  1546. bool erp_valid, directed_tim, is_mc = false;
  1547. u8 erp_value = 0;
  1548. /* Process beacon from the current BSS */
  1549. baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
  1550. if (baselen > len)
  1551. return;
  1552. ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
  1553. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, true);
  1554. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1555. return;
  1556. ifsta = &sdata->u.sta;
  1557. if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
  1558. memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
  1559. return;
  1560. if (rx_status->freq != local->hw.conf.channel->center_freq)
  1561. return;
  1562. ieee80211_sta_wmm_params(local, ifsta, elems.wmm_param,
  1563. elems.wmm_param_len);
  1564. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK &&
  1565. local->hw.conf.flags & IEEE80211_CONF_PS) {
  1566. directed_tim = check_tim(&elems, ifsta->aid, &is_mc);
  1567. if (directed_tim || is_mc) {
  1568. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  1569. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  1570. ieee80211_send_nullfunc(local, sdata, 0);
  1571. }
  1572. }
  1573. if (elems.erp_info && elems.erp_info_len >= 1) {
  1574. erp_valid = true;
  1575. erp_value = elems.erp_info[0];
  1576. } else {
  1577. erp_valid = false;
  1578. }
  1579. changed |= ieee80211_handle_bss_capability(sdata,
  1580. le16_to_cpu(mgmt->u.beacon.capab_info),
  1581. erp_valid, erp_value);
  1582. if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param) {
  1583. struct sta_info *sta;
  1584. struct ieee80211_supported_band *sband;
  1585. u16 ap_ht_cap_flags;
  1586. rcu_read_lock();
  1587. sta = sta_info_get(local, ifsta->bssid);
  1588. if (!sta) {
  1589. rcu_read_unlock();
  1590. return;
  1591. }
  1592. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  1593. ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
  1594. elems.ht_cap_elem, &sta->sta.ht_cap);
  1595. ap_ht_cap_flags = sta->sta.ht_cap.cap;
  1596. rcu_read_unlock();
  1597. changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
  1598. ap_ht_cap_flags);
  1599. }
  1600. if (elems.country_elem) {
  1601. /* Note we are only reviewing this on beacons
  1602. * for the BSSID we are associated to */
  1603. regulatory_hint_11d(local->hw.wiphy,
  1604. elems.country_elem, elems.country_elem_len);
  1605. /* TODO: IBSS also needs this */
  1606. if (elems.pwr_constr_elem)
  1607. ieee80211_handle_pwr_constr(sdata,
  1608. le16_to_cpu(mgmt->u.probe_resp.capab_info),
  1609. elems.pwr_constr_elem,
  1610. elems.pwr_constr_elem_len);
  1611. }
  1612. ieee80211_bss_info_change_notify(sdata, changed);
  1613. }
  1614. static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
  1615. struct ieee80211_if_sta *ifsta,
  1616. struct ieee80211_mgmt *mgmt,
  1617. size_t len)
  1618. {
  1619. struct ieee80211_local *local = sdata->local;
  1620. int tx_last_beacon;
  1621. struct sk_buff *skb;
  1622. struct ieee80211_mgmt *resp;
  1623. u8 *pos, *end;
  1624. if (sdata->vif.type != NL80211_IFTYPE_ADHOC ||
  1625. ifsta->state != IEEE80211_STA_MLME_IBSS_JOINED ||
  1626. len < 24 + 2 || !ifsta->probe_resp)
  1627. return;
  1628. if (local->ops->tx_last_beacon)
  1629. tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
  1630. else
  1631. tx_last_beacon = 1;
  1632. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  1633. printk(KERN_DEBUG "%s: RX ProbeReq SA=%pM DA=%pM BSSID=%pM"
  1634. " (tx_last_beacon=%d)\n",
  1635. sdata->dev->name, mgmt->sa, mgmt->da,
  1636. mgmt->bssid, tx_last_beacon);
  1637. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  1638. if (!tx_last_beacon)
  1639. return;
  1640. if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
  1641. memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
  1642. return;
  1643. end = ((u8 *) mgmt) + len;
  1644. pos = mgmt->u.probe_req.variable;
  1645. if (pos[0] != WLAN_EID_SSID ||
  1646. pos + 2 + pos[1] > end) {
  1647. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  1648. printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
  1649. "from %pM\n",
  1650. sdata->dev->name, mgmt->sa);
  1651. #endif
  1652. return;
  1653. }
  1654. if (pos[1] != 0 &&
  1655. (pos[1] != ifsta->ssid_len ||
  1656. memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
  1657. /* Ignore ProbeReq for foreign SSID */
  1658. return;
  1659. }
  1660. /* Reply with ProbeResp */
  1661. skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
  1662. if (!skb)
  1663. return;
  1664. resp = (struct ieee80211_mgmt *) skb->data;
  1665. memcpy(resp->da, mgmt->sa, ETH_ALEN);
  1666. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  1667. printk(KERN_DEBUG "%s: Sending ProbeResp to %pM\n",
  1668. sdata->dev->name, resp->da);
  1669. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  1670. ieee80211_tx_skb(sdata, skb, 0);
  1671. }
  1672. void ieee80211_sta_rx_mgmt(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
  1673. struct ieee80211_rx_status *rx_status)
  1674. {
  1675. struct ieee80211_local *local = sdata->local;
  1676. struct ieee80211_if_sta *ifsta;
  1677. struct ieee80211_mgmt *mgmt;
  1678. u16 fc;
  1679. if (skb->len < 24)
  1680. goto fail;
  1681. ifsta = &sdata->u.sta;
  1682. mgmt = (struct ieee80211_mgmt *) skb->data;
  1683. fc = le16_to_cpu(mgmt->frame_control);
  1684. switch (fc & IEEE80211_FCTL_STYPE) {
  1685. case IEEE80211_STYPE_PROBE_REQ:
  1686. case IEEE80211_STYPE_PROBE_RESP:
  1687. case IEEE80211_STYPE_BEACON:
  1688. memcpy(skb->cb, rx_status, sizeof(*rx_status));
  1689. case IEEE80211_STYPE_AUTH:
  1690. case IEEE80211_STYPE_ASSOC_RESP:
  1691. case IEEE80211_STYPE_REASSOC_RESP:
  1692. case IEEE80211_STYPE_DEAUTH:
  1693. case IEEE80211_STYPE_DISASSOC:
  1694. skb_queue_tail(&ifsta->skb_queue, skb);
  1695. queue_work(local->hw.workqueue, &ifsta->work);
  1696. return;
  1697. }
  1698. fail:
  1699. kfree_skb(skb);
  1700. }
  1701. static void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
  1702. struct sk_buff *skb)
  1703. {
  1704. struct ieee80211_rx_status *rx_status;
  1705. struct ieee80211_if_sta *ifsta;
  1706. struct ieee80211_mgmt *mgmt;
  1707. u16 fc;
  1708. ifsta = &sdata->u.sta;
  1709. rx_status = (struct ieee80211_rx_status *) skb->cb;
  1710. mgmt = (struct ieee80211_mgmt *) skb->data;
  1711. fc = le16_to_cpu(mgmt->frame_control);
  1712. switch (fc & IEEE80211_FCTL_STYPE) {
  1713. case IEEE80211_STYPE_PROBE_REQ:
  1714. ieee80211_rx_mgmt_probe_req(sdata, ifsta, mgmt, skb->len);
  1715. break;
  1716. case IEEE80211_STYPE_PROBE_RESP:
  1717. ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len, rx_status);
  1718. break;
  1719. case IEEE80211_STYPE_BEACON:
  1720. ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
  1721. break;
  1722. case IEEE80211_STYPE_AUTH:
  1723. ieee80211_rx_mgmt_auth(sdata, ifsta, mgmt, skb->len);
  1724. break;
  1725. case IEEE80211_STYPE_ASSOC_RESP:
  1726. ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
  1727. break;
  1728. case IEEE80211_STYPE_REASSOC_RESP:
  1729. ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
  1730. break;
  1731. case IEEE80211_STYPE_DEAUTH:
  1732. ieee80211_rx_mgmt_deauth(sdata, ifsta, mgmt, skb->len);
  1733. break;
  1734. case IEEE80211_STYPE_DISASSOC:
  1735. ieee80211_rx_mgmt_disassoc(sdata, ifsta, mgmt, skb->len);
  1736. break;
  1737. }
  1738. kfree_skb(skb);
  1739. }
  1740. static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
  1741. {
  1742. struct ieee80211_local *local = sdata->local;
  1743. int active = 0;
  1744. struct sta_info *sta;
  1745. rcu_read_lock();
  1746. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  1747. if (sta->sdata == sdata &&
  1748. time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
  1749. jiffies)) {
  1750. active++;
  1751. break;
  1752. }
  1753. }
  1754. rcu_read_unlock();
  1755. return active;
  1756. }
  1757. static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata,
  1758. struct ieee80211_if_sta *ifsta)
  1759. {
  1760. mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
  1761. ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
  1762. if (ieee80211_sta_active_ibss(sdata))
  1763. return;
  1764. if ((sdata->u.sta.flags & IEEE80211_STA_BSSID_SET) &&
  1765. (!(sdata->u.sta.flags & IEEE80211_STA_AUTO_CHANNEL_SEL)))
  1766. return;
  1767. printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
  1768. "IBSS networks with same SSID (merge)\n", sdata->dev->name);
  1769. ieee80211_request_scan(sdata, ifsta->ssid, ifsta->ssid_len);
  1770. }
  1771. static void ieee80211_sta_timer(unsigned long data)
  1772. {
  1773. struct ieee80211_sub_if_data *sdata =
  1774. (struct ieee80211_sub_if_data *) data;
  1775. struct ieee80211_if_sta *ifsta = &sdata->u.sta;
  1776. struct ieee80211_local *local = sdata->local;
  1777. set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
  1778. queue_work(local->hw.workqueue, &ifsta->work);
  1779. }
  1780. static void ieee80211_sta_reset_auth(struct ieee80211_sub_if_data *sdata,
  1781. struct ieee80211_if_sta *ifsta)
  1782. {
  1783. struct ieee80211_local *local = sdata->local;
  1784. if (local->ops->reset_tsf) {
  1785. /* Reset own TSF to allow time synchronization work. */
  1786. local->ops->reset_tsf(local_to_hw(local));
  1787. }
  1788. ifsta->wmm_last_param_set = -1; /* allow any WMM update */
  1789. if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
  1790. ifsta->auth_alg = WLAN_AUTH_OPEN;
  1791. else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
  1792. ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
  1793. else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
  1794. ifsta->auth_alg = WLAN_AUTH_LEAP;
  1795. else
  1796. ifsta->auth_alg = WLAN_AUTH_OPEN;
  1797. ifsta->auth_transaction = -1;
  1798. ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
  1799. ifsta->assoc_scan_tries = 0;
  1800. ifsta->direct_probe_tries = 0;
  1801. ifsta->auth_tries = 0;
  1802. ifsta->assoc_tries = 0;
  1803. netif_tx_stop_all_queues(sdata->dev);
  1804. netif_carrier_off(sdata->dev);
  1805. }
  1806. static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
  1807. const char *ssid, int ssid_len)
  1808. {
  1809. int tmp, hidden_ssid;
  1810. if (ssid_len == ifsta->ssid_len &&
  1811. !memcmp(ifsta->ssid, ssid, ssid_len))
  1812. return 1;
  1813. if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
  1814. return 0;
  1815. hidden_ssid = 1;
  1816. tmp = ssid_len;
  1817. while (tmp--) {
  1818. if (ssid[tmp] != '\0') {
  1819. hidden_ssid = 0;
  1820. break;
  1821. }
  1822. }
  1823. if (hidden_ssid && (ifsta->ssid_len == ssid_len || ssid_len == 0))
  1824. return 1;
  1825. if (ssid_len == 1 && ssid[0] == ' ')
  1826. return 1;
  1827. return 0;
  1828. }
  1829. static int ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata,
  1830. struct ieee80211_if_sta *ifsta)
  1831. {
  1832. struct ieee80211_local *local = sdata->local;
  1833. struct ieee80211_bss *bss;
  1834. struct ieee80211_supported_band *sband;
  1835. u8 bssid[ETH_ALEN], *pos;
  1836. int i;
  1837. int ret;
  1838. if (sdata->u.sta.flags & IEEE80211_STA_BSSID_SET) {
  1839. memcpy(bssid, ifsta->bssid, ETH_ALEN);
  1840. } else {
  1841. /* Generate random, not broadcast, locally administered BSSID. Mix in
  1842. * own MAC address to make sure that devices that do not have proper
  1843. * random number generator get different BSSID. */
  1844. get_random_bytes(bssid, ETH_ALEN);
  1845. for (i = 0; i < ETH_ALEN; i++)
  1846. bssid[i] ^= sdata->dev->dev_addr[i];
  1847. bssid[0] &= ~0x01;
  1848. bssid[0] |= 0x02;
  1849. }
  1850. printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %pM\n",
  1851. sdata->dev->name, bssid);
  1852. bss = ieee80211_rx_bss_add(local, bssid,
  1853. local->hw.conf.channel->center_freq,
  1854. sdata->u.sta.ssid, sdata->u.sta.ssid_len);
  1855. if (!bss)
  1856. return -ENOMEM;
  1857. bss->band = local->hw.conf.channel->band;
  1858. sband = local->hw.wiphy->bands[bss->band];
  1859. if (local->hw.conf.beacon_int == 0)
  1860. local->hw.conf.beacon_int = 100;
  1861. bss->beacon_int = local->hw.conf.beacon_int;
  1862. bss->last_update = jiffies;
  1863. bss->capability = WLAN_CAPABILITY_IBSS;
  1864. if (sdata->default_key)
  1865. bss->capability |= WLAN_CAPABILITY_PRIVACY;
  1866. else
  1867. sdata->drop_unencrypted = 0;
  1868. bss->supp_rates_len = sband->n_bitrates;
  1869. pos = bss->supp_rates;
  1870. for (i = 0; i < sband->n_bitrates; i++) {
  1871. int rate = sband->bitrates[i].bitrate;
  1872. *pos++ = (u8) (rate / 5);
  1873. }
  1874. ret = ieee80211_sta_join_ibss(sdata, ifsta, bss);
  1875. ieee80211_rx_bss_put(local, bss);
  1876. return ret;
  1877. }
  1878. static int ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata,
  1879. struct ieee80211_if_sta *ifsta)
  1880. {
  1881. struct ieee80211_local *local = sdata->local;
  1882. struct ieee80211_bss *bss;
  1883. int found = 0;
  1884. u8 bssid[ETH_ALEN];
  1885. int active_ibss;
  1886. if (ifsta->ssid_len == 0)
  1887. return -EINVAL;
  1888. active_ibss = ieee80211_sta_active_ibss(sdata);
  1889. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  1890. printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
  1891. sdata->dev->name, active_ibss);
  1892. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  1893. spin_lock_bh(&local->bss_lock);
  1894. list_for_each_entry(bss, &local->bss_list, list) {
  1895. if (ifsta->ssid_len != bss->ssid_len ||
  1896. memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
  1897. || !(bss->capability & WLAN_CAPABILITY_IBSS))
  1898. continue;
  1899. if ((ifsta->flags & IEEE80211_STA_BSSID_SET) &&
  1900. memcmp(ifsta->bssid, bss->bssid, ETH_ALEN) != 0)
  1901. continue;
  1902. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  1903. printk(KERN_DEBUG " bssid=%pM found\n", bss->bssid);
  1904. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  1905. memcpy(bssid, bss->bssid, ETH_ALEN);
  1906. found = 1;
  1907. if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
  1908. break;
  1909. }
  1910. spin_unlock_bh(&local->bss_lock);
  1911. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  1912. if (found)
  1913. printk(KERN_DEBUG " sta_find_ibss: selected %pM current "
  1914. "%pM\n", bssid, ifsta->bssid);
  1915. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  1916. if (found &&
  1917. ((!(ifsta->flags & IEEE80211_STA_PREV_BSSID_SET)) ||
  1918. memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0)) {
  1919. int ret;
  1920. int search_freq;
  1921. if (ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL)
  1922. search_freq = bss->freq;
  1923. else
  1924. search_freq = local->hw.conf.channel->center_freq;
  1925. bss = ieee80211_rx_bss_get(local, bssid, search_freq,
  1926. ifsta->ssid, ifsta->ssid_len);
  1927. if (!bss)
  1928. goto dont_join;
  1929. printk(KERN_DEBUG "%s: Selected IBSS BSSID %pM"
  1930. " based on configured SSID\n",
  1931. sdata->dev->name, bssid);
  1932. ret = ieee80211_sta_join_ibss(sdata, ifsta, bss);
  1933. ieee80211_rx_bss_put(local, bss);
  1934. return ret;
  1935. }
  1936. dont_join:
  1937. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  1938. printk(KERN_DEBUG " did not try to join ibss\n");
  1939. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  1940. /* Selected IBSS not found in current scan results - try to scan */
  1941. if (ifsta->state == IEEE80211_STA_MLME_IBSS_JOINED &&
  1942. !ieee80211_sta_active_ibss(sdata)) {
  1943. mod_timer(&ifsta->timer, jiffies +
  1944. IEEE80211_IBSS_MERGE_INTERVAL);
  1945. } else if (time_after(jiffies, local->last_scan_completed +
  1946. IEEE80211_SCAN_INTERVAL)) {
  1947. printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
  1948. "join\n", sdata->dev->name);
  1949. return ieee80211_request_scan(sdata, ifsta->ssid,
  1950. ifsta->ssid_len);
  1951. } else if (ifsta->state != IEEE80211_STA_MLME_IBSS_JOINED) {
  1952. int interval = IEEE80211_SCAN_INTERVAL;
  1953. if (time_after(jiffies, ifsta->ibss_join_req +
  1954. IEEE80211_IBSS_JOIN_TIMEOUT)) {
  1955. if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
  1956. (!(local->oper_channel->flags &
  1957. IEEE80211_CHAN_NO_IBSS)))
  1958. return ieee80211_sta_create_ibss(sdata, ifsta);
  1959. if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
  1960. printk(KERN_DEBUG "%s: IBSS not allowed on"
  1961. " %d MHz\n", sdata->dev->name,
  1962. local->hw.conf.channel->center_freq);
  1963. }
  1964. /* No IBSS found - decrease scan interval and continue
  1965. * scanning. */
  1966. interval = IEEE80211_SCAN_INTERVAL_SLOW;
  1967. }
  1968. ifsta->state = IEEE80211_STA_MLME_IBSS_SEARCH;
  1969. mod_timer(&ifsta->timer, jiffies + interval);
  1970. return 0;
  1971. }
  1972. return 0;
  1973. }
  1974. static int ieee80211_sta_config_auth(struct ieee80211_sub_if_data *sdata,
  1975. struct ieee80211_if_sta *ifsta)
  1976. {
  1977. struct ieee80211_local *local = sdata->local;
  1978. struct ieee80211_bss *bss, *selected = NULL;
  1979. int top_rssi = 0, freq;
  1980. spin_lock_bh(&local->bss_lock);
  1981. freq = local->oper_channel->center_freq;
  1982. list_for_each_entry(bss, &local->bss_list, list) {
  1983. if (!(bss->capability & WLAN_CAPABILITY_ESS))
  1984. continue;
  1985. if ((ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
  1986. IEEE80211_STA_AUTO_BSSID_SEL |
  1987. IEEE80211_STA_AUTO_CHANNEL_SEL)) &&
  1988. (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
  1989. !!sdata->default_key))
  1990. continue;
  1991. if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
  1992. bss->freq != freq)
  1993. continue;
  1994. if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
  1995. memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
  1996. continue;
  1997. if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
  1998. !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
  1999. continue;
  2000. if (!selected || top_rssi < bss->signal) {
  2001. selected = bss;
  2002. top_rssi = bss->signal;
  2003. }
  2004. }
  2005. if (selected)
  2006. atomic_inc(&selected->users);
  2007. spin_unlock_bh(&local->bss_lock);
  2008. if (selected) {
  2009. ieee80211_set_freq(sdata, selected->freq);
  2010. if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
  2011. ieee80211_sta_set_ssid(sdata, selected->ssid,
  2012. selected->ssid_len);
  2013. ieee80211_sta_set_bssid(sdata, selected->bssid);
  2014. ieee80211_sta_def_wmm_params(sdata, selected);
  2015. if (sdata->u.sta.mfp == IEEE80211_MFP_REQUIRED)
  2016. sdata->u.sta.flags |= IEEE80211_STA_MFP_ENABLED;
  2017. else
  2018. sdata->u.sta.flags &= ~IEEE80211_STA_MFP_ENABLED;
  2019. /* Send out direct probe if no probe resp was received or
  2020. * the one we have is outdated
  2021. */
  2022. if (!selected->last_probe_resp ||
  2023. time_after(jiffies, selected->last_probe_resp
  2024. + IEEE80211_SCAN_RESULT_EXPIRE))
  2025. ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
  2026. else
  2027. ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
  2028. ieee80211_rx_bss_put(local, selected);
  2029. ieee80211_sta_reset_auth(sdata, ifsta);
  2030. return 0;
  2031. } else {
  2032. if (ifsta->assoc_scan_tries < IEEE80211_ASSOC_SCANS_MAX_TRIES) {
  2033. ifsta->assoc_scan_tries++;
  2034. if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
  2035. ieee80211_start_scan(sdata, NULL, 0);
  2036. else
  2037. ieee80211_start_scan(sdata, ifsta->ssid,
  2038. ifsta->ssid_len);
  2039. ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
  2040. set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
  2041. } else {
  2042. ifsta->assoc_scan_tries = 0;
  2043. ifsta->state = IEEE80211_STA_MLME_DISABLED;
  2044. }
  2045. }
  2046. return -1;
  2047. }
  2048. static void ieee80211_sta_work(struct work_struct *work)
  2049. {
  2050. struct ieee80211_sub_if_data *sdata =
  2051. container_of(work, struct ieee80211_sub_if_data, u.sta.work);
  2052. struct ieee80211_local *local = sdata->local;
  2053. struct ieee80211_if_sta *ifsta;
  2054. struct sk_buff *skb;
  2055. if (!netif_running(sdata->dev))
  2056. return;
  2057. if (local->sw_scanning || local->hw_scanning)
  2058. return;
  2059. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION &&
  2060. sdata->vif.type != NL80211_IFTYPE_ADHOC))
  2061. return;
  2062. ifsta = &sdata->u.sta;
  2063. while ((skb = skb_dequeue(&ifsta->skb_queue)))
  2064. ieee80211_sta_rx_queued_mgmt(sdata, skb);
  2065. if (ifsta->state != IEEE80211_STA_MLME_DIRECT_PROBE &&
  2066. ifsta->state != IEEE80211_STA_MLME_AUTHENTICATE &&
  2067. ifsta->state != IEEE80211_STA_MLME_ASSOCIATE &&
  2068. test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
  2069. ieee80211_start_scan(sdata, ifsta->scan_ssid,
  2070. ifsta->scan_ssid_len);
  2071. return;
  2072. }
  2073. if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
  2074. if (ieee80211_sta_config_auth(sdata, ifsta))
  2075. return;
  2076. clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
  2077. } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
  2078. return;
  2079. switch (ifsta->state) {
  2080. case IEEE80211_STA_MLME_DISABLED:
  2081. break;
  2082. case IEEE80211_STA_MLME_DIRECT_PROBE:
  2083. ieee80211_direct_probe(sdata, ifsta);
  2084. break;
  2085. case IEEE80211_STA_MLME_AUTHENTICATE:
  2086. ieee80211_authenticate(sdata, ifsta);
  2087. break;
  2088. case IEEE80211_STA_MLME_ASSOCIATE:
  2089. ieee80211_associate(sdata, ifsta);
  2090. break;
  2091. case IEEE80211_STA_MLME_ASSOCIATED:
  2092. ieee80211_associated(sdata, ifsta);
  2093. break;
  2094. case IEEE80211_STA_MLME_IBSS_SEARCH:
  2095. ieee80211_sta_find_ibss(sdata, ifsta);
  2096. break;
  2097. case IEEE80211_STA_MLME_IBSS_JOINED:
  2098. ieee80211_sta_merge_ibss(sdata, ifsta);
  2099. break;
  2100. default:
  2101. WARN_ON(1);
  2102. break;
  2103. }
  2104. if (ieee80211_privacy_mismatch(sdata, ifsta)) {
  2105. printk(KERN_DEBUG "%s: privacy configuration mismatch and "
  2106. "mixed-cell disabled - disassociate\n", sdata->dev->name);
  2107. ieee80211_set_disassoc(sdata, ifsta, false, true,
  2108. WLAN_REASON_UNSPECIFIED);
  2109. }
  2110. }
  2111. static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
  2112. {
  2113. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  2114. queue_work(sdata->local->hw.workqueue,
  2115. &sdata->u.sta.work);
  2116. }
  2117. /* interface setup */
  2118. void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
  2119. {
  2120. struct ieee80211_if_sta *ifsta;
  2121. ifsta = &sdata->u.sta;
  2122. INIT_WORK(&ifsta->work, ieee80211_sta_work);
  2123. INIT_WORK(&ifsta->chswitch_work, ieee80211_chswitch_work);
  2124. setup_timer(&ifsta->timer, ieee80211_sta_timer,
  2125. (unsigned long) sdata);
  2126. setup_timer(&ifsta->chswitch_timer, ieee80211_chswitch_timer,
  2127. (unsigned long) sdata);
  2128. skb_queue_head_init(&ifsta->skb_queue);
  2129. ifsta->capab = WLAN_CAPABILITY_ESS;
  2130. ifsta->auth_algs = IEEE80211_AUTH_ALG_OPEN |
  2131. IEEE80211_AUTH_ALG_SHARED_KEY;
  2132. ifsta->flags |= IEEE80211_STA_CREATE_IBSS |
  2133. IEEE80211_STA_AUTO_BSSID_SEL |
  2134. IEEE80211_STA_AUTO_CHANNEL_SEL;
  2135. if (ieee80211_num_regular_queues(&sdata->local->hw) >= 4)
  2136. ifsta->flags |= IEEE80211_STA_WMM_ENABLED;
  2137. }
  2138. /*
  2139. * Add a new IBSS station, will also be called by the RX code when,
  2140. * in IBSS mode, receiving a frame from a yet-unknown station, hence
  2141. * must be callable in atomic context.
  2142. */
  2143. struct sta_info *ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata,
  2144. u8 *bssid,u8 *addr, u32 supp_rates)
  2145. {
  2146. struct ieee80211_local *local = sdata->local;
  2147. struct sta_info *sta;
  2148. int band = local->hw.conf.channel->band;
  2149. /* TODO: Could consider removing the least recently used entry and
  2150. * allow new one to be added. */
  2151. if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
  2152. if (net_ratelimit()) {
  2153. printk(KERN_DEBUG "%s: No room for a new IBSS STA "
  2154. "entry %pM\n", sdata->dev->name, addr);
  2155. }
  2156. return NULL;
  2157. }
  2158. if (compare_ether_addr(bssid, sdata->u.sta.bssid))
  2159. return NULL;
  2160. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  2161. printk(KERN_DEBUG "%s: Adding new IBSS station %pM (dev=%s)\n",
  2162. wiphy_name(local->hw.wiphy), addr, sdata->dev->name);
  2163. #endif
  2164. sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
  2165. if (!sta)
  2166. return NULL;
  2167. set_sta_flags(sta, WLAN_STA_AUTHORIZED);
  2168. /* make sure mandatory rates are always added */
  2169. sta->sta.supp_rates[band] = supp_rates |
  2170. ieee80211_mandatory_rates(local, band);
  2171. rate_control_rate_init(sta);
  2172. if (sta_info_insert(sta))
  2173. return NULL;
  2174. return sta;
  2175. }
  2176. /* configuration hooks */
  2177. void ieee80211_sta_req_auth(struct ieee80211_sub_if_data *sdata,
  2178. struct ieee80211_if_sta *ifsta)
  2179. {
  2180. struct ieee80211_local *local = sdata->local;
  2181. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  2182. return;
  2183. if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
  2184. IEEE80211_STA_AUTO_BSSID_SEL)) &&
  2185. (ifsta->flags & (IEEE80211_STA_SSID_SET |
  2186. IEEE80211_STA_AUTO_SSID_SEL))) {
  2187. if (ifsta->state == IEEE80211_STA_MLME_ASSOCIATED)
  2188. ieee80211_set_disassoc(sdata, ifsta, true, true,
  2189. WLAN_REASON_DEAUTH_LEAVING);
  2190. set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
  2191. queue_work(local->hw.workqueue, &ifsta->work);
  2192. }
  2193. }
  2194. int ieee80211_sta_set_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t len)
  2195. {
  2196. struct ieee80211_if_sta *ifsta;
  2197. if (len > IEEE80211_MAX_SSID_LEN)
  2198. return -EINVAL;
  2199. ifsta = &sdata->u.sta;
  2200. if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0) {
  2201. memset(ifsta->ssid, 0, sizeof(ifsta->ssid));
  2202. memcpy(ifsta->ssid, ssid, len);
  2203. ifsta->ssid_len = len;
  2204. }
  2205. ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
  2206. if (len)
  2207. ifsta->flags |= IEEE80211_STA_SSID_SET;
  2208. else
  2209. ifsta->flags &= ~IEEE80211_STA_SSID_SET;
  2210. if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  2211. ifsta->ibss_join_req = jiffies;
  2212. ifsta->state = IEEE80211_STA_MLME_IBSS_SEARCH;
  2213. return ieee80211_sta_find_ibss(sdata, ifsta);
  2214. }
  2215. return 0;
  2216. }
  2217. int ieee80211_sta_get_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t *len)
  2218. {
  2219. struct ieee80211_if_sta *ifsta = &sdata->u.sta;
  2220. memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
  2221. *len = ifsta->ssid_len;
  2222. return 0;
  2223. }
  2224. int ieee80211_sta_set_bssid(struct ieee80211_sub_if_data *sdata, u8 *bssid)
  2225. {
  2226. struct ieee80211_if_sta *ifsta;
  2227. ifsta = &sdata->u.sta;
  2228. if (is_valid_ether_addr(bssid)) {
  2229. memcpy(ifsta->bssid, bssid, ETH_ALEN);
  2230. ifsta->flags |= IEEE80211_STA_BSSID_SET;
  2231. } else {
  2232. memset(ifsta->bssid, 0, ETH_ALEN);
  2233. ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
  2234. }
  2235. if (netif_running(sdata->dev)) {
  2236. if (ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID)) {
  2237. printk(KERN_DEBUG "%s: Failed to config new BSSID to "
  2238. "the low-level driver\n", sdata->dev->name);
  2239. }
  2240. }
  2241. return ieee80211_sta_set_ssid(sdata, ifsta->ssid, ifsta->ssid_len);
  2242. }
  2243. int ieee80211_sta_set_extra_ie(struct ieee80211_sub_if_data *sdata, char *ie, size_t len)
  2244. {
  2245. struct ieee80211_if_sta *ifsta = &sdata->u.sta;
  2246. kfree(ifsta->extra_ie);
  2247. if (len == 0) {
  2248. ifsta->extra_ie = NULL;
  2249. ifsta->extra_ie_len = 0;
  2250. return 0;
  2251. }
  2252. ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
  2253. if (!ifsta->extra_ie) {
  2254. ifsta->extra_ie_len = 0;
  2255. return -ENOMEM;
  2256. }
  2257. memcpy(ifsta->extra_ie, ie, len);
  2258. ifsta->extra_ie_len = len;
  2259. return 0;
  2260. }
  2261. int ieee80211_sta_deauthenticate(struct ieee80211_sub_if_data *sdata, u16 reason)
  2262. {
  2263. struct ieee80211_if_sta *ifsta = &sdata->u.sta;
  2264. printk(KERN_DEBUG "%s: deauthenticating by local choice (reason=%d)\n",
  2265. sdata->dev->name, reason);
  2266. if (sdata->vif.type != NL80211_IFTYPE_STATION &&
  2267. sdata->vif.type != NL80211_IFTYPE_ADHOC)
  2268. return -EINVAL;
  2269. ieee80211_set_disassoc(sdata, ifsta, true, true, reason);
  2270. return 0;
  2271. }
  2272. int ieee80211_sta_disassociate(struct ieee80211_sub_if_data *sdata, u16 reason)
  2273. {
  2274. struct ieee80211_if_sta *ifsta = &sdata->u.sta;
  2275. printk(KERN_DEBUG "%s: disassociating by local choice (reason=%d)\n",
  2276. sdata->dev->name, reason);
  2277. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  2278. return -EINVAL;
  2279. if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
  2280. return -1;
  2281. ieee80211_set_disassoc(sdata, ifsta, false, true, reason);
  2282. return 0;
  2283. }
  2284. /* scan finished notification */
  2285. void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
  2286. {
  2287. struct ieee80211_sub_if_data *sdata = local->scan_sdata;
  2288. struct ieee80211_if_sta *ifsta;
  2289. if (sdata && sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  2290. ifsta = &sdata->u.sta;
  2291. if ((!(ifsta->flags & IEEE80211_STA_PREV_BSSID_SET)) ||
  2292. !ieee80211_sta_active_ibss(sdata))
  2293. ieee80211_sta_find_ibss(sdata, ifsta);
  2294. }
  2295. /* Restart STA timers */
  2296. rcu_read_lock();
  2297. list_for_each_entry_rcu(sdata, &local->interfaces, list)
  2298. ieee80211_restart_sta_timer(sdata);
  2299. rcu_read_unlock();
  2300. }
  2301. void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
  2302. {
  2303. struct ieee80211_local *local =
  2304. container_of(work, struct ieee80211_local,
  2305. dynamic_ps_disable_work);
  2306. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  2307. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  2308. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  2309. }
  2310. ieee80211_wake_queues_by_reason(&local->hw,
  2311. IEEE80211_QUEUE_STOP_REASON_PS);
  2312. }
  2313. void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
  2314. {
  2315. struct ieee80211_local *local =
  2316. container_of(work, struct ieee80211_local,
  2317. dynamic_ps_enable_work);
  2318. struct ieee80211_sub_if_data *sdata = local->scan_sdata;
  2319. if (local->hw.conf.flags & IEEE80211_CONF_PS)
  2320. return;
  2321. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
  2322. ieee80211_send_nullfunc(local, sdata, 1);
  2323. local->hw.conf.flags |= IEEE80211_CONF_PS;
  2324. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  2325. }
  2326. void ieee80211_dynamic_ps_timer(unsigned long data)
  2327. {
  2328. struct ieee80211_local *local = (void *) data;
  2329. queue_work(local->hw.workqueue, &local->dynamic_ps_enable_work);
  2330. }