mlme.c 70 KB

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