mlme.c 72 KB

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