mlme.c 71 KB

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