mlme.c 70 KB

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