mlme.c 77 KB

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