mlme.c 76 KB

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