mlme.c 72 KB

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