mlme.c 76 KB

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