mlme.c 77 KB

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