mlme.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796
  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 ieee80211_check_tim(struct ieee802_11_elems *elems, u16 aid)
  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. indexn1 = tim->bitmap_ctrl & 0xfe;
  543. indexn2 = elems->tim_len + indexn1 - 4;
  544. if (index < indexn1 || index > indexn2)
  545. return false;
  546. index -= indexn1;
  547. return !!(tim->virtual_map[index] & mask);
  548. }
  549. static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
  550. u16 capab, bool erp_valid, u8 erp)
  551. {
  552. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  553. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  554. struct ieee80211_if_sta *ifsta = &sdata->u.sta;
  555. #endif
  556. u32 changed = 0;
  557. bool use_protection;
  558. bool use_short_preamble;
  559. bool use_short_slot;
  560. if (erp_valid) {
  561. use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
  562. use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
  563. } else {
  564. use_protection = false;
  565. use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
  566. }
  567. use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
  568. if (use_protection != bss_conf->use_cts_prot) {
  569. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  570. if (net_ratelimit()) {
  571. printk(KERN_DEBUG "%s: CTS protection %s (BSSID=%pM)\n",
  572. sdata->dev->name,
  573. use_protection ? "enabled" : "disabled",
  574. ifsta->bssid);
  575. }
  576. #endif
  577. bss_conf->use_cts_prot = use_protection;
  578. changed |= BSS_CHANGED_ERP_CTS_PROT;
  579. }
  580. if (use_short_preamble != bss_conf->use_short_preamble) {
  581. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  582. if (net_ratelimit()) {
  583. printk(KERN_DEBUG "%s: switched to %s barker preamble"
  584. " (BSSID=%pM)\n",
  585. sdata->dev->name,
  586. use_short_preamble ? "short" : "long",
  587. ifsta->bssid);
  588. }
  589. #endif
  590. bss_conf->use_short_preamble = use_short_preamble;
  591. changed |= BSS_CHANGED_ERP_PREAMBLE;
  592. }
  593. if (use_short_slot != bss_conf->use_short_slot) {
  594. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  595. if (net_ratelimit()) {
  596. printk(KERN_DEBUG "%s: switched to %s slot time"
  597. " (BSSID=%pM)\n",
  598. sdata->dev->name,
  599. use_short_slot ? "short" : "long",
  600. ifsta->bssid);
  601. }
  602. #endif
  603. bss_conf->use_short_slot = use_short_slot;
  604. changed |= BSS_CHANGED_ERP_SLOT;
  605. }
  606. return changed;
  607. }
  608. static void ieee80211_sta_send_apinfo(struct ieee80211_sub_if_data *sdata,
  609. struct ieee80211_if_sta *ifsta)
  610. {
  611. union iwreq_data wrqu;
  612. memset(&wrqu, 0, sizeof(wrqu));
  613. if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
  614. memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
  615. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  616. wireless_send_event(sdata->dev, SIOCGIWAP, &wrqu, NULL);
  617. }
  618. static void ieee80211_sta_send_associnfo(struct ieee80211_sub_if_data *sdata,
  619. struct ieee80211_if_sta *ifsta)
  620. {
  621. char *buf;
  622. size_t len;
  623. int i;
  624. union iwreq_data wrqu;
  625. if (!ifsta->assocreq_ies && !ifsta->assocresp_ies)
  626. return;
  627. buf = kmalloc(50 + 2 * (ifsta->assocreq_ies_len +
  628. ifsta->assocresp_ies_len), GFP_KERNEL);
  629. if (!buf)
  630. return;
  631. len = sprintf(buf, "ASSOCINFO(");
  632. if (ifsta->assocreq_ies) {
  633. len += sprintf(buf + len, "ReqIEs=");
  634. for (i = 0; i < ifsta->assocreq_ies_len; i++) {
  635. len += sprintf(buf + len, "%02x",
  636. ifsta->assocreq_ies[i]);
  637. }
  638. }
  639. if (ifsta->assocresp_ies) {
  640. if (ifsta->assocreq_ies)
  641. len += sprintf(buf + len, " ");
  642. len += sprintf(buf + len, "RespIEs=");
  643. for (i = 0; i < ifsta->assocresp_ies_len; i++) {
  644. len += sprintf(buf + len, "%02x",
  645. ifsta->assocresp_ies[i]);
  646. }
  647. }
  648. len += sprintf(buf + len, ")");
  649. if (len > IW_CUSTOM_MAX) {
  650. len = sprintf(buf, "ASSOCRESPIE=");
  651. for (i = 0; i < ifsta->assocresp_ies_len; i++) {
  652. len += sprintf(buf + len, "%02x",
  653. ifsta->assocresp_ies[i]);
  654. }
  655. }
  656. if (len <= IW_CUSTOM_MAX) {
  657. memset(&wrqu, 0, sizeof(wrqu));
  658. wrqu.data.length = len;
  659. wireless_send_event(sdata->dev, IWEVCUSTOM, &wrqu, buf);
  660. }
  661. kfree(buf);
  662. }
  663. static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
  664. struct ieee80211_if_sta *ifsta,
  665. u32 bss_info_changed)
  666. {
  667. struct ieee80211_local *local = sdata->local;
  668. struct ieee80211_conf *conf = &local_to_hw(local)->conf;
  669. struct ieee80211_bss *bss;
  670. bss_info_changed |= BSS_CHANGED_ASSOC;
  671. ifsta->flags |= IEEE80211_STA_ASSOCIATED;
  672. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  673. return;
  674. bss = ieee80211_rx_bss_get(local, ifsta->bssid,
  675. conf->channel->center_freq,
  676. ifsta->ssid, ifsta->ssid_len);
  677. if (bss) {
  678. /* set timing information */
  679. sdata->vif.bss_conf.beacon_int = bss->beacon_int;
  680. sdata->vif.bss_conf.timestamp = bss->timestamp;
  681. sdata->vif.bss_conf.dtim_period = bss->dtim_period;
  682. bss_info_changed |= ieee80211_handle_bss_capability(sdata,
  683. bss->capability, bss->has_erp_value, bss->erp_value);
  684. ieee80211_rx_bss_put(local, bss);
  685. }
  686. ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
  687. memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
  688. ieee80211_sta_send_associnfo(sdata, ifsta);
  689. ifsta->last_probe = jiffies;
  690. ieee80211_led_assoc(local, 1);
  691. sdata->vif.bss_conf.assoc = 1;
  692. /*
  693. * For now just always ask the driver to update the basic rateset
  694. * when we have associated, we aren't checking whether it actually
  695. * changed or not.
  696. */
  697. bss_info_changed |= BSS_CHANGED_BASIC_RATES;
  698. ieee80211_bss_info_change_notify(sdata, bss_info_changed);
  699. if (local->powersave) {
  700. if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS) &&
  701. local->hw.conf.dynamic_ps_timeout > 0) {
  702. mod_timer(&local->dynamic_ps_timer, jiffies +
  703. msecs_to_jiffies(
  704. local->hw.conf.dynamic_ps_timeout));
  705. } else {
  706. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
  707. ieee80211_send_nullfunc(local, sdata, 1);
  708. conf->flags |= IEEE80211_CONF_PS;
  709. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  710. }
  711. }
  712. netif_tx_start_all_queues(sdata->dev);
  713. netif_carrier_on(sdata->dev);
  714. ieee80211_sta_send_apinfo(sdata, ifsta);
  715. }
  716. static void ieee80211_direct_probe(struct ieee80211_sub_if_data *sdata,
  717. struct ieee80211_if_sta *ifsta)
  718. {
  719. ifsta->direct_probe_tries++;
  720. if (ifsta->direct_probe_tries > IEEE80211_AUTH_MAX_TRIES) {
  721. printk(KERN_DEBUG "%s: direct probe to AP %pM timed out\n",
  722. sdata->dev->name, ifsta->bssid);
  723. ifsta->state = IEEE80211_STA_MLME_DISABLED;
  724. ieee80211_sta_send_apinfo(sdata, ifsta);
  725. /*
  726. * Most likely AP is not in the range so remove the
  727. * bss information associated to the AP
  728. */
  729. ieee80211_rx_bss_remove(sdata, ifsta->bssid,
  730. sdata->local->hw.conf.channel->center_freq,
  731. ifsta->ssid, ifsta->ssid_len);
  732. return;
  733. }
  734. printk(KERN_DEBUG "%s: direct probe to AP %pM try %d\n",
  735. sdata->dev->name, ifsta->bssid,
  736. ifsta->direct_probe_tries);
  737. ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
  738. set_bit(IEEE80211_STA_REQ_DIRECT_PROBE, &ifsta->request);
  739. /* Direct probe is sent to broadcast address as some APs
  740. * will not answer to direct packet in unassociated state.
  741. */
  742. ieee80211_send_probe_req(sdata, NULL,
  743. ifsta->ssid, ifsta->ssid_len);
  744. mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
  745. }
  746. static void ieee80211_authenticate(struct ieee80211_sub_if_data *sdata,
  747. struct ieee80211_if_sta *ifsta)
  748. {
  749. ifsta->auth_tries++;
  750. if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
  751. printk(KERN_DEBUG "%s: authentication with AP %pM"
  752. " timed out\n",
  753. sdata->dev->name, ifsta->bssid);
  754. ifsta->state = IEEE80211_STA_MLME_DISABLED;
  755. ieee80211_sta_send_apinfo(sdata, ifsta);
  756. ieee80211_rx_bss_remove(sdata, ifsta->bssid,
  757. sdata->local->hw.conf.channel->center_freq,
  758. ifsta->ssid, ifsta->ssid_len);
  759. return;
  760. }
  761. ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
  762. printk(KERN_DEBUG "%s: authenticate with AP %pM\n",
  763. sdata->dev->name, ifsta->bssid);
  764. ieee80211_send_auth(sdata, ifsta, 1, NULL, 0, 0);
  765. mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
  766. }
  767. /*
  768. * The disassoc 'reason' argument can be either our own reason
  769. * if self disconnected or a reason code from the AP.
  770. */
  771. static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
  772. struct ieee80211_if_sta *ifsta, bool deauth,
  773. bool self_disconnected, u16 reason)
  774. {
  775. struct ieee80211_local *local = sdata->local;
  776. struct sta_info *sta;
  777. u32 changed = 0, config_changed = 0;
  778. rcu_read_lock();
  779. sta = sta_info_get(local, ifsta->bssid);
  780. if (!sta) {
  781. rcu_read_unlock();
  782. return;
  783. }
  784. if (deauth) {
  785. ifsta->direct_probe_tries = 0;
  786. ifsta->auth_tries = 0;
  787. }
  788. ifsta->assoc_scan_tries = 0;
  789. ifsta->assoc_tries = 0;
  790. netif_tx_stop_all_queues(sdata->dev);
  791. netif_carrier_off(sdata->dev);
  792. ieee80211_sta_tear_down_BA_sessions(sdata, sta->sta.addr);
  793. if (self_disconnected) {
  794. if (deauth)
  795. ieee80211_send_deauth_disassoc(sdata,
  796. IEEE80211_STYPE_DEAUTH, reason);
  797. else
  798. ieee80211_send_deauth_disassoc(sdata,
  799. IEEE80211_STYPE_DISASSOC, reason);
  800. }
  801. ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
  802. changed |= ieee80211_reset_erp_info(sdata);
  803. ieee80211_led_assoc(local, 0);
  804. changed |= BSS_CHANGED_ASSOC;
  805. sdata->vif.bss_conf.assoc = false;
  806. ieee80211_sta_send_apinfo(sdata, ifsta);
  807. if (self_disconnected || reason == WLAN_REASON_DISASSOC_STA_HAS_LEFT) {
  808. ifsta->state = IEEE80211_STA_MLME_DISABLED;
  809. ieee80211_rx_bss_remove(sdata, ifsta->bssid,
  810. sdata->local->hw.conf.channel->center_freq,
  811. ifsta->ssid, ifsta->ssid_len);
  812. }
  813. rcu_read_unlock();
  814. /* channel(_type) changes are handled by ieee80211_hw_config */
  815. local->oper_channel_type = NL80211_CHAN_NO_HT;
  816. local->power_constr_level = 0;
  817. del_timer_sync(&local->dynamic_ps_timer);
  818. cancel_work_sync(&local->dynamic_ps_enable_work);
  819. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  820. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  821. config_changed |= IEEE80211_CONF_CHANGE_PS;
  822. }
  823. ieee80211_hw_config(local, config_changed);
  824. ieee80211_bss_info_change_notify(sdata, changed);
  825. rcu_read_lock();
  826. sta = sta_info_get(local, ifsta->bssid);
  827. if (!sta) {
  828. rcu_read_unlock();
  829. return;
  830. }
  831. sta_info_unlink(&sta);
  832. rcu_read_unlock();
  833. sta_info_destroy(sta);
  834. }
  835. static int ieee80211_sta_wep_configured(struct ieee80211_sub_if_data *sdata)
  836. {
  837. if (!sdata || !sdata->default_key ||
  838. sdata->default_key->conf.alg != ALG_WEP)
  839. return 0;
  840. return 1;
  841. }
  842. static int ieee80211_privacy_mismatch(struct ieee80211_sub_if_data *sdata,
  843. struct ieee80211_if_sta *ifsta)
  844. {
  845. struct ieee80211_local *local = sdata->local;
  846. struct ieee80211_bss *bss;
  847. int bss_privacy;
  848. int wep_privacy;
  849. int privacy_invoked;
  850. if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
  851. return 0;
  852. bss = ieee80211_rx_bss_get(local, ifsta->bssid,
  853. local->hw.conf.channel->center_freq,
  854. ifsta->ssid, ifsta->ssid_len);
  855. if (!bss)
  856. return 0;
  857. bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
  858. wep_privacy = !!ieee80211_sta_wep_configured(sdata);
  859. privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
  860. ieee80211_rx_bss_put(local, bss);
  861. if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
  862. return 0;
  863. return 1;
  864. }
  865. static void ieee80211_associate(struct ieee80211_sub_if_data *sdata,
  866. struct ieee80211_if_sta *ifsta)
  867. {
  868. ifsta->assoc_tries++;
  869. if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
  870. printk(KERN_DEBUG "%s: association with AP %pM"
  871. " timed out\n",
  872. sdata->dev->name, ifsta->bssid);
  873. ifsta->state = IEEE80211_STA_MLME_DISABLED;
  874. ieee80211_sta_send_apinfo(sdata, ifsta);
  875. ieee80211_rx_bss_remove(sdata, ifsta->bssid,
  876. sdata->local->hw.conf.channel->center_freq,
  877. ifsta->ssid, ifsta->ssid_len);
  878. return;
  879. }
  880. ifsta->state = IEEE80211_STA_MLME_ASSOCIATE;
  881. printk(KERN_DEBUG "%s: associate with AP %pM\n",
  882. sdata->dev->name, ifsta->bssid);
  883. if (ieee80211_privacy_mismatch(sdata, ifsta)) {
  884. printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
  885. "mixed-cell disabled - abort association\n", sdata->dev->name);
  886. ifsta->state = IEEE80211_STA_MLME_DISABLED;
  887. return;
  888. }
  889. ieee80211_send_assoc(sdata, ifsta);
  890. mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
  891. }
  892. static void ieee80211_associated(struct ieee80211_sub_if_data *sdata,
  893. struct ieee80211_if_sta *ifsta)
  894. {
  895. struct ieee80211_local *local = sdata->local;
  896. struct sta_info *sta;
  897. int disassoc;
  898. /* TODO: start monitoring current AP signal quality and number of
  899. * missed beacons. Scan other channels every now and then and search
  900. * for better APs. */
  901. /* TODO: remove expired BSSes */
  902. ifsta->state = IEEE80211_STA_MLME_ASSOCIATED;
  903. rcu_read_lock();
  904. sta = sta_info_get(local, ifsta->bssid);
  905. if (!sta) {
  906. printk(KERN_DEBUG "%s: No STA entry for own AP %pM\n",
  907. sdata->dev->name, ifsta->bssid);
  908. disassoc = 1;
  909. } else {
  910. disassoc = 0;
  911. if (time_after(jiffies,
  912. sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
  913. if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
  914. printk(KERN_DEBUG "%s: No ProbeResp from "
  915. "current AP %pM - assume out of "
  916. "range\n",
  917. sdata->dev->name, ifsta->bssid);
  918. disassoc = 1;
  919. } else
  920. ieee80211_send_probe_req(sdata, ifsta->bssid,
  921. ifsta->ssid,
  922. ifsta->ssid_len);
  923. ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
  924. } else {
  925. ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
  926. if (time_after(jiffies, ifsta->last_probe +
  927. IEEE80211_PROBE_INTERVAL)) {
  928. ifsta->last_probe = jiffies;
  929. ieee80211_send_probe_req(sdata, ifsta->bssid,
  930. ifsta->ssid,
  931. ifsta->ssid_len);
  932. }
  933. }
  934. }
  935. rcu_read_unlock();
  936. if (disassoc)
  937. ieee80211_set_disassoc(sdata, ifsta, true, true,
  938. WLAN_REASON_PREV_AUTH_NOT_VALID);
  939. else
  940. mod_timer(&ifsta->timer, jiffies +
  941. IEEE80211_MONITORING_INTERVAL);
  942. }
  943. static void ieee80211_auth_completed(struct ieee80211_sub_if_data *sdata,
  944. struct ieee80211_if_sta *ifsta)
  945. {
  946. printk(KERN_DEBUG "%s: authenticated\n", sdata->dev->name);
  947. ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
  948. ieee80211_associate(sdata, ifsta);
  949. }
  950. static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
  951. struct ieee80211_if_sta *ifsta,
  952. struct ieee80211_mgmt *mgmt,
  953. size_t len)
  954. {
  955. u8 *pos;
  956. struct ieee802_11_elems elems;
  957. pos = mgmt->u.auth.variable;
  958. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  959. if (!elems.challenge)
  960. return;
  961. ieee80211_send_auth(sdata, ifsta, 3, elems.challenge - 2,
  962. elems.challenge_len + 2, 1);
  963. }
  964. static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
  965. struct ieee80211_if_sta *ifsta,
  966. struct ieee80211_mgmt *mgmt,
  967. size_t len)
  968. {
  969. u16 auth_alg, auth_transaction, status_code;
  970. if (ifsta->state != IEEE80211_STA_MLME_AUTHENTICATE &&
  971. sdata->vif.type != NL80211_IFTYPE_ADHOC)
  972. return;
  973. if (len < 24 + 6)
  974. return;
  975. if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  976. memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
  977. return;
  978. if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  979. memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
  980. return;
  981. auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
  982. auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
  983. status_code = le16_to_cpu(mgmt->u.auth.status_code);
  984. if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  985. /*
  986. * IEEE 802.11 standard does not require authentication in IBSS
  987. * networks and most implementations do not seem to use it.
  988. * However, try to reply to authentication attempts if someone
  989. * has actually implemented this.
  990. */
  991. if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1)
  992. return;
  993. ieee80211_send_auth(sdata, ifsta, 2, NULL, 0, 0);
  994. }
  995. if (auth_alg != ifsta->auth_alg ||
  996. auth_transaction != ifsta->auth_transaction)
  997. return;
  998. if (status_code != WLAN_STATUS_SUCCESS) {
  999. if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
  1000. u8 algs[3];
  1001. const int num_algs = ARRAY_SIZE(algs);
  1002. int i, pos;
  1003. algs[0] = algs[1] = algs[2] = 0xff;
  1004. if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
  1005. algs[0] = WLAN_AUTH_OPEN;
  1006. if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
  1007. algs[1] = WLAN_AUTH_SHARED_KEY;
  1008. if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
  1009. algs[2] = WLAN_AUTH_LEAP;
  1010. if (ifsta->auth_alg == WLAN_AUTH_OPEN)
  1011. pos = 0;
  1012. else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
  1013. pos = 1;
  1014. else
  1015. pos = 2;
  1016. for (i = 0; i < num_algs; i++) {
  1017. pos++;
  1018. if (pos >= num_algs)
  1019. pos = 0;
  1020. if (algs[pos] == ifsta->auth_alg ||
  1021. algs[pos] == 0xff)
  1022. continue;
  1023. if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
  1024. !ieee80211_sta_wep_configured(sdata))
  1025. continue;
  1026. ifsta->auth_alg = algs[pos];
  1027. break;
  1028. }
  1029. }
  1030. return;
  1031. }
  1032. switch (ifsta->auth_alg) {
  1033. case WLAN_AUTH_OPEN:
  1034. case WLAN_AUTH_LEAP:
  1035. ieee80211_auth_completed(sdata, ifsta);
  1036. break;
  1037. case WLAN_AUTH_SHARED_KEY:
  1038. if (ifsta->auth_transaction == 4)
  1039. ieee80211_auth_completed(sdata, ifsta);
  1040. else
  1041. ieee80211_auth_challenge(sdata, ifsta, mgmt, len);
  1042. break;
  1043. }
  1044. }
  1045. static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
  1046. struct ieee80211_if_sta *ifsta,
  1047. struct ieee80211_mgmt *mgmt,
  1048. size_t len)
  1049. {
  1050. u16 reason_code;
  1051. if (len < 24 + 2)
  1052. return;
  1053. if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
  1054. return;
  1055. reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
  1056. if (ifsta->flags & IEEE80211_STA_AUTHENTICATED)
  1057. printk(KERN_DEBUG "%s: deauthenticated (Reason: %u)\n",
  1058. sdata->dev->name, reason_code);
  1059. if (ifsta->state == IEEE80211_STA_MLME_AUTHENTICATE ||
  1060. ifsta->state == IEEE80211_STA_MLME_ASSOCIATE ||
  1061. ifsta->state == IEEE80211_STA_MLME_ASSOCIATED) {
  1062. ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
  1063. mod_timer(&ifsta->timer, jiffies +
  1064. IEEE80211_RETRY_AUTH_INTERVAL);
  1065. }
  1066. ieee80211_set_disassoc(sdata, ifsta, true, false, 0);
  1067. ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
  1068. }
  1069. static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
  1070. struct ieee80211_if_sta *ifsta,
  1071. struct ieee80211_mgmt *mgmt,
  1072. size_t len)
  1073. {
  1074. u16 reason_code;
  1075. if (len < 24 + 2)
  1076. return;
  1077. if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
  1078. return;
  1079. reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
  1080. if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
  1081. printk(KERN_DEBUG "%s: disassociated (Reason: %u)\n",
  1082. sdata->dev->name, reason_code);
  1083. if (ifsta->state == IEEE80211_STA_MLME_ASSOCIATED) {
  1084. ifsta->state = IEEE80211_STA_MLME_ASSOCIATE;
  1085. mod_timer(&ifsta->timer, jiffies +
  1086. IEEE80211_RETRY_AUTH_INTERVAL);
  1087. }
  1088. ieee80211_set_disassoc(sdata, ifsta, false, false, reason_code);
  1089. }
  1090. static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
  1091. struct ieee80211_if_sta *ifsta,
  1092. struct ieee80211_mgmt *mgmt,
  1093. size_t len,
  1094. int reassoc)
  1095. {
  1096. struct ieee80211_local *local = sdata->local;
  1097. struct ieee80211_supported_band *sband;
  1098. struct sta_info *sta;
  1099. u32 rates, basic_rates;
  1100. u16 capab_info, status_code, aid;
  1101. struct ieee802_11_elems elems;
  1102. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  1103. u8 *pos;
  1104. u32 changed = 0;
  1105. int i, j;
  1106. bool have_higher_than_11mbit = false, newsta = false;
  1107. u16 ap_ht_cap_flags;
  1108. /* AssocResp and ReassocResp have identical structure, so process both
  1109. * of them in this function. */
  1110. if (ifsta->state != IEEE80211_STA_MLME_ASSOCIATE)
  1111. return;
  1112. if (len < 24 + 6)
  1113. return;
  1114. if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
  1115. return;
  1116. capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
  1117. status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
  1118. aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
  1119. printk(KERN_DEBUG "%s: RX %sssocResp from %pM (capab=0x%x "
  1120. "status=%d aid=%d)\n",
  1121. sdata->dev->name, reassoc ? "Rea" : "A", mgmt->sa,
  1122. capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
  1123. pos = mgmt->u.assoc_resp.variable;
  1124. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  1125. if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
  1126. elems.timeout_int && elems.timeout_int_len == 5 &&
  1127. elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
  1128. u32 tu, ms;
  1129. tu = get_unaligned_le32(elems.timeout_int + 1);
  1130. ms = tu * 1024 / 1000;
  1131. printk(KERN_DEBUG "%s: AP rejected association temporarily; "
  1132. "comeback duration %u TU (%u ms)\n",
  1133. sdata->dev->name, tu, ms);
  1134. if (ms > IEEE80211_ASSOC_TIMEOUT)
  1135. mod_timer(&ifsta->timer,
  1136. jiffies + msecs_to_jiffies(ms));
  1137. return;
  1138. }
  1139. if (status_code != WLAN_STATUS_SUCCESS) {
  1140. printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
  1141. sdata->dev->name, status_code);
  1142. /* if this was a reassociation, ensure we try a "full"
  1143. * association next time. This works around some broken APs
  1144. * which do not correctly reject reassociation requests. */
  1145. ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
  1146. return;
  1147. }
  1148. if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
  1149. printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
  1150. "set\n", sdata->dev->name, aid);
  1151. aid &= ~(BIT(15) | BIT(14));
  1152. if (!elems.supp_rates) {
  1153. printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
  1154. sdata->dev->name);
  1155. return;
  1156. }
  1157. printk(KERN_DEBUG "%s: associated\n", sdata->dev->name);
  1158. ifsta->aid = aid;
  1159. ifsta->ap_capab = capab_info;
  1160. kfree(ifsta->assocresp_ies);
  1161. ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
  1162. ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
  1163. if (ifsta->assocresp_ies)
  1164. memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
  1165. rcu_read_lock();
  1166. /* Add STA entry for the AP */
  1167. sta = sta_info_get(local, ifsta->bssid);
  1168. if (!sta) {
  1169. struct ieee80211_bss *bss;
  1170. newsta = true;
  1171. sta = sta_info_alloc(sdata, ifsta->bssid, GFP_ATOMIC);
  1172. if (!sta) {
  1173. printk(KERN_DEBUG "%s: failed to alloc STA entry for"
  1174. " the AP\n", sdata->dev->name);
  1175. rcu_read_unlock();
  1176. return;
  1177. }
  1178. bss = ieee80211_rx_bss_get(local, ifsta->bssid,
  1179. local->hw.conf.channel->center_freq,
  1180. ifsta->ssid, ifsta->ssid_len);
  1181. if (bss) {
  1182. sta->last_signal = bss->signal;
  1183. sta->last_qual = bss->qual;
  1184. sta->last_noise = bss->noise;
  1185. ieee80211_rx_bss_put(local, bss);
  1186. }
  1187. /* update new sta with its last rx activity */
  1188. sta->last_rx = jiffies;
  1189. }
  1190. /*
  1191. * FIXME: Do we really need to update the sta_info's information here?
  1192. * We already know about the AP (we found it in our list) so it
  1193. * should already be filled with the right info, no?
  1194. * As is stands, all this is racy because typically we assume
  1195. * the information that is filled in here (except flags) doesn't
  1196. * change while a STA structure is alive. As such, it should move
  1197. * to between the sta_info_alloc() and sta_info_insert() above.
  1198. */
  1199. set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
  1200. WLAN_STA_AUTHORIZED);
  1201. rates = 0;
  1202. basic_rates = 0;
  1203. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  1204. for (i = 0; i < elems.supp_rates_len; i++) {
  1205. int rate = (elems.supp_rates[i] & 0x7f) * 5;
  1206. bool is_basic = !!(elems.supp_rates[i] & 0x80);
  1207. if (rate > 110)
  1208. have_higher_than_11mbit = true;
  1209. for (j = 0; j < sband->n_bitrates; j++) {
  1210. if (sband->bitrates[j].bitrate == rate) {
  1211. rates |= BIT(j);
  1212. if (is_basic)
  1213. basic_rates |= BIT(j);
  1214. break;
  1215. }
  1216. }
  1217. }
  1218. for (i = 0; i < elems.ext_supp_rates_len; i++) {
  1219. int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
  1220. bool is_basic = !!(elems.supp_rates[i] & 0x80);
  1221. if (rate > 110)
  1222. have_higher_than_11mbit = true;
  1223. for (j = 0; j < sband->n_bitrates; j++) {
  1224. if (sband->bitrates[j].bitrate == rate) {
  1225. rates |= BIT(j);
  1226. if (is_basic)
  1227. basic_rates |= BIT(j);
  1228. break;
  1229. }
  1230. }
  1231. }
  1232. sta->sta.supp_rates[local->hw.conf.channel->band] = rates;
  1233. sdata->vif.bss_conf.basic_rates = basic_rates;
  1234. /* cf. IEEE 802.11 9.2.12 */
  1235. if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
  1236. have_higher_than_11mbit)
  1237. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  1238. else
  1239. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  1240. if (elems.ht_cap_elem)
  1241. ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
  1242. elems.ht_cap_elem, &sta->sta.ht_cap);
  1243. ap_ht_cap_flags = sta->sta.ht_cap.cap;
  1244. rate_control_rate_init(sta);
  1245. if (ifsta->flags & IEEE80211_STA_MFP_ENABLED)
  1246. set_sta_flags(sta, WLAN_STA_MFP);
  1247. if (elems.wmm_param)
  1248. set_sta_flags(sta, WLAN_STA_WME);
  1249. if (newsta) {
  1250. int err = sta_info_insert(sta);
  1251. if (err) {
  1252. printk(KERN_DEBUG "%s: failed to insert STA entry for"
  1253. " the AP (error %d)\n", sdata->dev->name, err);
  1254. rcu_read_unlock();
  1255. return;
  1256. }
  1257. }
  1258. rcu_read_unlock();
  1259. if (elems.wmm_param)
  1260. ieee80211_sta_wmm_params(local, ifsta, elems.wmm_param,
  1261. elems.wmm_param_len);
  1262. if (elems.ht_info_elem && elems.wmm_param &&
  1263. (ifsta->flags & IEEE80211_STA_WMM_ENABLED))
  1264. changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
  1265. ap_ht_cap_flags);
  1266. /* set AID and assoc capability,
  1267. * ieee80211_set_associated() will tell the driver */
  1268. bss_conf->aid = aid;
  1269. bss_conf->assoc_capability = capab_info;
  1270. ieee80211_set_associated(sdata, ifsta, changed);
  1271. ieee80211_associated(sdata, ifsta);
  1272. }
  1273. static int ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
  1274. struct ieee80211_if_sta *ifsta,
  1275. struct ieee80211_bss *bss)
  1276. {
  1277. struct ieee80211_local *local = sdata->local;
  1278. int res = 0, rates, i, j;
  1279. struct sk_buff *skb;
  1280. struct ieee80211_mgmt *mgmt;
  1281. u8 *pos;
  1282. struct ieee80211_supported_band *sband;
  1283. union iwreq_data wrqu;
  1284. if (local->ops->reset_tsf) {
  1285. /* Reset own TSF to allow time synchronization work. */
  1286. local->ops->reset_tsf(local_to_hw(local));
  1287. }
  1288. if ((ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) &&
  1289. memcmp(ifsta->bssid, bss->bssid, ETH_ALEN) == 0)
  1290. return res;
  1291. skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400 +
  1292. sdata->u.sta.ie_proberesp_len);
  1293. if (!skb) {
  1294. printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
  1295. "response\n", sdata->dev->name);
  1296. return -ENOMEM;
  1297. }
  1298. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  1299. if (!(ifsta->flags & IEEE80211_STA_PREV_BSSID_SET)) {
  1300. /* Remove possible STA entries from other IBSS networks. */
  1301. sta_info_flush_delayed(sdata);
  1302. }
  1303. memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
  1304. res = ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
  1305. if (res)
  1306. return res;
  1307. local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
  1308. sdata->drop_unencrypted = bss->capability &
  1309. WLAN_CAPABILITY_PRIVACY ? 1 : 0;
  1310. res = ieee80211_set_freq(sdata, bss->freq);
  1311. if (res)
  1312. return res;
  1313. /* Build IBSS probe response */
  1314. skb_reserve(skb, local->hw.extra_tx_headroom);
  1315. mgmt = (struct ieee80211_mgmt *)
  1316. skb_put(skb, 24 + sizeof(mgmt->u.beacon));
  1317. memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
  1318. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  1319. IEEE80211_STYPE_PROBE_RESP);
  1320. memset(mgmt->da, 0xff, ETH_ALEN);
  1321. memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
  1322. memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
  1323. mgmt->u.beacon.beacon_int =
  1324. cpu_to_le16(local->hw.conf.beacon_int);
  1325. mgmt->u.beacon.timestamp = cpu_to_le64(bss->timestamp);
  1326. mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
  1327. pos = skb_put(skb, 2 + ifsta->ssid_len);
  1328. *pos++ = WLAN_EID_SSID;
  1329. *pos++ = ifsta->ssid_len;
  1330. memcpy(pos, ifsta->ssid, ifsta->ssid_len);
  1331. rates = bss->supp_rates_len;
  1332. if (rates > 8)
  1333. rates = 8;
  1334. pos = skb_put(skb, 2 + rates);
  1335. *pos++ = WLAN_EID_SUPP_RATES;
  1336. *pos++ = rates;
  1337. memcpy(pos, bss->supp_rates, rates);
  1338. if (bss->band == IEEE80211_BAND_2GHZ) {
  1339. pos = skb_put(skb, 2 + 1);
  1340. *pos++ = WLAN_EID_DS_PARAMS;
  1341. *pos++ = 1;
  1342. *pos++ = ieee80211_frequency_to_channel(bss->freq);
  1343. }
  1344. pos = skb_put(skb, 2 + 2);
  1345. *pos++ = WLAN_EID_IBSS_PARAMS;
  1346. *pos++ = 2;
  1347. /* FIX: set ATIM window based on scan results */
  1348. *pos++ = 0;
  1349. *pos++ = 0;
  1350. if (bss->supp_rates_len > 8) {
  1351. rates = bss->supp_rates_len - 8;
  1352. pos = skb_put(skb, 2 + rates);
  1353. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  1354. *pos++ = rates;
  1355. memcpy(pos, &bss->supp_rates[8], rates);
  1356. }
  1357. add_extra_ies(skb, sdata->u.sta.ie_proberesp,
  1358. sdata->u.sta.ie_proberesp_len);
  1359. ifsta->probe_resp = skb;
  1360. ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON |
  1361. IEEE80211_IFCC_BEACON_ENABLED);
  1362. rates = 0;
  1363. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  1364. for (i = 0; i < bss->supp_rates_len; i++) {
  1365. int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
  1366. for (j = 0; j < sband->n_bitrates; j++)
  1367. if (sband->bitrates[j].bitrate == bitrate)
  1368. rates |= BIT(j);
  1369. }
  1370. ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
  1371. ieee80211_sta_def_wmm_params(sdata, bss);
  1372. ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
  1373. ifsta->state = IEEE80211_STA_MLME_IBSS_JOINED;
  1374. mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
  1375. ieee80211_led_assoc(local, true);
  1376. memset(&wrqu, 0, sizeof(wrqu));
  1377. memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
  1378. wireless_send_event(sdata->dev, SIOCGIWAP, &wrqu, NULL);
  1379. return res;
  1380. }
  1381. static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
  1382. struct ieee80211_mgmt *mgmt,
  1383. size_t len,
  1384. struct ieee80211_rx_status *rx_status,
  1385. struct ieee802_11_elems *elems,
  1386. bool beacon)
  1387. {
  1388. struct ieee80211_local *local = sdata->local;
  1389. int freq;
  1390. struct ieee80211_bss *bss;
  1391. struct sta_info *sta;
  1392. struct ieee80211_channel *channel;
  1393. u64 beacon_timestamp, rx_timestamp;
  1394. u32 supp_rates = 0;
  1395. enum ieee80211_band band = rx_status->band;
  1396. if (elems->ds_params && elems->ds_params_len == 1)
  1397. freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
  1398. else
  1399. freq = rx_status->freq;
  1400. channel = ieee80211_get_channel(local->hw.wiphy, freq);
  1401. if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
  1402. return;
  1403. if (sdata->vif.type == NL80211_IFTYPE_ADHOC && elems->supp_rates &&
  1404. memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0) {
  1405. supp_rates = ieee80211_sta_get_rates(local, elems, band);
  1406. rcu_read_lock();
  1407. sta = sta_info_get(local, mgmt->sa);
  1408. if (sta) {
  1409. u32 prev_rates;
  1410. prev_rates = sta->sta.supp_rates[band];
  1411. /* make sure mandatory rates are always added */
  1412. sta->sta.supp_rates[band] = supp_rates |
  1413. ieee80211_mandatory_rates(local, band);
  1414. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  1415. if (sta->sta.supp_rates[band] != prev_rates)
  1416. printk(KERN_DEBUG "%s: updated supp_rates set "
  1417. "for %pM based on beacon info (0x%llx | "
  1418. "0x%llx -> 0x%llx)\n",
  1419. sdata->dev->name,
  1420. sta->sta.addr,
  1421. (unsigned long long) prev_rates,
  1422. (unsigned long long) supp_rates,
  1423. (unsigned long long) sta->sta.supp_rates[band]);
  1424. #endif
  1425. } else {
  1426. ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa, supp_rates);
  1427. }
  1428. rcu_read_unlock();
  1429. }
  1430. bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
  1431. freq, beacon);
  1432. if (!bss)
  1433. return;
  1434. if (elems->ch_switch_elem && (elems->ch_switch_elem_len == 3) &&
  1435. (memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0)) {
  1436. struct ieee80211_channel_sw_ie *sw_elem =
  1437. (struct ieee80211_channel_sw_ie *)elems->ch_switch_elem;
  1438. ieee80211_process_chanswitch(sdata, sw_elem, bss);
  1439. }
  1440. /* was just updated in ieee80211_bss_info_update */
  1441. beacon_timestamp = bss->timestamp;
  1442. /*
  1443. * In STA mode, the remaining parameters should not be overridden
  1444. * by beacons because they're not necessarily accurate there.
  1445. */
  1446. if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  1447. bss->last_probe_resp && beacon) {
  1448. ieee80211_rx_bss_put(local, bss);
  1449. return;
  1450. }
  1451. /* check if we need to merge IBSS */
  1452. if (sdata->vif.type == NL80211_IFTYPE_ADHOC && beacon &&
  1453. (!(sdata->u.sta.flags & IEEE80211_STA_BSSID_SET)) &&
  1454. bss->capability & WLAN_CAPABILITY_IBSS &&
  1455. bss->freq == local->oper_channel->center_freq &&
  1456. elems->ssid_len == sdata->u.sta.ssid_len &&
  1457. memcmp(elems->ssid, sdata->u.sta.ssid,
  1458. sdata->u.sta.ssid_len) == 0) {
  1459. if (rx_status->flag & RX_FLAG_TSFT) {
  1460. /* in order for correct IBSS merging we need mactime
  1461. *
  1462. * since mactime is defined as the time the first data
  1463. * symbol of the frame hits the PHY, and the timestamp
  1464. * of the beacon is defined as "the time that the data
  1465. * symbol containing the first bit of the timestamp is
  1466. * transmitted to the PHY plus the transmitting STA’s
  1467. * delays through its local PHY from the MAC-PHY
  1468. * interface to its interface with the WM"
  1469. * (802.11 11.1.2) - equals the time this bit arrives at
  1470. * the receiver - we have to take into account the
  1471. * offset between the two.
  1472. * e.g: at 1 MBit that means mactime is 192 usec earlier
  1473. * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
  1474. */
  1475. int rate;
  1476. if (rx_status->flag & RX_FLAG_HT) {
  1477. rate = 65; /* TODO: HT rates */
  1478. } else {
  1479. rate = local->hw.wiphy->bands[band]->
  1480. bitrates[rx_status->rate_idx].bitrate;
  1481. }
  1482. rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
  1483. } else if (local && local->ops && local->ops->get_tsf)
  1484. /* second best option: get current TSF */
  1485. rx_timestamp = local->ops->get_tsf(local_to_hw(local));
  1486. else
  1487. /* can't merge without knowing the TSF */
  1488. rx_timestamp = -1LLU;
  1489. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  1490. printk(KERN_DEBUG "RX beacon SA=%pM BSSID="
  1491. "%pM TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
  1492. mgmt->sa, mgmt->bssid,
  1493. (unsigned long long)rx_timestamp,
  1494. (unsigned long long)beacon_timestamp,
  1495. (unsigned long long)(rx_timestamp - beacon_timestamp),
  1496. jiffies);
  1497. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  1498. if (beacon_timestamp > rx_timestamp) {
  1499. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  1500. printk(KERN_DEBUG "%s: beacon TSF higher than "
  1501. "local TSF - IBSS merge with BSSID %pM\n",
  1502. sdata->dev->name, mgmt->bssid);
  1503. #endif
  1504. ieee80211_sta_join_ibss(sdata, &sdata->u.sta, bss);
  1505. ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa, supp_rates);
  1506. }
  1507. }
  1508. ieee80211_rx_bss_put(local, bss);
  1509. }
  1510. static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
  1511. struct ieee80211_mgmt *mgmt,
  1512. size_t len,
  1513. struct ieee80211_rx_status *rx_status)
  1514. {
  1515. size_t baselen;
  1516. struct ieee802_11_elems elems;
  1517. struct ieee80211_if_sta *ifsta = &sdata->u.sta;
  1518. if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
  1519. return; /* ignore ProbeResp to foreign address */
  1520. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  1521. if (baselen > len)
  1522. return;
  1523. ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
  1524. &elems);
  1525. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
  1526. /* direct probe may be part of the association flow */
  1527. if (test_and_clear_bit(IEEE80211_STA_REQ_DIRECT_PROBE,
  1528. &ifsta->request)) {
  1529. printk(KERN_DEBUG "%s direct probe responded\n",
  1530. sdata->dev->name);
  1531. ieee80211_authenticate(sdata, ifsta);
  1532. }
  1533. }
  1534. static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
  1535. struct ieee80211_mgmt *mgmt,
  1536. size_t len,
  1537. struct ieee80211_rx_status *rx_status)
  1538. {
  1539. struct ieee80211_if_sta *ifsta;
  1540. size_t baselen;
  1541. struct ieee802_11_elems elems;
  1542. struct ieee80211_local *local = sdata->local;
  1543. u32 changed = 0;
  1544. bool erp_valid, directed_tim;
  1545. u8 erp_value = 0;
  1546. /* Process beacon from the current BSS */
  1547. baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
  1548. if (baselen > len)
  1549. return;
  1550. ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
  1551. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, true);
  1552. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1553. return;
  1554. ifsta = &sdata->u.sta;
  1555. if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
  1556. memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
  1557. return;
  1558. if (rx_status->freq != local->hw.conf.channel->center_freq)
  1559. return;
  1560. ieee80211_sta_wmm_params(local, ifsta, elems.wmm_param,
  1561. elems.wmm_param_len);
  1562. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK &&
  1563. local->hw.conf.flags & IEEE80211_CONF_PS) {
  1564. directed_tim = ieee80211_check_tim(&elems, ifsta->aid);
  1565. if (directed_tim) {
  1566. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  1567. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  1568. ieee80211_send_nullfunc(local, sdata, 0);
  1569. }
  1570. }
  1571. if (elems.erp_info && elems.erp_info_len >= 1) {
  1572. erp_valid = true;
  1573. erp_value = elems.erp_info[0];
  1574. } else {
  1575. erp_valid = false;
  1576. }
  1577. changed |= ieee80211_handle_bss_capability(sdata,
  1578. le16_to_cpu(mgmt->u.beacon.capab_info),
  1579. erp_valid, erp_value);
  1580. if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param) {
  1581. struct sta_info *sta;
  1582. struct ieee80211_supported_band *sband;
  1583. u16 ap_ht_cap_flags;
  1584. rcu_read_lock();
  1585. sta = sta_info_get(local, ifsta->bssid);
  1586. if (!sta) {
  1587. rcu_read_unlock();
  1588. return;
  1589. }
  1590. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  1591. ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
  1592. elems.ht_cap_elem, &sta->sta.ht_cap);
  1593. ap_ht_cap_flags = sta->sta.ht_cap.cap;
  1594. rcu_read_unlock();
  1595. changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
  1596. ap_ht_cap_flags);
  1597. }
  1598. if (elems.country_elem) {
  1599. /* Note we are only reviewing this on beacons
  1600. * for the BSSID we are associated to */
  1601. regulatory_hint_11d(local->hw.wiphy,
  1602. elems.country_elem, elems.country_elem_len);
  1603. /* TODO: IBSS also needs this */
  1604. if (elems.pwr_constr_elem)
  1605. ieee80211_handle_pwr_constr(sdata,
  1606. le16_to_cpu(mgmt->u.probe_resp.capab_info),
  1607. elems.pwr_constr_elem,
  1608. elems.pwr_constr_elem_len);
  1609. }
  1610. ieee80211_bss_info_change_notify(sdata, changed);
  1611. }
  1612. static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
  1613. struct ieee80211_if_sta *ifsta,
  1614. struct ieee80211_mgmt *mgmt,
  1615. size_t len)
  1616. {
  1617. struct ieee80211_local *local = sdata->local;
  1618. int tx_last_beacon;
  1619. struct sk_buff *skb;
  1620. struct ieee80211_mgmt *resp;
  1621. u8 *pos, *end;
  1622. if (sdata->vif.type != NL80211_IFTYPE_ADHOC ||
  1623. ifsta->state != IEEE80211_STA_MLME_IBSS_JOINED ||
  1624. len < 24 + 2 || !ifsta->probe_resp)
  1625. return;
  1626. if (local->ops->tx_last_beacon)
  1627. tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
  1628. else
  1629. tx_last_beacon = 1;
  1630. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  1631. printk(KERN_DEBUG "%s: RX ProbeReq SA=%pM DA=%pM BSSID=%pM"
  1632. " (tx_last_beacon=%d)\n",
  1633. sdata->dev->name, mgmt->sa, mgmt->da,
  1634. mgmt->bssid, tx_last_beacon);
  1635. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  1636. if (!tx_last_beacon)
  1637. return;
  1638. if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
  1639. memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
  1640. return;
  1641. end = ((u8 *) mgmt) + len;
  1642. pos = mgmt->u.probe_req.variable;
  1643. if (pos[0] != WLAN_EID_SSID ||
  1644. pos + 2 + pos[1] > end) {
  1645. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  1646. printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
  1647. "from %pM\n",
  1648. sdata->dev->name, mgmt->sa);
  1649. #endif
  1650. return;
  1651. }
  1652. if (pos[1] != 0 &&
  1653. (pos[1] != ifsta->ssid_len ||
  1654. memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
  1655. /* Ignore ProbeReq for foreign SSID */
  1656. return;
  1657. }
  1658. /* Reply with ProbeResp */
  1659. skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
  1660. if (!skb)
  1661. return;
  1662. resp = (struct ieee80211_mgmt *) skb->data;
  1663. memcpy(resp->da, mgmt->sa, ETH_ALEN);
  1664. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  1665. printk(KERN_DEBUG "%s: Sending ProbeResp to %pM\n",
  1666. sdata->dev->name, resp->da);
  1667. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  1668. ieee80211_tx_skb(sdata, skb, 0);
  1669. }
  1670. void ieee80211_sta_rx_mgmt(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
  1671. struct ieee80211_rx_status *rx_status)
  1672. {
  1673. struct ieee80211_local *local = sdata->local;
  1674. struct ieee80211_if_sta *ifsta;
  1675. struct ieee80211_mgmt *mgmt;
  1676. u16 fc;
  1677. if (skb->len < 24)
  1678. goto fail;
  1679. ifsta = &sdata->u.sta;
  1680. mgmt = (struct ieee80211_mgmt *) skb->data;
  1681. fc = le16_to_cpu(mgmt->frame_control);
  1682. switch (fc & IEEE80211_FCTL_STYPE) {
  1683. case IEEE80211_STYPE_PROBE_REQ:
  1684. case IEEE80211_STYPE_PROBE_RESP:
  1685. case IEEE80211_STYPE_BEACON:
  1686. memcpy(skb->cb, rx_status, sizeof(*rx_status));
  1687. case IEEE80211_STYPE_AUTH:
  1688. case IEEE80211_STYPE_ASSOC_RESP:
  1689. case IEEE80211_STYPE_REASSOC_RESP:
  1690. case IEEE80211_STYPE_DEAUTH:
  1691. case IEEE80211_STYPE_DISASSOC:
  1692. skb_queue_tail(&ifsta->skb_queue, skb);
  1693. queue_work(local->hw.workqueue, &ifsta->work);
  1694. return;
  1695. }
  1696. fail:
  1697. kfree_skb(skb);
  1698. }
  1699. static void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
  1700. struct sk_buff *skb)
  1701. {
  1702. struct ieee80211_rx_status *rx_status;
  1703. struct ieee80211_if_sta *ifsta;
  1704. struct ieee80211_mgmt *mgmt;
  1705. u16 fc;
  1706. ifsta = &sdata->u.sta;
  1707. rx_status = (struct ieee80211_rx_status *) skb->cb;
  1708. mgmt = (struct ieee80211_mgmt *) skb->data;
  1709. fc = le16_to_cpu(mgmt->frame_control);
  1710. switch (fc & IEEE80211_FCTL_STYPE) {
  1711. case IEEE80211_STYPE_PROBE_REQ:
  1712. ieee80211_rx_mgmt_probe_req(sdata, ifsta, mgmt, skb->len);
  1713. break;
  1714. case IEEE80211_STYPE_PROBE_RESP:
  1715. ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len, rx_status);
  1716. break;
  1717. case IEEE80211_STYPE_BEACON:
  1718. ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
  1719. break;
  1720. case IEEE80211_STYPE_AUTH:
  1721. ieee80211_rx_mgmt_auth(sdata, ifsta, mgmt, skb->len);
  1722. break;
  1723. case IEEE80211_STYPE_ASSOC_RESP:
  1724. ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
  1725. break;
  1726. case IEEE80211_STYPE_REASSOC_RESP:
  1727. ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
  1728. break;
  1729. case IEEE80211_STYPE_DEAUTH:
  1730. ieee80211_rx_mgmt_deauth(sdata, ifsta, mgmt, skb->len);
  1731. break;
  1732. case IEEE80211_STYPE_DISASSOC:
  1733. ieee80211_rx_mgmt_disassoc(sdata, ifsta, mgmt, skb->len);
  1734. break;
  1735. }
  1736. kfree_skb(skb);
  1737. }
  1738. static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
  1739. {
  1740. struct ieee80211_local *local = sdata->local;
  1741. int active = 0;
  1742. struct sta_info *sta;
  1743. rcu_read_lock();
  1744. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  1745. if (sta->sdata == sdata &&
  1746. time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
  1747. jiffies)) {
  1748. active++;
  1749. break;
  1750. }
  1751. }
  1752. rcu_read_unlock();
  1753. return active;
  1754. }
  1755. static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata,
  1756. struct ieee80211_if_sta *ifsta)
  1757. {
  1758. mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
  1759. ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
  1760. if (ieee80211_sta_active_ibss(sdata))
  1761. return;
  1762. if ((sdata->u.sta.flags & IEEE80211_STA_BSSID_SET) &&
  1763. (!(sdata->u.sta.flags & IEEE80211_STA_AUTO_CHANNEL_SEL)))
  1764. return;
  1765. printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
  1766. "IBSS networks with same SSID (merge)\n", sdata->dev->name);
  1767. ieee80211_request_scan(sdata, ifsta->ssid, ifsta->ssid_len);
  1768. }
  1769. static void ieee80211_sta_timer(unsigned long data)
  1770. {
  1771. struct ieee80211_sub_if_data *sdata =
  1772. (struct ieee80211_sub_if_data *) data;
  1773. struct ieee80211_if_sta *ifsta = &sdata->u.sta;
  1774. struct ieee80211_local *local = sdata->local;
  1775. set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
  1776. queue_work(local->hw.workqueue, &ifsta->work);
  1777. }
  1778. static void ieee80211_sta_reset_auth(struct ieee80211_sub_if_data *sdata,
  1779. struct ieee80211_if_sta *ifsta)
  1780. {
  1781. struct ieee80211_local *local = sdata->local;
  1782. if (local->ops->reset_tsf) {
  1783. /* Reset own TSF to allow time synchronization work. */
  1784. local->ops->reset_tsf(local_to_hw(local));
  1785. }
  1786. ifsta->wmm_last_param_set = -1; /* allow any WMM update */
  1787. if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
  1788. ifsta->auth_alg = WLAN_AUTH_OPEN;
  1789. else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
  1790. ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
  1791. else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
  1792. ifsta->auth_alg = WLAN_AUTH_LEAP;
  1793. else
  1794. ifsta->auth_alg = WLAN_AUTH_OPEN;
  1795. ifsta->auth_transaction = -1;
  1796. ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
  1797. ifsta->assoc_scan_tries = 0;
  1798. ifsta->direct_probe_tries = 0;
  1799. ifsta->auth_tries = 0;
  1800. ifsta->assoc_tries = 0;
  1801. netif_tx_stop_all_queues(sdata->dev);
  1802. netif_carrier_off(sdata->dev);
  1803. }
  1804. static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
  1805. const char *ssid, int ssid_len)
  1806. {
  1807. int tmp, hidden_ssid;
  1808. if (ssid_len == ifsta->ssid_len &&
  1809. !memcmp(ifsta->ssid, ssid, ssid_len))
  1810. return 1;
  1811. if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
  1812. return 0;
  1813. hidden_ssid = 1;
  1814. tmp = ssid_len;
  1815. while (tmp--) {
  1816. if (ssid[tmp] != '\0') {
  1817. hidden_ssid = 0;
  1818. break;
  1819. }
  1820. }
  1821. if (hidden_ssid && (ifsta->ssid_len == ssid_len || ssid_len == 0))
  1822. return 1;
  1823. if (ssid_len == 1 && ssid[0] == ' ')
  1824. return 1;
  1825. return 0;
  1826. }
  1827. static int ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata,
  1828. struct ieee80211_if_sta *ifsta)
  1829. {
  1830. struct ieee80211_local *local = sdata->local;
  1831. struct ieee80211_bss *bss;
  1832. struct ieee80211_supported_band *sband;
  1833. u8 bssid[ETH_ALEN], *pos;
  1834. int i;
  1835. int ret;
  1836. if (sdata->u.sta.flags & IEEE80211_STA_BSSID_SET) {
  1837. memcpy(bssid, ifsta->bssid, ETH_ALEN);
  1838. } else {
  1839. /* Generate random, not broadcast, locally administered BSSID. Mix in
  1840. * own MAC address to make sure that devices that do not have proper
  1841. * random number generator get different BSSID. */
  1842. get_random_bytes(bssid, ETH_ALEN);
  1843. for (i = 0; i < ETH_ALEN; i++)
  1844. bssid[i] ^= sdata->dev->dev_addr[i];
  1845. bssid[0] &= ~0x01;
  1846. bssid[0] |= 0x02;
  1847. }
  1848. printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %pM\n",
  1849. sdata->dev->name, bssid);
  1850. bss = ieee80211_rx_bss_add(local, bssid,
  1851. local->hw.conf.channel->center_freq,
  1852. sdata->u.sta.ssid, sdata->u.sta.ssid_len);
  1853. if (!bss)
  1854. return -ENOMEM;
  1855. bss->band = local->hw.conf.channel->band;
  1856. sband = local->hw.wiphy->bands[bss->band];
  1857. if (local->hw.conf.beacon_int == 0)
  1858. local->hw.conf.beacon_int = 100;
  1859. bss->beacon_int = local->hw.conf.beacon_int;
  1860. bss->last_update = jiffies;
  1861. bss->capability = WLAN_CAPABILITY_IBSS;
  1862. if (sdata->default_key)
  1863. bss->capability |= WLAN_CAPABILITY_PRIVACY;
  1864. else
  1865. sdata->drop_unencrypted = 0;
  1866. bss->supp_rates_len = sband->n_bitrates;
  1867. pos = bss->supp_rates;
  1868. for (i = 0; i < sband->n_bitrates; i++) {
  1869. int rate = sband->bitrates[i].bitrate;
  1870. *pos++ = (u8) (rate / 5);
  1871. }
  1872. ret = ieee80211_sta_join_ibss(sdata, ifsta, bss);
  1873. ieee80211_rx_bss_put(local, bss);
  1874. return ret;
  1875. }
  1876. static int ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata,
  1877. struct ieee80211_if_sta *ifsta)
  1878. {
  1879. struct ieee80211_local *local = sdata->local;
  1880. struct ieee80211_bss *bss;
  1881. int found = 0;
  1882. u8 bssid[ETH_ALEN];
  1883. int active_ibss;
  1884. if (ifsta->ssid_len == 0)
  1885. return -EINVAL;
  1886. active_ibss = ieee80211_sta_active_ibss(sdata);
  1887. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  1888. printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
  1889. sdata->dev->name, active_ibss);
  1890. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  1891. spin_lock_bh(&local->bss_lock);
  1892. list_for_each_entry(bss, &local->bss_list, list) {
  1893. if (ifsta->ssid_len != bss->ssid_len ||
  1894. memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
  1895. || !(bss->capability & WLAN_CAPABILITY_IBSS))
  1896. continue;
  1897. if ((ifsta->flags & IEEE80211_STA_BSSID_SET) &&
  1898. memcmp(ifsta->bssid, bss->bssid, ETH_ALEN) != 0)
  1899. continue;
  1900. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  1901. printk(KERN_DEBUG " bssid=%pM found\n", bss->bssid);
  1902. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  1903. memcpy(bssid, bss->bssid, ETH_ALEN);
  1904. found = 1;
  1905. if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
  1906. break;
  1907. }
  1908. spin_unlock_bh(&local->bss_lock);
  1909. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  1910. if (found)
  1911. printk(KERN_DEBUG " sta_find_ibss: selected %pM current "
  1912. "%pM\n", bssid, ifsta->bssid);
  1913. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  1914. if (found &&
  1915. ((!(ifsta->flags & IEEE80211_STA_PREV_BSSID_SET)) ||
  1916. memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0)) {
  1917. int ret;
  1918. int search_freq;
  1919. if (ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL)
  1920. search_freq = bss->freq;
  1921. else
  1922. search_freq = local->hw.conf.channel->center_freq;
  1923. bss = ieee80211_rx_bss_get(local, bssid, search_freq,
  1924. ifsta->ssid, ifsta->ssid_len);
  1925. if (!bss)
  1926. goto dont_join;
  1927. printk(KERN_DEBUG "%s: Selected IBSS BSSID %pM"
  1928. " based on configured SSID\n",
  1929. sdata->dev->name, bssid);
  1930. ret = ieee80211_sta_join_ibss(sdata, ifsta, bss);
  1931. ieee80211_rx_bss_put(local, bss);
  1932. return ret;
  1933. }
  1934. dont_join:
  1935. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  1936. printk(KERN_DEBUG " did not try to join ibss\n");
  1937. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  1938. /* Selected IBSS not found in current scan results - try to scan */
  1939. if (ifsta->state == IEEE80211_STA_MLME_IBSS_JOINED &&
  1940. !ieee80211_sta_active_ibss(sdata)) {
  1941. mod_timer(&ifsta->timer, jiffies +
  1942. IEEE80211_IBSS_MERGE_INTERVAL);
  1943. } else if (time_after(jiffies, local->last_scan_completed +
  1944. IEEE80211_SCAN_INTERVAL)) {
  1945. printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
  1946. "join\n", sdata->dev->name);
  1947. return ieee80211_request_scan(sdata, ifsta->ssid,
  1948. ifsta->ssid_len);
  1949. } else if (ifsta->state != IEEE80211_STA_MLME_IBSS_JOINED) {
  1950. int interval = IEEE80211_SCAN_INTERVAL;
  1951. if (time_after(jiffies, ifsta->ibss_join_req +
  1952. IEEE80211_IBSS_JOIN_TIMEOUT)) {
  1953. if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
  1954. (!(local->oper_channel->flags &
  1955. IEEE80211_CHAN_NO_IBSS)))
  1956. return ieee80211_sta_create_ibss(sdata, ifsta);
  1957. if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
  1958. printk(KERN_DEBUG "%s: IBSS not allowed on"
  1959. " %d MHz\n", sdata->dev->name,
  1960. local->hw.conf.channel->center_freq);
  1961. }
  1962. /* No IBSS found - decrease scan interval and continue
  1963. * scanning. */
  1964. interval = IEEE80211_SCAN_INTERVAL_SLOW;
  1965. }
  1966. ifsta->state = IEEE80211_STA_MLME_IBSS_SEARCH;
  1967. mod_timer(&ifsta->timer, jiffies + interval);
  1968. return 0;
  1969. }
  1970. return 0;
  1971. }
  1972. static int ieee80211_sta_config_auth(struct ieee80211_sub_if_data *sdata,
  1973. struct ieee80211_if_sta *ifsta)
  1974. {
  1975. struct ieee80211_local *local = sdata->local;
  1976. struct ieee80211_bss *bss, *selected = NULL;
  1977. int top_rssi = 0, freq;
  1978. spin_lock_bh(&local->bss_lock);
  1979. freq = local->oper_channel->center_freq;
  1980. list_for_each_entry(bss, &local->bss_list, list) {
  1981. if (!(bss->capability & WLAN_CAPABILITY_ESS))
  1982. continue;
  1983. if ((ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
  1984. IEEE80211_STA_AUTO_BSSID_SEL |
  1985. IEEE80211_STA_AUTO_CHANNEL_SEL)) &&
  1986. (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
  1987. !!sdata->default_key))
  1988. continue;
  1989. if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
  1990. bss->freq != freq)
  1991. continue;
  1992. if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
  1993. memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
  1994. continue;
  1995. if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
  1996. !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
  1997. continue;
  1998. if (!selected || top_rssi < bss->signal) {
  1999. selected = bss;
  2000. top_rssi = bss->signal;
  2001. }
  2002. }
  2003. if (selected)
  2004. atomic_inc(&selected->users);
  2005. spin_unlock_bh(&local->bss_lock);
  2006. if (selected) {
  2007. ieee80211_set_freq(sdata, selected->freq);
  2008. if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
  2009. ieee80211_sta_set_ssid(sdata, selected->ssid,
  2010. selected->ssid_len);
  2011. ieee80211_sta_set_bssid(sdata, selected->bssid);
  2012. ieee80211_sta_def_wmm_params(sdata, selected);
  2013. if (sdata->u.sta.mfp == IEEE80211_MFP_REQUIRED)
  2014. sdata->u.sta.flags |= IEEE80211_STA_MFP_ENABLED;
  2015. else
  2016. sdata->u.sta.flags &= ~IEEE80211_STA_MFP_ENABLED;
  2017. /* Send out direct probe if no probe resp was received or
  2018. * the one we have is outdated
  2019. */
  2020. if (!selected->last_probe_resp ||
  2021. time_after(jiffies, selected->last_probe_resp
  2022. + IEEE80211_SCAN_RESULT_EXPIRE))
  2023. ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
  2024. else
  2025. ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
  2026. ieee80211_rx_bss_put(local, selected);
  2027. ieee80211_sta_reset_auth(sdata, ifsta);
  2028. return 0;
  2029. } else {
  2030. if (ifsta->assoc_scan_tries < IEEE80211_ASSOC_SCANS_MAX_TRIES) {
  2031. ifsta->assoc_scan_tries++;
  2032. if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
  2033. ieee80211_start_scan(sdata, NULL, 0);
  2034. else
  2035. ieee80211_start_scan(sdata, ifsta->ssid,
  2036. ifsta->ssid_len);
  2037. ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
  2038. set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
  2039. } else {
  2040. ifsta->assoc_scan_tries = 0;
  2041. ifsta->state = IEEE80211_STA_MLME_DISABLED;
  2042. }
  2043. }
  2044. return -1;
  2045. }
  2046. static void ieee80211_sta_work(struct work_struct *work)
  2047. {
  2048. struct ieee80211_sub_if_data *sdata =
  2049. container_of(work, struct ieee80211_sub_if_data, u.sta.work);
  2050. struct ieee80211_local *local = sdata->local;
  2051. struct ieee80211_if_sta *ifsta;
  2052. struct sk_buff *skb;
  2053. if (!netif_running(sdata->dev))
  2054. return;
  2055. if (local->sw_scanning || local->hw_scanning)
  2056. return;
  2057. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION &&
  2058. sdata->vif.type != NL80211_IFTYPE_ADHOC))
  2059. return;
  2060. ifsta = &sdata->u.sta;
  2061. while ((skb = skb_dequeue(&ifsta->skb_queue)))
  2062. ieee80211_sta_rx_queued_mgmt(sdata, skb);
  2063. if (ifsta->state != IEEE80211_STA_MLME_DIRECT_PROBE &&
  2064. ifsta->state != IEEE80211_STA_MLME_AUTHENTICATE &&
  2065. ifsta->state != IEEE80211_STA_MLME_ASSOCIATE &&
  2066. test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
  2067. ieee80211_start_scan(sdata, ifsta->scan_ssid,
  2068. ifsta->scan_ssid_len);
  2069. return;
  2070. }
  2071. if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
  2072. if (ieee80211_sta_config_auth(sdata, ifsta))
  2073. return;
  2074. clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
  2075. } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
  2076. return;
  2077. switch (ifsta->state) {
  2078. case IEEE80211_STA_MLME_DISABLED:
  2079. break;
  2080. case IEEE80211_STA_MLME_DIRECT_PROBE:
  2081. ieee80211_direct_probe(sdata, ifsta);
  2082. break;
  2083. case IEEE80211_STA_MLME_AUTHENTICATE:
  2084. ieee80211_authenticate(sdata, ifsta);
  2085. break;
  2086. case IEEE80211_STA_MLME_ASSOCIATE:
  2087. ieee80211_associate(sdata, ifsta);
  2088. break;
  2089. case IEEE80211_STA_MLME_ASSOCIATED:
  2090. ieee80211_associated(sdata, ifsta);
  2091. break;
  2092. case IEEE80211_STA_MLME_IBSS_SEARCH:
  2093. ieee80211_sta_find_ibss(sdata, ifsta);
  2094. break;
  2095. case IEEE80211_STA_MLME_IBSS_JOINED:
  2096. ieee80211_sta_merge_ibss(sdata, ifsta);
  2097. break;
  2098. default:
  2099. WARN_ON(1);
  2100. break;
  2101. }
  2102. if (ieee80211_privacy_mismatch(sdata, ifsta)) {
  2103. printk(KERN_DEBUG "%s: privacy configuration mismatch and "
  2104. "mixed-cell disabled - disassociate\n", sdata->dev->name);
  2105. ieee80211_set_disassoc(sdata, ifsta, false, true,
  2106. WLAN_REASON_UNSPECIFIED);
  2107. }
  2108. }
  2109. static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
  2110. {
  2111. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  2112. queue_work(sdata->local->hw.workqueue,
  2113. &sdata->u.sta.work);
  2114. }
  2115. /* interface setup */
  2116. void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
  2117. {
  2118. struct ieee80211_if_sta *ifsta;
  2119. ifsta = &sdata->u.sta;
  2120. INIT_WORK(&ifsta->work, ieee80211_sta_work);
  2121. INIT_WORK(&ifsta->chswitch_work, ieee80211_chswitch_work);
  2122. setup_timer(&ifsta->timer, ieee80211_sta_timer,
  2123. (unsigned long) sdata);
  2124. setup_timer(&ifsta->chswitch_timer, ieee80211_chswitch_timer,
  2125. (unsigned long) sdata);
  2126. skb_queue_head_init(&ifsta->skb_queue);
  2127. ifsta->capab = WLAN_CAPABILITY_ESS;
  2128. ifsta->auth_algs = IEEE80211_AUTH_ALG_OPEN |
  2129. IEEE80211_AUTH_ALG_SHARED_KEY;
  2130. ifsta->flags |= IEEE80211_STA_CREATE_IBSS |
  2131. IEEE80211_STA_AUTO_BSSID_SEL |
  2132. IEEE80211_STA_AUTO_CHANNEL_SEL;
  2133. if (ieee80211_num_regular_queues(&sdata->local->hw) >= 4)
  2134. ifsta->flags |= IEEE80211_STA_WMM_ENABLED;
  2135. }
  2136. /*
  2137. * Add a new IBSS station, will also be called by the RX code when,
  2138. * in IBSS mode, receiving a frame from a yet-unknown station, hence
  2139. * must be callable in atomic context.
  2140. */
  2141. struct sta_info *ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata,
  2142. u8 *bssid,u8 *addr, u32 supp_rates)
  2143. {
  2144. struct ieee80211_local *local = sdata->local;
  2145. struct sta_info *sta;
  2146. int band = local->hw.conf.channel->band;
  2147. /* TODO: Could consider removing the least recently used entry and
  2148. * allow new one to be added. */
  2149. if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
  2150. if (net_ratelimit()) {
  2151. printk(KERN_DEBUG "%s: No room for a new IBSS STA "
  2152. "entry %pM\n", sdata->dev->name, addr);
  2153. }
  2154. return NULL;
  2155. }
  2156. if (compare_ether_addr(bssid, sdata->u.sta.bssid))
  2157. return NULL;
  2158. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  2159. printk(KERN_DEBUG "%s: Adding new IBSS station %pM (dev=%s)\n",
  2160. wiphy_name(local->hw.wiphy), addr, sdata->dev->name);
  2161. #endif
  2162. sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
  2163. if (!sta)
  2164. return NULL;
  2165. set_sta_flags(sta, WLAN_STA_AUTHORIZED);
  2166. /* make sure mandatory rates are always added */
  2167. sta->sta.supp_rates[band] = supp_rates |
  2168. ieee80211_mandatory_rates(local, band);
  2169. rate_control_rate_init(sta);
  2170. if (sta_info_insert(sta))
  2171. return NULL;
  2172. return sta;
  2173. }
  2174. /* configuration hooks */
  2175. void ieee80211_sta_req_auth(struct ieee80211_sub_if_data *sdata,
  2176. struct ieee80211_if_sta *ifsta)
  2177. {
  2178. struct ieee80211_local *local = sdata->local;
  2179. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  2180. return;
  2181. if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
  2182. IEEE80211_STA_AUTO_BSSID_SEL)) &&
  2183. (ifsta->flags & (IEEE80211_STA_SSID_SET |
  2184. IEEE80211_STA_AUTO_SSID_SEL))) {
  2185. if (ifsta->state == IEEE80211_STA_MLME_ASSOCIATED)
  2186. ieee80211_set_disassoc(sdata, ifsta, true, true,
  2187. WLAN_REASON_DEAUTH_LEAVING);
  2188. set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
  2189. queue_work(local->hw.workqueue, &ifsta->work);
  2190. }
  2191. }
  2192. int ieee80211_sta_set_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t len)
  2193. {
  2194. struct ieee80211_if_sta *ifsta;
  2195. if (len > IEEE80211_MAX_SSID_LEN)
  2196. return -EINVAL;
  2197. ifsta = &sdata->u.sta;
  2198. if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0) {
  2199. memset(ifsta->ssid, 0, sizeof(ifsta->ssid));
  2200. memcpy(ifsta->ssid, ssid, len);
  2201. ifsta->ssid_len = len;
  2202. }
  2203. ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
  2204. if (len)
  2205. ifsta->flags |= IEEE80211_STA_SSID_SET;
  2206. else
  2207. ifsta->flags &= ~IEEE80211_STA_SSID_SET;
  2208. if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  2209. ifsta->ibss_join_req = jiffies;
  2210. ifsta->state = IEEE80211_STA_MLME_IBSS_SEARCH;
  2211. return ieee80211_sta_find_ibss(sdata, ifsta);
  2212. }
  2213. return 0;
  2214. }
  2215. int ieee80211_sta_get_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t *len)
  2216. {
  2217. struct ieee80211_if_sta *ifsta = &sdata->u.sta;
  2218. memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
  2219. *len = ifsta->ssid_len;
  2220. return 0;
  2221. }
  2222. int ieee80211_sta_set_bssid(struct ieee80211_sub_if_data *sdata, u8 *bssid)
  2223. {
  2224. struct ieee80211_if_sta *ifsta;
  2225. ifsta = &sdata->u.sta;
  2226. if (is_valid_ether_addr(bssid)) {
  2227. memcpy(ifsta->bssid, bssid, ETH_ALEN);
  2228. ifsta->flags |= IEEE80211_STA_BSSID_SET;
  2229. } else {
  2230. memset(ifsta->bssid, 0, ETH_ALEN);
  2231. ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
  2232. }
  2233. if (netif_running(sdata->dev)) {
  2234. if (ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID)) {
  2235. printk(KERN_DEBUG "%s: Failed to config new BSSID to "
  2236. "the low-level driver\n", sdata->dev->name);
  2237. }
  2238. }
  2239. return ieee80211_sta_set_ssid(sdata, ifsta->ssid, ifsta->ssid_len);
  2240. }
  2241. int ieee80211_sta_set_extra_ie(struct ieee80211_sub_if_data *sdata, char *ie, size_t len)
  2242. {
  2243. struct ieee80211_if_sta *ifsta = &sdata->u.sta;
  2244. kfree(ifsta->extra_ie);
  2245. if (len == 0) {
  2246. ifsta->extra_ie = NULL;
  2247. ifsta->extra_ie_len = 0;
  2248. return 0;
  2249. }
  2250. ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
  2251. if (!ifsta->extra_ie) {
  2252. ifsta->extra_ie_len = 0;
  2253. return -ENOMEM;
  2254. }
  2255. memcpy(ifsta->extra_ie, ie, len);
  2256. ifsta->extra_ie_len = len;
  2257. return 0;
  2258. }
  2259. int ieee80211_sta_deauthenticate(struct ieee80211_sub_if_data *sdata, u16 reason)
  2260. {
  2261. struct ieee80211_if_sta *ifsta = &sdata->u.sta;
  2262. printk(KERN_DEBUG "%s: deauthenticating by local choice (reason=%d)\n",
  2263. sdata->dev->name, reason);
  2264. if (sdata->vif.type != NL80211_IFTYPE_STATION &&
  2265. sdata->vif.type != NL80211_IFTYPE_ADHOC)
  2266. return -EINVAL;
  2267. ieee80211_set_disassoc(sdata, ifsta, true, true, reason);
  2268. return 0;
  2269. }
  2270. int ieee80211_sta_disassociate(struct ieee80211_sub_if_data *sdata, u16 reason)
  2271. {
  2272. struct ieee80211_if_sta *ifsta = &sdata->u.sta;
  2273. printk(KERN_DEBUG "%s: disassociating by local choice (reason=%d)\n",
  2274. sdata->dev->name, reason);
  2275. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  2276. return -EINVAL;
  2277. if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
  2278. return -1;
  2279. ieee80211_set_disassoc(sdata, ifsta, false, true, reason);
  2280. return 0;
  2281. }
  2282. /* scan finished notification */
  2283. void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
  2284. {
  2285. struct ieee80211_sub_if_data *sdata = local->scan_sdata;
  2286. struct ieee80211_if_sta *ifsta;
  2287. if (sdata && sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  2288. ifsta = &sdata->u.sta;
  2289. if ((!(ifsta->flags & IEEE80211_STA_PREV_BSSID_SET)) ||
  2290. !ieee80211_sta_active_ibss(sdata))
  2291. ieee80211_sta_find_ibss(sdata, ifsta);
  2292. }
  2293. /* Restart STA timers */
  2294. rcu_read_lock();
  2295. list_for_each_entry_rcu(sdata, &local->interfaces, list)
  2296. ieee80211_restart_sta_timer(sdata);
  2297. rcu_read_unlock();
  2298. }
  2299. void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
  2300. {
  2301. struct ieee80211_local *local =
  2302. container_of(work, struct ieee80211_local,
  2303. dynamic_ps_disable_work);
  2304. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  2305. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  2306. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  2307. }
  2308. ieee80211_wake_queues_by_reason(&local->hw,
  2309. IEEE80211_QUEUE_STOP_REASON_PS);
  2310. }
  2311. void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
  2312. {
  2313. struct ieee80211_local *local =
  2314. container_of(work, struct ieee80211_local,
  2315. dynamic_ps_enable_work);
  2316. struct ieee80211_sub_if_data *sdata = local->scan_sdata;
  2317. if (local->hw.conf.flags & IEEE80211_CONF_PS)
  2318. return;
  2319. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
  2320. ieee80211_send_nullfunc(local, sdata, 1);
  2321. local->hw.conf.flags |= IEEE80211_CONF_PS;
  2322. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  2323. }
  2324. void ieee80211_dynamic_ps_timer(unsigned long data)
  2325. {
  2326. struct ieee80211_local *local = (void *) data;
  2327. queue_work(local->hw.workqueue, &local->dynamic_ps_enable_work);
  2328. }