mlme.c 70 KB

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