mlme.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112
  1. /*
  2. * BSS client mode implementation
  3. * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
  4. * Copyright 2004, Instant802 Networks, Inc.
  5. * Copyright 2005, Devicescape Software, Inc.
  6. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  7. * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/delay.h>
  14. #include <linux/if_ether.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/if_arp.h>
  17. #include <linux/etherdevice.h>
  18. #include <linux/rtnetlink.h>
  19. #include <net/mac80211.h>
  20. #include <asm/unaligned.h>
  21. #include "ieee80211_i.h"
  22. #include "rate.h"
  23. #include "led.h"
  24. #define IEEE80211_ASSOC_SCANS_MAX_TRIES 2
  25. #define IEEE80211_AUTH_TIMEOUT (HZ / 5)
  26. #define IEEE80211_AUTH_MAX_TRIES 3
  27. #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
  28. #define IEEE80211_ASSOC_MAX_TRIES 3
  29. #define IEEE80211_MONITORING_INTERVAL (2 * HZ)
  30. #define IEEE80211_PROBE_INTERVAL (60 * HZ)
  31. #define IEEE80211_RETRY_AUTH_INTERVAL (1 * HZ)
  32. /* utils */
  33. static int ecw2cw(int ecw)
  34. {
  35. return (1 << ecw) - 1;
  36. }
  37. static u8 *ieee80211_bss_get_ie(struct ieee80211_bss *bss, u8 ie)
  38. {
  39. u8 *end, *pos;
  40. pos = bss->cbss.information_elements;
  41. if (pos == NULL)
  42. return NULL;
  43. end = pos + bss->cbss.len_information_elements;
  44. while (pos + 1 < end) {
  45. if (pos + 2 + pos[1] > end)
  46. break;
  47. if (pos[0] == ie)
  48. return pos;
  49. pos += 2 + pos[1];
  50. }
  51. return NULL;
  52. }
  53. static int ieee80211_compatible_rates(struct ieee80211_bss *bss,
  54. struct ieee80211_supported_band *sband,
  55. u32 *rates)
  56. {
  57. int i, j, count;
  58. *rates = 0;
  59. count = 0;
  60. for (i = 0; i < bss->supp_rates_len; i++) {
  61. int rate = (bss->supp_rates[i] & 0x7F) * 5;
  62. for (j = 0; j < sband->n_bitrates; j++)
  63. if (sband->bitrates[j].bitrate == rate) {
  64. *rates |= BIT(j);
  65. count++;
  66. break;
  67. }
  68. }
  69. return count;
  70. }
  71. /* frame sending functions */
  72. static void add_extra_ies(struct sk_buff *skb, u8 *ies, size_t ies_len)
  73. {
  74. if (ies)
  75. memcpy(skb_put(skb, ies_len), ies, ies_len);
  76. }
  77. static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
  78. {
  79. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  80. struct ieee80211_local *local = sdata->local;
  81. struct sk_buff *skb;
  82. struct ieee80211_mgmt *mgmt;
  83. u8 *pos, *ies, *ht_ie, *e_ies;
  84. int i, len, count, rates_len, supp_rates_len;
  85. u16 capab;
  86. struct ieee80211_bss *bss;
  87. int wmm = 0;
  88. struct ieee80211_supported_band *sband;
  89. u32 rates = 0;
  90. size_t e_ies_len;
  91. if (ifmgd->flags & IEEE80211_STA_PREV_BSSID_SET) {
  92. e_ies = sdata->u.mgd.ie_reassocreq;
  93. e_ies_len = sdata->u.mgd.ie_reassocreq_len;
  94. } else {
  95. e_ies = sdata->u.mgd.ie_assocreq;
  96. e_ies_len = sdata->u.mgd.ie_assocreq_len;
  97. }
  98. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  99. sizeof(*mgmt) + 200 + ifmgd->extra_ie_len +
  100. ifmgd->ssid_len + e_ies_len);
  101. if (!skb) {
  102. printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
  103. "frame\n", sdata->dev->name);
  104. return;
  105. }
  106. skb_reserve(skb, local->hw.extra_tx_headroom);
  107. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  108. capab = ifmgd->capab;
  109. if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
  110. if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
  111. capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
  112. if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
  113. capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
  114. }
  115. bss = ieee80211_rx_bss_get(local, ifmgd->bssid,
  116. local->hw.conf.channel->center_freq,
  117. ifmgd->ssid, ifmgd->ssid_len);
  118. if (bss) {
  119. if (bss->cbss.capability & WLAN_CAPABILITY_PRIVACY)
  120. capab |= WLAN_CAPABILITY_PRIVACY;
  121. if (bss->wmm_used)
  122. wmm = 1;
  123. /* get all rates supported by the device and the AP as
  124. * some APs don't like getting a superset of their rates
  125. * in the association request (e.g. D-Link DAP 1353 in
  126. * b-only mode) */
  127. rates_len = ieee80211_compatible_rates(bss, sband, &rates);
  128. if ((bss->cbss.capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
  129. (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
  130. capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
  131. ieee80211_rx_bss_put(local, bss);
  132. } else {
  133. rates = ~0;
  134. rates_len = sband->n_bitrates;
  135. }
  136. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
  137. memset(mgmt, 0, 24);
  138. memcpy(mgmt->da, ifmgd->bssid, ETH_ALEN);
  139. memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
  140. memcpy(mgmt->bssid, ifmgd->bssid, ETH_ALEN);
  141. if (ifmgd->flags & IEEE80211_STA_PREV_BSSID_SET) {
  142. skb_put(skb, 10);
  143. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  144. IEEE80211_STYPE_REASSOC_REQ);
  145. mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
  146. mgmt->u.reassoc_req.listen_interval =
  147. cpu_to_le16(local->hw.conf.listen_interval);
  148. memcpy(mgmt->u.reassoc_req.current_ap, ifmgd->prev_bssid,
  149. ETH_ALEN);
  150. } else {
  151. skb_put(skb, 4);
  152. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  153. IEEE80211_STYPE_ASSOC_REQ);
  154. mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
  155. mgmt->u.assoc_req.listen_interval =
  156. cpu_to_le16(local->hw.conf.listen_interval);
  157. }
  158. /* SSID */
  159. ies = pos = skb_put(skb, 2 + ifmgd->ssid_len);
  160. *pos++ = WLAN_EID_SSID;
  161. *pos++ = ifmgd->ssid_len;
  162. memcpy(pos, ifmgd->ssid, ifmgd->ssid_len);
  163. /* add all rates which were marked to be used above */
  164. supp_rates_len = rates_len;
  165. if (supp_rates_len > 8)
  166. supp_rates_len = 8;
  167. len = sband->n_bitrates;
  168. pos = skb_put(skb, supp_rates_len + 2);
  169. *pos++ = WLAN_EID_SUPP_RATES;
  170. *pos++ = supp_rates_len;
  171. count = 0;
  172. for (i = 0; i < sband->n_bitrates; i++) {
  173. if (BIT(i) & rates) {
  174. int rate = sband->bitrates[i].bitrate;
  175. *pos++ = (u8) (rate / 5);
  176. if (++count == 8)
  177. break;
  178. }
  179. }
  180. if (rates_len > count) {
  181. pos = skb_put(skb, rates_len - count + 2);
  182. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  183. *pos++ = rates_len - count;
  184. for (i++; i < sband->n_bitrates; i++) {
  185. if (BIT(i) & rates) {
  186. int rate = sband->bitrates[i].bitrate;
  187. *pos++ = (u8) (rate / 5);
  188. }
  189. }
  190. }
  191. if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
  192. /* 1. power capabilities */
  193. pos = skb_put(skb, 4);
  194. *pos++ = WLAN_EID_PWR_CAPABILITY;
  195. *pos++ = 2;
  196. *pos++ = 0; /* min tx power */
  197. *pos++ = local->hw.conf.channel->max_power; /* max tx power */
  198. /* 2. supported channels */
  199. /* TODO: get this in reg domain format */
  200. pos = skb_put(skb, 2 * sband->n_channels + 2);
  201. *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
  202. *pos++ = 2 * sband->n_channels;
  203. for (i = 0; i < sband->n_channels; i++) {
  204. *pos++ = ieee80211_frequency_to_channel(
  205. sband->channels[i].center_freq);
  206. *pos++ = 1; /* one channel in the subband*/
  207. }
  208. }
  209. if (ifmgd->extra_ie) {
  210. pos = skb_put(skb, ifmgd->extra_ie_len);
  211. memcpy(pos, ifmgd->extra_ie, ifmgd->extra_ie_len);
  212. }
  213. if (wmm && (ifmgd->flags & IEEE80211_STA_WMM_ENABLED)) {
  214. pos = skb_put(skb, 9);
  215. *pos++ = WLAN_EID_VENDOR_SPECIFIC;
  216. *pos++ = 7; /* len */
  217. *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
  218. *pos++ = 0x50;
  219. *pos++ = 0xf2;
  220. *pos++ = 2; /* WME */
  221. *pos++ = 0; /* WME info */
  222. *pos++ = 1; /* WME ver */
  223. *pos++ = 0;
  224. }
  225. /* wmm support is a must to HT */
  226. /*
  227. * IEEE802.11n does not allow TKIP/WEP as pairwise
  228. * ciphers in HT mode. We still associate in non-ht
  229. * mode (11a/b/g) if any one of these ciphers is
  230. * configured as pairwise.
  231. */
  232. if (wmm && (ifmgd->flags & IEEE80211_STA_WMM_ENABLED) &&
  233. sband->ht_cap.ht_supported &&
  234. (ht_ie = ieee80211_bss_get_ie(bss, WLAN_EID_HT_INFORMATION)) &&
  235. ht_ie[1] >= sizeof(struct ieee80211_ht_info) &&
  236. (!(ifmgd->flags & IEEE80211_STA_TKIP_WEP_USED))) {
  237. struct ieee80211_ht_info *ht_info =
  238. (struct ieee80211_ht_info *)(ht_ie + 2);
  239. u16 cap = sband->ht_cap.cap;
  240. __le16 tmp;
  241. u32 flags = local->hw.conf.channel->flags;
  242. switch (ht_info->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  243. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  244. if (flags & IEEE80211_CHAN_NO_FAT_ABOVE) {
  245. cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  246. cap &= ~IEEE80211_HT_CAP_SGI_40;
  247. }
  248. break;
  249. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  250. if (flags & IEEE80211_CHAN_NO_FAT_BELOW) {
  251. cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  252. cap &= ~IEEE80211_HT_CAP_SGI_40;
  253. }
  254. break;
  255. }
  256. tmp = cpu_to_le16(cap);
  257. pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
  258. *pos++ = WLAN_EID_HT_CAPABILITY;
  259. *pos++ = sizeof(struct ieee80211_ht_cap);
  260. memset(pos, 0, sizeof(struct ieee80211_ht_cap));
  261. memcpy(pos, &tmp, sizeof(u16));
  262. pos += sizeof(u16);
  263. /* TODO: needs a define here for << 2 */
  264. *pos++ = sband->ht_cap.ampdu_factor |
  265. (sband->ht_cap.ampdu_density << 2);
  266. memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
  267. }
  268. add_extra_ies(skb, e_ies, e_ies_len);
  269. kfree(ifmgd->assocreq_ies);
  270. ifmgd->assocreq_ies_len = (skb->data + skb->len) - ies;
  271. ifmgd->assocreq_ies = kmalloc(ifmgd->assocreq_ies_len, GFP_KERNEL);
  272. if (ifmgd->assocreq_ies)
  273. memcpy(ifmgd->assocreq_ies, ies, ifmgd->assocreq_ies_len);
  274. ieee80211_tx_skb(sdata, skb, 0);
  275. }
  276. static void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
  277. u16 stype, u16 reason)
  278. {
  279. struct ieee80211_local *local = sdata->local;
  280. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  281. struct sk_buff *skb;
  282. struct ieee80211_mgmt *mgmt;
  283. u8 *ies;
  284. size_t ies_len;
  285. if (stype == IEEE80211_STYPE_DEAUTH) {
  286. ies = sdata->u.mgd.ie_deauth;
  287. ies_len = sdata->u.mgd.ie_deauth_len;
  288. } else {
  289. ies = sdata->u.mgd.ie_disassoc;
  290. ies_len = sdata->u.mgd.ie_disassoc_len;
  291. }
  292. skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) +
  293. ies_len);
  294. if (!skb) {
  295. printk(KERN_DEBUG "%s: failed to allocate buffer for "
  296. "deauth/disassoc frame\n", sdata->dev->name);
  297. return;
  298. }
  299. skb_reserve(skb, local->hw.extra_tx_headroom);
  300. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
  301. memset(mgmt, 0, 24);
  302. memcpy(mgmt->da, ifmgd->bssid, ETH_ALEN);
  303. memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
  304. memcpy(mgmt->bssid, ifmgd->bssid, ETH_ALEN);
  305. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
  306. skb_put(skb, 2);
  307. /* u.deauth.reason_code == u.disassoc.reason_code */
  308. mgmt->u.deauth.reason_code = cpu_to_le16(reason);
  309. add_extra_ies(skb, ies, ies_len);
  310. ieee80211_tx_skb(sdata, skb, ifmgd->flags & IEEE80211_STA_MFP_ENABLED);
  311. }
  312. void ieee80211_send_pspoll(struct ieee80211_local *local,
  313. struct ieee80211_sub_if_data *sdata)
  314. {
  315. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  316. struct ieee80211_pspoll *pspoll;
  317. struct sk_buff *skb;
  318. u16 fc;
  319. skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
  320. if (!skb) {
  321. printk(KERN_DEBUG "%s: failed to allocate buffer for "
  322. "pspoll frame\n", sdata->dev->name);
  323. return;
  324. }
  325. skb_reserve(skb, local->hw.extra_tx_headroom);
  326. pspoll = (struct ieee80211_pspoll *) skb_put(skb, sizeof(*pspoll));
  327. memset(pspoll, 0, sizeof(*pspoll));
  328. fc = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL | IEEE80211_FCTL_PM;
  329. pspoll->frame_control = cpu_to_le16(fc);
  330. pspoll->aid = cpu_to_le16(ifmgd->aid);
  331. /* aid in PS-Poll has its two MSBs each set to 1 */
  332. pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
  333. memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
  334. memcpy(pspoll->ta, sdata->dev->dev_addr, ETH_ALEN);
  335. ieee80211_tx_skb(sdata, skb, 0);
  336. }
  337. /* MLME */
  338. static void ieee80211_sta_wmm_params(struct ieee80211_local *local,
  339. struct ieee80211_if_managed *ifmgd,
  340. u8 *wmm_param, size_t wmm_param_len)
  341. {
  342. struct ieee80211_tx_queue_params params;
  343. size_t left;
  344. int count;
  345. u8 *pos;
  346. if (!(ifmgd->flags & IEEE80211_STA_WMM_ENABLED))
  347. return;
  348. if (!wmm_param)
  349. return;
  350. if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
  351. return;
  352. count = wmm_param[6] & 0x0f;
  353. if (count == ifmgd->wmm_last_param_set)
  354. return;
  355. ifmgd->wmm_last_param_set = count;
  356. pos = wmm_param + 8;
  357. left = wmm_param_len - 8;
  358. memset(&params, 0, sizeof(params));
  359. local->wmm_acm = 0;
  360. for (; left >= 4; left -= 4, pos += 4) {
  361. int aci = (pos[0] >> 5) & 0x03;
  362. int acm = (pos[0] >> 4) & 0x01;
  363. int queue;
  364. switch (aci) {
  365. case 1: /* AC_BK */
  366. queue = 3;
  367. if (acm)
  368. local->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
  369. break;
  370. case 2: /* AC_VI */
  371. queue = 1;
  372. if (acm)
  373. local->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
  374. break;
  375. case 3: /* AC_VO */
  376. queue = 0;
  377. if (acm)
  378. local->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
  379. break;
  380. case 0: /* AC_BE */
  381. default:
  382. queue = 2;
  383. if (acm)
  384. local->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
  385. break;
  386. }
  387. params.aifs = pos[0] & 0x0f;
  388. params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
  389. params.cw_min = ecw2cw(pos[1] & 0x0f);
  390. params.txop = get_unaligned_le16(pos + 2);
  391. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  392. printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
  393. "cWmin=%d cWmax=%d txop=%d\n",
  394. local->mdev->name, queue, aci, acm, params.aifs, params.cw_min,
  395. params.cw_max, params.txop);
  396. #endif
  397. if (local->ops->conf_tx &&
  398. local->ops->conf_tx(local_to_hw(local), queue, &params)) {
  399. printk(KERN_DEBUG "%s: failed to set TX queue "
  400. "parameters for queue %d\n", local->mdev->name, queue);
  401. }
  402. }
  403. }
  404. static bool ieee80211_check_tim(struct ieee802_11_elems *elems, u16 aid)
  405. {
  406. u8 mask;
  407. u8 index, indexn1, indexn2;
  408. struct ieee80211_tim_ie *tim = (struct ieee80211_tim_ie *) elems->tim;
  409. aid &= 0x3fff;
  410. index = aid / 8;
  411. mask = 1 << (aid & 7);
  412. indexn1 = tim->bitmap_ctrl & 0xfe;
  413. indexn2 = elems->tim_len + indexn1 - 4;
  414. if (index < indexn1 || index > indexn2)
  415. return false;
  416. index -= indexn1;
  417. return !!(tim->virtual_map[index] & mask);
  418. }
  419. static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
  420. u16 capab, bool erp_valid, u8 erp)
  421. {
  422. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  423. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  424. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  425. #endif
  426. u32 changed = 0;
  427. bool use_protection;
  428. bool use_short_preamble;
  429. bool use_short_slot;
  430. if (erp_valid) {
  431. use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
  432. use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
  433. } else {
  434. use_protection = false;
  435. use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
  436. }
  437. use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
  438. if (use_protection != bss_conf->use_cts_prot) {
  439. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  440. if (net_ratelimit()) {
  441. printk(KERN_DEBUG "%s: CTS protection %s (BSSID=%pM)\n",
  442. sdata->dev->name,
  443. use_protection ? "enabled" : "disabled",
  444. ifmgd->bssid);
  445. }
  446. #endif
  447. bss_conf->use_cts_prot = use_protection;
  448. changed |= BSS_CHANGED_ERP_CTS_PROT;
  449. }
  450. if (use_short_preamble != bss_conf->use_short_preamble) {
  451. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  452. if (net_ratelimit()) {
  453. printk(KERN_DEBUG "%s: switched to %s barker preamble"
  454. " (BSSID=%pM)\n",
  455. sdata->dev->name,
  456. use_short_preamble ? "short" : "long",
  457. ifmgd->bssid);
  458. }
  459. #endif
  460. bss_conf->use_short_preamble = use_short_preamble;
  461. changed |= BSS_CHANGED_ERP_PREAMBLE;
  462. }
  463. if (use_short_slot != bss_conf->use_short_slot) {
  464. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  465. if (net_ratelimit()) {
  466. printk(KERN_DEBUG "%s: switched to %s slot time"
  467. " (BSSID=%pM)\n",
  468. sdata->dev->name,
  469. use_short_slot ? "short" : "long",
  470. ifmgd->bssid);
  471. }
  472. #endif
  473. bss_conf->use_short_slot = use_short_slot;
  474. changed |= BSS_CHANGED_ERP_SLOT;
  475. }
  476. return changed;
  477. }
  478. static void ieee80211_sta_send_apinfo(struct ieee80211_sub_if_data *sdata)
  479. {
  480. union iwreq_data wrqu;
  481. memset(&wrqu, 0, sizeof(wrqu));
  482. if (sdata->u.mgd.flags & IEEE80211_STA_ASSOCIATED)
  483. memcpy(wrqu.ap_addr.sa_data, sdata->u.mgd.bssid, ETH_ALEN);
  484. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  485. wireless_send_event(sdata->dev, SIOCGIWAP, &wrqu, NULL);
  486. }
  487. static void ieee80211_sta_send_associnfo(struct ieee80211_sub_if_data *sdata)
  488. {
  489. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  490. char *buf;
  491. size_t len;
  492. int i;
  493. union iwreq_data wrqu;
  494. if (!ifmgd->assocreq_ies && !ifmgd->assocresp_ies)
  495. return;
  496. buf = kmalloc(50 + 2 * (ifmgd->assocreq_ies_len +
  497. ifmgd->assocresp_ies_len), GFP_KERNEL);
  498. if (!buf)
  499. return;
  500. len = sprintf(buf, "ASSOCINFO(");
  501. if (ifmgd->assocreq_ies) {
  502. len += sprintf(buf + len, "ReqIEs=");
  503. for (i = 0; i < ifmgd->assocreq_ies_len; i++) {
  504. len += sprintf(buf + len, "%02x",
  505. ifmgd->assocreq_ies[i]);
  506. }
  507. }
  508. if (ifmgd->assocresp_ies) {
  509. if (ifmgd->assocreq_ies)
  510. len += sprintf(buf + len, " ");
  511. len += sprintf(buf + len, "RespIEs=");
  512. for (i = 0; i < ifmgd->assocresp_ies_len; i++) {
  513. len += sprintf(buf + len, "%02x",
  514. ifmgd->assocresp_ies[i]);
  515. }
  516. }
  517. len += sprintf(buf + len, ")");
  518. if (len > IW_CUSTOM_MAX) {
  519. len = sprintf(buf, "ASSOCRESPIE=");
  520. for (i = 0; i < ifmgd->assocresp_ies_len; i++) {
  521. len += sprintf(buf + len, "%02x",
  522. ifmgd->assocresp_ies[i]);
  523. }
  524. }
  525. if (len <= IW_CUSTOM_MAX) {
  526. memset(&wrqu, 0, sizeof(wrqu));
  527. wrqu.data.length = len;
  528. wireless_send_event(sdata->dev, IWEVCUSTOM, &wrqu, buf);
  529. }
  530. kfree(buf);
  531. }
  532. static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
  533. u32 bss_info_changed)
  534. {
  535. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  536. struct ieee80211_local *local = sdata->local;
  537. struct ieee80211_conf *conf = &local_to_hw(local)->conf;
  538. struct ieee80211_bss *bss;
  539. bss_info_changed |= BSS_CHANGED_ASSOC;
  540. ifmgd->flags |= IEEE80211_STA_ASSOCIATED;
  541. bss = ieee80211_rx_bss_get(local, ifmgd->bssid,
  542. conf->channel->center_freq,
  543. ifmgd->ssid, ifmgd->ssid_len);
  544. if (bss) {
  545. /* set timing information */
  546. sdata->vif.bss_conf.beacon_int = bss->cbss.beacon_interval;
  547. sdata->vif.bss_conf.timestamp = bss->cbss.tsf;
  548. sdata->vif.bss_conf.dtim_period = bss->dtim_period;
  549. bss_info_changed |= ieee80211_handle_bss_capability(sdata,
  550. bss->cbss.capability, bss->has_erp_value, bss->erp_value);
  551. ieee80211_rx_bss_put(local, bss);
  552. }
  553. ifmgd->flags |= IEEE80211_STA_PREV_BSSID_SET;
  554. memcpy(ifmgd->prev_bssid, sdata->u.mgd.bssid, ETH_ALEN);
  555. ieee80211_sta_send_associnfo(sdata);
  556. ifmgd->last_probe = jiffies;
  557. ieee80211_led_assoc(local, 1);
  558. sdata->vif.bss_conf.assoc = 1;
  559. /*
  560. * For now just always ask the driver to update the basic rateset
  561. * when we have associated, we aren't checking whether it actually
  562. * changed or not.
  563. */
  564. bss_info_changed |= BSS_CHANGED_BASIC_RATES;
  565. ieee80211_bss_info_change_notify(sdata, bss_info_changed);
  566. if (local->powersave) {
  567. if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS) &&
  568. local->hw.conf.dynamic_ps_timeout > 0) {
  569. mod_timer(&local->dynamic_ps_timer, jiffies +
  570. msecs_to_jiffies(
  571. local->hw.conf.dynamic_ps_timeout));
  572. } else {
  573. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
  574. ieee80211_send_nullfunc(local, sdata, 1);
  575. conf->flags |= IEEE80211_CONF_PS;
  576. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  577. }
  578. }
  579. netif_tx_start_all_queues(sdata->dev);
  580. netif_carrier_on(sdata->dev);
  581. ieee80211_sta_send_apinfo(sdata);
  582. }
  583. static void ieee80211_direct_probe(struct ieee80211_sub_if_data *sdata)
  584. {
  585. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  586. struct ieee80211_local *local = sdata->local;
  587. ifmgd->direct_probe_tries++;
  588. if (ifmgd->direct_probe_tries > IEEE80211_AUTH_MAX_TRIES) {
  589. printk(KERN_DEBUG "%s: direct probe to AP %pM timed out\n",
  590. sdata->dev->name, ifmgd->bssid);
  591. ifmgd->state = IEEE80211_STA_MLME_DISABLED;
  592. ieee80211_sta_send_apinfo(sdata);
  593. /*
  594. * Most likely AP is not in the range so remove the
  595. * bss information associated to the AP
  596. */
  597. ieee80211_rx_bss_remove(sdata, ifmgd->bssid,
  598. sdata->local->hw.conf.channel->center_freq,
  599. ifmgd->ssid, ifmgd->ssid_len);
  600. /*
  601. * We might have a pending scan which had no chance to run yet
  602. * due to state == IEEE80211_STA_MLME_DIRECT_PROBE.
  603. * Hence, queue the STAs work again
  604. */
  605. queue_work(local->hw.workqueue, &ifmgd->work);
  606. return;
  607. }
  608. printk(KERN_DEBUG "%s: direct probe to AP %pM try %d\n",
  609. sdata->dev->name, ifmgd->bssid,
  610. ifmgd->direct_probe_tries);
  611. ifmgd->state = IEEE80211_STA_MLME_DIRECT_PROBE;
  612. set_bit(IEEE80211_STA_REQ_DIRECT_PROBE, &ifmgd->request);
  613. /* Direct probe is sent to broadcast address as some APs
  614. * will not answer to direct packet in unassociated state.
  615. */
  616. ieee80211_send_probe_req(sdata, NULL,
  617. ifmgd->ssid, ifmgd->ssid_len, NULL, 0);
  618. mod_timer(&ifmgd->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
  619. }
  620. static void ieee80211_authenticate(struct ieee80211_sub_if_data *sdata)
  621. {
  622. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  623. struct ieee80211_local *local = sdata->local;
  624. u8 *ies;
  625. size_t ies_len;
  626. ifmgd->auth_tries++;
  627. if (ifmgd->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
  628. printk(KERN_DEBUG "%s: authentication with AP %pM"
  629. " timed out\n",
  630. sdata->dev->name, ifmgd->bssid);
  631. ifmgd->state = IEEE80211_STA_MLME_DISABLED;
  632. ieee80211_sta_send_apinfo(sdata);
  633. ieee80211_rx_bss_remove(sdata, ifmgd->bssid,
  634. sdata->local->hw.conf.channel->center_freq,
  635. ifmgd->ssid, ifmgd->ssid_len);
  636. /*
  637. * We might have a pending scan which had no chance to run yet
  638. * due to state == IEEE80211_STA_MLME_AUTHENTICATE.
  639. * Hence, queue the STAs work again
  640. */
  641. queue_work(local->hw.workqueue, &ifmgd->work);
  642. return;
  643. }
  644. ifmgd->state = IEEE80211_STA_MLME_AUTHENTICATE;
  645. printk(KERN_DEBUG "%s: authenticate with AP %pM\n",
  646. sdata->dev->name, ifmgd->bssid);
  647. if (ifmgd->flags & IEEE80211_STA_EXT_SME) {
  648. ies = ifmgd->sme_auth_ie;
  649. ies_len = ifmgd->sme_auth_ie_len;
  650. } else {
  651. ies = NULL;
  652. ies_len = 0;
  653. }
  654. ieee80211_send_auth(sdata, 1, ifmgd->auth_alg, ies, ies_len,
  655. ifmgd->bssid, 0);
  656. ifmgd->auth_transaction = 2;
  657. mod_timer(&ifmgd->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
  658. }
  659. /*
  660. * The disassoc 'reason' argument can be either our own reason
  661. * if self disconnected or a reason code from the AP.
  662. */
  663. static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
  664. bool deauth, bool self_disconnected,
  665. u16 reason)
  666. {
  667. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  668. struct ieee80211_local *local = sdata->local;
  669. struct sta_info *sta;
  670. u32 changed = 0, config_changed = 0;
  671. rcu_read_lock();
  672. sta = sta_info_get(local, ifmgd->bssid);
  673. if (!sta) {
  674. rcu_read_unlock();
  675. return;
  676. }
  677. if (deauth) {
  678. ifmgd->direct_probe_tries = 0;
  679. ifmgd->auth_tries = 0;
  680. }
  681. ifmgd->assoc_scan_tries = 0;
  682. ifmgd->assoc_tries = 0;
  683. netif_tx_stop_all_queues(sdata->dev);
  684. netif_carrier_off(sdata->dev);
  685. ieee80211_sta_tear_down_BA_sessions(sta);
  686. if (self_disconnected) {
  687. if (deauth)
  688. ieee80211_send_deauth_disassoc(sdata,
  689. IEEE80211_STYPE_DEAUTH, reason);
  690. else
  691. ieee80211_send_deauth_disassoc(sdata,
  692. IEEE80211_STYPE_DISASSOC, reason);
  693. }
  694. ifmgd->flags &= ~IEEE80211_STA_ASSOCIATED;
  695. changed |= ieee80211_reset_erp_info(sdata);
  696. ieee80211_led_assoc(local, 0);
  697. changed |= BSS_CHANGED_ASSOC;
  698. sdata->vif.bss_conf.assoc = false;
  699. ieee80211_sta_send_apinfo(sdata);
  700. if (self_disconnected || reason == WLAN_REASON_DISASSOC_STA_HAS_LEFT) {
  701. ifmgd->state = IEEE80211_STA_MLME_DISABLED;
  702. ieee80211_rx_bss_remove(sdata, ifmgd->bssid,
  703. sdata->local->hw.conf.channel->center_freq,
  704. ifmgd->ssid, ifmgd->ssid_len);
  705. }
  706. rcu_read_unlock();
  707. /* channel(_type) changes are handled by ieee80211_hw_config */
  708. local->oper_channel_type = NL80211_CHAN_NO_HT;
  709. local->power_constr_level = 0;
  710. del_timer_sync(&local->dynamic_ps_timer);
  711. cancel_work_sync(&local->dynamic_ps_enable_work);
  712. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  713. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  714. config_changed |= IEEE80211_CONF_CHANGE_PS;
  715. }
  716. ieee80211_hw_config(local, config_changed);
  717. ieee80211_bss_info_change_notify(sdata, changed);
  718. rcu_read_lock();
  719. sta = sta_info_get(local, ifmgd->bssid);
  720. if (!sta) {
  721. rcu_read_unlock();
  722. return;
  723. }
  724. sta_info_unlink(&sta);
  725. rcu_read_unlock();
  726. sta_info_destroy(sta);
  727. }
  728. static int ieee80211_sta_wep_configured(struct ieee80211_sub_if_data *sdata)
  729. {
  730. if (!sdata || !sdata->default_key ||
  731. sdata->default_key->conf.alg != ALG_WEP)
  732. return 0;
  733. return 1;
  734. }
  735. static int ieee80211_privacy_mismatch(struct ieee80211_sub_if_data *sdata)
  736. {
  737. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  738. struct ieee80211_local *local = sdata->local;
  739. struct ieee80211_bss *bss;
  740. int bss_privacy;
  741. int wep_privacy;
  742. int privacy_invoked;
  743. if (!ifmgd || (ifmgd->flags & (IEEE80211_STA_MIXED_CELL |
  744. IEEE80211_STA_EXT_SME)))
  745. return 0;
  746. bss = ieee80211_rx_bss_get(local, ifmgd->bssid,
  747. local->hw.conf.channel->center_freq,
  748. ifmgd->ssid, ifmgd->ssid_len);
  749. if (!bss)
  750. return 0;
  751. bss_privacy = !!(bss->cbss.capability & WLAN_CAPABILITY_PRIVACY);
  752. wep_privacy = !!ieee80211_sta_wep_configured(sdata);
  753. privacy_invoked = !!(ifmgd->flags & IEEE80211_STA_PRIVACY_INVOKED);
  754. ieee80211_rx_bss_put(local, bss);
  755. if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
  756. return 0;
  757. return 1;
  758. }
  759. static void ieee80211_associate(struct ieee80211_sub_if_data *sdata)
  760. {
  761. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  762. struct ieee80211_local *local = sdata->local;
  763. ifmgd->assoc_tries++;
  764. if (ifmgd->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
  765. printk(KERN_DEBUG "%s: association with AP %pM"
  766. " timed out\n",
  767. sdata->dev->name, ifmgd->bssid);
  768. ifmgd->state = IEEE80211_STA_MLME_DISABLED;
  769. ieee80211_sta_send_apinfo(sdata);
  770. ieee80211_rx_bss_remove(sdata, ifmgd->bssid,
  771. sdata->local->hw.conf.channel->center_freq,
  772. ifmgd->ssid, ifmgd->ssid_len);
  773. /*
  774. * We might have a pending scan which had no chance to run yet
  775. * due to state == IEEE80211_STA_MLME_ASSOCIATE.
  776. * Hence, queue the STAs work again
  777. */
  778. queue_work(local->hw.workqueue, &ifmgd->work);
  779. return;
  780. }
  781. ifmgd->state = IEEE80211_STA_MLME_ASSOCIATE;
  782. printk(KERN_DEBUG "%s: associate with AP %pM\n",
  783. sdata->dev->name, ifmgd->bssid);
  784. if (ieee80211_privacy_mismatch(sdata)) {
  785. printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
  786. "mixed-cell disabled - abort association\n", sdata->dev->name);
  787. ifmgd->state = IEEE80211_STA_MLME_DISABLED;
  788. return;
  789. }
  790. ieee80211_send_assoc(sdata);
  791. mod_timer(&ifmgd->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
  792. }
  793. static void ieee80211_associated(struct ieee80211_sub_if_data *sdata)
  794. {
  795. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  796. struct ieee80211_local *local = sdata->local;
  797. struct sta_info *sta;
  798. int disassoc;
  799. /* TODO: start monitoring current AP signal quality and number of
  800. * missed beacons. Scan other channels every now and then and search
  801. * for better APs. */
  802. /* TODO: remove expired BSSes */
  803. ifmgd->state = IEEE80211_STA_MLME_ASSOCIATED;
  804. rcu_read_lock();
  805. sta = sta_info_get(local, ifmgd->bssid);
  806. if (!sta) {
  807. printk(KERN_DEBUG "%s: No STA entry for own AP %pM\n",
  808. sdata->dev->name, ifmgd->bssid);
  809. disassoc = 1;
  810. } else {
  811. disassoc = 0;
  812. if (time_after(jiffies,
  813. sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
  814. if (ifmgd->flags & IEEE80211_STA_PROBEREQ_POLL) {
  815. printk(KERN_DEBUG "%s: No ProbeResp from "
  816. "current AP %pM - assume out of "
  817. "range\n",
  818. sdata->dev->name, ifmgd->bssid);
  819. disassoc = 1;
  820. } else
  821. ieee80211_send_probe_req(sdata, ifmgd->bssid,
  822. ifmgd->ssid,
  823. ifmgd->ssid_len,
  824. NULL, 0);
  825. ifmgd->flags ^= IEEE80211_STA_PROBEREQ_POLL;
  826. } else {
  827. ifmgd->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
  828. if (time_after(jiffies, ifmgd->last_probe +
  829. IEEE80211_PROBE_INTERVAL)) {
  830. ifmgd->last_probe = jiffies;
  831. ieee80211_send_probe_req(sdata, ifmgd->bssid,
  832. ifmgd->ssid,
  833. ifmgd->ssid_len,
  834. NULL, 0);
  835. }
  836. }
  837. }
  838. rcu_read_unlock();
  839. if (disassoc)
  840. ieee80211_set_disassoc(sdata, true, true,
  841. WLAN_REASON_PREV_AUTH_NOT_VALID);
  842. else
  843. mod_timer(&ifmgd->timer, jiffies +
  844. IEEE80211_MONITORING_INTERVAL);
  845. }
  846. static void ieee80211_auth_completed(struct ieee80211_sub_if_data *sdata)
  847. {
  848. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  849. printk(KERN_DEBUG "%s: authenticated\n", sdata->dev->name);
  850. ifmgd->flags |= IEEE80211_STA_AUTHENTICATED;
  851. if (ifmgd->flags & IEEE80211_STA_EXT_SME) {
  852. /* Wait for SME to request association */
  853. ifmgd->state = IEEE80211_STA_MLME_DISABLED;
  854. } else
  855. ieee80211_associate(sdata);
  856. }
  857. static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
  858. struct ieee80211_mgmt *mgmt,
  859. size_t len)
  860. {
  861. u8 *pos;
  862. struct ieee802_11_elems elems;
  863. pos = mgmt->u.auth.variable;
  864. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  865. if (!elems.challenge)
  866. return;
  867. ieee80211_send_auth(sdata, 3, sdata->u.mgd.auth_alg,
  868. elems.challenge - 2, elems.challenge_len + 2,
  869. sdata->u.mgd.bssid, 1);
  870. sdata->u.mgd.auth_transaction = 4;
  871. }
  872. static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
  873. struct ieee80211_mgmt *mgmt,
  874. size_t len)
  875. {
  876. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  877. u16 auth_alg, auth_transaction, status_code;
  878. if (ifmgd->state != IEEE80211_STA_MLME_AUTHENTICATE)
  879. return;
  880. if (len < 24 + 6)
  881. return;
  882. if (memcmp(ifmgd->bssid, mgmt->sa, ETH_ALEN) != 0)
  883. return;
  884. if (memcmp(ifmgd->bssid, mgmt->bssid, ETH_ALEN) != 0)
  885. return;
  886. auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
  887. auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
  888. status_code = le16_to_cpu(mgmt->u.auth.status_code);
  889. if (auth_alg != ifmgd->auth_alg ||
  890. auth_transaction != ifmgd->auth_transaction)
  891. return;
  892. if (status_code != WLAN_STATUS_SUCCESS) {
  893. if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
  894. u8 algs[3];
  895. const int num_algs = ARRAY_SIZE(algs);
  896. int i, pos;
  897. algs[0] = algs[1] = algs[2] = 0xff;
  898. if (ifmgd->auth_algs & IEEE80211_AUTH_ALG_OPEN)
  899. algs[0] = WLAN_AUTH_OPEN;
  900. if (ifmgd->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
  901. algs[1] = WLAN_AUTH_SHARED_KEY;
  902. if (ifmgd->auth_algs & IEEE80211_AUTH_ALG_LEAP)
  903. algs[2] = WLAN_AUTH_LEAP;
  904. if (ifmgd->auth_alg == WLAN_AUTH_OPEN)
  905. pos = 0;
  906. else if (ifmgd->auth_alg == WLAN_AUTH_SHARED_KEY)
  907. pos = 1;
  908. else
  909. pos = 2;
  910. for (i = 0; i < num_algs; i++) {
  911. pos++;
  912. if (pos >= num_algs)
  913. pos = 0;
  914. if (algs[pos] == ifmgd->auth_alg ||
  915. algs[pos] == 0xff)
  916. continue;
  917. if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
  918. !ieee80211_sta_wep_configured(sdata))
  919. continue;
  920. ifmgd->auth_alg = algs[pos];
  921. break;
  922. }
  923. }
  924. return;
  925. }
  926. switch (ifmgd->auth_alg) {
  927. case WLAN_AUTH_OPEN:
  928. case WLAN_AUTH_LEAP:
  929. case WLAN_AUTH_FT:
  930. ieee80211_auth_completed(sdata);
  931. cfg80211_send_rx_auth(sdata->dev, (u8 *) mgmt, len);
  932. break;
  933. case WLAN_AUTH_SHARED_KEY:
  934. if (ifmgd->auth_transaction == 4) {
  935. ieee80211_auth_completed(sdata);
  936. cfg80211_send_rx_auth(sdata->dev, (u8 *) mgmt, len);
  937. } else
  938. ieee80211_auth_challenge(sdata, mgmt, len);
  939. break;
  940. }
  941. }
  942. static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
  943. struct ieee80211_mgmt *mgmt,
  944. size_t len)
  945. {
  946. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  947. u16 reason_code;
  948. if (len < 24 + 2)
  949. return;
  950. if (memcmp(ifmgd->bssid, mgmt->sa, ETH_ALEN))
  951. return;
  952. reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
  953. if (ifmgd->flags & IEEE80211_STA_AUTHENTICATED)
  954. printk(KERN_DEBUG "%s: deauthenticated (Reason: %u)\n",
  955. sdata->dev->name, reason_code);
  956. if (!(ifmgd->flags & IEEE80211_STA_EXT_SME) &&
  957. (ifmgd->state == IEEE80211_STA_MLME_AUTHENTICATE ||
  958. ifmgd->state == IEEE80211_STA_MLME_ASSOCIATE ||
  959. ifmgd->state == IEEE80211_STA_MLME_ASSOCIATED)) {
  960. ifmgd->state = IEEE80211_STA_MLME_DIRECT_PROBE;
  961. mod_timer(&ifmgd->timer, jiffies +
  962. IEEE80211_RETRY_AUTH_INTERVAL);
  963. }
  964. ieee80211_set_disassoc(sdata, true, false, 0);
  965. ifmgd->flags &= ~IEEE80211_STA_AUTHENTICATED;
  966. cfg80211_send_rx_deauth(sdata->dev, (u8 *) mgmt, len);
  967. }
  968. static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
  969. struct ieee80211_mgmt *mgmt,
  970. size_t len)
  971. {
  972. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  973. u16 reason_code;
  974. if (len < 24 + 2)
  975. return;
  976. if (memcmp(ifmgd->bssid, mgmt->sa, ETH_ALEN))
  977. return;
  978. reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
  979. if (ifmgd->flags & IEEE80211_STA_ASSOCIATED)
  980. printk(KERN_DEBUG "%s: disassociated (Reason: %u)\n",
  981. sdata->dev->name, reason_code);
  982. if (!(ifmgd->flags & IEEE80211_STA_EXT_SME) &&
  983. ifmgd->state == IEEE80211_STA_MLME_ASSOCIATED) {
  984. ifmgd->state = IEEE80211_STA_MLME_ASSOCIATE;
  985. mod_timer(&ifmgd->timer, jiffies +
  986. IEEE80211_RETRY_AUTH_INTERVAL);
  987. }
  988. ieee80211_set_disassoc(sdata, false, false, reason_code);
  989. cfg80211_send_rx_disassoc(sdata->dev, (u8 *) mgmt, len);
  990. }
  991. static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
  992. struct ieee80211_mgmt *mgmt,
  993. size_t len,
  994. int reassoc)
  995. {
  996. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  997. struct ieee80211_local *local = sdata->local;
  998. struct ieee80211_supported_band *sband;
  999. struct sta_info *sta;
  1000. u32 rates, basic_rates;
  1001. u16 capab_info, status_code, aid;
  1002. struct ieee802_11_elems elems;
  1003. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  1004. u8 *pos;
  1005. u32 changed = 0;
  1006. int i, j;
  1007. bool have_higher_than_11mbit = false, newsta = false;
  1008. u16 ap_ht_cap_flags;
  1009. /* AssocResp and ReassocResp have identical structure, so process both
  1010. * of them in this function. */
  1011. if (ifmgd->state != IEEE80211_STA_MLME_ASSOCIATE)
  1012. return;
  1013. if (len < 24 + 6)
  1014. return;
  1015. if (memcmp(ifmgd->bssid, mgmt->sa, ETH_ALEN) != 0)
  1016. return;
  1017. capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
  1018. status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
  1019. aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
  1020. printk(KERN_DEBUG "%s: RX %sssocResp from %pM (capab=0x%x "
  1021. "status=%d aid=%d)\n",
  1022. sdata->dev->name, reassoc ? "Rea" : "A", mgmt->sa,
  1023. capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
  1024. pos = mgmt->u.assoc_resp.variable;
  1025. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  1026. if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
  1027. elems.timeout_int && elems.timeout_int_len == 5 &&
  1028. elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
  1029. u32 tu, ms;
  1030. tu = get_unaligned_le32(elems.timeout_int + 1);
  1031. ms = tu * 1024 / 1000;
  1032. printk(KERN_DEBUG "%s: AP rejected association temporarily; "
  1033. "comeback duration %u TU (%u ms)\n",
  1034. sdata->dev->name, tu, ms);
  1035. if (ms > IEEE80211_ASSOC_TIMEOUT)
  1036. mod_timer(&ifmgd->timer,
  1037. jiffies + msecs_to_jiffies(ms));
  1038. return;
  1039. }
  1040. if (status_code != WLAN_STATUS_SUCCESS) {
  1041. printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
  1042. sdata->dev->name, status_code);
  1043. /* if this was a reassociation, ensure we try a "full"
  1044. * association next time. This works around some broken APs
  1045. * which do not correctly reject reassociation requests. */
  1046. ifmgd->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
  1047. return;
  1048. }
  1049. if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
  1050. printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
  1051. "set\n", sdata->dev->name, aid);
  1052. aid &= ~(BIT(15) | BIT(14));
  1053. if (!elems.supp_rates) {
  1054. printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
  1055. sdata->dev->name);
  1056. return;
  1057. }
  1058. printk(KERN_DEBUG "%s: associated\n", sdata->dev->name);
  1059. ifmgd->aid = aid;
  1060. ifmgd->ap_capab = capab_info;
  1061. kfree(ifmgd->assocresp_ies);
  1062. ifmgd->assocresp_ies_len = len - (pos - (u8 *) mgmt);
  1063. ifmgd->assocresp_ies = kmalloc(ifmgd->assocresp_ies_len, GFP_KERNEL);
  1064. if (ifmgd->assocresp_ies)
  1065. memcpy(ifmgd->assocresp_ies, pos, ifmgd->assocresp_ies_len);
  1066. rcu_read_lock();
  1067. /* Add STA entry for the AP */
  1068. sta = sta_info_get(local, ifmgd->bssid);
  1069. if (!sta) {
  1070. newsta = true;
  1071. sta = sta_info_alloc(sdata, ifmgd->bssid, GFP_ATOMIC);
  1072. if (!sta) {
  1073. printk(KERN_DEBUG "%s: failed to alloc STA entry for"
  1074. " the AP\n", sdata->dev->name);
  1075. rcu_read_unlock();
  1076. return;
  1077. }
  1078. /* update new sta with its last rx activity */
  1079. sta->last_rx = jiffies;
  1080. }
  1081. /*
  1082. * FIXME: Do we really need to update the sta_info's information here?
  1083. * We already know about the AP (we found it in our list) so it
  1084. * should already be filled with the right info, no?
  1085. * As is stands, all this is racy because typically we assume
  1086. * the information that is filled in here (except flags) doesn't
  1087. * change while a STA structure is alive. As such, it should move
  1088. * to between the sta_info_alloc() and sta_info_insert() above.
  1089. */
  1090. set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
  1091. WLAN_STA_AUTHORIZED);
  1092. rates = 0;
  1093. basic_rates = 0;
  1094. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  1095. for (i = 0; i < elems.supp_rates_len; i++) {
  1096. int rate = (elems.supp_rates[i] & 0x7f) * 5;
  1097. bool is_basic = !!(elems.supp_rates[i] & 0x80);
  1098. if (rate > 110)
  1099. have_higher_than_11mbit = true;
  1100. for (j = 0; j < sband->n_bitrates; j++) {
  1101. if (sband->bitrates[j].bitrate == rate) {
  1102. rates |= BIT(j);
  1103. if (is_basic)
  1104. basic_rates |= BIT(j);
  1105. break;
  1106. }
  1107. }
  1108. }
  1109. for (i = 0; i < elems.ext_supp_rates_len; i++) {
  1110. int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
  1111. bool is_basic = !!(elems.supp_rates[i] & 0x80);
  1112. if (rate > 110)
  1113. have_higher_than_11mbit = true;
  1114. for (j = 0; j < sband->n_bitrates; j++) {
  1115. if (sband->bitrates[j].bitrate == rate) {
  1116. rates |= BIT(j);
  1117. if (is_basic)
  1118. basic_rates |= BIT(j);
  1119. break;
  1120. }
  1121. }
  1122. }
  1123. sta->sta.supp_rates[local->hw.conf.channel->band] = rates;
  1124. sdata->vif.bss_conf.basic_rates = basic_rates;
  1125. /* cf. IEEE 802.11 9.2.12 */
  1126. if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
  1127. have_higher_than_11mbit)
  1128. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  1129. else
  1130. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  1131. /* If TKIP/WEP is used, no need to parse AP's HT capabilities */
  1132. if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_TKIP_WEP_USED))
  1133. ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
  1134. elems.ht_cap_elem, &sta->sta.ht_cap);
  1135. ap_ht_cap_flags = sta->sta.ht_cap.cap;
  1136. rate_control_rate_init(sta);
  1137. if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
  1138. set_sta_flags(sta, WLAN_STA_MFP);
  1139. if (elems.wmm_param)
  1140. set_sta_flags(sta, WLAN_STA_WME);
  1141. if (newsta) {
  1142. int err = sta_info_insert(sta);
  1143. if (err) {
  1144. printk(KERN_DEBUG "%s: failed to insert STA entry for"
  1145. " the AP (error %d)\n", sdata->dev->name, err);
  1146. rcu_read_unlock();
  1147. return;
  1148. }
  1149. }
  1150. rcu_read_unlock();
  1151. if (elems.wmm_param)
  1152. ieee80211_sta_wmm_params(local, ifmgd, elems.wmm_param,
  1153. elems.wmm_param_len);
  1154. if (elems.ht_info_elem && elems.wmm_param &&
  1155. (ifmgd->flags & IEEE80211_STA_WMM_ENABLED) &&
  1156. !(ifmgd->flags & IEEE80211_STA_TKIP_WEP_USED))
  1157. changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
  1158. ap_ht_cap_flags);
  1159. /* set AID and assoc capability,
  1160. * ieee80211_set_associated() will tell the driver */
  1161. bss_conf->aid = aid;
  1162. bss_conf->assoc_capability = capab_info;
  1163. ieee80211_set_associated(sdata, changed);
  1164. ieee80211_associated(sdata);
  1165. cfg80211_send_rx_assoc(sdata->dev, (u8 *) mgmt, len);
  1166. }
  1167. static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
  1168. struct ieee80211_mgmt *mgmt,
  1169. size_t len,
  1170. struct ieee80211_rx_status *rx_status,
  1171. struct ieee802_11_elems *elems,
  1172. bool beacon)
  1173. {
  1174. struct ieee80211_local *local = sdata->local;
  1175. int freq;
  1176. struct ieee80211_bss *bss;
  1177. struct ieee80211_channel *channel;
  1178. if (elems->ds_params && elems->ds_params_len == 1)
  1179. freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
  1180. else
  1181. freq = rx_status->freq;
  1182. channel = ieee80211_get_channel(local->hw.wiphy, freq);
  1183. if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
  1184. return;
  1185. bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
  1186. channel, beacon);
  1187. if (!bss)
  1188. return;
  1189. if (elems->ch_switch_elem && (elems->ch_switch_elem_len == 3) &&
  1190. (memcmp(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN) == 0)) {
  1191. struct ieee80211_channel_sw_ie *sw_elem =
  1192. (struct ieee80211_channel_sw_ie *)elems->ch_switch_elem;
  1193. ieee80211_process_chanswitch(sdata, sw_elem, bss);
  1194. }
  1195. ieee80211_rx_bss_put(local, bss);
  1196. }
  1197. static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
  1198. struct ieee80211_mgmt *mgmt,
  1199. size_t len,
  1200. struct ieee80211_rx_status *rx_status)
  1201. {
  1202. size_t baselen;
  1203. struct ieee802_11_elems elems;
  1204. if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
  1205. return; /* ignore ProbeResp to foreign address */
  1206. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  1207. if (baselen > len)
  1208. return;
  1209. ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
  1210. &elems);
  1211. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
  1212. /* direct probe may be part of the association flow */
  1213. if (test_and_clear_bit(IEEE80211_STA_REQ_DIRECT_PROBE,
  1214. &sdata->u.mgd.request)) {
  1215. printk(KERN_DEBUG "%s direct probe responded\n",
  1216. sdata->dev->name);
  1217. ieee80211_authenticate(sdata);
  1218. }
  1219. }
  1220. static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
  1221. struct ieee80211_mgmt *mgmt,
  1222. size_t len,
  1223. struct ieee80211_rx_status *rx_status)
  1224. {
  1225. struct ieee80211_if_managed *ifmgd;
  1226. size_t baselen;
  1227. struct ieee802_11_elems elems;
  1228. struct ieee80211_local *local = sdata->local;
  1229. u32 changed = 0;
  1230. bool erp_valid, directed_tim;
  1231. u8 erp_value = 0;
  1232. /* Process beacon from the current BSS */
  1233. baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
  1234. if (baselen > len)
  1235. return;
  1236. ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
  1237. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, true);
  1238. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1239. return;
  1240. ifmgd = &sdata->u.mgd;
  1241. if (!(ifmgd->flags & IEEE80211_STA_ASSOCIATED) ||
  1242. memcmp(ifmgd->bssid, mgmt->bssid, ETH_ALEN) != 0)
  1243. return;
  1244. if (rx_status->freq != local->hw.conf.channel->center_freq)
  1245. return;
  1246. ieee80211_sta_wmm_params(local, ifmgd, elems.wmm_param,
  1247. elems.wmm_param_len);
  1248. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
  1249. directed_tim = ieee80211_check_tim(&elems, ifmgd->aid);
  1250. if (directed_tim) {
  1251. if (local->hw.conf.dynamic_ps_timeout > 0) {
  1252. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  1253. ieee80211_hw_config(local,
  1254. IEEE80211_CONF_CHANGE_PS);
  1255. ieee80211_send_nullfunc(local, sdata, 0);
  1256. } else {
  1257. local->pspolling = true;
  1258. /*
  1259. * Here is assumed that the driver will be
  1260. * able to send ps-poll frame and receive a
  1261. * response even though power save mode is
  1262. * enabled, but some drivers might require
  1263. * to disable power save here. This needs
  1264. * to be investigated.
  1265. */
  1266. ieee80211_send_pspoll(local, sdata);
  1267. }
  1268. }
  1269. }
  1270. if (elems.erp_info && elems.erp_info_len >= 1) {
  1271. erp_valid = true;
  1272. erp_value = elems.erp_info[0];
  1273. } else {
  1274. erp_valid = false;
  1275. }
  1276. changed |= ieee80211_handle_bss_capability(sdata,
  1277. le16_to_cpu(mgmt->u.beacon.capab_info),
  1278. erp_valid, erp_value);
  1279. if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
  1280. !(ifmgd->flags & IEEE80211_STA_TKIP_WEP_USED)) {
  1281. struct sta_info *sta;
  1282. struct ieee80211_supported_band *sband;
  1283. u16 ap_ht_cap_flags;
  1284. rcu_read_lock();
  1285. sta = sta_info_get(local, ifmgd->bssid);
  1286. if (!sta) {
  1287. rcu_read_unlock();
  1288. return;
  1289. }
  1290. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  1291. ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
  1292. elems.ht_cap_elem, &sta->sta.ht_cap);
  1293. ap_ht_cap_flags = sta->sta.ht_cap.cap;
  1294. rcu_read_unlock();
  1295. changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
  1296. ap_ht_cap_flags);
  1297. }
  1298. if (elems.country_elem) {
  1299. /* Note we are only reviewing this on beacons
  1300. * for the BSSID we are associated to */
  1301. regulatory_hint_11d(local->hw.wiphy,
  1302. elems.country_elem, elems.country_elem_len);
  1303. /* TODO: IBSS also needs this */
  1304. if (elems.pwr_constr_elem)
  1305. ieee80211_handle_pwr_constr(sdata,
  1306. le16_to_cpu(mgmt->u.probe_resp.capab_info),
  1307. elems.pwr_constr_elem,
  1308. elems.pwr_constr_elem_len);
  1309. }
  1310. ieee80211_bss_info_change_notify(sdata, changed);
  1311. }
  1312. ieee80211_rx_result ieee80211_sta_rx_mgmt(struct ieee80211_sub_if_data *sdata,
  1313. struct sk_buff *skb,
  1314. struct ieee80211_rx_status *rx_status)
  1315. {
  1316. struct ieee80211_local *local = sdata->local;
  1317. struct ieee80211_mgmt *mgmt;
  1318. u16 fc;
  1319. if (skb->len < 24)
  1320. return RX_DROP_MONITOR;
  1321. mgmt = (struct ieee80211_mgmt *) skb->data;
  1322. fc = le16_to_cpu(mgmt->frame_control);
  1323. switch (fc & IEEE80211_FCTL_STYPE) {
  1324. case IEEE80211_STYPE_PROBE_REQ:
  1325. case IEEE80211_STYPE_PROBE_RESP:
  1326. case IEEE80211_STYPE_BEACON:
  1327. memcpy(skb->cb, rx_status, sizeof(*rx_status));
  1328. case IEEE80211_STYPE_AUTH:
  1329. case IEEE80211_STYPE_ASSOC_RESP:
  1330. case IEEE80211_STYPE_REASSOC_RESP:
  1331. case IEEE80211_STYPE_DEAUTH:
  1332. case IEEE80211_STYPE_DISASSOC:
  1333. skb_queue_tail(&sdata->u.mgd.skb_queue, skb);
  1334. queue_work(local->hw.workqueue, &sdata->u.mgd.work);
  1335. return RX_QUEUED;
  1336. }
  1337. return RX_DROP_MONITOR;
  1338. }
  1339. static void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
  1340. struct sk_buff *skb)
  1341. {
  1342. struct ieee80211_rx_status *rx_status;
  1343. struct ieee80211_mgmt *mgmt;
  1344. u16 fc;
  1345. rx_status = (struct ieee80211_rx_status *) skb->cb;
  1346. mgmt = (struct ieee80211_mgmt *) skb->data;
  1347. fc = le16_to_cpu(mgmt->frame_control);
  1348. switch (fc & IEEE80211_FCTL_STYPE) {
  1349. case IEEE80211_STYPE_PROBE_RESP:
  1350. ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len,
  1351. rx_status);
  1352. break;
  1353. case IEEE80211_STYPE_BEACON:
  1354. ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
  1355. rx_status);
  1356. break;
  1357. case IEEE80211_STYPE_AUTH:
  1358. ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
  1359. break;
  1360. case IEEE80211_STYPE_ASSOC_RESP:
  1361. ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, 0);
  1362. break;
  1363. case IEEE80211_STYPE_REASSOC_RESP:
  1364. ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, 1);
  1365. break;
  1366. case IEEE80211_STYPE_DEAUTH:
  1367. ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
  1368. break;
  1369. case IEEE80211_STYPE_DISASSOC:
  1370. ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
  1371. break;
  1372. }
  1373. kfree_skb(skb);
  1374. }
  1375. static void ieee80211_sta_timer(unsigned long data)
  1376. {
  1377. struct ieee80211_sub_if_data *sdata =
  1378. (struct ieee80211_sub_if_data *) data;
  1379. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1380. struct ieee80211_local *local = sdata->local;
  1381. set_bit(IEEE80211_STA_REQ_RUN, &ifmgd->request);
  1382. queue_work(local->hw.workqueue, &ifmgd->work);
  1383. }
  1384. static void ieee80211_sta_reset_auth(struct ieee80211_sub_if_data *sdata)
  1385. {
  1386. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1387. struct ieee80211_local *local = sdata->local;
  1388. if (local->ops->reset_tsf) {
  1389. /* Reset own TSF to allow time synchronization work. */
  1390. local->ops->reset_tsf(local_to_hw(local));
  1391. }
  1392. ifmgd->wmm_last_param_set = -1; /* allow any WMM update */
  1393. if (ifmgd->auth_algs & IEEE80211_AUTH_ALG_OPEN)
  1394. ifmgd->auth_alg = WLAN_AUTH_OPEN;
  1395. else if (ifmgd->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
  1396. ifmgd->auth_alg = WLAN_AUTH_SHARED_KEY;
  1397. else if (ifmgd->auth_algs & IEEE80211_AUTH_ALG_LEAP)
  1398. ifmgd->auth_alg = WLAN_AUTH_LEAP;
  1399. else if (ifmgd->auth_algs & IEEE80211_AUTH_ALG_FT)
  1400. ifmgd->auth_alg = WLAN_AUTH_FT;
  1401. else
  1402. ifmgd->auth_alg = WLAN_AUTH_OPEN;
  1403. ifmgd->auth_transaction = -1;
  1404. ifmgd->flags &= ~IEEE80211_STA_ASSOCIATED;
  1405. ifmgd->assoc_scan_tries = 0;
  1406. ifmgd->direct_probe_tries = 0;
  1407. ifmgd->auth_tries = 0;
  1408. ifmgd->assoc_tries = 0;
  1409. netif_tx_stop_all_queues(sdata->dev);
  1410. netif_carrier_off(sdata->dev);
  1411. }
  1412. static int ieee80211_sta_config_auth(struct ieee80211_sub_if_data *sdata)
  1413. {
  1414. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1415. struct ieee80211_local *local = sdata->local;
  1416. struct ieee80211_bss *bss;
  1417. u8 *bssid = ifmgd->bssid, *ssid = ifmgd->ssid;
  1418. u8 ssid_len = ifmgd->ssid_len;
  1419. u16 capa_mask = WLAN_CAPABILITY_ESS;
  1420. u16 capa_val = WLAN_CAPABILITY_ESS;
  1421. struct ieee80211_channel *chan = local->oper_channel;
  1422. if (!(ifmgd->flags & IEEE80211_STA_EXT_SME) &&
  1423. ifmgd->flags & (IEEE80211_STA_AUTO_SSID_SEL |
  1424. IEEE80211_STA_AUTO_BSSID_SEL |
  1425. IEEE80211_STA_AUTO_CHANNEL_SEL)) {
  1426. capa_mask |= WLAN_CAPABILITY_PRIVACY;
  1427. if (sdata->default_key)
  1428. capa_val |= WLAN_CAPABILITY_PRIVACY;
  1429. }
  1430. if (ifmgd->flags & IEEE80211_STA_AUTO_CHANNEL_SEL)
  1431. chan = NULL;
  1432. if (ifmgd->flags & IEEE80211_STA_AUTO_BSSID_SEL)
  1433. bssid = NULL;
  1434. if (ifmgd->flags & IEEE80211_STA_AUTO_SSID_SEL) {
  1435. ssid = NULL;
  1436. ssid_len = 0;
  1437. }
  1438. bss = (void *)cfg80211_get_bss(local->hw.wiphy, chan,
  1439. bssid, ssid, ssid_len,
  1440. capa_mask, capa_val);
  1441. if (bss) {
  1442. ieee80211_set_freq(sdata, bss->cbss.channel->center_freq);
  1443. if (!(ifmgd->flags & IEEE80211_STA_SSID_SET))
  1444. ieee80211_sta_set_ssid(sdata, bss->ssid,
  1445. bss->ssid_len);
  1446. ieee80211_sta_set_bssid(sdata, bss->cbss.bssid);
  1447. ieee80211_sta_def_wmm_params(sdata, bss->supp_rates_len,
  1448. bss->supp_rates);
  1449. if (sdata->u.mgd.mfp == IEEE80211_MFP_REQUIRED)
  1450. sdata->u.mgd.flags |= IEEE80211_STA_MFP_ENABLED;
  1451. else
  1452. sdata->u.mgd.flags &= ~IEEE80211_STA_MFP_ENABLED;
  1453. /* Send out direct probe if no probe resp was received or
  1454. * the one we have is outdated
  1455. */
  1456. if (!bss->last_probe_resp ||
  1457. time_after(jiffies, bss->last_probe_resp
  1458. + IEEE80211_SCAN_RESULT_EXPIRE))
  1459. ifmgd->state = IEEE80211_STA_MLME_DIRECT_PROBE;
  1460. else
  1461. ifmgd->state = IEEE80211_STA_MLME_AUTHENTICATE;
  1462. ieee80211_rx_bss_put(local, bss);
  1463. ieee80211_sta_reset_auth(sdata);
  1464. return 0;
  1465. } else {
  1466. if (ifmgd->assoc_scan_tries < IEEE80211_ASSOC_SCANS_MAX_TRIES) {
  1467. ifmgd->assoc_scan_tries++;
  1468. /* XXX maybe racy? */
  1469. if (local->scan_req)
  1470. return -1;
  1471. memcpy(local->int_scan_req.ssids[0].ssid,
  1472. ifmgd->ssid, IEEE80211_MAX_SSID_LEN);
  1473. if (ifmgd->flags & IEEE80211_STA_AUTO_SSID_SEL)
  1474. local->int_scan_req.ssids[0].ssid_len = 0;
  1475. else
  1476. local->int_scan_req.ssids[0].ssid_len = ifmgd->ssid_len;
  1477. if (ieee80211_start_scan(sdata, &local->int_scan_req))
  1478. ieee80211_scan_failed(local);
  1479. ifmgd->state = IEEE80211_STA_MLME_AUTHENTICATE;
  1480. set_bit(IEEE80211_STA_REQ_AUTH, &ifmgd->request);
  1481. } else {
  1482. ifmgd->assoc_scan_tries = 0;
  1483. ifmgd->state = IEEE80211_STA_MLME_DISABLED;
  1484. }
  1485. }
  1486. return -1;
  1487. }
  1488. static void ieee80211_sta_work(struct work_struct *work)
  1489. {
  1490. struct ieee80211_sub_if_data *sdata =
  1491. container_of(work, struct ieee80211_sub_if_data, u.mgd.work);
  1492. struct ieee80211_local *local = sdata->local;
  1493. struct ieee80211_if_managed *ifmgd;
  1494. struct sk_buff *skb;
  1495. if (!netif_running(sdata->dev))
  1496. return;
  1497. if (local->sw_scanning || local->hw_scanning)
  1498. return;
  1499. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  1500. return;
  1501. ifmgd = &sdata->u.mgd;
  1502. while ((skb = skb_dequeue(&ifmgd->skb_queue)))
  1503. ieee80211_sta_rx_queued_mgmt(sdata, skb);
  1504. if (ifmgd->state != IEEE80211_STA_MLME_DIRECT_PROBE &&
  1505. ifmgd->state != IEEE80211_STA_MLME_AUTHENTICATE &&
  1506. ifmgd->state != IEEE80211_STA_MLME_ASSOCIATE &&
  1507. test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifmgd->request)) {
  1508. /*
  1509. * The call to ieee80211_start_scan can fail but ieee80211_request_scan
  1510. * (which queued ieee80211_sta_work) did not return an error. Thus, call
  1511. * ieee80211_scan_failed here if ieee80211_start_scan fails in order to
  1512. * notify the scan requester.
  1513. */
  1514. if (ieee80211_start_scan(sdata, local->scan_req))
  1515. ieee80211_scan_failed(local);
  1516. return;
  1517. }
  1518. if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifmgd->request)) {
  1519. if (ieee80211_sta_config_auth(sdata))
  1520. return;
  1521. clear_bit(IEEE80211_STA_REQ_RUN, &ifmgd->request);
  1522. } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifmgd->request))
  1523. return;
  1524. switch (ifmgd->state) {
  1525. case IEEE80211_STA_MLME_DISABLED:
  1526. break;
  1527. case IEEE80211_STA_MLME_DIRECT_PROBE:
  1528. ieee80211_direct_probe(sdata);
  1529. break;
  1530. case IEEE80211_STA_MLME_AUTHENTICATE:
  1531. ieee80211_authenticate(sdata);
  1532. break;
  1533. case IEEE80211_STA_MLME_ASSOCIATE:
  1534. ieee80211_associate(sdata);
  1535. break;
  1536. case IEEE80211_STA_MLME_ASSOCIATED:
  1537. ieee80211_associated(sdata);
  1538. break;
  1539. default:
  1540. WARN_ON(1);
  1541. break;
  1542. }
  1543. if (ieee80211_privacy_mismatch(sdata)) {
  1544. printk(KERN_DEBUG "%s: privacy configuration mismatch and "
  1545. "mixed-cell disabled - disassociate\n", sdata->dev->name);
  1546. ieee80211_set_disassoc(sdata, false, true,
  1547. WLAN_REASON_UNSPECIFIED);
  1548. }
  1549. }
  1550. static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
  1551. {
  1552. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  1553. queue_work(sdata->local->hw.workqueue,
  1554. &sdata->u.mgd.work);
  1555. }
  1556. /* interface setup */
  1557. void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
  1558. {
  1559. struct ieee80211_if_managed *ifmgd;
  1560. ifmgd = &sdata->u.mgd;
  1561. INIT_WORK(&ifmgd->work, ieee80211_sta_work);
  1562. INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
  1563. setup_timer(&ifmgd->timer, ieee80211_sta_timer,
  1564. (unsigned long) sdata);
  1565. setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
  1566. (unsigned long) sdata);
  1567. skb_queue_head_init(&ifmgd->skb_queue);
  1568. ifmgd->capab = WLAN_CAPABILITY_ESS;
  1569. ifmgd->auth_algs = IEEE80211_AUTH_ALG_OPEN |
  1570. IEEE80211_AUTH_ALG_SHARED_KEY;
  1571. ifmgd->flags |= IEEE80211_STA_CREATE_IBSS |
  1572. IEEE80211_STA_AUTO_BSSID_SEL |
  1573. IEEE80211_STA_AUTO_CHANNEL_SEL;
  1574. if (sdata->local->hw.queues >= 4)
  1575. ifmgd->flags |= IEEE80211_STA_WMM_ENABLED;
  1576. }
  1577. /* configuration hooks */
  1578. void ieee80211_sta_req_auth(struct ieee80211_sub_if_data *sdata)
  1579. {
  1580. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1581. struct ieee80211_local *local = sdata->local;
  1582. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  1583. return;
  1584. if ((ifmgd->flags & (IEEE80211_STA_BSSID_SET |
  1585. IEEE80211_STA_AUTO_BSSID_SEL)) &&
  1586. (ifmgd->flags & (IEEE80211_STA_SSID_SET |
  1587. IEEE80211_STA_AUTO_SSID_SEL))) {
  1588. if (ifmgd->state == IEEE80211_STA_MLME_ASSOCIATED)
  1589. ieee80211_set_disassoc(sdata, true, true,
  1590. WLAN_REASON_DEAUTH_LEAVING);
  1591. if (!(ifmgd->flags & IEEE80211_STA_EXT_SME) ||
  1592. ifmgd->state != IEEE80211_STA_MLME_ASSOCIATE)
  1593. set_bit(IEEE80211_STA_REQ_AUTH, &ifmgd->request);
  1594. else if (ifmgd->flags & IEEE80211_STA_EXT_SME)
  1595. set_bit(IEEE80211_STA_REQ_RUN, &ifmgd->request);
  1596. queue_work(local->hw.workqueue, &ifmgd->work);
  1597. }
  1598. }
  1599. int ieee80211_sta_commit(struct ieee80211_sub_if_data *sdata)
  1600. {
  1601. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1602. if (ifmgd->ssid_len)
  1603. ifmgd->flags |= IEEE80211_STA_SSID_SET;
  1604. else
  1605. ifmgd->flags &= ~IEEE80211_STA_SSID_SET;
  1606. return 0;
  1607. }
  1608. int ieee80211_sta_set_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t len)
  1609. {
  1610. struct ieee80211_if_managed *ifmgd;
  1611. if (len > IEEE80211_MAX_SSID_LEN)
  1612. return -EINVAL;
  1613. ifmgd = &sdata->u.mgd;
  1614. if (ifmgd->ssid_len != len || memcmp(ifmgd->ssid, ssid, len) != 0) {
  1615. /*
  1616. * Do not use reassociation if SSID is changed (different ESS).
  1617. */
  1618. ifmgd->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
  1619. memset(ifmgd->ssid, 0, sizeof(ifmgd->ssid));
  1620. memcpy(ifmgd->ssid, ssid, len);
  1621. ifmgd->ssid_len = len;
  1622. }
  1623. return ieee80211_sta_commit(sdata);
  1624. }
  1625. int ieee80211_sta_get_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t *len)
  1626. {
  1627. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1628. memcpy(ssid, ifmgd->ssid, ifmgd->ssid_len);
  1629. *len = ifmgd->ssid_len;
  1630. return 0;
  1631. }
  1632. int ieee80211_sta_set_bssid(struct ieee80211_sub_if_data *sdata, u8 *bssid)
  1633. {
  1634. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1635. if (is_valid_ether_addr(bssid)) {
  1636. memcpy(ifmgd->bssid, bssid, ETH_ALEN);
  1637. ifmgd->flags |= IEEE80211_STA_BSSID_SET;
  1638. } else {
  1639. memset(ifmgd->bssid, 0, ETH_ALEN);
  1640. ifmgd->flags &= ~IEEE80211_STA_BSSID_SET;
  1641. }
  1642. if (netif_running(sdata->dev)) {
  1643. if (ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID)) {
  1644. printk(KERN_DEBUG "%s: Failed to config new BSSID to "
  1645. "the low-level driver\n", sdata->dev->name);
  1646. }
  1647. }
  1648. return ieee80211_sta_commit(sdata);
  1649. }
  1650. int ieee80211_sta_set_extra_ie(struct ieee80211_sub_if_data *sdata,
  1651. const char *ie, size_t len)
  1652. {
  1653. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1654. kfree(ifmgd->extra_ie);
  1655. if (len == 0) {
  1656. ifmgd->extra_ie = NULL;
  1657. ifmgd->extra_ie_len = 0;
  1658. return 0;
  1659. }
  1660. ifmgd->extra_ie = kmalloc(len, GFP_KERNEL);
  1661. if (!ifmgd->extra_ie) {
  1662. ifmgd->extra_ie_len = 0;
  1663. return -ENOMEM;
  1664. }
  1665. memcpy(ifmgd->extra_ie, ie, len);
  1666. ifmgd->extra_ie_len = len;
  1667. return 0;
  1668. }
  1669. int ieee80211_sta_deauthenticate(struct ieee80211_sub_if_data *sdata, u16 reason)
  1670. {
  1671. printk(KERN_DEBUG "%s: deauthenticating by local choice (reason=%d)\n",
  1672. sdata->dev->name, reason);
  1673. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1674. return -EINVAL;
  1675. ieee80211_set_disassoc(sdata, true, true, reason);
  1676. return 0;
  1677. }
  1678. int ieee80211_sta_disassociate(struct ieee80211_sub_if_data *sdata, u16 reason)
  1679. {
  1680. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1681. printk(KERN_DEBUG "%s: disassociating by local choice (reason=%d)\n",
  1682. sdata->dev->name, reason);
  1683. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1684. return -EINVAL;
  1685. if (!(ifmgd->flags & IEEE80211_STA_ASSOCIATED))
  1686. return -ENOLINK;
  1687. ieee80211_set_disassoc(sdata, false, true, reason);
  1688. return 0;
  1689. }
  1690. /* scan finished notification */
  1691. void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
  1692. {
  1693. struct ieee80211_sub_if_data *sdata = local->scan_sdata;
  1694. /* Restart STA timers */
  1695. rcu_read_lock();
  1696. list_for_each_entry_rcu(sdata, &local->interfaces, list)
  1697. ieee80211_restart_sta_timer(sdata);
  1698. rcu_read_unlock();
  1699. }
  1700. void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
  1701. {
  1702. struct ieee80211_local *local =
  1703. container_of(work, struct ieee80211_local,
  1704. dynamic_ps_disable_work);
  1705. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  1706. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  1707. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  1708. }
  1709. ieee80211_wake_queues_by_reason(&local->hw,
  1710. IEEE80211_QUEUE_STOP_REASON_PS);
  1711. }
  1712. void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
  1713. {
  1714. struct ieee80211_local *local =
  1715. container_of(work, struct ieee80211_local,
  1716. dynamic_ps_enable_work);
  1717. struct ieee80211_sub_if_data *sdata = local->scan_sdata;
  1718. if (local->hw.conf.flags & IEEE80211_CONF_PS)
  1719. return;
  1720. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
  1721. ieee80211_send_nullfunc(local, sdata, 1);
  1722. local->hw.conf.flags |= IEEE80211_CONF_PS;
  1723. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  1724. }
  1725. void ieee80211_dynamic_ps_timer(unsigned long data)
  1726. {
  1727. struct ieee80211_local *local = (void *) data;
  1728. queue_work(local->hw.workqueue, &local->dynamic_ps_enable_work);
  1729. }
  1730. void ieee80211_send_nullfunc(struct ieee80211_local *local,
  1731. struct ieee80211_sub_if_data *sdata,
  1732. int powersave)
  1733. {
  1734. struct sk_buff *skb;
  1735. struct ieee80211_hdr *nullfunc;
  1736. __le16 fc;
  1737. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  1738. return;
  1739. skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
  1740. if (!skb) {
  1741. printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
  1742. "frame\n", sdata->dev->name);
  1743. return;
  1744. }
  1745. skb_reserve(skb, local->hw.extra_tx_headroom);
  1746. nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
  1747. memset(nullfunc, 0, 24);
  1748. fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
  1749. IEEE80211_FCTL_TODS);
  1750. if (powersave)
  1751. fc |= cpu_to_le16(IEEE80211_FCTL_PM);
  1752. nullfunc->frame_control = fc;
  1753. memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
  1754. memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
  1755. memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
  1756. ieee80211_tx_skb(sdata, skb, 0);
  1757. }