mlme.c 72 KB

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