mlme.c 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442
  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. /* mgmt header + 1 byte category code */
  45. #define IEEE80211_MIN_ACTION_SIZE (24 + 1)
  46. #define IEEE80211_ADDBA_PARAM_POLICY_MASK 0x0002
  47. #define IEEE80211_ADDBA_PARAM_TID_MASK 0x003C
  48. #define IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK 0xFFA0
  49. #define IEEE80211_DELBA_PARAM_TID_MASK 0xF000
  50. #define IEEE80211_DELBA_PARAM_INITIATOR_MASK 0x0800
  51. /* next values represent the buffer size for A-MPDU frame.
  52. * According to IEEE802.11n spec size varies from 8K to 64K (in powers of 2) */
  53. #define IEEE80211_MIN_AMPDU_BUF 0x8
  54. #define IEEE80211_MAX_AMPDU_BUF 0x40
  55. /* utils */
  56. static int ecw2cw(int ecw)
  57. {
  58. return (1 << ecw) - 1;
  59. }
  60. static u8 *ieee80211_bss_get_ie(struct ieee80211_sta_bss *bss, u8 ie)
  61. {
  62. u8 *end, *pos;
  63. pos = bss->ies;
  64. if (pos == NULL)
  65. return NULL;
  66. end = pos + bss->ies_len;
  67. while (pos + 1 < end) {
  68. if (pos + 2 + pos[1] > end)
  69. break;
  70. if (pos[0] == ie)
  71. return pos;
  72. pos += 2 + pos[1];
  73. }
  74. return NULL;
  75. }
  76. /* frame sending functions */
  77. void ieee80211_sta_tx(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
  78. int encrypt)
  79. {
  80. skb->dev = sdata->local->mdev;
  81. skb_set_mac_header(skb, 0);
  82. skb_set_network_header(skb, 0);
  83. skb_set_transport_header(skb, 0);
  84. skb->iif = sdata->dev->ifindex;
  85. skb->do_not_encrypt = !encrypt;
  86. dev_queue_xmit(skb);
  87. }
  88. static void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
  89. struct ieee80211_if_sta *ifsta,
  90. int transaction, u8 *extra, size_t extra_len,
  91. int encrypt)
  92. {
  93. struct ieee80211_local *local = sdata->local;
  94. struct sk_buff *skb;
  95. struct ieee80211_mgmt *mgmt;
  96. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  97. sizeof(*mgmt) + 6 + extra_len);
  98. if (!skb) {
  99. printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
  100. "frame\n", sdata->dev->name);
  101. return;
  102. }
  103. skb_reserve(skb, local->hw.extra_tx_headroom);
  104. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
  105. memset(mgmt, 0, 24 + 6);
  106. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  107. IEEE80211_STYPE_AUTH);
  108. if (encrypt)
  109. mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  110. memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
  111. memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
  112. memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
  113. mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
  114. mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
  115. ifsta->auth_transaction = transaction + 1;
  116. mgmt->u.auth.status_code = cpu_to_le16(0);
  117. if (extra)
  118. memcpy(skb_put(skb, extra_len), extra, extra_len);
  119. ieee80211_sta_tx(sdata, skb, encrypt);
  120. }
  121. void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
  122. u8 *ssid, size_t ssid_len)
  123. {
  124. struct ieee80211_local *local = sdata->local;
  125. struct ieee80211_supported_band *sband;
  126. struct sk_buff *skb;
  127. struct ieee80211_mgmt *mgmt;
  128. u8 *pos, *supp_rates, *esupp_rates = NULL;
  129. int i;
  130. skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
  131. if (!skb) {
  132. printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
  133. "request\n", sdata->dev->name);
  134. return;
  135. }
  136. skb_reserve(skb, local->hw.extra_tx_headroom);
  137. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
  138. memset(mgmt, 0, 24);
  139. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  140. IEEE80211_STYPE_PROBE_REQ);
  141. memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
  142. if (dst) {
  143. memcpy(mgmt->da, dst, ETH_ALEN);
  144. memcpy(mgmt->bssid, dst, ETH_ALEN);
  145. } else {
  146. memset(mgmt->da, 0xff, ETH_ALEN);
  147. memset(mgmt->bssid, 0xff, ETH_ALEN);
  148. }
  149. pos = skb_put(skb, 2 + ssid_len);
  150. *pos++ = WLAN_EID_SSID;
  151. *pos++ = ssid_len;
  152. memcpy(pos, ssid, ssid_len);
  153. supp_rates = skb_put(skb, 2);
  154. supp_rates[0] = WLAN_EID_SUPP_RATES;
  155. supp_rates[1] = 0;
  156. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  157. for (i = 0; i < sband->n_bitrates; i++) {
  158. struct ieee80211_rate *rate = &sband->bitrates[i];
  159. if (esupp_rates) {
  160. pos = skb_put(skb, 1);
  161. esupp_rates[1]++;
  162. } else if (supp_rates[1] == 8) {
  163. esupp_rates = skb_put(skb, 3);
  164. esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
  165. esupp_rates[1] = 1;
  166. pos = &esupp_rates[2];
  167. } else {
  168. pos = skb_put(skb, 1);
  169. supp_rates[1]++;
  170. }
  171. *pos = rate->bitrate / 5;
  172. }
  173. ieee80211_sta_tx(sdata, skb, 0);
  174. }
  175. /* MLME */
  176. static void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
  177. struct ieee80211_sta_bss *bss,
  178. int ibss)
  179. {
  180. struct ieee80211_local *local = sdata->local;
  181. int i, have_higher_than_11mbit = 0;
  182. /* cf. IEEE 802.11 9.2.12 */
  183. for (i = 0; i < bss->supp_rates_len; i++)
  184. if ((bss->supp_rates[i] & 0x7f) * 5 > 110)
  185. have_higher_than_11mbit = 1;
  186. if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
  187. have_higher_than_11mbit)
  188. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  189. else
  190. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  191. if (local->ops->conf_tx) {
  192. struct ieee80211_tx_queue_params qparam;
  193. memset(&qparam, 0, sizeof(qparam));
  194. qparam.aifs = 2;
  195. if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
  196. !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE))
  197. qparam.cw_min = 31;
  198. else
  199. qparam.cw_min = 15;
  200. qparam.cw_max = 1023;
  201. qparam.txop = 0;
  202. for (i = 0; i < local_to_hw(local)->queues; i++)
  203. local->ops->conf_tx(local_to_hw(local), i, &qparam);
  204. }
  205. }
  206. static void ieee80211_sta_wmm_params(struct ieee80211_local *local,
  207. struct ieee80211_if_sta *ifsta,
  208. u8 *wmm_param, size_t wmm_param_len)
  209. {
  210. struct ieee80211_tx_queue_params params;
  211. size_t left;
  212. int count;
  213. u8 *pos;
  214. if (!(ifsta->flags & IEEE80211_STA_WMM_ENABLED))
  215. return;
  216. if (!wmm_param)
  217. return;
  218. if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
  219. return;
  220. count = wmm_param[6] & 0x0f;
  221. if (count == ifsta->wmm_last_param_set)
  222. return;
  223. ifsta->wmm_last_param_set = count;
  224. pos = wmm_param + 8;
  225. left = wmm_param_len - 8;
  226. memset(&params, 0, sizeof(params));
  227. if (!local->ops->conf_tx)
  228. return;
  229. local->wmm_acm = 0;
  230. for (; left >= 4; left -= 4, pos += 4) {
  231. int aci = (pos[0] >> 5) & 0x03;
  232. int acm = (pos[0] >> 4) & 0x01;
  233. int queue;
  234. switch (aci) {
  235. case 1:
  236. queue = 3;
  237. if (acm)
  238. local->wmm_acm |= BIT(0) | BIT(3);
  239. break;
  240. case 2:
  241. queue = 1;
  242. if (acm)
  243. local->wmm_acm |= BIT(4) | BIT(5);
  244. break;
  245. case 3:
  246. queue = 0;
  247. if (acm)
  248. local->wmm_acm |= BIT(6) | BIT(7);
  249. break;
  250. case 0:
  251. default:
  252. queue = 2;
  253. if (acm)
  254. local->wmm_acm |= BIT(1) | BIT(2);
  255. break;
  256. }
  257. params.aifs = pos[0] & 0x0f;
  258. params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
  259. params.cw_min = ecw2cw(pos[1] & 0x0f);
  260. params.txop = get_unaligned_le16(pos + 2);
  261. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  262. printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
  263. "cWmin=%d cWmax=%d txop=%d\n",
  264. local->mdev->name, queue, aci, acm, params.aifs, params.cw_min,
  265. params.cw_max, params.txop);
  266. #endif
  267. /* TODO: handle ACM (block TX, fallback to next lowest allowed
  268. * AC for now) */
  269. if (local->ops->conf_tx(local_to_hw(local), queue, &params)) {
  270. printk(KERN_DEBUG "%s: failed to set TX queue "
  271. "parameters for queue %d\n", local->mdev->name, queue);
  272. }
  273. }
  274. }
  275. static u32 ieee80211_handle_protect_preamb(struct ieee80211_sub_if_data *sdata,
  276. bool use_protection,
  277. bool use_short_preamble)
  278. {
  279. struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
  280. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  281. struct ieee80211_if_sta *ifsta = &sdata->u.sta;
  282. DECLARE_MAC_BUF(mac);
  283. #endif
  284. u32 changed = 0;
  285. if (use_protection != bss_conf->use_cts_prot) {
  286. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  287. if (net_ratelimit()) {
  288. printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
  289. "%s)\n",
  290. sdata->dev->name,
  291. use_protection ? "enabled" : "disabled",
  292. print_mac(mac, ifsta->bssid));
  293. }
  294. #endif
  295. bss_conf->use_cts_prot = use_protection;
  296. changed |= BSS_CHANGED_ERP_CTS_PROT;
  297. }
  298. if (use_short_preamble != bss_conf->use_short_preamble) {
  299. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  300. if (net_ratelimit()) {
  301. printk(KERN_DEBUG "%s: switched to %s barker preamble"
  302. " (BSSID=%s)\n",
  303. sdata->dev->name,
  304. use_short_preamble ? "short" : "long",
  305. print_mac(mac, ifsta->bssid));
  306. }
  307. #endif
  308. bss_conf->use_short_preamble = use_short_preamble;
  309. changed |= BSS_CHANGED_ERP_PREAMBLE;
  310. }
  311. return changed;
  312. }
  313. static u32 ieee80211_handle_erp_ie(struct ieee80211_sub_if_data *sdata,
  314. u8 erp_value)
  315. {
  316. bool use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
  317. bool use_short_preamble = (erp_value & WLAN_ERP_BARKER_PREAMBLE) == 0;
  318. return ieee80211_handle_protect_preamb(sdata,
  319. use_protection, use_short_preamble);
  320. }
  321. static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
  322. struct ieee80211_sta_bss *bss)
  323. {
  324. u32 changed = 0;
  325. if (bss->has_erp_value)
  326. changed |= ieee80211_handle_erp_ie(sdata, bss->erp_value);
  327. else {
  328. u16 capab = bss->capability;
  329. changed |= ieee80211_handle_protect_preamb(sdata, false,
  330. (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
  331. }
  332. return changed;
  333. }
  334. int ieee80211_ht_cap_ie_to_ht_info(struct ieee80211_ht_cap *ht_cap_ie,
  335. struct ieee80211_ht_info *ht_info)
  336. {
  337. if (ht_info == NULL)
  338. return -EINVAL;
  339. memset(ht_info, 0, sizeof(*ht_info));
  340. if (ht_cap_ie) {
  341. u8 ampdu_info = ht_cap_ie->ampdu_params_info;
  342. ht_info->ht_supported = 1;
  343. ht_info->cap = le16_to_cpu(ht_cap_ie->cap_info);
  344. ht_info->ampdu_factor =
  345. ampdu_info & IEEE80211_HT_CAP_AMPDU_FACTOR;
  346. ht_info->ampdu_density =
  347. (ampdu_info & IEEE80211_HT_CAP_AMPDU_DENSITY) >> 2;
  348. memcpy(ht_info->supp_mcs_set, ht_cap_ie->supp_mcs_set, 16);
  349. } else
  350. ht_info->ht_supported = 0;
  351. return 0;
  352. }
  353. int ieee80211_ht_addt_info_ie_to_ht_bss_info(
  354. struct ieee80211_ht_addt_info *ht_add_info_ie,
  355. struct ieee80211_ht_bss_info *bss_info)
  356. {
  357. if (bss_info == NULL)
  358. return -EINVAL;
  359. memset(bss_info, 0, sizeof(*bss_info));
  360. if (ht_add_info_ie) {
  361. u16 op_mode;
  362. op_mode = le16_to_cpu(ht_add_info_ie->operation_mode);
  363. bss_info->primary_channel = ht_add_info_ie->control_chan;
  364. bss_info->bss_cap = ht_add_info_ie->ht_param;
  365. bss_info->bss_op_mode = (u8)(op_mode & 0xff);
  366. }
  367. return 0;
  368. }
  369. static void ieee80211_sta_send_apinfo(struct ieee80211_sub_if_data *sdata,
  370. struct ieee80211_if_sta *ifsta)
  371. {
  372. union iwreq_data wrqu;
  373. memset(&wrqu, 0, sizeof(wrqu));
  374. if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
  375. memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
  376. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  377. wireless_send_event(sdata->dev, SIOCGIWAP, &wrqu, NULL);
  378. }
  379. static void ieee80211_sta_send_associnfo(struct ieee80211_sub_if_data *sdata,
  380. struct ieee80211_if_sta *ifsta)
  381. {
  382. union iwreq_data wrqu;
  383. if (ifsta->assocreq_ies) {
  384. memset(&wrqu, 0, sizeof(wrqu));
  385. wrqu.data.length = ifsta->assocreq_ies_len;
  386. wireless_send_event(sdata->dev, IWEVASSOCREQIE, &wrqu,
  387. ifsta->assocreq_ies);
  388. }
  389. if (ifsta->assocresp_ies) {
  390. memset(&wrqu, 0, sizeof(wrqu));
  391. wrqu.data.length = ifsta->assocresp_ies_len;
  392. wireless_send_event(sdata->dev, IWEVASSOCRESPIE, &wrqu,
  393. ifsta->assocresp_ies);
  394. }
  395. }
  396. static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
  397. struct ieee80211_if_sta *ifsta)
  398. {
  399. struct ieee80211_local *local = sdata->local;
  400. struct ieee80211_conf *conf = &local_to_hw(local)->conf;
  401. u32 changed = BSS_CHANGED_ASSOC;
  402. struct ieee80211_sta_bss *bss;
  403. ifsta->flags |= IEEE80211_STA_ASSOCIATED;
  404. if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
  405. return;
  406. bss = ieee80211_rx_bss_get(local, ifsta->bssid,
  407. conf->channel->center_freq,
  408. ifsta->ssid, ifsta->ssid_len);
  409. if (bss) {
  410. /* set timing information */
  411. sdata->bss_conf.beacon_int = bss->beacon_int;
  412. sdata->bss_conf.timestamp = bss->timestamp;
  413. sdata->bss_conf.dtim_period = bss->dtim_period;
  414. changed |= ieee80211_handle_bss_capability(sdata, bss);
  415. ieee80211_rx_bss_put(local, bss);
  416. }
  417. if (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
  418. changed |= BSS_CHANGED_HT;
  419. sdata->bss_conf.assoc_ht = 1;
  420. sdata->bss_conf.ht_conf = &conf->ht_conf;
  421. sdata->bss_conf.ht_bss_conf = &conf->ht_bss_conf;
  422. }
  423. ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
  424. memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
  425. ieee80211_sta_send_associnfo(sdata, ifsta);
  426. ifsta->last_probe = jiffies;
  427. ieee80211_led_assoc(local, 1);
  428. sdata->bss_conf.assoc = 1;
  429. ieee80211_bss_info_change_notify(sdata, changed);
  430. netif_tx_start_all_queues(sdata->dev);
  431. netif_carrier_on(sdata->dev);
  432. ieee80211_sta_send_apinfo(sdata, ifsta);
  433. }
  434. static void ieee80211_direct_probe(struct ieee80211_sub_if_data *sdata,
  435. struct ieee80211_if_sta *ifsta)
  436. {
  437. DECLARE_MAC_BUF(mac);
  438. ifsta->direct_probe_tries++;
  439. if (ifsta->direct_probe_tries > IEEE80211_AUTH_MAX_TRIES) {
  440. printk(KERN_DEBUG "%s: direct probe to AP %s timed out\n",
  441. sdata->dev->name, print_mac(mac, ifsta->bssid));
  442. ifsta->state = IEEE80211_STA_MLME_DISABLED;
  443. return;
  444. }
  445. printk(KERN_DEBUG "%s: direct probe to AP %s try %d\n",
  446. sdata->dev->name, print_mac(mac, ifsta->bssid),
  447. ifsta->direct_probe_tries);
  448. ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
  449. set_bit(IEEE80211_STA_REQ_DIRECT_PROBE, &ifsta->request);
  450. /* Direct probe is sent to broadcast address as some APs
  451. * will not answer to direct packet in unassociated state.
  452. */
  453. ieee80211_send_probe_req(sdata, NULL,
  454. ifsta->ssid, ifsta->ssid_len);
  455. mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
  456. }
  457. static void ieee80211_authenticate(struct ieee80211_sub_if_data *sdata,
  458. struct ieee80211_if_sta *ifsta)
  459. {
  460. DECLARE_MAC_BUF(mac);
  461. ifsta->auth_tries++;
  462. if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
  463. printk(KERN_DEBUG "%s: authentication with AP %s"
  464. " timed out\n",
  465. sdata->dev->name, print_mac(mac, ifsta->bssid));
  466. ifsta->state = IEEE80211_STA_MLME_DISABLED;
  467. return;
  468. }
  469. ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
  470. printk(KERN_DEBUG "%s: authenticate with AP %s\n",
  471. sdata->dev->name, print_mac(mac, ifsta->bssid));
  472. ieee80211_send_auth(sdata, ifsta, 1, NULL, 0, 0);
  473. mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
  474. }
  475. static int ieee80211_compatible_rates(struct ieee80211_sta_bss *bss,
  476. struct ieee80211_supported_band *sband,
  477. u64 *rates)
  478. {
  479. int i, j, count;
  480. *rates = 0;
  481. count = 0;
  482. for (i = 0; i < bss->supp_rates_len; i++) {
  483. int rate = (bss->supp_rates[i] & 0x7F) * 5;
  484. for (j = 0; j < sband->n_bitrates; j++)
  485. if (sband->bitrates[j].bitrate == rate) {
  486. *rates |= BIT(j);
  487. count++;
  488. break;
  489. }
  490. }
  491. return count;
  492. }
  493. static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata,
  494. struct ieee80211_if_sta *ifsta)
  495. {
  496. struct ieee80211_local *local = sdata->local;
  497. struct sk_buff *skb;
  498. struct ieee80211_mgmt *mgmt;
  499. u8 *pos, *ies, *ht_add_ie;
  500. int i, len, count, rates_len, supp_rates_len;
  501. u16 capab;
  502. struct ieee80211_sta_bss *bss;
  503. int wmm = 0;
  504. struct ieee80211_supported_band *sband;
  505. u64 rates = 0;
  506. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  507. sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
  508. ifsta->ssid_len);
  509. if (!skb) {
  510. printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
  511. "frame\n", sdata->dev->name);
  512. return;
  513. }
  514. skb_reserve(skb, local->hw.extra_tx_headroom);
  515. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  516. capab = ifsta->capab;
  517. if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
  518. if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
  519. capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
  520. if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
  521. capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
  522. }
  523. bss = ieee80211_rx_bss_get(local, ifsta->bssid,
  524. local->hw.conf.channel->center_freq,
  525. ifsta->ssid, ifsta->ssid_len);
  526. if (bss) {
  527. if (bss->capability & WLAN_CAPABILITY_PRIVACY)
  528. capab |= WLAN_CAPABILITY_PRIVACY;
  529. if (bss->wmm_used)
  530. wmm = 1;
  531. /* get all rates supported by the device and the AP as
  532. * some APs don't like getting a superset of their rates
  533. * in the association request (e.g. D-Link DAP 1353 in
  534. * b-only mode) */
  535. rates_len = ieee80211_compatible_rates(bss, sband, &rates);
  536. if ((bss->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
  537. (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
  538. capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
  539. ieee80211_rx_bss_put(local, bss);
  540. } else {
  541. rates = ~0;
  542. rates_len = sband->n_bitrates;
  543. }
  544. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
  545. memset(mgmt, 0, 24);
  546. memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
  547. memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
  548. memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
  549. if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
  550. skb_put(skb, 10);
  551. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  552. IEEE80211_STYPE_REASSOC_REQ);
  553. mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
  554. mgmt->u.reassoc_req.listen_interval =
  555. cpu_to_le16(local->hw.conf.listen_interval);
  556. memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
  557. ETH_ALEN);
  558. } else {
  559. skb_put(skb, 4);
  560. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  561. IEEE80211_STYPE_ASSOC_REQ);
  562. mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
  563. mgmt->u.reassoc_req.listen_interval =
  564. cpu_to_le16(local->hw.conf.listen_interval);
  565. }
  566. /* SSID */
  567. ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
  568. *pos++ = WLAN_EID_SSID;
  569. *pos++ = ifsta->ssid_len;
  570. memcpy(pos, ifsta->ssid, ifsta->ssid_len);
  571. /* add all rates which were marked to be used above */
  572. supp_rates_len = rates_len;
  573. if (supp_rates_len > 8)
  574. supp_rates_len = 8;
  575. len = sband->n_bitrates;
  576. pos = skb_put(skb, supp_rates_len + 2);
  577. *pos++ = WLAN_EID_SUPP_RATES;
  578. *pos++ = supp_rates_len;
  579. count = 0;
  580. for (i = 0; i < sband->n_bitrates; i++) {
  581. if (BIT(i) & rates) {
  582. int rate = sband->bitrates[i].bitrate;
  583. *pos++ = (u8) (rate / 5);
  584. if (++count == 8)
  585. break;
  586. }
  587. }
  588. if (rates_len > count) {
  589. pos = skb_put(skb, rates_len - count + 2);
  590. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  591. *pos++ = rates_len - count;
  592. for (i++; i < sband->n_bitrates; i++) {
  593. if (BIT(i) & rates) {
  594. int rate = sband->bitrates[i].bitrate;
  595. *pos++ = (u8) (rate / 5);
  596. }
  597. }
  598. }
  599. if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
  600. /* 1. power capabilities */
  601. pos = skb_put(skb, 4);
  602. *pos++ = WLAN_EID_PWR_CAPABILITY;
  603. *pos++ = 2;
  604. *pos++ = 0; /* min tx power */
  605. *pos++ = local->hw.conf.channel->max_power; /* max tx power */
  606. /* 2. supported channels */
  607. /* TODO: get this in reg domain format */
  608. pos = skb_put(skb, 2 * sband->n_channels + 2);
  609. *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
  610. *pos++ = 2 * sband->n_channels;
  611. for (i = 0; i < sband->n_channels; i++) {
  612. *pos++ = ieee80211_frequency_to_channel(
  613. sband->channels[i].center_freq);
  614. *pos++ = 1; /* one channel in the subband*/
  615. }
  616. }
  617. if (ifsta->extra_ie) {
  618. pos = skb_put(skb, ifsta->extra_ie_len);
  619. memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
  620. }
  621. if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
  622. pos = skb_put(skb, 9);
  623. *pos++ = WLAN_EID_VENDOR_SPECIFIC;
  624. *pos++ = 7; /* len */
  625. *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
  626. *pos++ = 0x50;
  627. *pos++ = 0xf2;
  628. *pos++ = 2; /* WME */
  629. *pos++ = 0; /* WME info */
  630. *pos++ = 1; /* WME ver */
  631. *pos++ = 0;
  632. }
  633. /* wmm support is a must to HT */
  634. if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED) &&
  635. sband->ht_info.ht_supported &&
  636. (ht_add_ie = ieee80211_bss_get_ie(bss, WLAN_EID_HT_EXTRA_INFO))) {
  637. struct ieee80211_ht_addt_info *ht_add_info =
  638. (struct ieee80211_ht_addt_info *)ht_add_ie;
  639. u16 cap = sband->ht_info.cap;
  640. __le16 tmp;
  641. u32 flags = local->hw.conf.channel->flags;
  642. switch (ht_add_info->ht_param & IEEE80211_HT_IE_CHA_SEC_OFFSET) {
  643. case IEEE80211_HT_IE_CHA_SEC_ABOVE:
  644. if (flags & IEEE80211_CHAN_NO_FAT_ABOVE) {
  645. cap &= ~IEEE80211_HT_CAP_SUP_WIDTH;
  646. cap &= ~IEEE80211_HT_CAP_SGI_40;
  647. }
  648. break;
  649. case IEEE80211_HT_IE_CHA_SEC_BELOW:
  650. if (flags & IEEE80211_CHAN_NO_FAT_BELOW) {
  651. cap &= ~IEEE80211_HT_CAP_SUP_WIDTH;
  652. cap &= ~IEEE80211_HT_CAP_SGI_40;
  653. }
  654. break;
  655. }
  656. tmp = cpu_to_le16(cap);
  657. pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
  658. *pos++ = WLAN_EID_HT_CAPABILITY;
  659. *pos++ = sizeof(struct ieee80211_ht_cap);
  660. memset(pos, 0, sizeof(struct ieee80211_ht_cap));
  661. memcpy(pos, &tmp, sizeof(u16));
  662. pos += sizeof(u16);
  663. /* TODO: needs a define here for << 2 */
  664. *pos++ = sband->ht_info.ampdu_factor |
  665. (sband->ht_info.ampdu_density << 2);
  666. memcpy(pos, sband->ht_info.supp_mcs_set, 16);
  667. }
  668. kfree(ifsta->assocreq_ies);
  669. ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
  670. ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
  671. if (ifsta->assocreq_ies)
  672. memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
  673. ieee80211_sta_tx(sdata, skb, 0);
  674. }
  675. static void ieee80211_send_deauth(struct ieee80211_sub_if_data *sdata,
  676. struct ieee80211_if_sta *ifsta, u16 reason)
  677. {
  678. struct ieee80211_local *local = sdata->local;
  679. struct sk_buff *skb;
  680. struct ieee80211_mgmt *mgmt;
  681. skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
  682. if (!skb) {
  683. printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
  684. "frame\n", sdata->dev->name);
  685. return;
  686. }
  687. skb_reserve(skb, local->hw.extra_tx_headroom);
  688. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
  689. memset(mgmt, 0, 24);
  690. memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
  691. memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
  692. memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
  693. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  694. IEEE80211_STYPE_DEAUTH);
  695. skb_put(skb, 2);
  696. mgmt->u.deauth.reason_code = cpu_to_le16(reason);
  697. ieee80211_sta_tx(sdata, skb, 0);
  698. }
  699. static int ieee80211_sta_wep_configured(struct ieee80211_sub_if_data *sdata)
  700. {
  701. if (!sdata || !sdata->default_key ||
  702. sdata->default_key->conf.alg != ALG_WEP)
  703. return 0;
  704. return 1;
  705. }
  706. static void ieee80211_send_disassoc(struct ieee80211_sub_if_data *sdata,
  707. struct ieee80211_if_sta *ifsta, u16 reason)
  708. {
  709. struct ieee80211_local *local = sdata->local;
  710. struct sk_buff *skb;
  711. struct ieee80211_mgmt *mgmt;
  712. skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
  713. if (!skb) {
  714. printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
  715. "frame\n", sdata->dev->name);
  716. return;
  717. }
  718. skb_reserve(skb, local->hw.extra_tx_headroom);
  719. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
  720. memset(mgmt, 0, 24);
  721. memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
  722. memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
  723. memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
  724. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  725. IEEE80211_STYPE_DISASSOC);
  726. skb_put(skb, 2);
  727. mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
  728. ieee80211_sta_tx(sdata, skb, 0);
  729. }
  730. static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
  731. struct ieee80211_if_sta *ifsta, bool deauth,
  732. bool self_disconnected, u16 reason)
  733. {
  734. struct ieee80211_local *local = sdata->local;
  735. struct sta_info *sta;
  736. u32 changed = BSS_CHANGED_ASSOC;
  737. rcu_read_lock();
  738. sta = sta_info_get(local, ifsta->bssid);
  739. if (!sta) {
  740. rcu_read_unlock();
  741. return;
  742. }
  743. if (deauth) {
  744. ifsta->direct_probe_tries = 0;
  745. ifsta->auth_tries = 0;
  746. }
  747. ifsta->assoc_scan_tries = 0;
  748. ifsta->assoc_tries = 0;
  749. netif_tx_stop_all_queues(sdata->dev);
  750. netif_carrier_off(sdata->dev);
  751. ieee80211_sta_tear_down_BA_sessions(sdata, sta->addr);
  752. if (self_disconnected) {
  753. if (deauth)
  754. ieee80211_send_deauth(sdata, ifsta, reason);
  755. else
  756. ieee80211_send_disassoc(sdata, ifsta, reason);
  757. }
  758. ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
  759. changed |= ieee80211_reset_erp_info(sdata);
  760. if (sdata->bss_conf.assoc_ht)
  761. changed |= BSS_CHANGED_HT;
  762. sdata->bss_conf.assoc_ht = 0;
  763. sdata->bss_conf.ht_conf = NULL;
  764. sdata->bss_conf.ht_bss_conf = NULL;
  765. ieee80211_led_assoc(local, 0);
  766. sdata->bss_conf.assoc = 0;
  767. ieee80211_sta_send_apinfo(sdata, ifsta);
  768. if (self_disconnected)
  769. ifsta->state = IEEE80211_STA_MLME_DISABLED;
  770. sta_info_unlink(&sta);
  771. rcu_read_unlock();
  772. sta_info_destroy(sta);
  773. }
  774. static int ieee80211_privacy_mismatch(struct ieee80211_sub_if_data *sdata,
  775. struct ieee80211_if_sta *ifsta)
  776. {
  777. struct ieee80211_local *local = sdata->local;
  778. struct ieee80211_sta_bss *bss;
  779. int bss_privacy;
  780. int wep_privacy;
  781. int privacy_invoked;
  782. if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
  783. return 0;
  784. bss = ieee80211_rx_bss_get(local, ifsta->bssid,
  785. local->hw.conf.channel->center_freq,
  786. ifsta->ssid, ifsta->ssid_len);
  787. if (!bss)
  788. return 0;
  789. bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
  790. wep_privacy = !!ieee80211_sta_wep_configured(sdata);
  791. privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
  792. ieee80211_rx_bss_put(local, bss);
  793. if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
  794. return 0;
  795. return 1;
  796. }
  797. static void ieee80211_associate(struct ieee80211_sub_if_data *sdata,
  798. struct ieee80211_if_sta *ifsta)
  799. {
  800. DECLARE_MAC_BUF(mac);
  801. ifsta->assoc_tries++;
  802. if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
  803. printk(KERN_DEBUG "%s: association with AP %s"
  804. " timed out\n",
  805. sdata->dev->name, print_mac(mac, ifsta->bssid));
  806. ifsta->state = IEEE80211_STA_MLME_DISABLED;
  807. return;
  808. }
  809. ifsta->state = IEEE80211_STA_MLME_ASSOCIATE;
  810. printk(KERN_DEBUG "%s: associate with AP %s\n",
  811. sdata->dev->name, print_mac(mac, ifsta->bssid));
  812. if (ieee80211_privacy_mismatch(sdata, ifsta)) {
  813. printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
  814. "mixed-cell disabled - abort association\n", sdata->dev->name);
  815. ifsta->state = IEEE80211_STA_MLME_DISABLED;
  816. return;
  817. }
  818. ieee80211_send_assoc(sdata, ifsta);
  819. mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
  820. }
  821. static void ieee80211_associated(struct ieee80211_sub_if_data *sdata,
  822. struct ieee80211_if_sta *ifsta)
  823. {
  824. struct ieee80211_local *local = sdata->local;
  825. struct sta_info *sta;
  826. int disassoc;
  827. DECLARE_MAC_BUF(mac);
  828. /* TODO: start monitoring current AP signal quality and number of
  829. * missed beacons. Scan other channels every now and then and search
  830. * for better APs. */
  831. /* TODO: remove expired BSSes */
  832. ifsta->state = IEEE80211_STA_MLME_ASSOCIATED;
  833. rcu_read_lock();
  834. sta = sta_info_get(local, ifsta->bssid);
  835. if (!sta) {
  836. printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
  837. sdata->dev->name, print_mac(mac, ifsta->bssid));
  838. disassoc = 1;
  839. } else {
  840. disassoc = 0;
  841. if (time_after(jiffies,
  842. sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
  843. if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
  844. printk(KERN_DEBUG "%s: No ProbeResp from "
  845. "current AP %s - assume out of "
  846. "range\n",
  847. sdata->dev->name, print_mac(mac, ifsta->bssid));
  848. disassoc = 1;
  849. } else
  850. ieee80211_send_probe_req(sdata, ifsta->bssid,
  851. local->scan_ssid,
  852. local->scan_ssid_len);
  853. ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
  854. } else {
  855. ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
  856. if (time_after(jiffies, ifsta->last_probe +
  857. IEEE80211_PROBE_INTERVAL)) {
  858. ifsta->last_probe = jiffies;
  859. ieee80211_send_probe_req(sdata, ifsta->bssid,
  860. ifsta->ssid,
  861. ifsta->ssid_len);
  862. }
  863. }
  864. }
  865. rcu_read_unlock();
  866. if (disassoc)
  867. ieee80211_set_disassoc(sdata, ifsta, true, true,
  868. WLAN_REASON_PREV_AUTH_NOT_VALID);
  869. else
  870. mod_timer(&ifsta->timer, jiffies +
  871. IEEE80211_MONITORING_INTERVAL);
  872. }
  873. static void ieee80211_auth_completed(struct ieee80211_sub_if_data *sdata,
  874. struct ieee80211_if_sta *ifsta)
  875. {
  876. printk(KERN_DEBUG "%s: authenticated\n", sdata->dev->name);
  877. ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
  878. ieee80211_associate(sdata, ifsta);
  879. }
  880. static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
  881. struct ieee80211_if_sta *ifsta,
  882. struct ieee80211_mgmt *mgmt,
  883. size_t len)
  884. {
  885. u8 *pos;
  886. struct ieee802_11_elems elems;
  887. pos = mgmt->u.auth.variable;
  888. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  889. if (!elems.challenge)
  890. return;
  891. ieee80211_send_auth(sdata, ifsta, 3, elems.challenge - 2,
  892. elems.challenge_len + 2, 1);
  893. }
  894. static void ieee80211_send_addba_resp(struct ieee80211_sub_if_data *sdata, u8 *da, u16 tid,
  895. u8 dialog_token, u16 status, u16 policy,
  896. u16 buf_size, u16 timeout)
  897. {
  898. struct ieee80211_if_sta *ifsta = &sdata->u.sta;
  899. struct ieee80211_local *local = sdata->local;
  900. struct sk_buff *skb;
  901. struct ieee80211_mgmt *mgmt;
  902. u16 capab;
  903. skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
  904. if (!skb) {
  905. printk(KERN_DEBUG "%s: failed to allocate buffer "
  906. "for addba resp frame\n", sdata->dev->name);
  907. return;
  908. }
  909. skb_reserve(skb, local->hw.extra_tx_headroom);
  910. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
  911. memset(mgmt, 0, 24);
  912. memcpy(mgmt->da, da, ETH_ALEN);
  913. memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
  914. if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
  915. memcpy(mgmt->bssid, sdata->dev->dev_addr, ETH_ALEN);
  916. else
  917. memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
  918. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  919. IEEE80211_STYPE_ACTION);
  920. skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
  921. mgmt->u.action.category = WLAN_CATEGORY_BACK;
  922. mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
  923. mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
  924. capab = (u16)(policy << 1); /* bit 1 aggregation policy */
  925. capab |= (u16)(tid << 2); /* bit 5:2 TID number */
  926. capab |= (u16)(buf_size << 6); /* bit 15:6 max size of aggregation */
  927. mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
  928. mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
  929. mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
  930. ieee80211_sta_tx(sdata, skb, 0);
  931. return;
  932. }
  933. void ieee80211_send_addba_request(struct ieee80211_sub_if_data *sdata, const u8 *da,
  934. u16 tid, u8 dialog_token, u16 start_seq_num,
  935. u16 agg_size, u16 timeout)
  936. {
  937. struct ieee80211_local *local = sdata->local;
  938. struct ieee80211_if_sta *ifsta = &sdata->u.sta;
  939. struct sk_buff *skb;
  940. struct ieee80211_mgmt *mgmt;
  941. u16 capab;
  942. skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
  943. if (!skb) {
  944. printk(KERN_ERR "%s: failed to allocate buffer "
  945. "for addba request frame\n", sdata->dev->name);
  946. return;
  947. }
  948. skb_reserve(skb, local->hw.extra_tx_headroom);
  949. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
  950. memset(mgmt, 0, 24);
  951. memcpy(mgmt->da, da, ETH_ALEN);
  952. memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
  953. if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
  954. memcpy(mgmt->bssid, sdata->dev->dev_addr, ETH_ALEN);
  955. else
  956. memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
  957. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  958. IEEE80211_STYPE_ACTION);
  959. skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));
  960. mgmt->u.action.category = WLAN_CATEGORY_BACK;
  961. mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;
  962. mgmt->u.action.u.addba_req.dialog_token = dialog_token;
  963. capab = (u16)(1 << 1); /* bit 1 aggregation policy */
  964. capab |= (u16)(tid << 2); /* bit 5:2 TID number */
  965. capab |= (u16)(agg_size << 6); /* bit 15:6 max size of aggergation */
  966. mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);
  967. mgmt->u.action.u.addba_req.timeout = cpu_to_le16(timeout);
  968. mgmt->u.action.u.addba_req.start_seq_num =
  969. cpu_to_le16(start_seq_num << 4);
  970. ieee80211_sta_tx(sdata, skb, 0);
  971. }
  972. /*
  973. * After accepting the AddBA Request we activated a timer,
  974. * resetting it after each frame that arrives from the originator.
  975. * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
  976. */
  977. static void sta_rx_agg_session_timer_expired(unsigned long data)
  978. {
  979. /* not an elegant detour, but there is no choice as the timer passes
  980. * only one argument, and various sta_info are needed here, so init
  981. * flow in sta_info_create gives the TID as data, while the timer_to_id
  982. * array gives the sta through container_of */
  983. u8 *ptid = (u8 *)data;
  984. u8 *timer_to_id = ptid - *ptid;
  985. struct sta_info *sta = container_of(timer_to_id, struct sta_info,
  986. timer_to_tid[0]);
  987. #ifdef CONFIG_MAC80211_HT_DEBUG
  988. printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
  989. #endif
  990. ieee80211_sta_stop_rx_ba_session(sta->sdata, sta->addr,
  991. (u16)*ptid, WLAN_BACK_TIMER,
  992. WLAN_REASON_QSTA_TIMEOUT);
  993. }
  994. static void ieee80211_sta_process_addba_request(struct ieee80211_local *local,
  995. struct ieee80211_mgmt *mgmt,
  996. size_t len)
  997. {
  998. struct ieee80211_hw *hw = &local->hw;
  999. struct ieee80211_conf *conf = &hw->conf;
  1000. struct sta_info *sta;
  1001. struct tid_ampdu_rx *tid_agg_rx;
  1002. u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
  1003. u8 dialog_token;
  1004. int ret = -EOPNOTSUPP;
  1005. DECLARE_MAC_BUF(mac);
  1006. rcu_read_lock();
  1007. sta = sta_info_get(local, mgmt->sa);
  1008. if (!sta) {
  1009. rcu_read_unlock();
  1010. return;
  1011. }
  1012. /* extract session parameters from addba request frame */
  1013. dialog_token = mgmt->u.action.u.addba_req.dialog_token;
  1014. timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
  1015. start_seq_num =
  1016. le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
  1017. capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
  1018. ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
  1019. tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
  1020. buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
  1021. status = WLAN_STATUS_REQUEST_DECLINED;
  1022. /* sanity check for incoming parameters:
  1023. * check if configuration can support the BA policy
  1024. * and if buffer size does not exceeds max value */
  1025. if (((ba_policy != 1)
  1026. && (!(conf->ht_conf.cap & IEEE80211_HT_CAP_DELAY_BA)))
  1027. || (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
  1028. status = WLAN_STATUS_INVALID_QOS_PARAM;
  1029. #ifdef CONFIG_MAC80211_HT_DEBUG
  1030. if (net_ratelimit())
  1031. printk(KERN_DEBUG "AddBA Req with bad params from "
  1032. "%s on tid %u. policy %d, buffer size %d\n",
  1033. print_mac(mac, mgmt->sa), tid, ba_policy,
  1034. buf_size);
  1035. #endif /* CONFIG_MAC80211_HT_DEBUG */
  1036. goto end_no_lock;
  1037. }
  1038. /* determine default buffer size */
  1039. if (buf_size == 0) {
  1040. struct ieee80211_supported_band *sband;
  1041. sband = local->hw.wiphy->bands[conf->channel->band];
  1042. buf_size = IEEE80211_MIN_AMPDU_BUF;
  1043. buf_size = buf_size << sband->ht_info.ampdu_factor;
  1044. }
  1045. /* examine state machine */
  1046. spin_lock_bh(&sta->lock);
  1047. if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_IDLE) {
  1048. #ifdef CONFIG_MAC80211_HT_DEBUG
  1049. if (net_ratelimit())
  1050. printk(KERN_DEBUG "unexpected AddBA Req from "
  1051. "%s on tid %u\n",
  1052. print_mac(mac, mgmt->sa), tid);
  1053. #endif /* CONFIG_MAC80211_HT_DEBUG */
  1054. goto end;
  1055. }
  1056. /* prepare A-MPDU MLME for Rx aggregation */
  1057. sta->ampdu_mlme.tid_rx[tid] =
  1058. kmalloc(sizeof(struct tid_ampdu_rx), GFP_ATOMIC);
  1059. if (!sta->ampdu_mlme.tid_rx[tid]) {
  1060. #ifdef CONFIG_MAC80211_HT_DEBUG
  1061. if (net_ratelimit())
  1062. printk(KERN_ERR "allocate rx mlme to tid %d failed\n",
  1063. tid);
  1064. #endif
  1065. goto end;
  1066. }
  1067. /* rx timer */
  1068. sta->ampdu_mlme.tid_rx[tid]->session_timer.function =
  1069. sta_rx_agg_session_timer_expired;
  1070. sta->ampdu_mlme.tid_rx[tid]->session_timer.data =
  1071. (unsigned long)&sta->timer_to_tid[tid];
  1072. init_timer(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
  1073. tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
  1074. /* prepare reordering buffer */
  1075. tid_agg_rx->reorder_buf =
  1076. kmalloc(buf_size * sizeof(struct sk_buff *), GFP_ATOMIC);
  1077. if (!tid_agg_rx->reorder_buf) {
  1078. #ifdef CONFIG_MAC80211_HT_DEBUG
  1079. if (net_ratelimit())
  1080. printk(KERN_ERR "can not allocate reordering buffer "
  1081. "to tid %d\n", tid);
  1082. #endif
  1083. kfree(sta->ampdu_mlme.tid_rx[tid]);
  1084. goto end;
  1085. }
  1086. memset(tid_agg_rx->reorder_buf, 0,
  1087. buf_size * sizeof(struct sk_buff *));
  1088. if (local->ops->ampdu_action)
  1089. ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
  1090. sta->addr, tid, &start_seq_num);
  1091. #ifdef CONFIG_MAC80211_HT_DEBUG
  1092. printk(KERN_DEBUG "Rx A-MPDU request on tid %d result %d\n", tid, ret);
  1093. #endif /* CONFIG_MAC80211_HT_DEBUG */
  1094. if (ret) {
  1095. kfree(tid_agg_rx->reorder_buf);
  1096. kfree(tid_agg_rx);
  1097. sta->ampdu_mlme.tid_rx[tid] = NULL;
  1098. goto end;
  1099. }
  1100. /* change state and send addba resp */
  1101. sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_OPERATIONAL;
  1102. tid_agg_rx->dialog_token = dialog_token;
  1103. tid_agg_rx->ssn = start_seq_num;
  1104. tid_agg_rx->head_seq_num = start_seq_num;
  1105. tid_agg_rx->buf_size = buf_size;
  1106. tid_agg_rx->timeout = timeout;
  1107. tid_agg_rx->stored_mpdu_num = 0;
  1108. status = WLAN_STATUS_SUCCESS;
  1109. end:
  1110. spin_unlock_bh(&sta->lock);
  1111. end_no_lock:
  1112. ieee80211_send_addba_resp(sta->sdata, sta->addr, tid,
  1113. dialog_token, status, 1, buf_size, timeout);
  1114. rcu_read_unlock();
  1115. }
  1116. static void ieee80211_sta_process_addba_resp(struct ieee80211_local *local,
  1117. struct ieee80211_mgmt *mgmt,
  1118. size_t len)
  1119. {
  1120. struct ieee80211_hw *hw = &local->hw;
  1121. struct sta_info *sta;
  1122. u16 capab;
  1123. u16 tid;
  1124. u8 *state;
  1125. rcu_read_lock();
  1126. sta = sta_info_get(local, mgmt->sa);
  1127. if (!sta) {
  1128. rcu_read_unlock();
  1129. return;
  1130. }
  1131. capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
  1132. tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
  1133. state = &sta->ampdu_mlme.tid_state_tx[tid];
  1134. spin_lock_bh(&sta->lock);
  1135. if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
  1136. spin_unlock_bh(&sta->lock);
  1137. goto addba_resp_exit;
  1138. }
  1139. if (mgmt->u.action.u.addba_resp.dialog_token !=
  1140. sta->ampdu_mlme.tid_tx[tid]->dialog_token) {
  1141. spin_unlock_bh(&sta->lock);
  1142. #ifdef CONFIG_MAC80211_HT_DEBUG
  1143. printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
  1144. #endif /* CONFIG_MAC80211_HT_DEBUG */
  1145. goto addba_resp_exit;
  1146. }
  1147. del_timer_sync(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
  1148. #ifdef CONFIG_MAC80211_HT_DEBUG
  1149. printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
  1150. #endif /* CONFIG_MAC80211_HT_DEBUG */
  1151. if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
  1152. == WLAN_STATUS_SUCCESS) {
  1153. *state |= HT_ADDBA_RECEIVED_MSK;
  1154. sta->ampdu_mlme.addba_req_num[tid] = 0;
  1155. if (*state == HT_AGG_STATE_OPERATIONAL)
  1156. ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
  1157. spin_unlock_bh(&sta->lock);
  1158. } else {
  1159. sta->ampdu_mlme.addba_req_num[tid]++;
  1160. /* this will allow the state check in stop_BA_session */
  1161. *state = HT_AGG_STATE_OPERATIONAL;
  1162. spin_unlock_bh(&sta->lock);
  1163. ieee80211_stop_tx_ba_session(hw, sta->addr, tid,
  1164. WLAN_BACK_INITIATOR);
  1165. }
  1166. addba_resp_exit:
  1167. rcu_read_unlock();
  1168. }
  1169. void ieee80211_send_delba(struct ieee80211_sub_if_data *sdata, const u8 *da, u16 tid,
  1170. u16 initiator, u16 reason_code)
  1171. {
  1172. struct ieee80211_local *local = sdata->local;
  1173. struct ieee80211_if_sta *ifsta = &sdata->u.sta;
  1174. struct sk_buff *skb;
  1175. struct ieee80211_mgmt *mgmt;
  1176. u16 params;
  1177. skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
  1178. if (!skb) {
  1179. printk(KERN_ERR "%s: failed to allocate buffer "
  1180. "for delba frame\n", sdata->dev->name);
  1181. return;
  1182. }
  1183. skb_reserve(skb, local->hw.extra_tx_headroom);
  1184. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
  1185. memset(mgmt, 0, 24);
  1186. memcpy(mgmt->da, da, ETH_ALEN);
  1187. memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
  1188. if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
  1189. memcpy(mgmt->bssid, sdata->dev->dev_addr, ETH_ALEN);
  1190. else
  1191. memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
  1192. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  1193. IEEE80211_STYPE_ACTION);
  1194. skb_put(skb, 1 + sizeof(mgmt->u.action.u.delba));
  1195. mgmt->u.action.category = WLAN_CATEGORY_BACK;
  1196. mgmt->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
  1197. params = (u16)(initiator << 11); /* bit 11 initiator */
  1198. params |= (u16)(tid << 12); /* bit 15:12 TID number */
  1199. mgmt->u.action.u.delba.params = cpu_to_le16(params);
  1200. mgmt->u.action.u.delba.reason_code = cpu_to_le16(reason_code);
  1201. ieee80211_sta_tx(sdata, skb, 0);
  1202. }
  1203. void ieee80211_send_bar(struct ieee80211_sub_if_data *sdata, u8 *ra, u16 tid, u16 ssn)
  1204. {
  1205. struct ieee80211_local *local = sdata->local;
  1206. struct sk_buff *skb;
  1207. struct ieee80211_bar *bar;
  1208. u16 bar_control = 0;
  1209. skb = dev_alloc_skb(sizeof(*bar) + local->hw.extra_tx_headroom);
  1210. if (!skb) {
  1211. printk(KERN_ERR "%s: failed to allocate buffer for "
  1212. "bar frame\n", sdata->dev->name);
  1213. return;
  1214. }
  1215. skb_reserve(skb, local->hw.extra_tx_headroom);
  1216. bar = (struct ieee80211_bar *)skb_put(skb, sizeof(*bar));
  1217. memset(bar, 0, sizeof(*bar));
  1218. bar->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  1219. IEEE80211_STYPE_BACK_REQ);
  1220. memcpy(bar->ra, ra, ETH_ALEN);
  1221. memcpy(bar->ta, sdata->dev->dev_addr, ETH_ALEN);
  1222. bar_control |= (u16)IEEE80211_BAR_CTRL_ACK_POLICY_NORMAL;
  1223. bar_control |= (u16)IEEE80211_BAR_CTRL_CBMTID_COMPRESSED_BA;
  1224. bar_control |= (u16)(tid << 12);
  1225. bar->control = cpu_to_le16(bar_control);
  1226. bar->start_seq_num = cpu_to_le16(ssn);
  1227. ieee80211_sta_tx(sdata, skb, 0);
  1228. }
  1229. void ieee80211_sta_stop_rx_ba_session(struct ieee80211_sub_if_data *sdata, u8 *ra, u16 tid,
  1230. u16 initiator, u16 reason)
  1231. {
  1232. struct ieee80211_local *local = sdata->local;
  1233. struct ieee80211_hw *hw = &local->hw;
  1234. struct sta_info *sta;
  1235. int ret, i;
  1236. DECLARE_MAC_BUF(mac);
  1237. rcu_read_lock();
  1238. sta = sta_info_get(local, ra);
  1239. if (!sta) {
  1240. rcu_read_unlock();
  1241. return;
  1242. }
  1243. /* check if TID is in operational state */
  1244. spin_lock_bh(&sta->lock);
  1245. if (sta->ampdu_mlme.tid_state_rx[tid]
  1246. != HT_AGG_STATE_OPERATIONAL) {
  1247. spin_unlock_bh(&sta->lock);
  1248. rcu_read_unlock();
  1249. return;
  1250. }
  1251. sta->ampdu_mlme.tid_state_rx[tid] =
  1252. HT_AGG_STATE_REQ_STOP_BA_MSK |
  1253. (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
  1254. spin_unlock_bh(&sta->lock);
  1255. /* stop HW Rx aggregation. ampdu_action existence
  1256. * already verified in session init so we add the BUG_ON */
  1257. BUG_ON(!local->ops->ampdu_action);
  1258. #ifdef CONFIG_MAC80211_HT_DEBUG
  1259. printk(KERN_DEBUG "Rx BA session stop requested for %s tid %u\n",
  1260. print_mac(mac, ra), tid);
  1261. #endif /* CONFIG_MAC80211_HT_DEBUG */
  1262. ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_STOP,
  1263. ra, tid, NULL);
  1264. if (ret)
  1265. printk(KERN_DEBUG "HW problem - can not stop rx "
  1266. "aggregation for tid %d\n", tid);
  1267. /* shutdown timer has not expired */
  1268. if (initiator != WLAN_BACK_TIMER)
  1269. del_timer_sync(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
  1270. /* check if this is a self generated aggregation halt */
  1271. if (initiator == WLAN_BACK_RECIPIENT || initiator == WLAN_BACK_TIMER)
  1272. ieee80211_send_delba(sdata, ra, tid, 0, reason);
  1273. /* free the reordering buffer */
  1274. for (i = 0; i < sta->ampdu_mlme.tid_rx[tid]->buf_size; i++) {
  1275. if (sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]) {
  1276. /* release the reordered frames */
  1277. dev_kfree_skb(sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]);
  1278. sta->ampdu_mlme.tid_rx[tid]->stored_mpdu_num--;
  1279. sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i] = NULL;
  1280. }
  1281. }
  1282. /* free resources */
  1283. kfree(sta->ampdu_mlme.tid_rx[tid]->reorder_buf);
  1284. kfree(sta->ampdu_mlme.tid_rx[tid]);
  1285. sta->ampdu_mlme.tid_rx[tid] = NULL;
  1286. sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_IDLE;
  1287. rcu_read_unlock();
  1288. }
  1289. static void ieee80211_sta_process_delba(struct ieee80211_sub_if_data *sdata,
  1290. struct ieee80211_mgmt *mgmt, size_t len)
  1291. {
  1292. struct ieee80211_local *local = sdata->local;
  1293. struct sta_info *sta;
  1294. u16 tid, params;
  1295. u16 initiator;
  1296. DECLARE_MAC_BUF(mac);
  1297. rcu_read_lock();
  1298. sta = sta_info_get(local, mgmt->sa);
  1299. if (!sta) {
  1300. rcu_read_unlock();
  1301. return;
  1302. }
  1303. params = le16_to_cpu(mgmt->u.action.u.delba.params);
  1304. tid = (params & IEEE80211_DELBA_PARAM_TID_MASK) >> 12;
  1305. initiator = (params & IEEE80211_DELBA_PARAM_INITIATOR_MASK) >> 11;
  1306. #ifdef CONFIG_MAC80211_HT_DEBUG
  1307. if (net_ratelimit())
  1308. printk(KERN_DEBUG "delba from %s (%s) tid %d reason code %d\n",
  1309. print_mac(mac, mgmt->sa),
  1310. initiator ? "initiator" : "recipient", tid,
  1311. mgmt->u.action.u.delba.reason_code);
  1312. #endif /* CONFIG_MAC80211_HT_DEBUG */
  1313. if (initiator == WLAN_BACK_INITIATOR)
  1314. ieee80211_sta_stop_rx_ba_session(sdata, sta->addr, tid,
  1315. WLAN_BACK_INITIATOR, 0);
  1316. else { /* WLAN_BACK_RECIPIENT */
  1317. spin_lock_bh(&sta->lock);
  1318. sta->ampdu_mlme.tid_state_tx[tid] =
  1319. HT_AGG_STATE_OPERATIONAL;
  1320. spin_unlock_bh(&sta->lock);
  1321. ieee80211_stop_tx_ba_session(&local->hw, sta->addr, tid,
  1322. WLAN_BACK_RECIPIENT);
  1323. }
  1324. rcu_read_unlock();
  1325. }
  1326. /*
  1327. * After sending add Block Ack request we activated a timer until
  1328. * add Block Ack response will arrive from the recipient.
  1329. * If this timer expires sta_addba_resp_timer_expired will be executed.
  1330. */
  1331. void sta_addba_resp_timer_expired(unsigned long data)
  1332. {
  1333. /* not an elegant detour, but there is no choice as the timer passes
  1334. * only one argument, and both sta_info and TID are needed, so init
  1335. * flow in sta_info_create gives the TID as data, while the timer_to_id
  1336. * array gives the sta through container_of */
  1337. u16 tid = *(u8 *)data;
  1338. struct sta_info *temp_sta = container_of((void *)data,
  1339. struct sta_info, timer_to_tid[tid]);
  1340. struct ieee80211_local *local = temp_sta->local;
  1341. struct ieee80211_hw *hw = &local->hw;
  1342. struct sta_info *sta;
  1343. u8 *state;
  1344. rcu_read_lock();
  1345. sta = sta_info_get(local, temp_sta->addr);
  1346. if (!sta) {
  1347. rcu_read_unlock();
  1348. return;
  1349. }
  1350. state = &sta->ampdu_mlme.tid_state_tx[tid];
  1351. /* check if the TID waits for addBA response */
  1352. spin_lock_bh(&sta->lock);
  1353. if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
  1354. spin_unlock_bh(&sta->lock);
  1355. *state = HT_AGG_STATE_IDLE;
  1356. #ifdef CONFIG_MAC80211_HT_DEBUG
  1357. printk(KERN_DEBUG "timer expired on tid %d but we are not "
  1358. "expecting addBA response there", tid);
  1359. #endif
  1360. goto timer_expired_exit;
  1361. }
  1362. #ifdef CONFIG_MAC80211_HT_DEBUG
  1363. printk(KERN_DEBUG "addBA response timer expired on tid %d\n", tid);
  1364. #endif
  1365. /* go through the state check in stop_BA_session */
  1366. *state = HT_AGG_STATE_OPERATIONAL;
  1367. spin_unlock_bh(&sta->lock);
  1368. ieee80211_stop_tx_ba_session(hw, temp_sta->addr, tid,
  1369. WLAN_BACK_INITIATOR);
  1370. timer_expired_exit:
  1371. rcu_read_unlock();
  1372. }
  1373. void ieee80211_sta_tear_down_BA_sessions(struct ieee80211_sub_if_data *sdata, u8 *addr)
  1374. {
  1375. struct ieee80211_local *local = sdata->local;
  1376. int i;
  1377. for (i = 0; i < STA_TID_NUM; i++) {
  1378. ieee80211_stop_tx_ba_session(&local->hw, addr, i,
  1379. WLAN_BACK_INITIATOR);
  1380. ieee80211_sta_stop_rx_ba_session(sdata, addr, i,
  1381. WLAN_BACK_RECIPIENT,
  1382. WLAN_REASON_QSTA_LEAVE_QBSS);
  1383. }
  1384. }
  1385. static void ieee80211_send_refuse_measurement_request(struct ieee80211_sub_if_data *sdata,
  1386. struct ieee80211_msrment_ie *request_ie,
  1387. const u8 *da, const u8 *bssid,
  1388. u8 dialog_token)
  1389. {
  1390. struct ieee80211_local *local = sdata->local;
  1391. struct sk_buff *skb;
  1392. struct ieee80211_mgmt *msr_report;
  1393. skb = dev_alloc_skb(sizeof(*msr_report) + local->hw.extra_tx_headroom +
  1394. sizeof(struct ieee80211_msrment_ie));
  1395. if (!skb) {
  1396. printk(KERN_ERR "%s: failed to allocate buffer for "
  1397. "measurement report frame\n", sdata->dev->name);
  1398. return;
  1399. }
  1400. skb_reserve(skb, local->hw.extra_tx_headroom);
  1401. msr_report = (struct ieee80211_mgmt *)skb_put(skb, 24);
  1402. memset(msr_report, 0, 24);
  1403. memcpy(msr_report->da, da, ETH_ALEN);
  1404. memcpy(msr_report->sa, sdata->dev->dev_addr, ETH_ALEN);
  1405. memcpy(msr_report->bssid, bssid, ETH_ALEN);
  1406. msr_report->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  1407. IEEE80211_STYPE_ACTION);
  1408. skb_put(skb, 1 + sizeof(msr_report->u.action.u.measurement));
  1409. msr_report->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
  1410. msr_report->u.action.u.measurement.action_code =
  1411. WLAN_ACTION_SPCT_MSR_RPRT;
  1412. msr_report->u.action.u.measurement.dialog_token = dialog_token;
  1413. msr_report->u.action.u.measurement.element_id = WLAN_EID_MEASURE_REPORT;
  1414. msr_report->u.action.u.measurement.length =
  1415. sizeof(struct ieee80211_msrment_ie);
  1416. memset(&msr_report->u.action.u.measurement.msr_elem, 0,
  1417. sizeof(struct ieee80211_msrment_ie));
  1418. msr_report->u.action.u.measurement.msr_elem.token = request_ie->token;
  1419. msr_report->u.action.u.measurement.msr_elem.mode |=
  1420. IEEE80211_SPCT_MSR_RPRT_MODE_REFUSED;
  1421. msr_report->u.action.u.measurement.msr_elem.type = request_ie->type;
  1422. ieee80211_sta_tx(sdata, skb, 0);
  1423. }
  1424. static void ieee80211_sta_process_measurement_req(struct ieee80211_sub_if_data *sdata,
  1425. struct ieee80211_mgmt *mgmt,
  1426. size_t len)
  1427. {
  1428. /*
  1429. * Ignoring measurement request is spec violation.
  1430. * Mandatory measurements must be reported optional
  1431. * measurements might be refused or reported incapable
  1432. * For now just refuse
  1433. * TODO: Answer basic measurement as unmeasured
  1434. */
  1435. ieee80211_send_refuse_measurement_request(sdata,
  1436. &mgmt->u.action.u.measurement.msr_elem,
  1437. mgmt->sa, mgmt->bssid,
  1438. mgmt->u.action.u.measurement.dialog_token);
  1439. }
  1440. static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
  1441. struct ieee80211_if_sta *ifsta,
  1442. struct ieee80211_mgmt *mgmt,
  1443. size_t len)
  1444. {
  1445. u16 auth_alg, auth_transaction, status_code;
  1446. DECLARE_MAC_BUF(mac);
  1447. if (ifsta->state != IEEE80211_STA_MLME_AUTHENTICATE &&
  1448. sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
  1449. return;
  1450. if (len < 24 + 6)
  1451. return;
  1452. if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
  1453. memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
  1454. return;
  1455. if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
  1456. memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
  1457. return;
  1458. auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
  1459. auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
  1460. status_code = le16_to_cpu(mgmt->u.auth.status_code);
  1461. if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
  1462. /*
  1463. * IEEE 802.11 standard does not require authentication in IBSS
  1464. * networks and most implementations do not seem to use it.
  1465. * However, try to reply to authentication attempts if someone
  1466. * has actually implemented this.
  1467. */
  1468. if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1)
  1469. return;
  1470. ieee80211_send_auth(sdata, ifsta, 2, NULL, 0, 0);
  1471. }
  1472. if (auth_alg != ifsta->auth_alg ||
  1473. auth_transaction != ifsta->auth_transaction)
  1474. return;
  1475. if (status_code != WLAN_STATUS_SUCCESS) {
  1476. if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
  1477. u8 algs[3];
  1478. const int num_algs = ARRAY_SIZE(algs);
  1479. int i, pos;
  1480. algs[0] = algs[1] = algs[2] = 0xff;
  1481. if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
  1482. algs[0] = WLAN_AUTH_OPEN;
  1483. if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
  1484. algs[1] = WLAN_AUTH_SHARED_KEY;
  1485. if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
  1486. algs[2] = WLAN_AUTH_LEAP;
  1487. if (ifsta->auth_alg == WLAN_AUTH_OPEN)
  1488. pos = 0;
  1489. else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
  1490. pos = 1;
  1491. else
  1492. pos = 2;
  1493. for (i = 0; i < num_algs; i++) {
  1494. pos++;
  1495. if (pos >= num_algs)
  1496. pos = 0;
  1497. if (algs[pos] == ifsta->auth_alg ||
  1498. algs[pos] == 0xff)
  1499. continue;
  1500. if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
  1501. !ieee80211_sta_wep_configured(sdata))
  1502. continue;
  1503. ifsta->auth_alg = algs[pos];
  1504. break;
  1505. }
  1506. }
  1507. return;
  1508. }
  1509. switch (ifsta->auth_alg) {
  1510. case WLAN_AUTH_OPEN:
  1511. case WLAN_AUTH_LEAP:
  1512. ieee80211_auth_completed(sdata, ifsta);
  1513. break;
  1514. case WLAN_AUTH_SHARED_KEY:
  1515. if (ifsta->auth_transaction == 4)
  1516. ieee80211_auth_completed(sdata, ifsta);
  1517. else
  1518. ieee80211_auth_challenge(sdata, ifsta, mgmt, len);
  1519. break;
  1520. }
  1521. }
  1522. static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
  1523. struct ieee80211_if_sta *ifsta,
  1524. struct ieee80211_mgmt *mgmt,
  1525. size_t len)
  1526. {
  1527. u16 reason_code;
  1528. DECLARE_MAC_BUF(mac);
  1529. if (len < 24 + 2)
  1530. return;
  1531. if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
  1532. return;
  1533. reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
  1534. if (ifsta->flags & IEEE80211_STA_AUTHENTICATED)
  1535. printk(KERN_DEBUG "%s: deauthenticated\n", sdata->dev->name);
  1536. if (ifsta->state == IEEE80211_STA_MLME_AUTHENTICATE ||
  1537. ifsta->state == IEEE80211_STA_MLME_ASSOCIATE ||
  1538. ifsta->state == IEEE80211_STA_MLME_ASSOCIATED) {
  1539. ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
  1540. mod_timer(&ifsta->timer, jiffies +
  1541. IEEE80211_RETRY_AUTH_INTERVAL);
  1542. }
  1543. ieee80211_set_disassoc(sdata, ifsta, true, false, 0);
  1544. ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
  1545. }
  1546. static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
  1547. struct ieee80211_if_sta *ifsta,
  1548. struct ieee80211_mgmt *mgmt,
  1549. size_t len)
  1550. {
  1551. u16 reason_code;
  1552. DECLARE_MAC_BUF(mac);
  1553. if (len < 24 + 2)
  1554. return;
  1555. if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
  1556. return;
  1557. reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
  1558. if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
  1559. printk(KERN_DEBUG "%s: disassociated\n", sdata->dev->name);
  1560. if (ifsta->state == IEEE80211_STA_MLME_ASSOCIATED) {
  1561. ifsta->state = IEEE80211_STA_MLME_ASSOCIATE;
  1562. mod_timer(&ifsta->timer, jiffies +
  1563. IEEE80211_RETRY_AUTH_INTERVAL);
  1564. }
  1565. ieee80211_set_disassoc(sdata, ifsta, false, false, 0);
  1566. }
  1567. static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
  1568. struct ieee80211_if_sta *ifsta,
  1569. struct ieee80211_mgmt *mgmt,
  1570. size_t len,
  1571. int reassoc)
  1572. {
  1573. struct ieee80211_local *local = sdata->local;
  1574. struct ieee80211_supported_band *sband;
  1575. struct sta_info *sta;
  1576. u64 rates, basic_rates;
  1577. u16 capab_info, status_code, aid;
  1578. struct ieee802_11_elems elems;
  1579. struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
  1580. u8 *pos;
  1581. int i, j;
  1582. DECLARE_MAC_BUF(mac);
  1583. bool have_higher_than_11mbit = false;
  1584. /* AssocResp and ReassocResp have identical structure, so process both
  1585. * of them in this function. */
  1586. if (ifsta->state != IEEE80211_STA_MLME_ASSOCIATE)
  1587. return;
  1588. if (len < 24 + 6)
  1589. return;
  1590. if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
  1591. return;
  1592. capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
  1593. status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
  1594. aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
  1595. printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
  1596. "status=%d aid=%d)\n",
  1597. sdata->dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
  1598. capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
  1599. if (status_code != WLAN_STATUS_SUCCESS) {
  1600. printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
  1601. sdata->dev->name, status_code);
  1602. /* if this was a reassociation, ensure we try a "full"
  1603. * association next time. This works around some broken APs
  1604. * which do not correctly reject reassociation requests. */
  1605. ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
  1606. return;
  1607. }
  1608. if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
  1609. printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
  1610. "set\n", sdata->dev->name, aid);
  1611. aid &= ~(BIT(15) | BIT(14));
  1612. pos = mgmt->u.assoc_resp.variable;
  1613. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  1614. if (!elems.supp_rates) {
  1615. printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
  1616. sdata->dev->name);
  1617. return;
  1618. }
  1619. printk(KERN_DEBUG "%s: associated\n", sdata->dev->name);
  1620. ifsta->aid = aid;
  1621. ifsta->ap_capab = capab_info;
  1622. kfree(ifsta->assocresp_ies);
  1623. ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
  1624. ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
  1625. if (ifsta->assocresp_ies)
  1626. memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
  1627. rcu_read_lock();
  1628. /* Add STA entry for the AP */
  1629. sta = sta_info_get(local, ifsta->bssid);
  1630. if (!sta) {
  1631. struct ieee80211_sta_bss *bss;
  1632. int err;
  1633. sta = sta_info_alloc(sdata, ifsta->bssid, GFP_ATOMIC);
  1634. if (!sta) {
  1635. printk(KERN_DEBUG "%s: failed to alloc STA entry for"
  1636. " the AP\n", sdata->dev->name);
  1637. rcu_read_unlock();
  1638. return;
  1639. }
  1640. bss = ieee80211_rx_bss_get(local, ifsta->bssid,
  1641. local->hw.conf.channel->center_freq,
  1642. ifsta->ssid, ifsta->ssid_len);
  1643. if (bss) {
  1644. sta->last_signal = bss->signal;
  1645. sta->last_qual = bss->qual;
  1646. sta->last_noise = bss->noise;
  1647. ieee80211_rx_bss_put(local, bss);
  1648. }
  1649. err = sta_info_insert(sta);
  1650. if (err) {
  1651. printk(KERN_DEBUG "%s: failed to insert STA entry for"
  1652. " the AP (error %d)\n", sdata->dev->name, err);
  1653. rcu_read_unlock();
  1654. return;
  1655. }
  1656. /* update new sta with its last rx activity */
  1657. sta->last_rx = jiffies;
  1658. }
  1659. /*
  1660. * FIXME: Do we really need to update the sta_info's information here?
  1661. * We already know about the AP (we found it in our list) so it
  1662. * should already be filled with the right info, no?
  1663. * As is stands, all this is racy because typically we assume
  1664. * the information that is filled in here (except flags) doesn't
  1665. * change while a STA structure is alive. As such, it should move
  1666. * to between the sta_info_alloc() and sta_info_insert() above.
  1667. */
  1668. set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
  1669. WLAN_STA_AUTHORIZED);
  1670. rates = 0;
  1671. basic_rates = 0;
  1672. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  1673. for (i = 0; i < elems.supp_rates_len; i++) {
  1674. int rate = (elems.supp_rates[i] & 0x7f) * 5;
  1675. if (rate > 110)
  1676. have_higher_than_11mbit = true;
  1677. for (j = 0; j < sband->n_bitrates; j++) {
  1678. if (sband->bitrates[j].bitrate == rate)
  1679. rates |= BIT(j);
  1680. if (elems.supp_rates[i] & 0x80)
  1681. basic_rates |= BIT(j);
  1682. }
  1683. }
  1684. for (i = 0; i < elems.ext_supp_rates_len; i++) {
  1685. int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
  1686. if (rate > 110)
  1687. have_higher_than_11mbit = true;
  1688. for (j = 0; j < sband->n_bitrates; j++) {
  1689. if (sband->bitrates[j].bitrate == rate)
  1690. rates |= BIT(j);
  1691. if (elems.ext_supp_rates[i] & 0x80)
  1692. basic_rates |= BIT(j);
  1693. }
  1694. }
  1695. sta->supp_rates[local->hw.conf.channel->band] = rates;
  1696. sdata->basic_rates = basic_rates;
  1697. /* cf. IEEE 802.11 9.2.12 */
  1698. if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
  1699. have_higher_than_11mbit)
  1700. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  1701. else
  1702. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  1703. if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
  1704. (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
  1705. struct ieee80211_ht_bss_info bss_info;
  1706. ieee80211_ht_cap_ie_to_ht_info(
  1707. (struct ieee80211_ht_cap *)
  1708. elems.ht_cap_elem, &sta->ht_info);
  1709. ieee80211_ht_addt_info_ie_to_ht_bss_info(
  1710. (struct ieee80211_ht_addt_info *)
  1711. elems.ht_info_elem, &bss_info);
  1712. ieee80211_handle_ht(local, 1, &sta->ht_info, &bss_info);
  1713. }
  1714. rate_control_rate_init(sta, local);
  1715. if (elems.wmm_param) {
  1716. set_sta_flags(sta, WLAN_STA_WME);
  1717. rcu_read_unlock();
  1718. ieee80211_sta_wmm_params(local, ifsta, elems.wmm_param,
  1719. elems.wmm_param_len);
  1720. } else
  1721. rcu_read_unlock();
  1722. /* set AID and assoc capability,
  1723. * ieee80211_set_associated() will tell the driver */
  1724. bss_conf->aid = aid;
  1725. bss_conf->assoc_capability = capab_info;
  1726. ieee80211_set_associated(sdata, ifsta);
  1727. ieee80211_associated(sdata, ifsta);
  1728. }
  1729. static int ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
  1730. struct ieee80211_if_sta *ifsta,
  1731. struct ieee80211_sta_bss *bss)
  1732. {
  1733. struct ieee80211_local *local = sdata->local;
  1734. int res, rates, i, j;
  1735. struct sk_buff *skb;
  1736. struct ieee80211_mgmt *mgmt;
  1737. u8 *pos;
  1738. struct ieee80211_supported_band *sband;
  1739. union iwreq_data wrqu;
  1740. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  1741. /* Remove possible STA entries from other IBSS networks. */
  1742. sta_info_flush_delayed(sdata);
  1743. if (local->ops->reset_tsf) {
  1744. /* Reset own TSF to allow time synchronization work. */
  1745. local->ops->reset_tsf(local_to_hw(local));
  1746. }
  1747. memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
  1748. res = ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
  1749. if (res)
  1750. return res;
  1751. local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
  1752. sdata->drop_unencrypted = bss->capability &
  1753. WLAN_CAPABILITY_PRIVACY ? 1 : 0;
  1754. res = ieee80211_set_freq(sdata, bss->freq);
  1755. if (res)
  1756. return res;
  1757. /* Build IBSS probe response */
  1758. skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
  1759. if (skb) {
  1760. skb_reserve(skb, local->hw.extra_tx_headroom);
  1761. mgmt = (struct ieee80211_mgmt *)
  1762. skb_put(skb, 24 + sizeof(mgmt->u.beacon));
  1763. memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
  1764. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  1765. IEEE80211_STYPE_PROBE_RESP);
  1766. memset(mgmt->da, 0xff, ETH_ALEN);
  1767. memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
  1768. memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
  1769. mgmt->u.beacon.beacon_int =
  1770. cpu_to_le16(local->hw.conf.beacon_int);
  1771. mgmt->u.beacon.timestamp = cpu_to_le64(bss->timestamp);
  1772. mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
  1773. pos = skb_put(skb, 2 + ifsta->ssid_len);
  1774. *pos++ = WLAN_EID_SSID;
  1775. *pos++ = ifsta->ssid_len;
  1776. memcpy(pos, ifsta->ssid, ifsta->ssid_len);
  1777. rates = bss->supp_rates_len;
  1778. if (rates > 8)
  1779. rates = 8;
  1780. pos = skb_put(skb, 2 + rates);
  1781. *pos++ = WLAN_EID_SUPP_RATES;
  1782. *pos++ = rates;
  1783. memcpy(pos, bss->supp_rates, rates);
  1784. if (bss->band == IEEE80211_BAND_2GHZ) {
  1785. pos = skb_put(skb, 2 + 1);
  1786. *pos++ = WLAN_EID_DS_PARAMS;
  1787. *pos++ = 1;
  1788. *pos++ = ieee80211_frequency_to_channel(bss->freq);
  1789. }
  1790. pos = skb_put(skb, 2 + 2);
  1791. *pos++ = WLAN_EID_IBSS_PARAMS;
  1792. *pos++ = 2;
  1793. /* FIX: set ATIM window based on scan results */
  1794. *pos++ = 0;
  1795. *pos++ = 0;
  1796. if (bss->supp_rates_len > 8) {
  1797. rates = bss->supp_rates_len - 8;
  1798. pos = skb_put(skb, 2 + rates);
  1799. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  1800. *pos++ = rates;
  1801. memcpy(pos, &bss->supp_rates[8], rates);
  1802. }
  1803. ifsta->probe_resp = skb;
  1804. ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
  1805. }
  1806. rates = 0;
  1807. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  1808. for (i = 0; i < bss->supp_rates_len; i++) {
  1809. int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
  1810. for (j = 0; j < sband->n_bitrates; j++)
  1811. if (sband->bitrates[j].bitrate == bitrate)
  1812. rates |= BIT(j);
  1813. }
  1814. ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
  1815. ieee80211_sta_def_wmm_params(sdata, bss, 1);
  1816. ifsta->state = IEEE80211_STA_MLME_IBSS_JOINED;
  1817. mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
  1818. memset(&wrqu, 0, sizeof(wrqu));
  1819. memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
  1820. wireless_send_event(sdata->dev, SIOCGIWAP, &wrqu, NULL);
  1821. return res;
  1822. }
  1823. u64 ieee80211_sta_get_rates(struct ieee80211_local *local,
  1824. struct ieee802_11_elems *elems,
  1825. enum ieee80211_band band)
  1826. {
  1827. struct ieee80211_supported_band *sband;
  1828. struct ieee80211_rate *bitrates;
  1829. size_t num_rates;
  1830. u64 supp_rates;
  1831. int i, j;
  1832. sband = local->hw.wiphy->bands[band];
  1833. if (!sband) {
  1834. WARN_ON(1);
  1835. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  1836. }
  1837. bitrates = sband->bitrates;
  1838. num_rates = sband->n_bitrates;
  1839. supp_rates = 0;
  1840. for (i = 0; i < elems->supp_rates_len +
  1841. elems->ext_supp_rates_len; i++) {
  1842. u8 rate = 0;
  1843. int own_rate;
  1844. if (i < elems->supp_rates_len)
  1845. rate = elems->supp_rates[i];
  1846. else if (elems->ext_supp_rates)
  1847. rate = elems->ext_supp_rates
  1848. [i - elems->supp_rates_len];
  1849. own_rate = 5 * (rate & 0x7f);
  1850. for (j = 0; j < num_rates; j++)
  1851. if (bitrates[j].bitrate == own_rate)
  1852. supp_rates |= BIT(j);
  1853. }
  1854. return supp_rates;
  1855. }
  1856. static u64 ieee80211_sta_get_mandatory_rates(struct ieee80211_local *local,
  1857. enum ieee80211_band band)
  1858. {
  1859. struct ieee80211_supported_band *sband;
  1860. struct ieee80211_rate *bitrates;
  1861. u64 mandatory_rates;
  1862. enum ieee80211_rate_flags mandatory_flag;
  1863. int i;
  1864. sband = local->hw.wiphy->bands[band];
  1865. if (!sband) {
  1866. WARN_ON(1);
  1867. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  1868. }
  1869. if (band == IEEE80211_BAND_2GHZ)
  1870. mandatory_flag = IEEE80211_RATE_MANDATORY_B;
  1871. else
  1872. mandatory_flag = IEEE80211_RATE_MANDATORY_A;
  1873. bitrates = sband->bitrates;
  1874. mandatory_rates = 0;
  1875. for (i = 0; i < sband->n_bitrates; i++)
  1876. if (bitrates[i].flags & mandatory_flag)
  1877. mandatory_rates |= BIT(i);
  1878. return mandatory_rates;
  1879. }
  1880. static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
  1881. struct ieee80211_mgmt *mgmt,
  1882. size_t len,
  1883. struct ieee80211_rx_status *rx_status,
  1884. struct ieee802_11_elems *elems,
  1885. bool beacon)
  1886. {
  1887. struct ieee80211_local *local = sdata->local;
  1888. int freq;
  1889. struct ieee80211_sta_bss *bss;
  1890. struct sta_info *sta;
  1891. struct ieee80211_channel *channel;
  1892. u64 beacon_timestamp, rx_timestamp;
  1893. u64 supp_rates = 0;
  1894. enum ieee80211_band band = rx_status->band;
  1895. DECLARE_MAC_BUF(mac);
  1896. DECLARE_MAC_BUF(mac2);
  1897. if (elems->ds_params && elems->ds_params_len == 1)
  1898. freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
  1899. else
  1900. freq = rx_status->freq;
  1901. channel = ieee80211_get_channel(local->hw.wiphy, freq);
  1902. if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
  1903. return;
  1904. if (ieee80211_vif_is_mesh(&sdata->vif) && elems->mesh_id &&
  1905. elems->mesh_config && mesh_matches_local(elems, sdata)) {
  1906. supp_rates = ieee80211_sta_get_rates(local, elems, band);
  1907. mesh_neighbour_update(mgmt->sa, supp_rates, sdata,
  1908. mesh_peer_accepts_plinks(elems));
  1909. }
  1910. if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && elems->supp_rates &&
  1911. memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0) {
  1912. supp_rates = ieee80211_sta_get_rates(local, elems, band);
  1913. rcu_read_lock();
  1914. sta = sta_info_get(local, mgmt->sa);
  1915. if (sta) {
  1916. u64 prev_rates;
  1917. prev_rates = sta->supp_rates[band];
  1918. /* make sure mandatory rates are always added */
  1919. sta->supp_rates[band] = supp_rates |
  1920. ieee80211_sta_get_mandatory_rates(local, band);
  1921. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  1922. if (sta->supp_rates[band] != prev_rates)
  1923. printk(KERN_DEBUG "%s: updated supp_rates set "
  1924. "for %s based on beacon info (0x%llx | "
  1925. "0x%llx -> 0x%llx)\n",
  1926. sdata->dev->name, print_mac(mac, sta->addr),
  1927. (unsigned long long) prev_rates,
  1928. (unsigned long long) supp_rates,
  1929. (unsigned long long) sta->supp_rates[band]);
  1930. #endif
  1931. } else {
  1932. ieee80211_ibss_add_sta(sdata, NULL, mgmt->bssid,
  1933. mgmt->sa, supp_rates);
  1934. }
  1935. rcu_read_unlock();
  1936. }
  1937. bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
  1938. freq, beacon);
  1939. if (!bss)
  1940. return;
  1941. /* was just updated in ieee80211_bss_info_update */
  1942. beacon_timestamp = bss->timestamp;
  1943. /*
  1944. * In STA mode, the remaining parameters should not be overridden
  1945. * by beacons because they're not necessarily accurate there.
  1946. */
  1947. if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
  1948. bss->last_probe_resp && beacon) {
  1949. ieee80211_rx_bss_put(local, bss);
  1950. return;
  1951. }
  1952. /* check if we need to merge IBSS */
  1953. if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && beacon &&
  1954. bss->capability & WLAN_CAPABILITY_IBSS &&
  1955. bss->freq == local->oper_channel->center_freq &&
  1956. elems->ssid_len == sdata->u.sta.ssid_len &&
  1957. memcmp(elems->ssid, sdata->u.sta.ssid,
  1958. sdata->u.sta.ssid_len) == 0) {
  1959. if (rx_status->flag & RX_FLAG_TSFT) {
  1960. /* in order for correct IBSS merging we need mactime
  1961. *
  1962. * since mactime is defined as the time the first data
  1963. * symbol of the frame hits the PHY, and the timestamp
  1964. * of the beacon is defined as "the time that the data
  1965. * symbol containing the first bit of the timestamp is
  1966. * transmitted to the PHY plus the transmitting STA’s
  1967. * delays through its local PHY from the MAC-PHY
  1968. * interface to its interface with the WM"
  1969. * (802.11 11.1.2) - equals the time this bit arrives at
  1970. * the receiver - we have to take into account the
  1971. * offset between the two.
  1972. * e.g: at 1 MBit that means mactime is 192 usec earlier
  1973. * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
  1974. */
  1975. int rate = local->hw.wiphy->bands[band]->
  1976. bitrates[rx_status->rate_idx].bitrate;
  1977. rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
  1978. } else if (local && local->ops && local->ops->get_tsf)
  1979. /* second best option: get current TSF */
  1980. rx_timestamp = local->ops->get_tsf(local_to_hw(local));
  1981. else
  1982. /* can't merge without knowing the TSF */
  1983. rx_timestamp = -1LLU;
  1984. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  1985. printk(KERN_DEBUG "RX beacon SA=%s BSSID="
  1986. "%s TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
  1987. print_mac(mac, mgmt->sa),
  1988. print_mac(mac2, mgmt->bssid),
  1989. (unsigned long long)rx_timestamp,
  1990. (unsigned long long)beacon_timestamp,
  1991. (unsigned long long)(rx_timestamp - beacon_timestamp),
  1992. jiffies);
  1993. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  1994. if (beacon_timestamp > rx_timestamp) {
  1995. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  1996. printk(KERN_DEBUG "%s: beacon TSF higher than "
  1997. "local TSF - IBSS merge with BSSID %s\n",
  1998. sdata->dev->name, print_mac(mac, mgmt->bssid));
  1999. #endif
  2000. ieee80211_sta_join_ibss(sdata, &sdata->u.sta, bss);
  2001. ieee80211_ibss_add_sta(sdata, NULL,
  2002. mgmt->bssid, mgmt->sa,
  2003. supp_rates);
  2004. }
  2005. }
  2006. ieee80211_rx_bss_put(local, bss);
  2007. }
  2008. static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
  2009. struct ieee80211_mgmt *mgmt,
  2010. size_t len,
  2011. struct ieee80211_rx_status *rx_status)
  2012. {
  2013. size_t baselen;
  2014. struct ieee802_11_elems elems;
  2015. struct ieee80211_if_sta *ifsta = &sdata->u.sta;
  2016. if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
  2017. return; /* ignore ProbeResp to foreign address */
  2018. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  2019. if (baselen > len)
  2020. return;
  2021. ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
  2022. &elems);
  2023. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
  2024. /* direct probe may be part of the association flow */
  2025. if (test_and_clear_bit(IEEE80211_STA_REQ_DIRECT_PROBE,
  2026. &ifsta->request)) {
  2027. printk(KERN_DEBUG "%s direct probe responded\n",
  2028. sdata->dev->name);
  2029. ieee80211_authenticate(sdata, ifsta);
  2030. }
  2031. }
  2032. static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
  2033. struct ieee80211_mgmt *mgmt,
  2034. size_t len,
  2035. struct ieee80211_rx_status *rx_status)
  2036. {
  2037. struct ieee80211_if_sta *ifsta;
  2038. size_t baselen;
  2039. struct ieee802_11_elems elems;
  2040. struct ieee80211_local *local = sdata->local;
  2041. struct ieee80211_conf *conf = &local->hw.conf;
  2042. u32 changed = 0;
  2043. /* Process beacon from the current BSS */
  2044. baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
  2045. if (baselen > len)
  2046. return;
  2047. ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
  2048. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, true);
  2049. if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
  2050. return;
  2051. ifsta = &sdata->u.sta;
  2052. if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
  2053. memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
  2054. return;
  2055. ieee80211_sta_wmm_params(local, ifsta, elems.wmm_param,
  2056. elems.wmm_param_len);
  2057. if (elems.erp_info && elems.erp_info_len >= 1)
  2058. changed |= ieee80211_handle_erp_ie(sdata, elems.erp_info[0]);
  2059. else {
  2060. u16 capab = le16_to_cpu(mgmt->u.beacon.capab_info);
  2061. changed |= ieee80211_handle_protect_preamb(sdata, false,
  2062. (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
  2063. }
  2064. if (elems.ht_cap_elem && elems.ht_info_elem &&
  2065. elems.wmm_param && conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
  2066. struct ieee80211_ht_bss_info bss_info;
  2067. ieee80211_ht_addt_info_ie_to_ht_bss_info(
  2068. (struct ieee80211_ht_addt_info *)
  2069. elems.ht_info_elem, &bss_info);
  2070. changed |= ieee80211_handle_ht(local, 1, &conf->ht_conf,
  2071. &bss_info);
  2072. }
  2073. ieee80211_bss_info_change_notify(sdata, changed);
  2074. }
  2075. static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
  2076. struct ieee80211_if_sta *ifsta,
  2077. struct ieee80211_mgmt *mgmt,
  2078. size_t len,
  2079. struct ieee80211_rx_status *rx_status)
  2080. {
  2081. struct ieee80211_local *local = sdata->local;
  2082. int tx_last_beacon;
  2083. struct sk_buff *skb;
  2084. struct ieee80211_mgmt *resp;
  2085. u8 *pos, *end;
  2086. DECLARE_MAC_BUF(mac);
  2087. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  2088. DECLARE_MAC_BUF(mac2);
  2089. DECLARE_MAC_BUF(mac3);
  2090. #endif
  2091. if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS ||
  2092. ifsta->state != IEEE80211_STA_MLME_IBSS_JOINED ||
  2093. len < 24 + 2 || !ifsta->probe_resp)
  2094. return;
  2095. if (local->ops->tx_last_beacon)
  2096. tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
  2097. else
  2098. tx_last_beacon = 1;
  2099. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  2100. printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
  2101. "%s (tx_last_beacon=%d)\n",
  2102. sdata->dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
  2103. print_mac(mac3, mgmt->bssid), tx_last_beacon);
  2104. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  2105. if (!tx_last_beacon)
  2106. return;
  2107. if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
  2108. memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
  2109. return;
  2110. end = ((u8 *) mgmt) + len;
  2111. pos = mgmt->u.probe_req.variable;
  2112. if (pos[0] != WLAN_EID_SSID ||
  2113. pos + 2 + pos[1] > end) {
  2114. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  2115. printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
  2116. "from %s\n",
  2117. sdata->dev->name, print_mac(mac, mgmt->sa));
  2118. #endif
  2119. return;
  2120. }
  2121. if (pos[1] != 0 &&
  2122. (pos[1] != ifsta->ssid_len ||
  2123. memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
  2124. /* Ignore ProbeReq for foreign SSID */
  2125. return;
  2126. }
  2127. /* Reply with ProbeResp */
  2128. skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
  2129. if (!skb)
  2130. return;
  2131. resp = (struct ieee80211_mgmt *) skb->data;
  2132. memcpy(resp->da, mgmt->sa, ETH_ALEN);
  2133. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  2134. printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
  2135. sdata->dev->name, print_mac(mac, resp->da));
  2136. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  2137. ieee80211_sta_tx(sdata, skb, 0);
  2138. }
  2139. static void ieee80211_rx_mgmt_action(struct ieee80211_sub_if_data *sdata,
  2140. struct ieee80211_if_sta *ifsta,
  2141. struct ieee80211_mgmt *mgmt,
  2142. size_t len,
  2143. struct ieee80211_rx_status *rx_status)
  2144. {
  2145. struct ieee80211_local *local = sdata->local;
  2146. /* all categories we currently handle have action_code */
  2147. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  2148. return;
  2149. switch (mgmt->u.action.category) {
  2150. case WLAN_CATEGORY_SPECTRUM_MGMT:
  2151. if (local->hw.conf.channel->band != IEEE80211_BAND_5GHZ)
  2152. break;
  2153. switch (mgmt->u.action.u.measurement.action_code) {
  2154. case WLAN_ACTION_SPCT_MSR_REQ:
  2155. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2156. sizeof(mgmt->u.action.u.measurement)))
  2157. break;
  2158. ieee80211_sta_process_measurement_req(sdata, mgmt, len);
  2159. break;
  2160. }
  2161. break;
  2162. case WLAN_CATEGORY_BACK:
  2163. switch (mgmt->u.action.u.addba_req.action_code) {
  2164. case WLAN_ACTION_ADDBA_REQ:
  2165. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2166. sizeof(mgmt->u.action.u.addba_req)))
  2167. break;
  2168. ieee80211_sta_process_addba_request(local, mgmt, len);
  2169. break;
  2170. case WLAN_ACTION_ADDBA_RESP:
  2171. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2172. sizeof(mgmt->u.action.u.addba_resp)))
  2173. break;
  2174. ieee80211_sta_process_addba_resp(local, mgmt, len);
  2175. break;
  2176. case WLAN_ACTION_DELBA:
  2177. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2178. sizeof(mgmt->u.action.u.delba)))
  2179. break;
  2180. ieee80211_sta_process_delba(sdata, mgmt, len);
  2181. break;
  2182. }
  2183. break;
  2184. case PLINK_CATEGORY:
  2185. if (ieee80211_vif_is_mesh(&sdata->vif))
  2186. mesh_rx_plink_frame(sdata, mgmt, len, rx_status);
  2187. break;
  2188. case MESH_PATH_SEL_CATEGORY:
  2189. if (ieee80211_vif_is_mesh(&sdata->vif))
  2190. mesh_rx_path_sel_frame(sdata, mgmt, len);
  2191. break;
  2192. }
  2193. }
  2194. void ieee80211_sta_rx_mgmt(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
  2195. struct ieee80211_rx_status *rx_status)
  2196. {
  2197. struct ieee80211_local *local = sdata->local;
  2198. struct ieee80211_if_sta *ifsta;
  2199. struct ieee80211_mgmt *mgmt;
  2200. u16 fc;
  2201. if (skb->len < 24)
  2202. goto fail;
  2203. ifsta = &sdata->u.sta;
  2204. mgmt = (struct ieee80211_mgmt *) skb->data;
  2205. fc = le16_to_cpu(mgmt->frame_control);
  2206. switch (fc & IEEE80211_FCTL_STYPE) {
  2207. case IEEE80211_STYPE_PROBE_REQ:
  2208. case IEEE80211_STYPE_PROBE_RESP:
  2209. case IEEE80211_STYPE_BEACON:
  2210. case IEEE80211_STYPE_ACTION:
  2211. memcpy(skb->cb, rx_status, sizeof(*rx_status));
  2212. case IEEE80211_STYPE_AUTH:
  2213. case IEEE80211_STYPE_ASSOC_RESP:
  2214. case IEEE80211_STYPE_REASSOC_RESP:
  2215. case IEEE80211_STYPE_DEAUTH:
  2216. case IEEE80211_STYPE_DISASSOC:
  2217. skb_queue_tail(&ifsta->skb_queue, skb);
  2218. queue_work(local->hw.workqueue, &ifsta->work);
  2219. return;
  2220. }
  2221. fail:
  2222. kfree_skb(skb);
  2223. }
  2224. static void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
  2225. struct sk_buff *skb)
  2226. {
  2227. struct ieee80211_rx_status *rx_status;
  2228. struct ieee80211_if_sta *ifsta;
  2229. struct ieee80211_mgmt *mgmt;
  2230. u16 fc;
  2231. ifsta = &sdata->u.sta;
  2232. rx_status = (struct ieee80211_rx_status *) skb->cb;
  2233. mgmt = (struct ieee80211_mgmt *) skb->data;
  2234. fc = le16_to_cpu(mgmt->frame_control);
  2235. switch (fc & IEEE80211_FCTL_STYPE) {
  2236. case IEEE80211_STYPE_PROBE_REQ:
  2237. ieee80211_rx_mgmt_probe_req(sdata, ifsta, mgmt, skb->len,
  2238. rx_status);
  2239. break;
  2240. case IEEE80211_STYPE_PROBE_RESP:
  2241. ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len, rx_status);
  2242. break;
  2243. case IEEE80211_STYPE_BEACON:
  2244. ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
  2245. break;
  2246. case IEEE80211_STYPE_AUTH:
  2247. ieee80211_rx_mgmt_auth(sdata, ifsta, mgmt, skb->len);
  2248. break;
  2249. case IEEE80211_STYPE_ASSOC_RESP:
  2250. ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
  2251. break;
  2252. case IEEE80211_STYPE_REASSOC_RESP:
  2253. ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
  2254. break;
  2255. case IEEE80211_STYPE_DEAUTH:
  2256. ieee80211_rx_mgmt_deauth(sdata, ifsta, mgmt, skb->len);
  2257. break;
  2258. case IEEE80211_STYPE_DISASSOC:
  2259. ieee80211_rx_mgmt_disassoc(sdata, ifsta, mgmt, skb->len);
  2260. break;
  2261. case IEEE80211_STYPE_ACTION:
  2262. ieee80211_rx_mgmt_action(sdata, ifsta, mgmt, skb->len, rx_status);
  2263. break;
  2264. }
  2265. kfree_skb(skb);
  2266. }
  2267. static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
  2268. {
  2269. struct ieee80211_local *local = sdata->local;
  2270. int active = 0;
  2271. struct sta_info *sta;
  2272. rcu_read_lock();
  2273. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  2274. if (sta->sdata == sdata &&
  2275. time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
  2276. jiffies)) {
  2277. active++;
  2278. break;
  2279. }
  2280. }
  2281. rcu_read_unlock();
  2282. return active;
  2283. }
  2284. static void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata, unsigned long exp_time)
  2285. {
  2286. struct ieee80211_local *local = sdata->local;
  2287. struct sta_info *sta, *tmp;
  2288. LIST_HEAD(tmp_list);
  2289. DECLARE_MAC_BUF(mac);
  2290. unsigned long flags;
  2291. spin_lock_irqsave(&local->sta_lock, flags);
  2292. list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
  2293. if (time_after(jiffies, sta->last_rx + exp_time)) {
  2294. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  2295. printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
  2296. sdata->dev->name, print_mac(mac, sta->addr));
  2297. #endif
  2298. __sta_info_unlink(&sta);
  2299. if (sta)
  2300. list_add(&sta->list, &tmp_list);
  2301. }
  2302. spin_unlock_irqrestore(&local->sta_lock, flags);
  2303. list_for_each_entry_safe(sta, tmp, &tmp_list, list)
  2304. sta_info_destroy(sta);
  2305. }
  2306. static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata,
  2307. struct ieee80211_if_sta *ifsta)
  2308. {
  2309. mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
  2310. ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
  2311. if (ieee80211_sta_active_ibss(sdata))
  2312. return;
  2313. printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
  2314. "IBSS networks with same SSID (merge)\n", sdata->dev->name);
  2315. ieee80211_sta_req_scan(sdata, ifsta->ssid, ifsta->ssid_len);
  2316. }
  2317. #ifdef CONFIG_MAC80211_MESH
  2318. static void ieee80211_mesh_housekeeping(struct ieee80211_sub_if_data *sdata,
  2319. struct ieee80211_if_sta *ifsta)
  2320. {
  2321. bool free_plinks;
  2322. ieee80211_sta_expire(sdata, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
  2323. mesh_path_expire(sdata);
  2324. free_plinks = mesh_plink_availables(sdata);
  2325. if (free_plinks != sdata->u.sta.accepting_plinks)
  2326. ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
  2327. mod_timer(&ifsta->timer, jiffies +
  2328. IEEE80211_MESH_HOUSEKEEPING_INTERVAL);
  2329. }
  2330. void ieee80211_start_mesh(struct ieee80211_sub_if_data *sdata)
  2331. {
  2332. struct ieee80211_if_sta *ifsta;
  2333. ifsta = &sdata->u.sta;
  2334. ifsta->state = IEEE80211_STA_MLME_MESH_UP;
  2335. ieee80211_sta_timer((unsigned long)sdata);
  2336. ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
  2337. }
  2338. #endif
  2339. void ieee80211_sta_timer(unsigned long data)
  2340. {
  2341. struct ieee80211_sub_if_data *sdata =
  2342. (struct ieee80211_sub_if_data *) data;
  2343. struct ieee80211_if_sta *ifsta = &sdata->u.sta;
  2344. struct ieee80211_local *local = sdata->local;
  2345. set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
  2346. queue_work(local->hw.workqueue, &ifsta->work);
  2347. }
  2348. static void ieee80211_sta_reset_auth(struct ieee80211_sub_if_data *sdata,
  2349. struct ieee80211_if_sta *ifsta)
  2350. {
  2351. struct ieee80211_local *local = sdata->local;
  2352. if (local->ops->reset_tsf) {
  2353. /* Reset own TSF to allow time synchronization work. */
  2354. local->ops->reset_tsf(local_to_hw(local));
  2355. }
  2356. ifsta->wmm_last_param_set = -1; /* allow any WMM update */
  2357. if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
  2358. ifsta->auth_alg = WLAN_AUTH_OPEN;
  2359. else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
  2360. ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
  2361. else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
  2362. ifsta->auth_alg = WLAN_AUTH_LEAP;
  2363. else
  2364. ifsta->auth_alg = WLAN_AUTH_OPEN;
  2365. ifsta->auth_transaction = -1;
  2366. ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
  2367. ifsta->assoc_scan_tries = 0;
  2368. ifsta->direct_probe_tries = 0;
  2369. ifsta->auth_tries = 0;
  2370. ifsta->assoc_tries = 0;
  2371. netif_tx_stop_all_queues(sdata->dev);
  2372. netif_carrier_off(sdata->dev);
  2373. }
  2374. void ieee80211_sta_req_auth(struct ieee80211_sub_if_data *sdata,
  2375. struct ieee80211_if_sta *ifsta)
  2376. {
  2377. struct ieee80211_local *local = sdata->local;
  2378. if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
  2379. return;
  2380. if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
  2381. IEEE80211_STA_AUTO_BSSID_SEL)) &&
  2382. (ifsta->flags & (IEEE80211_STA_SSID_SET |
  2383. IEEE80211_STA_AUTO_SSID_SEL))) {
  2384. if (ifsta->state == IEEE80211_STA_MLME_ASSOCIATED)
  2385. ieee80211_set_disassoc(sdata, ifsta, true, true,
  2386. WLAN_REASON_DEAUTH_LEAVING);
  2387. set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
  2388. queue_work(local->hw.workqueue, &ifsta->work);
  2389. }
  2390. }
  2391. static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
  2392. const char *ssid, int ssid_len)
  2393. {
  2394. int tmp, hidden_ssid;
  2395. if (ssid_len == ifsta->ssid_len &&
  2396. !memcmp(ifsta->ssid, ssid, ssid_len))
  2397. return 1;
  2398. if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
  2399. return 0;
  2400. hidden_ssid = 1;
  2401. tmp = ssid_len;
  2402. while (tmp--) {
  2403. if (ssid[tmp] != '\0') {
  2404. hidden_ssid = 0;
  2405. break;
  2406. }
  2407. }
  2408. if (hidden_ssid && ifsta->ssid_len == ssid_len)
  2409. return 1;
  2410. if (ssid_len == 1 && ssid[0] == ' ')
  2411. return 1;
  2412. return 0;
  2413. }
  2414. static int ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata,
  2415. struct ieee80211_if_sta *ifsta)
  2416. {
  2417. struct ieee80211_local *local = sdata->local;
  2418. struct ieee80211_sta_bss *bss;
  2419. struct ieee80211_supported_band *sband;
  2420. u8 bssid[ETH_ALEN], *pos;
  2421. int i;
  2422. int ret;
  2423. DECLARE_MAC_BUF(mac);
  2424. #if 0
  2425. /* Easier testing, use fixed BSSID. */
  2426. memset(bssid, 0xfe, ETH_ALEN);
  2427. #else
  2428. /* Generate random, not broadcast, locally administered BSSID. Mix in
  2429. * own MAC address to make sure that devices that do not have proper
  2430. * random number generator get different BSSID. */
  2431. get_random_bytes(bssid, ETH_ALEN);
  2432. for (i = 0; i < ETH_ALEN; i++)
  2433. bssid[i] ^= sdata->dev->dev_addr[i];
  2434. bssid[0] &= ~0x01;
  2435. bssid[0] |= 0x02;
  2436. #endif
  2437. printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
  2438. sdata->dev->name, print_mac(mac, bssid));
  2439. bss = ieee80211_rx_bss_add(local, bssid,
  2440. local->hw.conf.channel->center_freq,
  2441. sdata->u.sta.ssid, sdata->u.sta.ssid_len);
  2442. if (!bss)
  2443. return -ENOMEM;
  2444. bss->band = local->hw.conf.channel->band;
  2445. sband = local->hw.wiphy->bands[bss->band];
  2446. if (local->hw.conf.beacon_int == 0)
  2447. local->hw.conf.beacon_int = 100;
  2448. bss->beacon_int = local->hw.conf.beacon_int;
  2449. bss->last_update = jiffies;
  2450. bss->capability = WLAN_CAPABILITY_IBSS;
  2451. if (sdata->default_key)
  2452. bss->capability |= WLAN_CAPABILITY_PRIVACY;
  2453. else
  2454. sdata->drop_unencrypted = 0;
  2455. bss->supp_rates_len = sband->n_bitrates;
  2456. pos = bss->supp_rates;
  2457. for (i = 0; i < sband->n_bitrates; i++) {
  2458. int rate = sband->bitrates[i].bitrate;
  2459. *pos++ = (u8) (rate / 5);
  2460. }
  2461. ret = ieee80211_sta_join_ibss(sdata, ifsta, bss);
  2462. ieee80211_rx_bss_put(local, bss);
  2463. return ret;
  2464. }
  2465. static int ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata,
  2466. struct ieee80211_if_sta *ifsta)
  2467. {
  2468. struct ieee80211_local *local = sdata->local;
  2469. struct ieee80211_sta_bss *bss;
  2470. int found = 0;
  2471. u8 bssid[ETH_ALEN];
  2472. int active_ibss;
  2473. DECLARE_MAC_BUF(mac);
  2474. DECLARE_MAC_BUF(mac2);
  2475. if (ifsta->ssid_len == 0)
  2476. return -EINVAL;
  2477. active_ibss = ieee80211_sta_active_ibss(sdata);
  2478. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  2479. printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
  2480. sdata->dev->name, active_ibss);
  2481. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  2482. spin_lock_bh(&local->sta_bss_lock);
  2483. list_for_each_entry(bss, &local->sta_bss_list, list) {
  2484. if (ifsta->ssid_len != bss->ssid_len ||
  2485. memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
  2486. || !(bss->capability & WLAN_CAPABILITY_IBSS))
  2487. continue;
  2488. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  2489. printk(KERN_DEBUG " bssid=%s found\n",
  2490. print_mac(mac, bss->bssid));
  2491. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  2492. memcpy(bssid, bss->bssid, ETH_ALEN);
  2493. found = 1;
  2494. if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
  2495. break;
  2496. }
  2497. spin_unlock_bh(&local->sta_bss_lock);
  2498. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  2499. if (found)
  2500. printk(KERN_DEBUG " sta_find_ibss: selected %s current "
  2501. "%s\n", print_mac(mac, bssid),
  2502. print_mac(mac2, ifsta->bssid));
  2503. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  2504. if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
  2505. int ret;
  2506. int search_freq;
  2507. if (ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL)
  2508. search_freq = bss->freq;
  2509. else
  2510. search_freq = local->hw.conf.channel->center_freq;
  2511. bss = ieee80211_rx_bss_get(local, bssid, search_freq,
  2512. ifsta->ssid, ifsta->ssid_len);
  2513. if (!bss)
  2514. goto dont_join;
  2515. printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
  2516. " based on configured SSID\n",
  2517. sdata->dev->name, print_mac(mac, bssid));
  2518. ret = ieee80211_sta_join_ibss(sdata, ifsta, bss);
  2519. ieee80211_rx_bss_put(local, bss);
  2520. return ret;
  2521. }
  2522. dont_join:
  2523. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  2524. printk(KERN_DEBUG " did not try to join ibss\n");
  2525. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  2526. /* Selected IBSS not found in current scan results - try to scan */
  2527. if (ifsta->state == IEEE80211_STA_MLME_IBSS_JOINED &&
  2528. !ieee80211_sta_active_ibss(sdata)) {
  2529. mod_timer(&ifsta->timer, jiffies +
  2530. IEEE80211_IBSS_MERGE_INTERVAL);
  2531. } else if (time_after(jiffies, local->last_scan_completed +
  2532. IEEE80211_SCAN_INTERVAL)) {
  2533. printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
  2534. "join\n", sdata->dev->name);
  2535. return ieee80211_sta_req_scan(sdata, ifsta->ssid,
  2536. ifsta->ssid_len);
  2537. } else if (ifsta->state != IEEE80211_STA_MLME_IBSS_JOINED) {
  2538. int interval = IEEE80211_SCAN_INTERVAL;
  2539. if (time_after(jiffies, ifsta->ibss_join_req +
  2540. IEEE80211_IBSS_JOIN_TIMEOUT)) {
  2541. if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
  2542. (!(local->oper_channel->flags &
  2543. IEEE80211_CHAN_NO_IBSS)))
  2544. return ieee80211_sta_create_ibss(sdata, ifsta);
  2545. if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
  2546. printk(KERN_DEBUG "%s: IBSS not allowed on"
  2547. " %d MHz\n", sdata->dev->name,
  2548. local->hw.conf.channel->center_freq);
  2549. }
  2550. /* No IBSS found - decrease scan interval and continue
  2551. * scanning. */
  2552. interval = IEEE80211_SCAN_INTERVAL_SLOW;
  2553. }
  2554. ifsta->state = IEEE80211_STA_MLME_IBSS_SEARCH;
  2555. mod_timer(&ifsta->timer, jiffies + interval);
  2556. return 0;
  2557. }
  2558. return 0;
  2559. }
  2560. int ieee80211_sta_set_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t len)
  2561. {
  2562. struct ieee80211_if_sta *ifsta;
  2563. int res;
  2564. if (len > IEEE80211_MAX_SSID_LEN)
  2565. return -EINVAL;
  2566. ifsta = &sdata->u.sta;
  2567. if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0) {
  2568. memset(ifsta->ssid, 0, sizeof(ifsta->ssid));
  2569. memcpy(ifsta->ssid, ssid, len);
  2570. ifsta->ssid_len = len;
  2571. ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
  2572. res = 0;
  2573. /*
  2574. * Hack! MLME code needs to be cleaned up to have different
  2575. * entry points for configuration and internal selection change
  2576. */
  2577. if (netif_running(sdata->dev))
  2578. res = ieee80211_if_config(sdata, IEEE80211_IFCC_SSID);
  2579. if (res) {
  2580. printk(KERN_DEBUG "%s: Failed to config new SSID to "
  2581. "the low-level driver\n", sdata->dev->name);
  2582. return res;
  2583. }
  2584. }
  2585. if (len)
  2586. ifsta->flags |= IEEE80211_STA_SSID_SET;
  2587. else
  2588. ifsta->flags &= ~IEEE80211_STA_SSID_SET;
  2589. if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
  2590. !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
  2591. ifsta->ibss_join_req = jiffies;
  2592. ifsta->state = IEEE80211_STA_MLME_IBSS_SEARCH;
  2593. return ieee80211_sta_find_ibss(sdata, ifsta);
  2594. }
  2595. return 0;
  2596. }
  2597. int ieee80211_sta_get_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t *len)
  2598. {
  2599. struct ieee80211_if_sta *ifsta = &sdata->u.sta;
  2600. memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
  2601. *len = ifsta->ssid_len;
  2602. return 0;
  2603. }
  2604. int ieee80211_sta_set_bssid(struct ieee80211_sub_if_data *sdata, u8 *bssid)
  2605. {
  2606. struct ieee80211_if_sta *ifsta;
  2607. int res;
  2608. ifsta = &sdata->u.sta;
  2609. if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
  2610. memcpy(ifsta->bssid, bssid, ETH_ALEN);
  2611. res = 0;
  2612. /*
  2613. * Hack! See also ieee80211_sta_set_ssid.
  2614. */
  2615. if (netif_running(sdata->dev))
  2616. res = ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
  2617. if (res) {
  2618. printk(KERN_DEBUG "%s: Failed to config new BSSID to "
  2619. "the low-level driver\n", sdata->dev->name);
  2620. return res;
  2621. }
  2622. }
  2623. if (is_valid_ether_addr(bssid))
  2624. ifsta->flags |= IEEE80211_STA_BSSID_SET;
  2625. else
  2626. ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
  2627. return 0;
  2628. }
  2629. int ieee80211_sta_set_extra_ie(struct ieee80211_sub_if_data *sdata, char *ie, size_t len)
  2630. {
  2631. struct ieee80211_if_sta *ifsta = &sdata->u.sta;
  2632. kfree(ifsta->extra_ie);
  2633. if (len == 0) {
  2634. ifsta->extra_ie = NULL;
  2635. ifsta->extra_ie_len = 0;
  2636. return 0;
  2637. }
  2638. ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
  2639. if (!ifsta->extra_ie) {
  2640. ifsta->extra_ie_len = 0;
  2641. return -ENOMEM;
  2642. }
  2643. memcpy(ifsta->extra_ie, ie, len);
  2644. ifsta->extra_ie_len = len;
  2645. return 0;
  2646. }
  2647. struct sta_info *ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata,
  2648. struct sk_buff *skb, u8 *bssid,
  2649. u8 *addr, u64 supp_rates)
  2650. {
  2651. struct ieee80211_local *local = sdata->local;
  2652. struct sta_info *sta;
  2653. DECLARE_MAC_BUF(mac);
  2654. int band = local->hw.conf.channel->band;
  2655. /* TODO: Could consider removing the least recently used entry and
  2656. * allow new one to be added. */
  2657. if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
  2658. if (net_ratelimit()) {
  2659. printk(KERN_DEBUG "%s: No room for a new IBSS STA "
  2660. "entry %s\n", sdata->dev->name, print_mac(mac, addr));
  2661. }
  2662. return NULL;
  2663. }
  2664. if (compare_ether_addr(bssid, sdata->u.sta.bssid))
  2665. return NULL;
  2666. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  2667. printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
  2668. wiphy_name(local->hw.wiphy), print_mac(mac, addr), sdata->dev->name);
  2669. #endif
  2670. sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
  2671. if (!sta)
  2672. return NULL;
  2673. set_sta_flags(sta, WLAN_STA_AUTHORIZED);
  2674. /* make sure mandatory rates are always added */
  2675. sta->supp_rates[band] = supp_rates |
  2676. ieee80211_sta_get_mandatory_rates(local, band);
  2677. rate_control_rate_init(sta, local);
  2678. if (sta_info_insert(sta))
  2679. return NULL;
  2680. return sta;
  2681. }
  2682. static int ieee80211_sta_config_auth(struct ieee80211_sub_if_data *sdata,
  2683. struct ieee80211_if_sta *ifsta)
  2684. {
  2685. struct ieee80211_local *local = sdata->local;
  2686. struct ieee80211_sta_bss *bss, *selected = NULL;
  2687. int top_rssi = 0, freq;
  2688. spin_lock_bh(&local->sta_bss_lock);
  2689. freq = local->oper_channel->center_freq;
  2690. list_for_each_entry(bss, &local->sta_bss_list, list) {
  2691. if (!(bss->capability & WLAN_CAPABILITY_ESS))
  2692. continue;
  2693. if ((ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
  2694. IEEE80211_STA_AUTO_BSSID_SEL |
  2695. IEEE80211_STA_AUTO_CHANNEL_SEL)) &&
  2696. (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
  2697. !!sdata->default_key))
  2698. continue;
  2699. if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
  2700. bss->freq != freq)
  2701. continue;
  2702. if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
  2703. memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
  2704. continue;
  2705. if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
  2706. !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
  2707. continue;
  2708. if (!selected || top_rssi < bss->signal) {
  2709. selected = bss;
  2710. top_rssi = bss->signal;
  2711. }
  2712. }
  2713. if (selected)
  2714. atomic_inc(&selected->users);
  2715. spin_unlock_bh(&local->sta_bss_lock);
  2716. if (selected) {
  2717. ieee80211_set_freq(sdata, selected->freq);
  2718. if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
  2719. ieee80211_sta_set_ssid(sdata, selected->ssid,
  2720. selected->ssid_len);
  2721. ieee80211_sta_set_bssid(sdata, selected->bssid);
  2722. ieee80211_sta_def_wmm_params(sdata, selected, 0);
  2723. /* Send out direct probe if no probe resp was received or
  2724. * the one we have is outdated
  2725. */
  2726. if (!selected->last_probe_resp ||
  2727. time_after(jiffies, selected->last_probe_resp
  2728. + IEEE80211_SCAN_RESULT_EXPIRE))
  2729. ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
  2730. else
  2731. ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
  2732. ieee80211_rx_bss_put(local, selected);
  2733. ieee80211_sta_reset_auth(sdata, ifsta);
  2734. return 0;
  2735. } else {
  2736. if (ifsta->assoc_scan_tries < IEEE80211_ASSOC_SCANS_MAX_TRIES) {
  2737. ifsta->assoc_scan_tries++;
  2738. if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
  2739. ieee80211_sta_start_scan(sdata, NULL, 0);
  2740. else
  2741. ieee80211_sta_start_scan(sdata, ifsta->ssid,
  2742. ifsta->ssid_len);
  2743. ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
  2744. set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
  2745. } else
  2746. ifsta->state = IEEE80211_STA_MLME_DISABLED;
  2747. }
  2748. return -1;
  2749. }
  2750. int ieee80211_sta_deauthenticate(struct ieee80211_sub_if_data *sdata, u16 reason)
  2751. {
  2752. struct ieee80211_if_sta *ifsta = &sdata->u.sta;
  2753. printk(KERN_DEBUG "%s: deauthenticating by local choice (reason=%d)\n",
  2754. sdata->dev->name, reason);
  2755. if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
  2756. sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
  2757. return -EINVAL;
  2758. ieee80211_set_disassoc(sdata, ifsta, true, true, reason);
  2759. return 0;
  2760. }
  2761. int ieee80211_sta_disassociate(struct ieee80211_sub_if_data *sdata, u16 reason)
  2762. {
  2763. struct ieee80211_if_sta *ifsta = &sdata->u.sta;
  2764. printk(KERN_DEBUG "%s: disassociating by local choice (reason=%d)\n",
  2765. sdata->dev->name, reason);
  2766. if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
  2767. return -EINVAL;
  2768. if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
  2769. return -1;
  2770. ieee80211_set_disassoc(sdata, ifsta, false, true, reason);
  2771. return 0;
  2772. }
  2773. void ieee80211_notify_mac(struct ieee80211_hw *hw,
  2774. enum ieee80211_notification_types notif_type)
  2775. {
  2776. struct ieee80211_local *local = hw_to_local(hw);
  2777. struct ieee80211_sub_if_data *sdata;
  2778. switch (notif_type) {
  2779. case IEEE80211_NOTIFY_RE_ASSOC:
  2780. rcu_read_lock();
  2781. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  2782. if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
  2783. continue;
  2784. ieee80211_sta_req_auth(sdata, &sdata->u.sta);
  2785. }
  2786. rcu_read_unlock();
  2787. break;
  2788. }
  2789. }
  2790. EXPORT_SYMBOL(ieee80211_notify_mac);
  2791. void ieee80211_sta_work(struct work_struct *work)
  2792. {
  2793. struct ieee80211_sub_if_data *sdata =
  2794. container_of(work, struct ieee80211_sub_if_data, u.sta.work);
  2795. struct ieee80211_local *local = sdata->local;
  2796. struct ieee80211_if_sta *ifsta;
  2797. struct sk_buff *skb;
  2798. if (!netif_running(sdata->dev))
  2799. return;
  2800. if (local->sta_sw_scanning || local->sta_hw_scanning)
  2801. return;
  2802. if (WARN_ON(sdata->vif.type != IEEE80211_IF_TYPE_STA &&
  2803. sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
  2804. sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT))
  2805. return;
  2806. ifsta = &sdata->u.sta;
  2807. while ((skb = skb_dequeue(&ifsta->skb_queue)))
  2808. ieee80211_sta_rx_queued_mgmt(sdata, skb);
  2809. #ifdef CONFIG_MAC80211_MESH
  2810. if (ifsta->preq_queue_len &&
  2811. time_after(jiffies,
  2812. ifsta->last_preq + msecs_to_jiffies(ifsta->mshcfg.dot11MeshHWMPpreqMinInterval)))
  2813. mesh_path_start_discovery(sdata);
  2814. #endif
  2815. if (ifsta->state != IEEE80211_STA_MLME_DIRECT_PROBE &&
  2816. ifsta->state != IEEE80211_STA_MLME_AUTHENTICATE &&
  2817. ifsta->state != IEEE80211_STA_MLME_ASSOCIATE &&
  2818. test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
  2819. if (ifsta->scan_ssid_len)
  2820. ieee80211_sta_start_scan(sdata, ifsta->scan_ssid, ifsta->scan_ssid_len);
  2821. else
  2822. ieee80211_sta_start_scan(sdata, NULL, 0);
  2823. return;
  2824. }
  2825. if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
  2826. if (ieee80211_sta_config_auth(sdata, ifsta))
  2827. return;
  2828. clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
  2829. } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
  2830. return;
  2831. switch (ifsta->state) {
  2832. case IEEE80211_STA_MLME_DISABLED:
  2833. break;
  2834. case IEEE80211_STA_MLME_DIRECT_PROBE:
  2835. ieee80211_direct_probe(sdata, ifsta);
  2836. break;
  2837. case IEEE80211_STA_MLME_AUTHENTICATE:
  2838. ieee80211_authenticate(sdata, ifsta);
  2839. break;
  2840. case IEEE80211_STA_MLME_ASSOCIATE:
  2841. ieee80211_associate(sdata, ifsta);
  2842. break;
  2843. case IEEE80211_STA_MLME_ASSOCIATED:
  2844. ieee80211_associated(sdata, ifsta);
  2845. break;
  2846. case IEEE80211_STA_MLME_IBSS_SEARCH:
  2847. ieee80211_sta_find_ibss(sdata, ifsta);
  2848. break;
  2849. case IEEE80211_STA_MLME_IBSS_JOINED:
  2850. ieee80211_sta_merge_ibss(sdata, ifsta);
  2851. break;
  2852. #ifdef CONFIG_MAC80211_MESH
  2853. case IEEE80211_STA_MLME_MESH_UP:
  2854. ieee80211_mesh_housekeeping(sdata, ifsta);
  2855. break;
  2856. #endif
  2857. default:
  2858. WARN_ON(1);
  2859. break;
  2860. }
  2861. if (ieee80211_privacy_mismatch(sdata, ifsta)) {
  2862. printk(KERN_DEBUG "%s: privacy configuration mismatch and "
  2863. "mixed-cell disabled - disassociate\n", sdata->dev->name);
  2864. ieee80211_set_disassoc(sdata, ifsta, false, true,
  2865. WLAN_REASON_UNSPECIFIED);
  2866. }
  2867. }
  2868. void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
  2869. {
  2870. struct ieee80211_sub_if_data *sdata = local->scan_sdata;
  2871. struct ieee80211_if_sta *ifsta;
  2872. if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
  2873. ifsta = &sdata->u.sta;
  2874. if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
  2875. (!(ifsta->state == IEEE80211_STA_MLME_IBSS_JOINED) &&
  2876. !ieee80211_sta_active_ibss(sdata)))
  2877. ieee80211_sta_find_ibss(sdata, ifsta);
  2878. }
  2879. }