mlme.c 79 KB

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