mlme.c 70 KB

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