mlme.c 70 KB

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