mlme.c 71 KB

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