hostap_ap.c 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Intersil Prism2 driver with Host AP (software access point) support
  4. * Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen
  5. * <j@w1.fi>
  6. * Copyright (c) 2002-2005, Jouni Malinen <j@w1.fi>
  7. *
  8. * This file is to be included into hostap.c when S/W AP functionality is
  9. * compiled.
  10. *
  11. * AP: FIX:
  12. * - if unicast Class 2 (assoc,reassoc,disassoc) frame received from
  13. * unauthenticated STA, send deauth. frame (8802.11: 5.5)
  14. * - if unicast Class 3 (data with to/from DS,deauth,pspoll) frame received
  15. * from authenticated, but unassoc STA, send disassoc frame (8802.11: 5.5)
  16. * - if unicast Class 3 received from unauthenticated STA, send deauth. frame
  17. * (8802.11: 5.5)
  18. */
  19. #include <linux/proc_fs.h>
  20. #include <linux/seq_file.h>
  21. #include <linux/delay.h>
  22. #include <linux/random.h>
  23. #include <linux/if_arp.h>
  24. #include <linux/slab.h>
  25. #include <linux/export.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/etherdevice.h>
  28. #include "hostap_wlan.h"
  29. #include "hostap.h"
  30. #include "hostap_ap.h"
  31. static int other_ap_policy[MAX_PARM_DEVICES] = { AP_OTHER_AP_SKIP_ALL,
  32. DEF_INTS };
  33. module_param_array(other_ap_policy, int, NULL, 0444);
  34. MODULE_PARM_DESC(other_ap_policy, "Other AP beacon monitoring policy (0-3)");
  35. static int ap_max_inactivity[MAX_PARM_DEVICES] = { AP_MAX_INACTIVITY_SEC,
  36. DEF_INTS };
  37. module_param_array(ap_max_inactivity, int, NULL, 0444);
  38. MODULE_PARM_DESC(ap_max_inactivity, "AP timeout (in seconds) for station "
  39. "inactivity");
  40. static int ap_bridge_packets[MAX_PARM_DEVICES] = { 1, DEF_INTS };
  41. module_param_array(ap_bridge_packets, int, NULL, 0444);
  42. MODULE_PARM_DESC(ap_bridge_packets, "Bridge packets directly between "
  43. "stations");
  44. static int autom_ap_wds[MAX_PARM_DEVICES] = { 0, DEF_INTS };
  45. module_param_array(autom_ap_wds, int, NULL, 0444);
  46. MODULE_PARM_DESC(autom_ap_wds, "Add WDS connections to other APs "
  47. "automatically");
  48. static struct sta_info* ap_get_sta(struct ap_data *ap, u8 *sta);
  49. static void hostap_event_expired_sta(struct net_device *dev,
  50. struct sta_info *sta);
  51. static void handle_add_proc_queue(struct work_struct *work);
  52. #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
  53. static void handle_wds_oper_queue(struct work_struct *work);
  54. static void prism2_send_mgmt(struct net_device *dev,
  55. u16 type_subtype, char *body,
  56. int body_len, u8 *addr, u16 tx_cb_idx);
  57. #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
  58. #ifndef PRISM2_NO_PROCFS_DEBUG
  59. static int ap_debug_proc_show(struct seq_file *m, void *v)
  60. {
  61. struct ap_data *ap = PDE_DATA(file_inode(m->file));
  62. seq_printf(m, "BridgedUnicastFrames=%u\n", ap->bridged_unicast);
  63. seq_printf(m, "BridgedMulticastFrames=%u\n", ap->bridged_multicast);
  64. seq_printf(m, "max_inactivity=%u\n", ap->max_inactivity / HZ);
  65. seq_printf(m, "bridge_packets=%u\n", ap->bridge_packets);
  66. seq_printf(m, "nullfunc_ack=%u\n", ap->nullfunc_ack);
  67. seq_printf(m, "autom_ap_wds=%u\n", ap->autom_ap_wds);
  68. seq_printf(m, "auth_algs=%u\n", ap->local->auth_algs);
  69. seq_printf(m, "tx_drop_nonassoc=%u\n", ap->tx_drop_nonassoc);
  70. return 0;
  71. }
  72. #endif /* PRISM2_NO_PROCFS_DEBUG */
  73. static void ap_sta_hash_add(struct ap_data *ap, struct sta_info *sta)
  74. {
  75. sta->hnext = ap->sta_hash[STA_HASH(sta->addr)];
  76. ap->sta_hash[STA_HASH(sta->addr)] = sta;
  77. }
  78. static void ap_sta_hash_del(struct ap_data *ap, struct sta_info *sta)
  79. {
  80. struct sta_info *s;
  81. s = ap->sta_hash[STA_HASH(sta->addr)];
  82. if (s == NULL) return;
  83. if (ether_addr_equal(s->addr, sta->addr)) {
  84. ap->sta_hash[STA_HASH(sta->addr)] = s->hnext;
  85. return;
  86. }
  87. while (s->hnext != NULL && !ether_addr_equal(s->hnext->addr, sta->addr))
  88. s = s->hnext;
  89. if (s->hnext != NULL)
  90. s->hnext = s->hnext->hnext;
  91. else
  92. printk("AP: could not remove STA %pM from hash table\n",
  93. sta->addr);
  94. }
  95. static void ap_free_sta(struct ap_data *ap, struct sta_info *sta)
  96. {
  97. if (sta->ap && sta->local)
  98. hostap_event_expired_sta(sta->local->dev, sta);
  99. if (ap->proc != NULL) {
  100. char name[20];
  101. sprintf(name, "%pM", sta->addr);
  102. remove_proc_entry(name, ap->proc);
  103. }
  104. if (sta->crypt) {
  105. sta->crypt->ops->deinit(sta->crypt->priv);
  106. kfree(sta->crypt);
  107. sta->crypt = NULL;
  108. }
  109. skb_queue_purge(&sta->tx_buf);
  110. ap->num_sta--;
  111. #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
  112. if (sta->aid > 0)
  113. ap->sta_aid[sta->aid - 1] = NULL;
  114. if (!sta->ap)
  115. kfree(sta->u.sta.challenge);
  116. del_timer_sync(&sta->timer);
  117. #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
  118. kfree(sta);
  119. }
  120. static void hostap_set_tim(local_info_t *local, int aid, int set)
  121. {
  122. if (local->func->set_tim)
  123. local->func->set_tim(local->dev, aid, set);
  124. }
  125. static void hostap_event_new_sta(struct net_device *dev, struct sta_info *sta)
  126. {
  127. union iwreq_data wrqu;
  128. memset(&wrqu, 0, sizeof(wrqu));
  129. memcpy(wrqu.addr.sa_data, sta->addr, ETH_ALEN);
  130. wrqu.addr.sa_family = ARPHRD_ETHER;
  131. wireless_send_event(dev, IWEVREGISTERED, &wrqu, NULL);
  132. }
  133. static void hostap_event_expired_sta(struct net_device *dev,
  134. struct sta_info *sta)
  135. {
  136. union iwreq_data wrqu;
  137. memset(&wrqu, 0, sizeof(wrqu));
  138. memcpy(wrqu.addr.sa_data, sta->addr, ETH_ALEN);
  139. wrqu.addr.sa_family = ARPHRD_ETHER;
  140. wireless_send_event(dev, IWEVEXPIRED, &wrqu, NULL);
  141. }
  142. #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
  143. static void ap_handle_timer(struct timer_list *t)
  144. {
  145. struct sta_info *sta = from_timer(sta, t, timer);
  146. local_info_t *local;
  147. struct ap_data *ap;
  148. unsigned long next_time = 0;
  149. int was_assoc;
  150. if (sta == NULL || sta->local == NULL || sta->local->ap == NULL) {
  151. PDEBUG(DEBUG_AP, "ap_handle_timer() called with NULL data\n");
  152. return;
  153. }
  154. local = sta->local;
  155. ap = local->ap;
  156. was_assoc = sta->flags & WLAN_STA_ASSOC;
  157. if (atomic_read(&sta->users) != 0)
  158. next_time = jiffies + HZ;
  159. else if ((sta->flags & WLAN_STA_PERM) && !(sta->flags & WLAN_STA_AUTH))
  160. next_time = jiffies + ap->max_inactivity;
  161. if (time_before(jiffies, sta->last_rx + ap->max_inactivity)) {
  162. /* station activity detected; reset timeout state */
  163. sta->timeout_next = STA_NULLFUNC;
  164. next_time = sta->last_rx + ap->max_inactivity;
  165. } else if (sta->timeout_next == STA_DISASSOC &&
  166. !(sta->flags & WLAN_STA_PENDING_POLL)) {
  167. /* STA ACKed data nullfunc frame poll */
  168. sta->timeout_next = STA_NULLFUNC;
  169. next_time = jiffies + ap->max_inactivity;
  170. }
  171. if (next_time) {
  172. sta->timer.expires = next_time;
  173. add_timer(&sta->timer);
  174. return;
  175. }
  176. if (sta->ap)
  177. sta->timeout_next = STA_DEAUTH;
  178. if (sta->timeout_next == STA_DEAUTH && !(sta->flags & WLAN_STA_PERM)) {
  179. spin_lock(&ap->sta_table_lock);
  180. ap_sta_hash_del(ap, sta);
  181. list_del(&sta->list);
  182. spin_unlock(&ap->sta_table_lock);
  183. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
  184. } else if (sta->timeout_next == STA_DISASSOC)
  185. sta->flags &= ~WLAN_STA_ASSOC;
  186. if (was_assoc && !(sta->flags & WLAN_STA_ASSOC) && !sta->ap)
  187. hostap_event_expired_sta(local->dev, sta);
  188. if (sta->timeout_next == STA_DEAUTH && sta->aid > 0 &&
  189. !skb_queue_empty(&sta->tx_buf)) {
  190. hostap_set_tim(local, sta->aid, 0);
  191. sta->flags &= ~WLAN_STA_TIM;
  192. }
  193. if (sta->ap) {
  194. if (ap->autom_ap_wds) {
  195. PDEBUG(DEBUG_AP, "%s: removing automatic WDS "
  196. "connection to AP %pM\n",
  197. local->dev->name, sta->addr);
  198. hostap_wds_link_oper(local, sta->addr, WDS_DEL);
  199. }
  200. } else if (sta->timeout_next == STA_NULLFUNC) {
  201. /* send data frame to poll STA and check whether this frame
  202. * is ACKed */
  203. /* FIX: IEEE80211_STYPE_NULLFUNC would be more appropriate, but
  204. * it is apparently not retried so TX Exc events are not
  205. * received for it */
  206. sta->flags |= WLAN_STA_PENDING_POLL;
  207. prism2_send_mgmt(local->dev, IEEE80211_FTYPE_DATA |
  208. IEEE80211_STYPE_DATA, NULL, 0,
  209. sta->addr, ap->tx_callback_poll);
  210. } else {
  211. int deauth = sta->timeout_next == STA_DEAUTH;
  212. __le16 resp;
  213. PDEBUG(DEBUG_AP, "%s: sending %s info to STA %pM"
  214. "(last=%lu, jiffies=%lu)\n",
  215. local->dev->name,
  216. deauth ? "deauthentication" : "disassociation",
  217. sta->addr, sta->last_rx, jiffies);
  218. resp = cpu_to_le16(deauth ? WLAN_REASON_PREV_AUTH_NOT_VALID :
  219. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
  220. prism2_send_mgmt(local->dev, IEEE80211_FTYPE_MGMT |
  221. (deauth ? IEEE80211_STYPE_DEAUTH :
  222. IEEE80211_STYPE_DISASSOC),
  223. (char *) &resp, 2, sta->addr, 0);
  224. }
  225. if (sta->timeout_next == STA_DEAUTH) {
  226. if (sta->flags & WLAN_STA_PERM) {
  227. PDEBUG(DEBUG_AP, "%s: STA %pM"
  228. " would have been removed, "
  229. "but it has 'perm' flag\n",
  230. local->dev->name, sta->addr);
  231. } else
  232. ap_free_sta(ap, sta);
  233. return;
  234. }
  235. if (sta->timeout_next == STA_NULLFUNC) {
  236. sta->timeout_next = STA_DISASSOC;
  237. sta->timer.expires = jiffies + AP_DISASSOC_DELAY;
  238. } else {
  239. sta->timeout_next = STA_DEAUTH;
  240. sta->timer.expires = jiffies + AP_DEAUTH_DELAY;
  241. }
  242. add_timer(&sta->timer);
  243. }
  244. void hostap_deauth_all_stas(struct net_device *dev, struct ap_data *ap,
  245. int resend)
  246. {
  247. u8 addr[ETH_ALEN];
  248. __le16 resp;
  249. int i;
  250. PDEBUG(DEBUG_AP, "%s: Deauthenticate all stations\n", dev->name);
  251. eth_broadcast_addr(addr);
  252. resp = cpu_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
  253. /* deauth message sent; try to resend it few times; the message is
  254. * broadcast, so it may be delayed until next DTIM; there is not much
  255. * else we can do at this point since the driver is going to be shut
  256. * down */
  257. for (i = 0; i < 5; i++) {
  258. prism2_send_mgmt(dev, IEEE80211_FTYPE_MGMT |
  259. IEEE80211_STYPE_DEAUTH,
  260. (char *) &resp, 2, addr, 0);
  261. if (!resend || ap->num_sta <= 0)
  262. return;
  263. mdelay(50);
  264. }
  265. }
  266. static int ap_control_proc_show(struct seq_file *m, void *v)
  267. {
  268. struct ap_data *ap = PDE_DATA(file_inode(m->file));
  269. char *policy_txt;
  270. struct mac_entry *entry;
  271. if (v == SEQ_START_TOKEN) {
  272. switch (ap->mac_restrictions.policy) {
  273. case MAC_POLICY_OPEN:
  274. policy_txt = "open";
  275. break;
  276. case MAC_POLICY_ALLOW:
  277. policy_txt = "allow";
  278. break;
  279. case MAC_POLICY_DENY:
  280. policy_txt = "deny";
  281. break;
  282. default:
  283. policy_txt = "unknown";
  284. break;
  285. }
  286. seq_printf(m, "MAC policy: %s\n", policy_txt);
  287. seq_printf(m, "MAC entries: %u\n", ap->mac_restrictions.entries);
  288. seq_puts(m, "MAC list:\n");
  289. return 0;
  290. }
  291. entry = v;
  292. seq_printf(m, "%pM\n", entry->addr);
  293. return 0;
  294. }
  295. static void *ap_control_proc_start(struct seq_file *m, loff_t *_pos)
  296. {
  297. struct ap_data *ap = PDE_DATA(file_inode(m->file));
  298. spin_lock_bh(&ap->mac_restrictions.lock);
  299. return seq_list_start_head(&ap->mac_restrictions.mac_list, *_pos);
  300. }
  301. static void *ap_control_proc_next(struct seq_file *m, void *v, loff_t *_pos)
  302. {
  303. struct ap_data *ap = PDE_DATA(file_inode(m->file));
  304. return seq_list_next(v, &ap->mac_restrictions.mac_list, _pos);
  305. }
  306. static void ap_control_proc_stop(struct seq_file *m, void *v)
  307. {
  308. struct ap_data *ap = PDE_DATA(file_inode(m->file));
  309. spin_unlock_bh(&ap->mac_restrictions.lock);
  310. }
  311. static const struct seq_operations ap_control_proc_seqops = {
  312. .start = ap_control_proc_start,
  313. .next = ap_control_proc_next,
  314. .stop = ap_control_proc_stop,
  315. .show = ap_control_proc_show,
  316. };
  317. int ap_control_add_mac(struct mac_restrictions *mac_restrictions, u8 *mac)
  318. {
  319. struct mac_entry *entry;
  320. entry = kmalloc(sizeof(struct mac_entry), GFP_KERNEL);
  321. if (entry == NULL)
  322. return -ENOMEM;
  323. memcpy(entry->addr, mac, ETH_ALEN);
  324. spin_lock_bh(&mac_restrictions->lock);
  325. list_add_tail(&entry->list, &mac_restrictions->mac_list);
  326. mac_restrictions->entries++;
  327. spin_unlock_bh(&mac_restrictions->lock);
  328. return 0;
  329. }
  330. int ap_control_del_mac(struct mac_restrictions *mac_restrictions, u8 *mac)
  331. {
  332. struct list_head *ptr;
  333. struct mac_entry *entry;
  334. spin_lock_bh(&mac_restrictions->lock);
  335. for (ptr = mac_restrictions->mac_list.next;
  336. ptr != &mac_restrictions->mac_list; ptr = ptr->next) {
  337. entry = list_entry(ptr, struct mac_entry, list);
  338. if (ether_addr_equal(entry->addr, mac)) {
  339. list_del(ptr);
  340. kfree(entry);
  341. mac_restrictions->entries--;
  342. spin_unlock_bh(&mac_restrictions->lock);
  343. return 0;
  344. }
  345. }
  346. spin_unlock_bh(&mac_restrictions->lock);
  347. return -1;
  348. }
  349. static int ap_control_mac_deny(struct mac_restrictions *mac_restrictions,
  350. u8 *mac)
  351. {
  352. struct mac_entry *entry;
  353. int found = 0;
  354. if (mac_restrictions->policy == MAC_POLICY_OPEN)
  355. return 0;
  356. spin_lock_bh(&mac_restrictions->lock);
  357. list_for_each_entry(entry, &mac_restrictions->mac_list, list) {
  358. if (ether_addr_equal(entry->addr, mac)) {
  359. found = 1;
  360. break;
  361. }
  362. }
  363. spin_unlock_bh(&mac_restrictions->lock);
  364. if (mac_restrictions->policy == MAC_POLICY_ALLOW)
  365. return !found;
  366. else
  367. return found;
  368. }
  369. void ap_control_flush_macs(struct mac_restrictions *mac_restrictions)
  370. {
  371. struct list_head *ptr, *n;
  372. struct mac_entry *entry;
  373. if (mac_restrictions->entries == 0)
  374. return;
  375. spin_lock_bh(&mac_restrictions->lock);
  376. for (ptr = mac_restrictions->mac_list.next, n = ptr->next;
  377. ptr != &mac_restrictions->mac_list;
  378. ptr = n, n = ptr->next) {
  379. entry = list_entry(ptr, struct mac_entry, list);
  380. list_del(ptr);
  381. kfree(entry);
  382. }
  383. mac_restrictions->entries = 0;
  384. spin_unlock_bh(&mac_restrictions->lock);
  385. }
  386. int ap_control_kick_mac(struct ap_data *ap, struct net_device *dev, u8 *mac)
  387. {
  388. struct sta_info *sta;
  389. __le16 resp;
  390. spin_lock_bh(&ap->sta_table_lock);
  391. sta = ap_get_sta(ap, mac);
  392. if (sta) {
  393. ap_sta_hash_del(ap, sta);
  394. list_del(&sta->list);
  395. }
  396. spin_unlock_bh(&ap->sta_table_lock);
  397. if (!sta)
  398. return -EINVAL;
  399. resp = cpu_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
  400. prism2_send_mgmt(dev, IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_DEAUTH,
  401. (char *) &resp, 2, sta->addr, 0);
  402. if ((sta->flags & WLAN_STA_ASSOC) && !sta->ap)
  403. hostap_event_expired_sta(dev, sta);
  404. ap_free_sta(ap, sta);
  405. return 0;
  406. }
  407. #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
  408. void ap_control_kickall(struct ap_data *ap)
  409. {
  410. struct list_head *ptr, *n;
  411. struct sta_info *sta;
  412. spin_lock_bh(&ap->sta_table_lock);
  413. for (ptr = ap->sta_list.next, n = ptr->next; ptr != &ap->sta_list;
  414. ptr = n, n = ptr->next) {
  415. sta = list_entry(ptr, struct sta_info, list);
  416. ap_sta_hash_del(ap, sta);
  417. list_del(&sta->list);
  418. if ((sta->flags & WLAN_STA_ASSOC) && !sta->ap && sta->local)
  419. hostap_event_expired_sta(sta->local->dev, sta);
  420. ap_free_sta(ap, sta);
  421. }
  422. spin_unlock_bh(&ap->sta_table_lock);
  423. }
  424. #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
  425. static int prism2_ap_proc_show(struct seq_file *m, void *v)
  426. {
  427. struct sta_info *sta = v;
  428. int i;
  429. if (v == SEQ_START_TOKEN) {
  430. seq_printf(m, "# BSSID CHAN SIGNAL NOISE RATE SSID FLAGS\n");
  431. return 0;
  432. }
  433. if (!sta->ap)
  434. return 0;
  435. seq_printf(m, "%pM %d %d %d %d '",
  436. sta->addr,
  437. sta->u.ap.channel, sta->last_rx_signal,
  438. sta->last_rx_silence, sta->last_rx_rate);
  439. for (i = 0; i < sta->u.ap.ssid_len; i++) {
  440. if (sta->u.ap.ssid[i] >= 32 && sta->u.ap.ssid[i] < 127)
  441. seq_putc(m, sta->u.ap.ssid[i]);
  442. else
  443. seq_printf(m, "<%02x>", sta->u.ap.ssid[i]);
  444. }
  445. seq_putc(m, '\'');
  446. if (sta->capability & WLAN_CAPABILITY_ESS)
  447. seq_puts(m, " [ESS]");
  448. if (sta->capability & WLAN_CAPABILITY_IBSS)
  449. seq_puts(m, " [IBSS]");
  450. if (sta->capability & WLAN_CAPABILITY_PRIVACY)
  451. seq_puts(m, " [WEP]");
  452. seq_putc(m, '\n');
  453. return 0;
  454. }
  455. static void *prism2_ap_proc_start(struct seq_file *m, loff_t *_pos)
  456. {
  457. struct ap_data *ap = PDE_DATA(file_inode(m->file));
  458. spin_lock_bh(&ap->sta_table_lock);
  459. return seq_list_start_head(&ap->sta_list, *_pos);
  460. }
  461. static void *prism2_ap_proc_next(struct seq_file *m, void *v, loff_t *_pos)
  462. {
  463. struct ap_data *ap = PDE_DATA(file_inode(m->file));
  464. return seq_list_next(v, &ap->sta_list, _pos);
  465. }
  466. static void prism2_ap_proc_stop(struct seq_file *m, void *v)
  467. {
  468. struct ap_data *ap = PDE_DATA(file_inode(m->file));
  469. spin_unlock_bh(&ap->sta_table_lock);
  470. }
  471. static const struct seq_operations prism2_ap_proc_seqops = {
  472. .start = prism2_ap_proc_start,
  473. .next = prism2_ap_proc_next,
  474. .stop = prism2_ap_proc_stop,
  475. .show = prism2_ap_proc_show,
  476. };
  477. #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
  478. void hostap_check_sta_fw_version(struct ap_data *ap, int sta_fw_ver)
  479. {
  480. if (!ap)
  481. return;
  482. if (sta_fw_ver == PRISM2_FW_VER(0,8,0)) {
  483. PDEBUG(DEBUG_AP, "Using data::nullfunc ACK workaround - "
  484. "firmware upgrade recommended\n");
  485. ap->nullfunc_ack = 1;
  486. } else
  487. ap->nullfunc_ack = 0;
  488. if (sta_fw_ver == PRISM2_FW_VER(1,4,2)) {
  489. printk(KERN_WARNING "%s: Warning: secondary station firmware "
  490. "version 1.4.2 does not seem to work in Host AP mode\n",
  491. ap->local->dev->name);
  492. }
  493. }
  494. /* Called only as a tasklet (software IRQ) */
  495. static void hostap_ap_tx_cb(struct sk_buff *skb, int ok, void *data)
  496. {
  497. struct ap_data *ap = data;
  498. struct ieee80211_hdr *hdr;
  499. if (!ap->local->hostapd || !ap->local->apdev) {
  500. dev_kfree_skb(skb);
  501. return;
  502. }
  503. /* Pass the TX callback frame to the hostapd; use 802.11 header version
  504. * 1 to indicate failure (no ACK) and 2 success (frame ACKed) */
  505. hdr = (struct ieee80211_hdr *) skb->data;
  506. hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_VERS);
  507. hdr->frame_control |= cpu_to_le16(ok ? BIT(1) : BIT(0));
  508. skb->dev = ap->local->apdev;
  509. skb_pull(skb, hostap_80211_get_hdrlen(hdr->frame_control));
  510. skb->pkt_type = PACKET_OTHERHOST;
  511. skb->protocol = cpu_to_be16(ETH_P_802_2);
  512. memset(skb->cb, 0, sizeof(skb->cb));
  513. netif_rx(skb);
  514. }
  515. #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
  516. /* Called only as a tasklet (software IRQ) */
  517. static void hostap_ap_tx_cb_auth(struct sk_buff *skb, int ok, void *data)
  518. {
  519. struct ap_data *ap = data;
  520. struct net_device *dev = ap->local->dev;
  521. struct ieee80211_hdr *hdr;
  522. u16 auth_alg, auth_transaction, status;
  523. __le16 *pos;
  524. struct sta_info *sta = NULL;
  525. char *txt = NULL;
  526. if (ap->local->hostapd) {
  527. dev_kfree_skb(skb);
  528. return;
  529. }
  530. hdr = (struct ieee80211_hdr *) skb->data;
  531. if (!ieee80211_is_auth(hdr->frame_control) ||
  532. skb->len < IEEE80211_MGMT_HDR_LEN + 6) {
  533. printk(KERN_DEBUG "%s: hostap_ap_tx_cb_auth received invalid "
  534. "frame\n", dev->name);
  535. dev_kfree_skb(skb);
  536. return;
  537. }
  538. pos = (__le16 *) (skb->data + IEEE80211_MGMT_HDR_LEN);
  539. auth_alg = le16_to_cpu(*pos++);
  540. auth_transaction = le16_to_cpu(*pos++);
  541. status = le16_to_cpu(*pos++);
  542. if (!ok) {
  543. txt = "frame was not ACKed";
  544. goto done;
  545. }
  546. spin_lock(&ap->sta_table_lock);
  547. sta = ap_get_sta(ap, hdr->addr1);
  548. if (sta)
  549. atomic_inc(&sta->users);
  550. spin_unlock(&ap->sta_table_lock);
  551. if (!sta) {
  552. txt = "STA not found";
  553. goto done;
  554. }
  555. if (status == WLAN_STATUS_SUCCESS &&
  556. ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
  557. (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
  558. txt = "STA authenticated";
  559. sta->flags |= WLAN_STA_AUTH;
  560. sta->last_auth = jiffies;
  561. } else if (status != WLAN_STATUS_SUCCESS)
  562. txt = "authentication failed";
  563. done:
  564. if (sta)
  565. atomic_dec(&sta->users);
  566. if (txt) {
  567. PDEBUG(DEBUG_AP, "%s: %pM auth_cb - alg=%d "
  568. "trans#=%d status=%d - %s\n",
  569. dev->name, hdr->addr1,
  570. auth_alg, auth_transaction, status, txt);
  571. }
  572. dev_kfree_skb(skb);
  573. }
  574. /* Called only as a tasklet (software IRQ) */
  575. static void hostap_ap_tx_cb_assoc(struct sk_buff *skb, int ok, void *data)
  576. {
  577. struct ap_data *ap = data;
  578. struct net_device *dev = ap->local->dev;
  579. struct ieee80211_hdr *hdr;
  580. u16 status;
  581. __le16 *pos;
  582. struct sta_info *sta = NULL;
  583. char *txt = NULL;
  584. if (ap->local->hostapd) {
  585. dev_kfree_skb(skb);
  586. return;
  587. }
  588. hdr = (struct ieee80211_hdr *) skb->data;
  589. if ((!ieee80211_is_assoc_resp(hdr->frame_control) &&
  590. !ieee80211_is_reassoc_resp(hdr->frame_control)) ||
  591. skb->len < IEEE80211_MGMT_HDR_LEN + 4) {
  592. printk(KERN_DEBUG "%s: hostap_ap_tx_cb_assoc received invalid "
  593. "frame\n", dev->name);
  594. dev_kfree_skb(skb);
  595. return;
  596. }
  597. if (!ok) {
  598. txt = "frame was not ACKed";
  599. goto done;
  600. }
  601. spin_lock(&ap->sta_table_lock);
  602. sta = ap_get_sta(ap, hdr->addr1);
  603. if (sta)
  604. atomic_inc(&sta->users);
  605. spin_unlock(&ap->sta_table_lock);
  606. if (!sta) {
  607. txt = "STA not found";
  608. goto done;
  609. }
  610. pos = (__le16 *) (skb->data + IEEE80211_MGMT_HDR_LEN);
  611. pos++;
  612. status = le16_to_cpu(*pos++);
  613. if (status == WLAN_STATUS_SUCCESS) {
  614. if (!(sta->flags & WLAN_STA_ASSOC))
  615. hostap_event_new_sta(dev, sta);
  616. txt = "STA associated";
  617. sta->flags |= WLAN_STA_ASSOC;
  618. sta->last_assoc = jiffies;
  619. } else
  620. txt = "association failed";
  621. done:
  622. if (sta)
  623. atomic_dec(&sta->users);
  624. if (txt) {
  625. PDEBUG(DEBUG_AP, "%s: %pM assoc_cb - %s\n",
  626. dev->name, hdr->addr1, txt);
  627. }
  628. dev_kfree_skb(skb);
  629. }
  630. /* Called only as a tasklet (software IRQ); TX callback for poll frames used
  631. * in verifying whether the STA is still present. */
  632. static void hostap_ap_tx_cb_poll(struct sk_buff *skb, int ok, void *data)
  633. {
  634. struct ap_data *ap = data;
  635. struct ieee80211_hdr *hdr;
  636. struct sta_info *sta;
  637. if (skb->len < 24)
  638. goto fail;
  639. hdr = (struct ieee80211_hdr *) skb->data;
  640. if (ok) {
  641. spin_lock(&ap->sta_table_lock);
  642. sta = ap_get_sta(ap, hdr->addr1);
  643. if (sta)
  644. sta->flags &= ~WLAN_STA_PENDING_POLL;
  645. spin_unlock(&ap->sta_table_lock);
  646. } else {
  647. PDEBUG(DEBUG_AP,
  648. "%s: STA %pM did not ACK activity poll frame\n",
  649. ap->local->dev->name, hdr->addr1);
  650. }
  651. fail:
  652. dev_kfree_skb(skb);
  653. }
  654. #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
  655. void hostap_init_data(local_info_t *local)
  656. {
  657. struct ap_data *ap = local->ap;
  658. if (ap == NULL) {
  659. printk(KERN_WARNING "hostap_init_data: ap == NULL\n");
  660. return;
  661. }
  662. memset(ap, 0, sizeof(struct ap_data));
  663. ap->local = local;
  664. ap->ap_policy = GET_INT_PARM(other_ap_policy, local->card_idx);
  665. ap->bridge_packets = GET_INT_PARM(ap_bridge_packets, local->card_idx);
  666. ap->max_inactivity =
  667. GET_INT_PARM(ap_max_inactivity, local->card_idx) * HZ;
  668. ap->autom_ap_wds = GET_INT_PARM(autom_ap_wds, local->card_idx);
  669. spin_lock_init(&ap->sta_table_lock);
  670. INIT_LIST_HEAD(&ap->sta_list);
  671. /* Initialize task queue structure for AP management */
  672. INIT_WORK(&local->ap->add_sta_proc_queue, handle_add_proc_queue);
  673. ap->tx_callback_idx =
  674. hostap_tx_callback_register(local, hostap_ap_tx_cb, ap);
  675. if (ap->tx_callback_idx == 0)
  676. printk(KERN_WARNING "%s: failed to register TX callback for "
  677. "AP\n", local->dev->name);
  678. #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
  679. INIT_WORK(&local->ap->wds_oper_queue, handle_wds_oper_queue);
  680. ap->tx_callback_auth =
  681. hostap_tx_callback_register(local, hostap_ap_tx_cb_auth, ap);
  682. ap->tx_callback_assoc =
  683. hostap_tx_callback_register(local, hostap_ap_tx_cb_assoc, ap);
  684. ap->tx_callback_poll =
  685. hostap_tx_callback_register(local, hostap_ap_tx_cb_poll, ap);
  686. if (ap->tx_callback_auth == 0 || ap->tx_callback_assoc == 0 ||
  687. ap->tx_callback_poll == 0)
  688. printk(KERN_WARNING "%s: failed to register TX callback for "
  689. "AP\n", local->dev->name);
  690. spin_lock_init(&ap->mac_restrictions.lock);
  691. INIT_LIST_HEAD(&ap->mac_restrictions.mac_list);
  692. #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
  693. ap->initialized = 1;
  694. }
  695. void hostap_init_ap_proc(local_info_t *local)
  696. {
  697. struct ap_data *ap = local->ap;
  698. ap->proc = local->proc;
  699. if (ap->proc == NULL)
  700. return;
  701. #ifndef PRISM2_NO_PROCFS_DEBUG
  702. proc_create_single_data("ap_debug", 0, ap->proc, ap_debug_proc_show, ap);
  703. #endif /* PRISM2_NO_PROCFS_DEBUG */
  704. #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
  705. proc_create_seq_data("ap_control", 0, ap->proc, &ap_control_proc_seqops,
  706. ap);
  707. proc_create_seq_data("ap", 0, ap->proc, &prism2_ap_proc_seqops, ap);
  708. #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
  709. }
  710. void hostap_free_data(struct ap_data *ap)
  711. {
  712. struct sta_info *n, *sta;
  713. if (ap == NULL || !ap->initialized) {
  714. printk(KERN_DEBUG "hostap_free_data: ap has not yet been "
  715. "initialized - skip resource freeing\n");
  716. return;
  717. }
  718. flush_work(&ap->add_sta_proc_queue);
  719. #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
  720. flush_work(&ap->wds_oper_queue);
  721. if (ap->crypt)
  722. ap->crypt->deinit(ap->crypt_priv);
  723. ap->crypt = ap->crypt_priv = NULL;
  724. #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
  725. list_for_each_entry_safe(sta, n, &ap->sta_list, list) {
  726. ap_sta_hash_del(ap, sta);
  727. list_del(&sta->list);
  728. if ((sta->flags & WLAN_STA_ASSOC) && !sta->ap && sta->local)
  729. hostap_event_expired_sta(sta->local->dev, sta);
  730. ap_free_sta(ap, sta);
  731. }
  732. #ifndef PRISM2_NO_PROCFS_DEBUG
  733. if (ap->proc != NULL) {
  734. remove_proc_entry("ap_debug", ap->proc);
  735. }
  736. #endif /* PRISM2_NO_PROCFS_DEBUG */
  737. #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
  738. if (ap->proc != NULL) {
  739. remove_proc_entry("ap", ap->proc);
  740. remove_proc_entry("ap_control", ap->proc);
  741. }
  742. ap_control_flush_macs(&ap->mac_restrictions);
  743. #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
  744. ap->initialized = 0;
  745. }
  746. /* caller should have mutex for AP STA list handling */
  747. static struct sta_info* ap_get_sta(struct ap_data *ap, u8 *sta)
  748. {
  749. struct sta_info *s;
  750. s = ap->sta_hash[STA_HASH(sta)];
  751. while (s != NULL && !ether_addr_equal(s->addr, sta))
  752. s = s->hnext;
  753. return s;
  754. }
  755. #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
  756. /* Called from timer handler and from scheduled AP queue handlers */
  757. static void prism2_send_mgmt(struct net_device *dev,
  758. u16 type_subtype, char *body,
  759. int body_len, u8 *addr, u16 tx_cb_idx)
  760. {
  761. struct hostap_interface *iface;
  762. local_info_t *local;
  763. struct ieee80211_hdr *hdr;
  764. u16 fc;
  765. struct sk_buff *skb;
  766. struct hostap_skb_tx_data *meta;
  767. int hdrlen;
  768. iface = netdev_priv(dev);
  769. local = iface->local;
  770. dev = local->dev; /* always use master radio device */
  771. iface = netdev_priv(dev);
  772. if (!(dev->flags & IFF_UP)) {
  773. PDEBUG(DEBUG_AP, "%s: prism2_send_mgmt - device is not UP - "
  774. "cannot send frame\n", dev->name);
  775. return;
  776. }
  777. skb = dev_alloc_skb(sizeof(*hdr) + body_len);
  778. if (skb == NULL) {
  779. PDEBUG(DEBUG_AP, "%s: prism2_send_mgmt failed to allocate "
  780. "skb\n", dev->name);
  781. return;
  782. }
  783. fc = type_subtype;
  784. hdrlen = hostap_80211_get_hdrlen(cpu_to_le16(type_subtype));
  785. hdr = skb_put_zero(skb, hdrlen);
  786. if (body)
  787. skb_put_data(skb, body, body_len);
  788. /* FIX: ctrl::ack sending used special HFA384X_TX_CTRL_802_11
  789. * tx_control instead of using local->tx_control */
  790. memcpy(hdr->addr1, addr, ETH_ALEN); /* DA / RA */
  791. if (ieee80211_is_data(hdr->frame_control)) {
  792. fc |= IEEE80211_FCTL_FROMDS;
  793. memcpy(hdr->addr2, dev->dev_addr, ETH_ALEN); /* BSSID */
  794. memcpy(hdr->addr3, dev->dev_addr, ETH_ALEN); /* SA */
  795. } else if (ieee80211_is_ctl(hdr->frame_control)) {
  796. /* control:ACK does not have addr2 or addr3 */
  797. eth_zero_addr(hdr->addr2);
  798. eth_zero_addr(hdr->addr3);
  799. } else {
  800. memcpy(hdr->addr2, dev->dev_addr, ETH_ALEN); /* SA */
  801. memcpy(hdr->addr3, dev->dev_addr, ETH_ALEN); /* BSSID */
  802. }
  803. hdr->frame_control = cpu_to_le16(fc);
  804. meta = (struct hostap_skb_tx_data *) skb->cb;
  805. memset(meta, 0, sizeof(*meta));
  806. meta->magic = HOSTAP_SKB_TX_DATA_MAGIC;
  807. meta->iface = iface;
  808. meta->tx_cb_idx = tx_cb_idx;
  809. skb->dev = dev;
  810. skb_reset_mac_header(skb);
  811. skb_reset_network_header(skb);
  812. dev_queue_xmit(skb);
  813. }
  814. #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
  815. static int prism2_sta_proc_show(struct seq_file *m, void *v)
  816. {
  817. struct sta_info *sta = m->private;
  818. int i;
  819. /* FIX: possible race condition.. the STA data could have just expired,
  820. * but proc entry was still here so that the read could have started;
  821. * some locking should be done here.. */
  822. seq_printf(m,
  823. "%s=%pM\nusers=%d\naid=%d\n"
  824. "flags=0x%04x%s%s%s%s%s%s%s\n"
  825. "capability=0x%02x\nlisten_interval=%d\nsupported_rates=",
  826. sta->ap ? "AP" : "STA",
  827. sta->addr, atomic_read(&sta->users), sta->aid,
  828. sta->flags,
  829. sta->flags & WLAN_STA_AUTH ? " AUTH" : "",
  830. sta->flags & WLAN_STA_ASSOC ? " ASSOC" : "",
  831. sta->flags & WLAN_STA_PS ? " PS" : "",
  832. sta->flags & WLAN_STA_TIM ? " TIM" : "",
  833. sta->flags & WLAN_STA_PERM ? " PERM" : "",
  834. sta->flags & WLAN_STA_AUTHORIZED ? " AUTHORIZED" : "",
  835. sta->flags & WLAN_STA_PENDING_POLL ? " POLL" : "",
  836. sta->capability, sta->listen_interval);
  837. /* supported_rates: 500 kbit/s units with msb ignored */
  838. for (i = 0; i < sizeof(sta->supported_rates); i++)
  839. if (sta->supported_rates[i] != 0)
  840. seq_printf(m, "%d%sMbps ",
  841. (sta->supported_rates[i] & 0x7f) / 2,
  842. sta->supported_rates[i] & 1 ? ".5" : "");
  843. seq_printf(m,
  844. "\njiffies=%lu\nlast_auth=%lu\nlast_assoc=%lu\n"
  845. "last_rx=%lu\nlast_tx=%lu\nrx_packets=%lu\n"
  846. "tx_packets=%lu\n"
  847. "rx_bytes=%lu\ntx_bytes=%lu\nbuffer_count=%d\n"
  848. "last_rx: silence=%d dBm signal=%d dBm rate=%d%s Mbps\n"
  849. "tx_rate=%d\ntx[1M]=%d\ntx[2M]=%d\ntx[5.5M]=%d\n"
  850. "tx[11M]=%d\n"
  851. "rx[1M]=%d\nrx[2M]=%d\nrx[5.5M]=%d\nrx[11M]=%d\n",
  852. jiffies, sta->last_auth, sta->last_assoc, sta->last_rx,
  853. sta->last_tx,
  854. sta->rx_packets, sta->tx_packets, sta->rx_bytes,
  855. sta->tx_bytes, skb_queue_len(&sta->tx_buf),
  856. sta->last_rx_silence,
  857. sta->last_rx_signal, sta->last_rx_rate / 10,
  858. sta->last_rx_rate % 10 ? ".5" : "",
  859. sta->tx_rate, sta->tx_count[0], sta->tx_count[1],
  860. sta->tx_count[2], sta->tx_count[3], sta->rx_count[0],
  861. sta->rx_count[1], sta->rx_count[2], sta->rx_count[3]);
  862. if (sta->crypt && sta->crypt->ops && sta->crypt->ops->print_stats)
  863. sta->crypt->ops->print_stats(m, sta->crypt->priv);
  864. #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
  865. if (sta->ap) {
  866. if (sta->u.ap.channel >= 0)
  867. seq_printf(m, "channel=%d\n", sta->u.ap.channel);
  868. seq_puts(m, "ssid=");
  869. for (i = 0; i < sta->u.ap.ssid_len; i++) {
  870. if (sta->u.ap.ssid[i] >= 32 && sta->u.ap.ssid[i] < 127)
  871. seq_putc(m, sta->u.ap.ssid[i]);
  872. else
  873. seq_printf(m, "<%02x>", sta->u.ap.ssid[i]);
  874. }
  875. seq_putc(m, '\n');
  876. }
  877. #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
  878. return 0;
  879. }
  880. static void handle_add_proc_queue(struct work_struct *work)
  881. {
  882. struct ap_data *ap = container_of(work, struct ap_data,
  883. add_sta_proc_queue);
  884. struct sta_info *sta;
  885. char name[20];
  886. struct add_sta_proc_data *entry, *prev;
  887. entry = ap->add_sta_proc_entries;
  888. ap->add_sta_proc_entries = NULL;
  889. while (entry) {
  890. spin_lock_bh(&ap->sta_table_lock);
  891. sta = ap_get_sta(ap, entry->addr);
  892. if (sta)
  893. atomic_inc(&sta->users);
  894. spin_unlock_bh(&ap->sta_table_lock);
  895. if (sta) {
  896. sprintf(name, "%pM", sta->addr);
  897. sta->proc = proc_create_single_data(
  898. name, 0, ap->proc,
  899. prism2_sta_proc_show, sta);
  900. atomic_dec(&sta->users);
  901. }
  902. prev = entry;
  903. entry = entry->next;
  904. kfree(prev);
  905. }
  906. }
  907. static struct sta_info * ap_add_sta(struct ap_data *ap, u8 *addr)
  908. {
  909. struct sta_info *sta;
  910. sta = kzalloc(sizeof(struct sta_info), GFP_ATOMIC);
  911. if (sta == NULL) {
  912. PDEBUG(DEBUG_AP, "AP: kmalloc failed\n");
  913. return NULL;
  914. }
  915. /* initialize STA info data */
  916. sta->local = ap->local;
  917. skb_queue_head_init(&sta->tx_buf);
  918. memcpy(sta->addr, addr, ETH_ALEN);
  919. atomic_inc(&sta->users);
  920. spin_lock_bh(&ap->sta_table_lock);
  921. list_add(&sta->list, &ap->sta_list);
  922. ap->num_sta++;
  923. ap_sta_hash_add(ap, sta);
  924. spin_unlock_bh(&ap->sta_table_lock);
  925. if (ap->proc) {
  926. struct add_sta_proc_data *entry;
  927. /* schedule a non-interrupt context process to add a procfs
  928. * entry for the STA since procfs code use GFP_KERNEL */
  929. entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
  930. if (entry) {
  931. memcpy(entry->addr, sta->addr, ETH_ALEN);
  932. entry->next = ap->add_sta_proc_entries;
  933. ap->add_sta_proc_entries = entry;
  934. schedule_work(&ap->add_sta_proc_queue);
  935. } else
  936. printk(KERN_DEBUG "Failed to add STA proc data\n");
  937. }
  938. #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
  939. timer_setup(&sta->timer, ap_handle_timer, 0);
  940. sta->timer.expires = jiffies + ap->max_inactivity;
  941. if (!ap->local->hostapd)
  942. add_timer(&sta->timer);
  943. #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
  944. return sta;
  945. }
  946. static int ap_tx_rate_ok(int rateidx, struct sta_info *sta,
  947. local_info_t *local)
  948. {
  949. if (rateidx > sta->tx_max_rate ||
  950. !(sta->tx_supp_rates & (1 << rateidx)))
  951. return 0;
  952. if (local->tx_rate_control != 0 &&
  953. !(local->tx_rate_control & (1 << rateidx)))
  954. return 0;
  955. return 1;
  956. }
  957. static void prism2_check_tx_rates(struct sta_info *sta)
  958. {
  959. int i;
  960. sta->tx_supp_rates = 0;
  961. for (i = 0; i < sizeof(sta->supported_rates); i++) {
  962. if ((sta->supported_rates[i] & 0x7f) == 2)
  963. sta->tx_supp_rates |= WLAN_RATE_1M;
  964. if ((sta->supported_rates[i] & 0x7f) == 4)
  965. sta->tx_supp_rates |= WLAN_RATE_2M;
  966. if ((sta->supported_rates[i] & 0x7f) == 11)
  967. sta->tx_supp_rates |= WLAN_RATE_5M5;
  968. if ((sta->supported_rates[i] & 0x7f) == 22)
  969. sta->tx_supp_rates |= WLAN_RATE_11M;
  970. }
  971. sta->tx_max_rate = sta->tx_rate = sta->tx_rate_idx = 0;
  972. if (sta->tx_supp_rates & WLAN_RATE_1M) {
  973. sta->tx_max_rate = 0;
  974. if (ap_tx_rate_ok(0, sta, sta->local)) {
  975. sta->tx_rate = 10;
  976. sta->tx_rate_idx = 0;
  977. }
  978. }
  979. if (sta->tx_supp_rates & WLAN_RATE_2M) {
  980. sta->tx_max_rate = 1;
  981. if (ap_tx_rate_ok(1, sta, sta->local)) {
  982. sta->tx_rate = 20;
  983. sta->tx_rate_idx = 1;
  984. }
  985. }
  986. if (sta->tx_supp_rates & WLAN_RATE_5M5) {
  987. sta->tx_max_rate = 2;
  988. if (ap_tx_rate_ok(2, sta, sta->local)) {
  989. sta->tx_rate = 55;
  990. sta->tx_rate_idx = 2;
  991. }
  992. }
  993. if (sta->tx_supp_rates & WLAN_RATE_11M) {
  994. sta->tx_max_rate = 3;
  995. if (ap_tx_rate_ok(3, sta, sta->local)) {
  996. sta->tx_rate = 110;
  997. sta->tx_rate_idx = 3;
  998. }
  999. }
  1000. }
  1001. #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
  1002. static void ap_crypt_init(struct ap_data *ap)
  1003. {
  1004. ap->crypt = lib80211_get_crypto_ops("WEP");
  1005. if (ap->crypt) {
  1006. if (ap->crypt->init) {
  1007. ap->crypt_priv = ap->crypt->init(0);
  1008. if (ap->crypt_priv == NULL)
  1009. ap->crypt = NULL;
  1010. else {
  1011. u8 key[WEP_KEY_LEN];
  1012. get_random_bytes(key, WEP_KEY_LEN);
  1013. ap->crypt->set_key(key, WEP_KEY_LEN, NULL,
  1014. ap->crypt_priv);
  1015. }
  1016. }
  1017. }
  1018. if (ap->crypt == NULL) {
  1019. printk(KERN_WARNING "AP could not initialize WEP: load module "
  1020. "lib80211_crypt_wep.ko\n");
  1021. }
  1022. }
  1023. /* Generate challenge data for shared key authentication. IEEE 802.11 specifies
  1024. * that WEP algorithm is used for generating challenge. This should be unique,
  1025. * but otherwise there is not really need for randomness etc. Initialize WEP
  1026. * with pseudo random key and then use increasing IV to get unique challenge
  1027. * streams.
  1028. *
  1029. * Called only as a scheduled task for pending AP frames.
  1030. */
  1031. static char * ap_auth_make_challenge(struct ap_data *ap)
  1032. {
  1033. char *tmpbuf;
  1034. struct sk_buff *skb;
  1035. if (ap->crypt == NULL) {
  1036. ap_crypt_init(ap);
  1037. if (ap->crypt == NULL)
  1038. return NULL;
  1039. }
  1040. tmpbuf = kmalloc(WLAN_AUTH_CHALLENGE_LEN, GFP_ATOMIC);
  1041. if (tmpbuf == NULL) {
  1042. PDEBUG(DEBUG_AP, "AP: kmalloc failed for challenge\n");
  1043. return NULL;
  1044. }
  1045. skb = dev_alloc_skb(WLAN_AUTH_CHALLENGE_LEN +
  1046. ap->crypt->extra_mpdu_prefix_len +
  1047. ap->crypt->extra_mpdu_postfix_len);
  1048. if (skb == NULL) {
  1049. kfree(tmpbuf);
  1050. return NULL;
  1051. }
  1052. skb_reserve(skb, ap->crypt->extra_mpdu_prefix_len);
  1053. skb_put_zero(skb, WLAN_AUTH_CHALLENGE_LEN);
  1054. if (ap->crypt->encrypt_mpdu(skb, 0, ap->crypt_priv)) {
  1055. dev_kfree_skb(skb);
  1056. kfree(tmpbuf);
  1057. return NULL;
  1058. }
  1059. skb_copy_from_linear_data_offset(skb, ap->crypt->extra_mpdu_prefix_len,
  1060. tmpbuf, WLAN_AUTH_CHALLENGE_LEN);
  1061. dev_kfree_skb(skb);
  1062. return tmpbuf;
  1063. }
  1064. /* Called only as a scheduled task for pending AP frames. */
  1065. static void handle_authen(local_info_t *local, struct sk_buff *skb,
  1066. struct hostap_80211_rx_status *rx_stats)
  1067. {
  1068. struct net_device *dev = local->dev;
  1069. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1070. size_t hdrlen;
  1071. struct ap_data *ap = local->ap;
  1072. char body[8 + WLAN_AUTH_CHALLENGE_LEN], *challenge = NULL;
  1073. int len, olen;
  1074. u16 auth_alg, auth_transaction, status_code;
  1075. __le16 *pos;
  1076. u16 resp = WLAN_STATUS_SUCCESS;
  1077. struct sta_info *sta = NULL;
  1078. struct lib80211_crypt_data *crypt;
  1079. char *txt = "";
  1080. len = skb->len - IEEE80211_MGMT_HDR_LEN;
  1081. hdrlen = hostap_80211_get_hdrlen(hdr->frame_control);
  1082. if (len < 6) {
  1083. PDEBUG(DEBUG_AP, "%s: handle_authen - too short payload "
  1084. "(len=%d) from %pM\n", dev->name, len, hdr->addr2);
  1085. return;
  1086. }
  1087. spin_lock_bh(&local->ap->sta_table_lock);
  1088. sta = ap_get_sta(local->ap, hdr->addr2);
  1089. if (sta)
  1090. atomic_inc(&sta->users);
  1091. spin_unlock_bh(&local->ap->sta_table_lock);
  1092. if (sta && sta->crypt)
  1093. crypt = sta->crypt;
  1094. else {
  1095. int idx = 0;
  1096. if (skb->len >= hdrlen + 3)
  1097. idx = skb->data[hdrlen + 3] >> 6;
  1098. crypt = local->crypt_info.crypt[idx];
  1099. }
  1100. pos = (__le16 *) (skb->data + IEEE80211_MGMT_HDR_LEN);
  1101. auth_alg = __le16_to_cpu(*pos);
  1102. pos++;
  1103. auth_transaction = __le16_to_cpu(*pos);
  1104. pos++;
  1105. status_code = __le16_to_cpu(*pos);
  1106. pos++;
  1107. if (ether_addr_equal(dev->dev_addr, hdr->addr2) ||
  1108. ap_control_mac_deny(&ap->mac_restrictions, hdr->addr2)) {
  1109. txt = "authentication denied";
  1110. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  1111. goto fail;
  1112. }
  1113. if (((local->auth_algs & PRISM2_AUTH_OPEN) &&
  1114. auth_alg == WLAN_AUTH_OPEN) ||
  1115. ((local->auth_algs & PRISM2_AUTH_SHARED_KEY) &&
  1116. crypt && auth_alg == WLAN_AUTH_SHARED_KEY)) {
  1117. } else {
  1118. txt = "unsupported algorithm";
  1119. resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
  1120. goto fail;
  1121. }
  1122. if (len >= 8) {
  1123. u8 *u = (u8 *) pos;
  1124. if (*u == WLAN_EID_CHALLENGE) {
  1125. if (*(u + 1) != WLAN_AUTH_CHALLENGE_LEN) {
  1126. txt = "invalid challenge len";
  1127. resp = WLAN_STATUS_CHALLENGE_FAIL;
  1128. goto fail;
  1129. }
  1130. if (len - 8 < WLAN_AUTH_CHALLENGE_LEN) {
  1131. txt = "challenge underflow";
  1132. resp = WLAN_STATUS_CHALLENGE_FAIL;
  1133. goto fail;
  1134. }
  1135. challenge = (char *) (u + 2);
  1136. }
  1137. }
  1138. if (sta && sta->ap) {
  1139. if (time_after(jiffies, sta->u.ap.last_beacon +
  1140. (10 * sta->listen_interval * HZ) / 1024)) {
  1141. PDEBUG(DEBUG_AP, "%s: no beacons received for a while,"
  1142. " assuming AP %pM is now STA\n",
  1143. dev->name, sta->addr);
  1144. sta->ap = 0;
  1145. sta->flags = 0;
  1146. sta->u.sta.challenge = NULL;
  1147. } else {
  1148. txt = "AP trying to authenticate?";
  1149. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  1150. goto fail;
  1151. }
  1152. }
  1153. if ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 1) ||
  1154. (auth_alg == WLAN_AUTH_SHARED_KEY &&
  1155. (auth_transaction == 1 ||
  1156. (auth_transaction == 3 && sta != NULL &&
  1157. sta->u.sta.challenge != NULL)))) {
  1158. } else {
  1159. txt = "unknown authentication transaction number";
  1160. resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
  1161. goto fail;
  1162. }
  1163. if (sta == NULL) {
  1164. txt = "new STA";
  1165. if (local->ap->num_sta >= MAX_STA_COUNT) {
  1166. /* FIX: might try to remove some old STAs first? */
  1167. txt = "no more room for new STAs";
  1168. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  1169. goto fail;
  1170. }
  1171. sta = ap_add_sta(local->ap, hdr->addr2);
  1172. if (sta == NULL) {
  1173. txt = "ap_add_sta failed";
  1174. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  1175. goto fail;
  1176. }
  1177. }
  1178. switch (auth_alg) {
  1179. case WLAN_AUTH_OPEN:
  1180. txt = "authOK";
  1181. /* IEEE 802.11 standard is not completely clear about
  1182. * whether STA is considered authenticated after
  1183. * authentication OK frame has been send or after it
  1184. * has been ACKed. In order to reduce interoperability
  1185. * issues, mark the STA authenticated before ACK. */
  1186. sta->flags |= WLAN_STA_AUTH;
  1187. break;
  1188. case WLAN_AUTH_SHARED_KEY:
  1189. if (auth_transaction == 1) {
  1190. if (sta->u.sta.challenge == NULL) {
  1191. sta->u.sta.challenge =
  1192. ap_auth_make_challenge(local->ap);
  1193. if (sta->u.sta.challenge == NULL) {
  1194. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  1195. goto fail;
  1196. }
  1197. }
  1198. } else {
  1199. if (sta->u.sta.challenge == NULL ||
  1200. challenge == NULL ||
  1201. memcmp(sta->u.sta.challenge, challenge,
  1202. WLAN_AUTH_CHALLENGE_LEN) != 0 ||
  1203. !ieee80211_has_protected(hdr->frame_control)) {
  1204. txt = "challenge response incorrect";
  1205. resp = WLAN_STATUS_CHALLENGE_FAIL;
  1206. goto fail;
  1207. }
  1208. txt = "challenge OK - authOK";
  1209. /* IEEE 802.11 standard is not completely clear about
  1210. * whether STA is considered authenticated after
  1211. * authentication OK frame has been send or after it
  1212. * has been ACKed. In order to reduce interoperability
  1213. * issues, mark the STA authenticated before ACK. */
  1214. sta->flags |= WLAN_STA_AUTH;
  1215. kfree(sta->u.sta.challenge);
  1216. sta->u.sta.challenge = NULL;
  1217. }
  1218. break;
  1219. }
  1220. fail:
  1221. pos = (__le16 *) body;
  1222. *pos = cpu_to_le16(auth_alg);
  1223. pos++;
  1224. *pos = cpu_to_le16(auth_transaction + 1);
  1225. pos++;
  1226. *pos = cpu_to_le16(resp); /* status_code */
  1227. pos++;
  1228. olen = 6;
  1229. if (resp == WLAN_STATUS_SUCCESS && sta != NULL &&
  1230. sta->u.sta.challenge != NULL &&
  1231. auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 1) {
  1232. u8 *tmp = (u8 *) pos;
  1233. *tmp++ = WLAN_EID_CHALLENGE;
  1234. *tmp++ = WLAN_AUTH_CHALLENGE_LEN;
  1235. pos++;
  1236. memcpy(pos, sta->u.sta.challenge, WLAN_AUTH_CHALLENGE_LEN);
  1237. olen += 2 + WLAN_AUTH_CHALLENGE_LEN;
  1238. }
  1239. prism2_send_mgmt(dev, IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_AUTH,
  1240. body, olen, hdr->addr2, ap->tx_callback_auth);
  1241. if (sta) {
  1242. sta->last_rx = jiffies;
  1243. atomic_dec(&sta->users);
  1244. }
  1245. if (resp) {
  1246. PDEBUG(DEBUG_AP, "%s: %pM auth (alg=%d "
  1247. "trans#=%d stat=%d len=%d fc=%04x) ==> %d (%s)\n",
  1248. dev->name, hdr->addr2,
  1249. auth_alg, auth_transaction, status_code, len,
  1250. le16_to_cpu(hdr->frame_control), resp, txt);
  1251. }
  1252. }
  1253. /* Called only as a scheduled task for pending AP frames. */
  1254. static void handle_assoc(local_info_t *local, struct sk_buff *skb,
  1255. struct hostap_80211_rx_status *rx_stats, int reassoc)
  1256. {
  1257. struct net_device *dev = local->dev;
  1258. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1259. char body[12], *p, *lpos;
  1260. int len, left;
  1261. __le16 *pos;
  1262. u16 resp = WLAN_STATUS_SUCCESS;
  1263. struct sta_info *sta = NULL;
  1264. int send_deauth = 0;
  1265. char *txt = "";
  1266. u8 prev_ap[ETH_ALEN];
  1267. left = len = skb->len - IEEE80211_MGMT_HDR_LEN;
  1268. if (len < (reassoc ? 10 : 4)) {
  1269. PDEBUG(DEBUG_AP, "%s: handle_assoc - too short payload "
  1270. "(len=%d, reassoc=%d) from %pM\n",
  1271. dev->name, len, reassoc, hdr->addr2);
  1272. return;
  1273. }
  1274. spin_lock_bh(&local->ap->sta_table_lock);
  1275. sta = ap_get_sta(local->ap, hdr->addr2);
  1276. if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
  1277. spin_unlock_bh(&local->ap->sta_table_lock);
  1278. txt = "trying to associate before authentication";
  1279. send_deauth = 1;
  1280. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  1281. sta = NULL; /* do not decrement sta->users */
  1282. goto fail;
  1283. }
  1284. atomic_inc(&sta->users);
  1285. spin_unlock_bh(&local->ap->sta_table_lock);
  1286. pos = (__le16 *) (skb->data + IEEE80211_MGMT_HDR_LEN);
  1287. sta->capability = __le16_to_cpu(*pos);
  1288. pos++; left -= 2;
  1289. sta->listen_interval = __le16_to_cpu(*pos);
  1290. pos++; left -= 2;
  1291. if (reassoc) {
  1292. memcpy(prev_ap, pos, ETH_ALEN);
  1293. pos++; pos++; pos++; left -= 6;
  1294. } else
  1295. eth_zero_addr(prev_ap);
  1296. if (left >= 2) {
  1297. unsigned int ileft;
  1298. unsigned char *u = (unsigned char *) pos;
  1299. if (*u == WLAN_EID_SSID) {
  1300. u++; left--;
  1301. ileft = *u;
  1302. u++; left--;
  1303. if (ileft > left || ileft > MAX_SSID_LEN) {
  1304. txt = "SSID overflow";
  1305. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  1306. goto fail;
  1307. }
  1308. if (ileft != strlen(local->essid) ||
  1309. memcmp(local->essid, u, ileft) != 0) {
  1310. txt = "not our SSID";
  1311. resp = WLAN_STATUS_ASSOC_DENIED_UNSPEC;
  1312. goto fail;
  1313. }
  1314. u += ileft;
  1315. left -= ileft;
  1316. }
  1317. if (left >= 2 && *u == WLAN_EID_SUPP_RATES) {
  1318. u++; left--;
  1319. ileft = *u;
  1320. u++; left--;
  1321. if (ileft > left || ileft == 0 ||
  1322. ileft > WLAN_SUPP_RATES_MAX) {
  1323. txt = "SUPP_RATES len error";
  1324. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  1325. goto fail;
  1326. }
  1327. memset(sta->supported_rates, 0,
  1328. sizeof(sta->supported_rates));
  1329. memcpy(sta->supported_rates, u, ileft);
  1330. prism2_check_tx_rates(sta);
  1331. u += ileft;
  1332. left -= ileft;
  1333. }
  1334. if (left > 0) {
  1335. PDEBUG(DEBUG_AP, "%s: assoc from %pM"
  1336. " with extra data (%d bytes) [",
  1337. dev->name, hdr->addr2, left);
  1338. while (left > 0) {
  1339. PDEBUG2(DEBUG_AP, "<%02x>", *u);
  1340. u++; left--;
  1341. }
  1342. PDEBUG2(DEBUG_AP, "]\n");
  1343. }
  1344. } else {
  1345. txt = "frame underflow";
  1346. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  1347. goto fail;
  1348. }
  1349. /* get a unique AID */
  1350. if (sta->aid > 0)
  1351. txt = "OK, old AID";
  1352. else {
  1353. spin_lock_bh(&local->ap->sta_table_lock);
  1354. for (sta->aid = 1; sta->aid <= MAX_AID_TABLE_SIZE; sta->aid++)
  1355. if (local->ap->sta_aid[sta->aid - 1] == NULL)
  1356. break;
  1357. if (sta->aid > MAX_AID_TABLE_SIZE) {
  1358. sta->aid = 0;
  1359. spin_unlock_bh(&local->ap->sta_table_lock);
  1360. resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
  1361. txt = "no room for more AIDs";
  1362. } else {
  1363. local->ap->sta_aid[sta->aid - 1] = sta;
  1364. spin_unlock_bh(&local->ap->sta_table_lock);
  1365. txt = "OK, new AID";
  1366. }
  1367. }
  1368. fail:
  1369. pos = (__le16 *) body;
  1370. if (send_deauth) {
  1371. *pos = cpu_to_le16(WLAN_REASON_STA_REQ_ASSOC_WITHOUT_AUTH);
  1372. pos++;
  1373. } else {
  1374. /* FIX: CF-Pollable and CF-PollReq should be set to match the
  1375. * values in beacons/probe responses */
  1376. /* FIX: how about privacy and WEP? */
  1377. /* capability */
  1378. *pos = cpu_to_le16(WLAN_CAPABILITY_ESS);
  1379. pos++;
  1380. /* status_code */
  1381. *pos = cpu_to_le16(resp);
  1382. pos++;
  1383. *pos = cpu_to_le16((sta && sta->aid > 0 ? sta->aid : 0) |
  1384. BIT(14) | BIT(15)); /* AID */
  1385. pos++;
  1386. /* Supported rates (Information element) */
  1387. p = (char *) pos;
  1388. *p++ = WLAN_EID_SUPP_RATES;
  1389. lpos = p;
  1390. *p++ = 0; /* len */
  1391. if (local->tx_rate_control & WLAN_RATE_1M) {
  1392. *p++ = local->basic_rates & WLAN_RATE_1M ? 0x82 : 0x02;
  1393. (*lpos)++;
  1394. }
  1395. if (local->tx_rate_control & WLAN_RATE_2M) {
  1396. *p++ = local->basic_rates & WLAN_RATE_2M ? 0x84 : 0x04;
  1397. (*lpos)++;
  1398. }
  1399. if (local->tx_rate_control & WLAN_RATE_5M5) {
  1400. *p++ = local->basic_rates & WLAN_RATE_5M5 ?
  1401. 0x8b : 0x0b;
  1402. (*lpos)++;
  1403. }
  1404. if (local->tx_rate_control & WLAN_RATE_11M) {
  1405. *p++ = local->basic_rates & WLAN_RATE_11M ?
  1406. 0x96 : 0x16;
  1407. (*lpos)++;
  1408. }
  1409. pos = (__le16 *) p;
  1410. }
  1411. prism2_send_mgmt(dev, IEEE80211_FTYPE_MGMT |
  1412. (send_deauth ? IEEE80211_STYPE_DEAUTH :
  1413. (reassoc ? IEEE80211_STYPE_REASSOC_RESP :
  1414. IEEE80211_STYPE_ASSOC_RESP)),
  1415. body, (u8 *) pos - (u8 *) body,
  1416. hdr->addr2,
  1417. send_deauth ? 0 : local->ap->tx_callback_assoc);
  1418. if (sta) {
  1419. if (resp == WLAN_STATUS_SUCCESS) {
  1420. sta->last_rx = jiffies;
  1421. /* STA will be marked associated from TX callback, if
  1422. * AssocResp is ACKed */
  1423. }
  1424. atomic_dec(&sta->users);
  1425. }
  1426. #if 0
  1427. PDEBUG(DEBUG_AP, "%s: %pM %sassoc (len=%d "
  1428. "prev_ap=%pM) => %d(%d) (%s)\n",
  1429. dev->name,
  1430. hdr->addr2,
  1431. reassoc ? "re" : "", len,
  1432. prev_ap,
  1433. resp, send_deauth, txt);
  1434. #endif
  1435. }
  1436. /* Called only as a scheduled task for pending AP frames. */
  1437. static void handle_deauth(local_info_t *local, struct sk_buff *skb,
  1438. struct hostap_80211_rx_status *rx_stats)
  1439. {
  1440. struct net_device *dev = local->dev;
  1441. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1442. char *body = (char *) (skb->data + IEEE80211_MGMT_HDR_LEN);
  1443. int len;
  1444. u16 reason_code;
  1445. __le16 *pos;
  1446. struct sta_info *sta = NULL;
  1447. len = skb->len - IEEE80211_MGMT_HDR_LEN;
  1448. if (len < 2) {
  1449. printk("handle_deauth - too short payload (len=%d)\n", len);
  1450. return;
  1451. }
  1452. pos = (__le16 *) body;
  1453. reason_code = le16_to_cpu(*pos);
  1454. PDEBUG(DEBUG_AP, "%s: deauthentication: %pM len=%d, "
  1455. "reason_code=%d\n", dev->name, hdr->addr2,
  1456. len, reason_code);
  1457. spin_lock_bh(&local->ap->sta_table_lock);
  1458. sta = ap_get_sta(local->ap, hdr->addr2);
  1459. if (sta != NULL) {
  1460. if ((sta->flags & WLAN_STA_ASSOC) && !sta->ap)
  1461. hostap_event_expired_sta(local->dev, sta);
  1462. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
  1463. }
  1464. spin_unlock_bh(&local->ap->sta_table_lock);
  1465. if (sta == NULL) {
  1466. printk("%s: deauthentication from %pM, "
  1467. "reason_code=%d, but STA not authenticated\n", dev->name,
  1468. hdr->addr2, reason_code);
  1469. }
  1470. }
  1471. /* Called only as a scheduled task for pending AP frames. */
  1472. static void handle_disassoc(local_info_t *local, struct sk_buff *skb,
  1473. struct hostap_80211_rx_status *rx_stats)
  1474. {
  1475. struct net_device *dev = local->dev;
  1476. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1477. char *body = skb->data + IEEE80211_MGMT_HDR_LEN;
  1478. int len;
  1479. u16 reason_code;
  1480. __le16 *pos;
  1481. struct sta_info *sta = NULL;
  1482. len = skb->len - IEEE80211_MGMT_HDR_LEN;
  1483. if (len < 2) {
  1484. printk("handle_disassoc - too short payload (len=%d)\n", len);
  1485. return;
  1486. }
  1487. pos = (__le16 *) body;
  1488. reason_code = le16_to_cpu(*pos);
  1489. PDEBUG(DEBUG_AP, "%s: disassociation: %pM len=%d, "
  1490. "reason_code=%d\n", dev->name, hdr->addr2,
  1491. len, reason_code);
  1492. spin_lock_bh(&local->ap->sta_table_lock);
  1493. sta = ap_get_sta(local->ap, hdr->addr2);
  1494. if (sta != NULL) {
  1495. if ((sta->flags & WLAN_STA_ASSOC) && !sta->ap)
  1496. hostap_event_expired_sta(local->dev, sta);
  1497. sta->flags &= ~WLAN_STA_ASSOC;
  1498. }
  1499. spin_unlock_bh(&local->ap->sta_table_lock);
  1500. if (sta == NULL) {
  1501. printk("%s: disassociation from %pM, "
  1502. "reason_code=%d, but STA not authenticated\n",
  1503. dev->name, hdr->addr2, reason_code);
  1504. }
  1505. }
  1506. /* Called only as a scheduled task for pending AP frames. */
  1507. static void ap_handle_data_nullfunc(local_info_t *local,
  1508. struct ieee80211_hdr *hdr)
  1509. {
  1510. struct net_device *dev = local->dev;
  1511. /* some STA f/w's seem to require control::ACK frame for
  1512. * data::nullfunc, but at least Prism2 station f/w version 0.8.0 does
  1513. * not send this..
  1514. * send control::ACK for the data::nullfunc */
  1515. printk(KERN_DEBUG "Sending control::ACK for data::nullfunc\n");
  1516. prism2_send_mgmt(dev, IEEE80211_FTYPE_CTL | IEEE80211_STYPE_ACK,
  1517. NULL, 0, hdr->addr2, 0);
  1518. }
  1519. /* Called only as a scheduled task for pending AP frames. */
  1520. static void ap_handle_dropped_data(local_info_t *local,
  1521. struct ieee80211_hdr *hdr)
  1522. {
  1523. struct net_device *dev = local->dev;
  1524. struct sta_info *sta;
  1525. __le16 reason;
  1526. spin_lock_bh(&local->ap->sta_table_lock);
  1527. sta = ap_get_sta(local->ap, hdr->addr2);
  1528. if (sta)
  1529. atomic_inc(&sta->users);
  1530. spin_unlock_bh(&local->ap->sta_table_lock);
  1531. if (sta != NULL && (sta->flags & WLAN_STA_ASSOC)) {
  1532. PDEBUG(DEBUG_AP, "ap_handle_dropped_data: STA is now okay?\n");
  1533. atomic_dec(&sta->users);
  1534. return;
  1535. }
  1536. reason = cpu_to_le16(WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
  1537. prism2_send_mgmt(dev, IEEE80211_FTYPE_MGMT |
  1538. ((sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) ?
  1539. IEEE80211_STYPE_DEAUTH : IEEE80211_STYPE_DISASSOC),
  1540. (char *) &reason, sizeof(reason), hdr->addr2, 0);
  1541. if (sta)
  1542. atomic_dec(&sta->users);
  1543. }
  1544. #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
  1545. /* Called only as a scheduled task for pending AP frames. */
  1546. static void pspoll_send_buffered(local_info_t *local, struct sta_info *sta,
  1547. struct sk_buff *skb)
  1548. {
  1549. struct hostap_skb_tx_data *meta;
  1550. if (!(sta->flags & WLAN_STA_PS)) {
  1551. /* Station has moved to non-PS mode, so send all buffered
  1552. * frames using normal device queue. */
  1553. dev_queue_xmit(skb);
  1554. return;
  1555. }
  1556. /* add a flag for hostap_handle_sta_tx() to know that this skb should
  1557. * be passed through even though STA is using PS */
  1558. meta = (struct hostap_skb_tx_data *) skb->cb;
  1559. meta->flags |= HOSTAP_TX_FLAGS_BUFFERED_FRAME;
  1560. if (!skb_queue_empty(&sta->tx_buf)) {
  1561. /* indicate to STA that more frames follow */
  1562. meta->flags |= HOSTAP_TX_FLAGS_ADD_MOREDATA;
  1563. }
  1564. dev_queue_xmit(skb);
  1565. }
  1566. /* Called only as a scheduled task for pending AP frames. */
  1567. static void handle_pspoll(local_info_t *local,
  1568. struct ieee80211_hdr *hdr,
  1569. struct hostap_80211_rx_status *rx_stats)
  1570. {
  1571. struct net_device *dev = local->dev;
  1572. struct sta_info *sta;
  1573. u16 aid;
  1574. struct sk_buff *skb;
  1575. PDEBUG(DEBUG_PS2, "handle_pspoll: BSSID=%pM, TA=%pM PWRMGT=%d\n",
  1576. hdr->addr1, hdr->addr2, !!ieee80211_has_pm(hdr->frame_control));
  1577. if (!ether_addr_equal(hdr->addr1, dev->dev_addr)) {
  1578. PDEBUG(DEBUG_AP,
  1579. "handle_pspoll - addr1(BSSID)=%pM not own MAC\n",
  1580. hdr->addr1);
  1581. return;
  1582. }
  1583. aid = le16_to_cpu(hdr->duration_id);
  1584. if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14))) {
  1585. PDEBUG(DEBUG_PS, " PSPOLL and AID[15:14] not set\n");
  1586. return;
  1587. }
  1588. aid &= ~(BIT(15) | BIT(14));
  1589. if (aid == 0 || aid > MAX_AID_TABLE_SIZE) {
  1590. PDEBUG(DEBUG_PS, " invalid aid=%d\n", aid);
  1591. return;
  1592. }
  1593. PDEBUG(DEBUG_PS2, " aid=%d\n", aid);
  1594. spin_lock_bh(&local->ap->sta_table_lock);
  1595. sta = ap_get_sta(local->ap, hdr->addr2);
  1596. if (sta)
  1597. atomic_inc(&sta->users);
  1598. spin_unlock_bh(&local->ap->sta_table_lock);
  1599. if (sta == NULL) {
  1600. PDEBUG(DEBUG_PS, " STA not found\n");
  1601. return;
  1602. }
  1603. if (sta->aid != aid) {
  1604. PDEBUG(DEBUG_PS, " received aid=%i does not match with "
  1605. "assoc.aid=%d\n", aid, sta->aid);
  1606. return;
  1607. }
  1608. /* FIX: todo:
  1609. * - add timeout for buffering (clear aid in TIM vector if buffer timed
  1610. * out (expiry time must be longer than ListenInterval for
  1611. * the corresponding STA; "8802-11: 11.2.1.9 AP aging function"
  1612. * - what to do, if buffered, pspolled, and sent frame is not ACKed by
  1613. * sta; store buffer for later use and leave TIM aid bit set? use
  1614. * TX event to check whether frame was ACKed?
  1615. */
  1616. while ((skb = skb_dequeue(&sta->tx_buf)) != NULL) {
  1617. /* send buffered frame .. */
  1618. PDEBUG(DEBUG_PS2, "Sending buffered frame to STA after PS POLL"
  1619. " (buffer_count=%d)\n", skb_queue_len(&sta->tx_buf));
  1620. pspoll_send_buffered(local, sta, skb);
  1621. if (sta->flags & WLAN_STA_PS) {
  1622. /* send only one buffered packet per PS Poll */
  1623. /* FIX: should ignore further PS Polls until the
  1624. * buffered packet that was just sent is acknowledged
  1625. * (Tx or TxExc event) */
  1626. break;
  1627. }
  1628. }
  1629. if (skb_queue_empty(&sta->tx_buf)) {
  1630. /* try to clear aid from TIM */
  1631. if (!(sta->flags & WLAN_STA_TIM))
  1632. PDEBUG(DEBUG_PS2, "Re-unsetting TIM for aid %d\n",
  1633. aid);
  1634. hostap_set_tim(local, aid, 0);
  1635. sta->flags &= ~WLAN_STA_TIM;
  1636. }
  1637. atomic_dec(&sta->users);
  1638. }
  1639. #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
  1640. static void handle_wds_oper_queue(struct work_struct *work)
  1641. {
  1642. struct ap_data *ap = container_of(work, struct ap_data,
  1643. wds_oper_queue);
  1644. local_info_t *local = ap->local;
  1645. struct wds_oper_data *entry, *prev;
  1646. spin_lock_bh(&local->lock);
  1647. entry = local->ap->wds_oper_entries;
  1648. local->ap->wds_oper_entries = NULL;
  1649. spin_unlock_bh(&local->lock);
  1650. while (entry) {
  1651. PDEBUG(DEBUG_AP, "%s: %s automatic WDS connection "
  1652. "to AP %pM\n",
  1653. local->dev->name,
  1654. entry->type == WDS_ADD ? "adding" : "removing",
  1655. entry->addr);
  1656. if (entry->type == WDS_ADD)
  1657. prism2_wds_add(local, entry->addr, 0);
  1658. else if (entry->type == WDS_DEL)
  1659. prism2_wds_del(local, entry->addr, 0, 1);
  1660. prev = entry;
  1661. entry = entry->next;
  1662. kfree(prev);
  1663. }
  1664. }
  1665. /* Called only as a scheduled task for pending AP frames. */
  1666. static void handle_beacon(local_info_t *local, struct sk_buff *skb,
  1667. struct hostap_80211_rx_status *rx_stats)
  1668. {
  1669. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1670. char *body = skb->data + IEEE80211_MGMT_HDR_LEN;
  1671. int len, left;
  1672. u16 beacon_int, capability;
  1673. __le16 *pos;
  1674. char *ssid = NULL;
  1675. unsigned char *supp_rates = NULL;
  1676. int ssid_len = 0, supp_rates_len = 0;
  1677. struct sta_info *sta = NULL;
  1678. int new_sta = 0, channel = -1;
  1679. len = skb->len - IEEE80211_MGMT_HDR_LEN;
  1680. if (len < 8 + 2 + 2) {
  1681. printk(KERN_DEBUG "handle_beacon - too short payload "
  1682. "(len=%d)\n", len);
  1683. return;
  1684. }
  1685. pos = (__le16 *) body;
  1686. left = len;
  1687. /* Timestamp (8 octets) */
  1688. pos += 4; left -= 8;
  1689. /* Beacon interval (2 octets) */
  1690. beacon_int = le16_to_cpu(*pos);
  1691. pos++; left -= 2;
  1692. /* Capability information (2 octets) */
  1693. capability = le16_to_cpu(*pos);
  1694. pos++; left -= 2;
  1695. if (local->ap->ap_policy != AP_OTHER_AP_EVEN_IBSS &&
  1696. capability & WLAN_CAPABILITY_IBSS)
  1697. return;
  1698. if (left >= 2) {
  1699. unsigned int ileft;
  1700. unsigned char *u = (unsigned char *) pos;
  1701. if (*u == WLAN_EID_SSID) {
  1702. u++; left--;
  1703. ileft = *u;
  1704. u++; left--;
  1705. if (ileft > left || ileft > MAX_SSID_LEN) {
  1706. PDEBUG(DEBUG_AP, "SSID: overflow\n");
  1707. return;
  1708. }
  1709. if (local->ap->ap_policy == AP_OTHER_AP_SAME_SSID &&
  1710. (ileft != strlen(local->essid) ||
  1711. memcmp(local->essid, u, ileft) != 0)) {
  1712. /* not our SSID */
  1713. return;
  1714. }
  1715. ssid = u;
  1716. ssid_len = ileft;
  1717. u += ileft;
  1718. left -= ileft;
  1719. }
  1720. if (*u == WLAN_EID_SUPP_RATES) {
  1721. u++; left--;
  1722. ileft = *u;
  1723. u++; left--;
  1724. if (ileft > left || ileft == 0 || ileft > 8) {
  1725. PDEBUG(DEBUG_AP, " - SUPP_RATES len error\n");
  1726. return;
  1727. }
  1728. supp_rates = u;
  1729. supp_rates_len = ileft;
  1730. u += ileft;
  1731. left -= ileft;
  1732. }
  1733. if (*u == WLAN_EID_DS_PARAMS) {
  1734. u++; left--;
  1735. ileft = *u;
  1736. u++; left--;
  1737. if (ileft > left || ileft != 1) {
  1738. PDEBUG(DEBUG_AP, " - DS_PARAMS len error\n");
  1739. return;
  1740. }
  1741. channel = *u;
  1742. u += ileft;
  1743. left -= ileft;
  1744. }
  1745. }
  1746. spin_lock_bh(&local->ap->sta_table_lock);
  1747. sta = ap_get_sta(local->ap, hdr->addr2);
  1748. if (sta != NULL)
  1749. atomic_inc(&sta->users);
  1750. spin_unlock_bh(&local->ap->sta_table_lock);
  1751. if (sta == NULL) {
  1752. /* add new AP */
  1753. new_sta = 1;
  1754. sta = ap_add_sta(local->ap, hdr->addr2);
  1755. if (sta == NULL) {
  1756. printk(KERN_INFO "prism2: kmalloc failed for AP "
  1757. "data structure\n");
  1758. return;
  1759. }
  1760. hostap_event_new_sta(local->dev, sta);
  1761. /* mark APs authentication and associated for pseudo ad-hoc
  1762. * style communication */
  1763. sta->flags = WLAN_STA_AUTH | WLAN_STA_ASSOC;
  1764. if (local->ap->autom_ap_wds) {
  1765. hostap_wds_link_oper(local, sta->addr, WDS_ADD);
  1766. }
  1767. }
  1768. sta->ap = 1;
  1769. if (ssid) {
  1770. sta->u.ap.ssid_len = ssid_len;
  1771. memcpy(sta->u.ap.ssid, ssid, ssid_len);
  1772. sta->u.ap.ssid[ssid_len] = '\0';
  1773. } else {
  1774. sta->u.ap.ssid_len = 0;
  1775. sta->u.ap.ssid[0] = '\0';
  1776. }
  1777. sta->u.ap.channel = channel;
  1778. sta->rx_packets++;
  1779. sta->rx_bytes += len;
  1780. sta->u.ap.last_beacon = sta->last_rx = jiffies;
  1781. sta->capability = capability;
  1782. sta->listen_interval = beacon_int;
  1783. atomic_dec(&sta->users);
  1784. if (new_sta) {
  1785. memset(sta->supported_rates, 0, sizeof(sta->supported_rates));
  1786. memcpy(sta->supported_rates, supp_rates, supp_rates_len);
  1787. prism2_check_tx_rates(sta);
  1788. }
  1789. }
  1790. #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
  1791. /* Called only as a tasklet. */
  1792. static void handle_ap_item(local_info_t *local, struct sk_buff *skb,
  1793. struct hostap_80211_rx_status *rx_stats)
  1794. {
  1795. #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
  1796. struct net_device *dev = local->dev;
  1797. #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
  1798. u16 fc, type, stype;
  1799. struct ieee80211_hdr *hdr;
  1800. /* FIX: should give skb->len to handler functions and check that the
  1801. * buffer is long enough */
  1802. hdr = (struct ieee80211_hdr *) skb->data;
  1803. fc = le16_to_cpu(hdr->frame_control);
  1804. type = fc & IEEE80211_FCTL_FTYPE;
  1805. stype = fc & IEEE80211_FCTL_STYPE;
  1806. #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
  1807. if (!local->hostapd && type == IEEE80211_FTYPE_DATA) {
  1808. PDEBUG(DEBUG_AP, "handle_ap_item - data frame\n");
  1809. if (!(fc & IEEE80211_FCTL_TODS) ||
  1810. (fc & IEEE80211_FCTL_FROMDS)) {
  1811. if (stype == IEEE80211_STYPE_NULLFUNC) {
  1812. /* no ToDS nullfunc seems to be used to check
  1813. * AP association; so send reject message to
  1814. * speed up re-association */
  1815. ap_handle_dropped_data(local, hdr);
  1816. goto done;
  1817. }
  1818. PDEBUG(DEBUG_AP, " not ToDS frame (fc=0x%04x)\n",
  1819. fc);
  1820. goto done;
  1821. }
  1822. if (!ether_addr_equal(hdr->addr1, dev->dev_addr)) {
  1823. PDEBUG(DEBUG_AP, "handle_ap_item - addr1(BSSID)=%pM"
  1824. " not own MAC\n", hdr->addr1);
  1825. goto done;
  1826. }
  1827. if (local->ap->nullfunc_ack &&
  1828. stype == IEEE80211_STYPE_NULLFUNC)
  1829. ap_handle_data_nullfunc(local, hdr);
  1830. else
  1831. ap_handle_dropped_data(local, hdr);
  1832. goto done;
  1833. }
  1834. if (type == IEEE80211_FTYPE_MGMT && stype == IEEE80211_STYPE_BEACON) {
  1835. handle_beacon(local, skb, rx_stats);
  1836. goto done;
  1837. }
  1838. #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
  1839. if (type == IEEE80211_FTYPE_CTL && stype == IEEE80211_STYPE_PSPOLL) {
  1840. handle_pspoll(local, hdr, rx_stats);
  1841. goto done;
  1842. }
  1843. if (local->hostapd) {
  1844. PDEBUG(DEBUG_AP, "Unknown frame in AP queue: type=0x%02x "
  1845. "subtype=0x%02x\n", type, stype);
  1846. goto done;
  1847. }
  1848. #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
  1849. if (type != IEEE80211_FTYPE_MGMT) {
  1850. PDEBUG(DEBUG_AP, "handle_ap_item - not a management frame?\n");
  1851. goto done;
  1852. }
  1853. if (!ether_addr_equal(hdr->addr1, dev->dev_addr)) {
  1854. PDEBUG(DEBUG_AP, "handle_ap_item - addr1(DA)=%pM"
  1855. " not own MAC\n", hdr->addr1);
  1856. goto done;
  1857. }
  1858. if (!ether_addr_equal(hdr->addr3, dev->dev_addr)) {
  1859. PDEBUG(DEBUG_AP, "handle_ap_item - addr3(BSSID)=%pM"
  1860. " not own MAC\n", hdr->addr3);
  1861. goto done;
  1862. }
  1863. switch (stype) {
  1864. case IEEE80211_STYPE_ASSOC_REQ:
  1865. handle_assoc(local, skb, rx_stats, 0);
  1866. break;
  1867. case IEEE80211_STYPE_ASSOC_RESP:
  1868. PDEBUG(DEBUG_AP, "==> ASSOC RESP (ignored)\n");
  1869. break;
  1870. case IEEE80211_STYPE_REASSOC_REQ:
  1871. handle_assoc(local, skb, rx_stats, 1);
  1872. break;
  1873. case IEEE80211_STYPE_REASSOC_RESP:
  1874. PDEBUG(DEBUG_AP, "==> REASSOC RESP (ignored)\n");
  1875. break;
  1876. case IEEE80211_STYPE_ATIM:
  1877. PDEBUG(DEBUG_AP, "==> ATIM (ignored)\n");
  1878. break;
  1879. case IEEE80211_STYPE_DISASSOC:
  1880. handle_disassoc(local, skb, rx_stats);
  1881. break;
  1882. case IEEE80211_STYPE_AUTH:
  1883. handle_authen(local, skb, rx_stats);
  1884. break;
  1885. case IEEE80211_STYPE_DEAUTH:
  1886. handle_deauth(local, skb, rx_stats);
  1887. break;
  1888. default:
  1889. PDEBUG(DEBUG_AP, "Unknown mgmt frame subtype 0x%02x\n",
  1890. stype >> 4);
  1891. break;
  1892. }
  1893. #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
  1894. done:
  1895. dev_kfree_skb(skb);
  1896. }
  1897. /* Called only as a tasklet (software IRQ) */
  1898. void hostap_rx(struct net_device *dev, struct sk_buff *skb,
  1899. struct hostap_80211_rx_status *rx_stats)
  1900. {
  1901. struct hostap_interface *iface;
  1902. local_info_t *local;
  1903. struct ieee80211_hdr *hdr;
  1904. iface = netdev_priv(dev);
  1905. local = iface->local;
  1906. if (skb->len < 16)
  1907. goto drop;
  1908. dev->stats.rx_packets++;
  1909. hdr = (struct ieee80211_hdr *) skb->data;
  1910. if (local->ap->ap_policy == AP_OTHER_AP_SKIP_ALL &&
  1911. ieee80211_is_beacon(hdr->frame_control))
  1912. goto drop;
  1913. skb->protocol = cpu_to_be16(ETH_P_HOSTAP);
  1914. handle_ap_item(local, skb, rx_stats);
  1915. return;
  1916. drop:
  1917. dev_kfree_skb(skb);
  1918. }
  1919. /* Called only as a tasklet (software IRQ) */
  1920. static void schedule_packet_send(local_info_t *local, struct sta_info *sta)
  1921. {
  1922. struct sk_buff *skb;
  1923. struct ieee80211_hdr *hdr;
  1924. struct hostap_80211_rx_status rx_stats;
  1925. if (skb_queue_empty(&sta->tx_buf))
  1926. return;
  1927. skb = dev_alloc_skb(16);
  1928. if (skb == NULL) {
  1929. printk(KERN_DEBUG "%s: schedule_packet_send: skb alloc "
  1930. "failed\n", local->dev->name);
  1931. return;
  1932. }
  1933. hdr = skb_put(skb, 16);
  1934. /* Generate a fake pspoll frame to start packet delivery */
  1935. hdr->frame_control = cpu_to_le16(
  1936. IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL);
  1937. memcpy(hdr->addr1, local->dev->dev_addr, ETH_ALEN);
  1938. memcpy(hdr->addr2, sta->addr, ETH_ALEN);
  1939. hdr->duration_id = cpu_to_le16(sta->aid | BIT(15) | BIT(14));
  1940. PDEBUG(DEBUG_PS2,
  1941. "%s: Scheduling buffered packet delivery for STA %pM\n",
  1942. local->dev->name, sta->addr);
  1943. skb->dev = local->dev;
  1944. memset(&rx_stats, 0, sizeof(rx_stats));
  1945. hostap_rx(local->dev, skb, &rx_stats);
  1946. }
  1947. int prism2_ap_get_sta_qual(local_info_t *local, struct sockaddr addr[],
  1948. struct iw_quality qual[], int buf_size,
  1949. int aplist)
  1950. {
  1951. struct ap_data *ap = local->ap;
  1952. struct list_head *ptr;
  1953. int count = 0;
  1954. spin_lock_bh(&ap->sta_table_lock);
  1955. for (ptr = ap->sta_list.next; ptr != NULL && ptr != &ap->sta_list;
  1956. ptr = ptr->next) {
  1957. struct sta_info *sta = (struct sta_info *) ptr;
  1958. if (aplist && !sta->ap)
  1959. continue;
  1960. addr[count].sa_family = ARPHRD_ETHER;
  1961. memcpy(addr[count].sa_data, sta->addr, ETH_ALEN);
  1962. if (sta->last_rx_silence == 0)
  1963. qual[count].qual = sta->last_rx_signal < 27 ?
  1964. 0 : (sta->last_rx_signal - 27) * 92 / 127;
  1965. else
  1966. qual[count].qual = sta->last_rx_signal -
  1967. sta->last_rx_silence - 35;
  1968. qual[count].level = HFA384X_LEVEL_TO_dBm(sta->last_rx_signal);
  1969. qual[count].noise = HFA384X_LEVEL_TO_dBm(sta->last_rx_silence);
  1970. qual[count].updated = sta->last_rx_updated;
  1971. sta->last_rx_updated = IW_QUAL_DBM;
  1972. count++;
  1973. if (count >= buf_size)
  1974. break;
  1975. }
  1976. spin_unlock_bh(&ap->sta_table_lock);
  1977. return count;
  1978. }
  1979. /* Translate our list of Access Points & Stations to a card independent
  1980. * format that the Wireless Tools will understand - Jean II */
  1981. int prism2_ap_translate_scan(struct net_device *dev,
  1982. struct iw_request_info *info, char *buffer)
  1983. {
  1984. struct hostap_interface *iface;
  1985. local_info_t *local;
  1986. struct ap_data *ap;
  1987. struct list_head *ptr;
  1988. struct iw_event iwe;
  1989. char *current_ev = buffer;
  1990. char *end_buf = buffer + IW_SCAN_MAX_DATA;
  1991. #if !defined(PRISM2_NO_KERNEL_IEEE80211_MGMT)
  1992. char buf[64];
  1993. #endif
  1994. iface = netdev_priv(dev);
  1995. local = iface->local;
  1996. ap = local->ap;
  1997. spin_lock_bh(&ap->sta_table_lock);
  1998. for (ptr = ap->sta_list.next; ptr != NULL && ptr != &ap->sta_list;
  1999. ptr = ptr->next) {
  2000. struct sta_info *sta = (struct sta_info *) ptr;
  2001. /* First entry *MUST* be the AP MAC address */
  2002. memset(&iwe, 0, sizeof(iwe));
  2003. iwe.cmd = SIOCGIWAP;
  2004. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  2005. memcpy(iwe.u.ap_addr.sa_data, sta->addr, ETH_ALEN);
  2006. iwe.len = IW_EV_ADDR_LEN;
  2007. current_ev = iwe_stream_add_event(info, current_ev, end_buf,
  2008. &iwe, IW_EV_ADDR_LEN);
  2009. /* Use the mode to indicate if it's a station or
  2010. * an Access Point */
  2011. memset(&iwe, 0, sizeof(iwe));
  2012. iwe.cmd = SIOCGIWMODE;
  2013. if (sta->ap)
  2014. iwe.u.mode = IW_MODE_MASTER;
  2015. else
  2016. iwe.u.mode = IW_MODE_INFRA;
  2017. iwe.len = IW_EV_UINT_LEN;
  2018. current_ev = iwe_stream_add_event(info, current_ev, end_buf,
  2019. &iwe, IW_EV_UINT_LEN);
  2020. /* Some quality */
  2021. memset(&iwe, 0, sizeof(iwe));
  2022. iwe.cmd = IWEVQUAL;
  2023. if (sta->last_rx_silence == 0)
  2024. iwe.u.qual.qual = sta->last_rx_signal < 27 ?
  2025. 0 : (sta->last_rx_signal - 27) * 92 / 127;
  2026. else
  2027. iwe.u.qual.qual = sta->last_rx_signal -
  2028. sta->last_rx_silence - 35;
  2029. iwe.u.qual.level = HFA384X_LEVEL_TO_dBm(sta->last_rx_signal);
  2030. iwe.u.qual.noise = HFA384X_LEVEL_TO_dBm(sta->last_rx_silence);
  2031. iwe.u.qual.updated = sta->last_rx_updated;
  2032. iwe.len = IW_EV_QUAL_LEN;
  2033. current_ev = iwe_stream_add_event(info, current_ev, end_buf,
  2034. &iwe, IW_EV_QUAL_LEN);
  2035. #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
  2036. if (sta->ap) {
  2037. memset(&iwe, 0, sizeof(iwe));
  2038. iwe.cmd = SIOCGIWESSID;
  2039. iwe.u.data.length = sta->u.ap.ssid_len;
  2040. iwe.u.data.flags = 1;
  2041. current_ev = iwe_stream_add_point(info, current_ev,
  2042. end_buf, &iwe,
  2043. sta->u.ap.ssid);
  2044. memset(&iwe, 0, sizeof(iwe));
  2045. iwe.cmd = SIOCGIWENCODE;
  2046. if (sta->capability & WLAN_CAPABILITY_PRIVACY)
  2047. iwe.u.data.flags =
  2048. IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
  2049. else
  2050. iwe.u.data.flags = IW_ENCODE_DISABLED;
  2051. current_ev = iwe_stream_add_point(info, current_ev,
  2052. end_buf, &iwe,
  2053. sta->u.ap.ssid);
  2054. if (sta->u.ap.channel > 0 &&
  2055. sta->u.ap.channel <= FREQ_COUNT) {
  2056. memset(&iwe, 0, sizeof(iwe));
  2057. iwe.cmd = SIOCGIWFREQ;
  2058. iwe.u.freq.m = freq_list[sta->u.ap.channel - 1]
  2059. * 100000;
  2060. iwe.u.freq.e = 1;
  2061. current_ev = iwe_stream_add_event(
  2062. info, current_ev, end_buf, &iwe,
  2063. IW_EV_FREQ_LEN);
  2064. }
  2065. memset(&iwe, 0, sizeof(iwe));
  2066. iwe.cmd = IWEVCUSTOM;
  2067. sprintf(buf, "beacon_interval=%d",
  2068. sta->listen_interval);
  2069. iwe.u.data.length = strlen(buf);
  2070. current_ev = iwe_stream_add_point(info, current_ev,
  2071. end_buf, &iwe, buf);
  2072. }
  2073. #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
  2074. sta->last_rx_updated = IW_QUAL_DBM;
  2075. /* To be continued, we should make good use of IWEVCUSTOM */
  2076. }
  2077. spin_unlock_bh(&ap->sta_table_lock);
  2078. return current_ev - buffer;
  2079. }
  2080. static int prism2_hostapd_add_sta(struct ap_data *ap,
  2081. struct prism2_hostapd_param *param)
  2082. {
  2083. struct sta_info *sta;
  2084. spin_lock_bh(&ap->sta_table_lock);
  2085. sta = ap_get_sta(ap, param->sta_addr);
  2086. if (sta)
  2087. atomic_inc(&sta->users);
  2088. spin_unlock_bh(&ap->sta_table_lock);
  2089. if (sta == NULL) {
  2090. sta = ap_add_sta(ap, param->sta_addr);
  2091. if (sta == NULL)
  2092. return -1;
  2093. }
  2094. if (!(sta->flags & WLAN_STA_ASSOC) && !sta->ap && sta->local)
  2095. hostap_event_new_sta(sta->local->dev, sta);
  2096. sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
  2097. sta->last_rx = jiffies;
  2098. sta->aid = param->u.add_sta.aid;
  2099. sta->capability = param->u.add_sta.capability;
  2100. sta->tx_supp_rates = param->u.add_sta.tx_supp_rates;
  2101. if (sta->tx_supp_rates & WLAN_RATE_1M)
  2102. sta->supported_rates[0] = 2;
  2103. if (sta->tx_supp_rates & WLAN_RATE_2M)
  2104. sta->supported_rates[1] = 4;
  2105. if (sta->tx_supp_rates & WLAN_RATE_5M5)
  2106. sta->supported_rates[2] = 11;
  2107. if (sta->tx_supp_rates & WLAN_RATE_11M)
  2108. sta->supported_rates[3] = 22;
  2109. prism2_check_tx_rates(sta);
  2110. atomic_dec(&sta->users);
  2111. return 0;
  2112. }
  2113. static int prism2_hostapd_remove_sta(struct ap_data *ap,
  2114. struct prism2_hostapd_param *param)
  2115. {
  2116. struct sta_info *sta;
  2117. spin_lock_bh(&ap->sta_table_lock);
  2118. sta = ap_get_sta(ap, param->sta_addr);
  2119. if (sta) {
  2120. ap_sta_hash_del(ap, sta);
  2121. list_del(&sta->list);
  2122. }
  2123. spin_unlock_bh(&ap->sta_table_lock);
  2124. if (!sta)
  2125. return -ENOENT;
  2126. if ((sta->flags & WLAN_STA_ASSOC) && !sta->ap && sta->local)
  2127. hostap_event_expired_sta(sta->local->dev, sta);
  2128. ap_free_sta(ap, sta);
  2129. return 0;
  2130. }
  2131. static int prism2_hostapd_get_info_sta(struct ap_data *ap,
  2132. struct prism2_hostapd_param *param)
  2133. {
  2134. struct sta_info *sta;
  2135. spin_lock_bh(&ap->sta_table_lock);
  2136. sta = ap_get_sta(ap, param->sta_addr);
  2137. if (sta)
  2138. atomic_inc(&sta->users);
  2139. spin_unlock_bh(&ap->sta_table_lock);
  2140. if (!sta)
  2141. return -ENOENT;
  2142. param->u.get_info_sta.inactive_sec = (jiffies - sta->last_rx) / HZ;
  2143. atomic_dec(&sta->users);
  2144. return 1;
  2145. }
  2146. static int prism2_hostapd_set_flags_sta(struct ap_data *ap,
  2147. struct prism2_hostapd_param *param)
  2148. {
  2149. struct sta_info *sta;
  2150. spin_lock_bh(&ap->sta_table_lock);
  2151. sta = ap_get_sta(ap, param->sta_addr);
  2152. if (sta) {
  2153. sta->flags |= param->u.set_flags_sta.flags_or;
  2154. sta->flags &= param->u.set_flags_sta.flags_and;
  2155. }
  2156. spin_unlock_bh(&ap->sta_table_lock);
  2157. if (!sta)
  2158. return -ENOENT;
  2159. return 0;
  2160. }
  2161. static int prism2_hostapd_sta_clear_stats(struct ap_data *ap,
  2162. struct prism2_hostapd_param *param)
  2163. {
  2164. struct sta_info *sta;
  2165. int rate;
  2166. spin_lock_bh(&ap->sta_table_lock);
  2167. sta = ap_get_sta(ap, param->sta_addr);
  2168. if (sta) {
  2169. sta->rx_packets = sta->tx_packets = 0;
  2170. sta->rx_bytes = sta->tx_bytes = 0;
  2171. for (rate = 0; rate < WLAN_RATE_COUNT; rate++) {
  2172. sta->tx_count[rate] = 0;
  2173. sta->rx_count[rate] = 0;
  2174. }
  2175. }
  2176. spin_unlock_bh(&ap->sta_table_lock);
  2177. if (!sta)
  2178. return -ENOENT;
  2179. return 0;
  2180. }
  2181. int prism2_hostapd(struct ap_data *ap, struct prism2_hostapd_param *param)
  2182. {
  2183. switch (param->cmd) {
  2184. case PRISM2_HOSTAPD_FLUSH:
  2185. ap_control_kickall(ap);
  2186. return 0;
  2187. case PRISM2_HOSTAPD_ADD_STA:
  2188. return prism2_hostapd_add_sta(ap, param);
  2189. case PRISM2_HOSTAPD_REMOVE_STA:
  2190. return prism2_hostapd_remove_sta(ap, param);
  2191. case PRISM2_HOSTAPD_GET_INFO_STA:
  2192. return prism2_hostapd_get_info_sta(ap, param);
  2193. case PRISM2_HOSTAPD_SET_FLAGS_STA:
  2194. return prism2_hostapd_set_flags_sta(ap, param);
  2195. case PRISM2_HOSTAPD_STA_CLEAR_STATS:
  2196. return prism2_hostapd_sta_clear_stats(ap, param);
  2197. default:
  2198. printk(KERN_WARNING "prism2_hostapd: unknown cmd=%d\n",
  2199. param->cmd);
  2200. return -EOPNOTSUPP;
  2201. }
  2202. }
  2203. /* Update station info for host-based TX rate control and return current
  2204. * TX rate */
  2205. static int ap_update_sta_tx_rate(struct sta_info *sta, struct net_device *dev)
  2206. {
  2207. int ret = sta->tx_rate;
  2208. struct hostap_interface *iface;
  2209. local_info_t *local;
  2210. iface = netdev_priv(dev);
  2211. local = iface->local;
  2212. sta->tx_count[sta->tx_rate_idx]++;
  2213. sta->tx_since_last_failure++;
  2214. sta->tx_consecutive_exc = 0;
  2215. if (sta->tx_since_last_failure >= WLAN_RATE_UPDATE_COUNT &&
  2216. sta->tx_rate_idx < sta->tx_max_rate) {
  2217. /* use next higher rate */
  2218. int old_rate, new_rate;
  2219. old_rate = new_rate = sta->tx_rate_idx;
  2220. while (new_rate < sta->tx_max_rate) {
  2221. new_rate++;
  2222. if (ap_tx_rate_ok(new_rate, sta, local)) {
  2223. sta->tx_rate_idx = new_rate;
  2224. break;
  2225. }
  2226. }
  2227. if (old_rate != sta->tx_rate_idx) {
  2228. switch (sta->tx_rate_idx) {
  2229. case 0: sta->tx_rate = 10; break;
  2230. case 1: sta->tx_rate = 20; break;
  2231. case 2: sta->tx_rate = 55; break;
  2232. case 3: sta->tx_rate = 110; break;
  2233. default: sta->tx_rate = 0; break;
  2234. }
  2235. PDEBUG(DEBUG_AP, "%s: STA %pM TX rate raised to %d\n",
  2236. dev->name, sta->addr, sta->tx_rate);
  2237. }
  2238. sta->tx_since_last_failure = 0;
  2239. }
  2240. return ret;
  2241. }
  2242. /* Called only from software IRQ. Called for each TX frame prior possible
  2243. * encryption and transmit. */
  2244. ap_tx_ret hostap_handle_sta_tx(local_info_t *local, struct hostap_tx_data *tx)
  2245. {
  2246. struct sta_info *sta = NULL;
  2247. struct sk_buff *skb = tx->skb;
  2248. int set_tim, ret;
  2249. struct ieee80211_hdr *hdr;
  2250. struct hostap_skb_tx_data *meta;
  2251. meta = (struct hostap_skb_tx_data *) skb->cb;
  2252. ret = AP_TX_CONTINUE;
  2253. if (local->ap == NULL || skb->len < 10 ||
  2254. meta->iface->type == HOSTAP_INTERFACE_STA)
  2255. goto out;
  2256. hdr = (struct ieee80211_hdr *) skb->data;
  2257. if (hdr->addr1[0] & 0x01) {
  2258. /* broadcast/multicast frame - no AP related processing */
  2259. if (local->ap->num_sta <= 0)
  2260. ret = AP_TX_DROP;
  2261. goto out;
  2262. }
  2263. /* unicast packet - check whether destination STA is associated */
  2264. spin_lock(&local->ap->sta_table_lock);
  2265. sta = ap_get_sta(local->ap, hdr->addr1);
  2266. if (sta)
  2267. atomic_inc(&sta->users);
  2268. spin_unlock(&local->ap->sta_table_lock);
  2269. if (local->iw_mode == IW_MODE_MASTER && sta == NULL &&
  2270. !(meta->flags & HOSTAP_TX_FLAGS_WDS) &&
  2271. meta->iface->type != HOSTAP_INTERFACE_MASTER &&
  2272. meta->iface->type != HOSTAP_INTERFACE_AP) {
  2273. #if 0
  2274. /* This can happen, e.g., when wlan0 is added to a bridge and
  2275. * bridging code does not know which port is the correct target
  2276. * for a unicast frame. In this case, the packet is send to all
  2277. * ports of the bridge. Since this is a valid scenario, do not
  2278. * print out any errors here. */
  2279. if (net_ratelimit()) {
  2280. printk(KERN_DEBUG "AP: drop packet to non-associated "
  2281. "STA %pM\n", hdr->addr1);
  2282. }
  2283. #endif
  2284. local->ap->tx_drop_nonassoc++;
  2285. ret = AP_TX_DROP;
  2286. goto out;
  2287. }
  2288. if (sta == NULL)
  2289. goto out;
  2290. if (!(sta->flags & WLAN_STA_AUTHORIZED))
  2291. ret = AP_TX_CONTINUE_NOT_AUTHORIZED;
  2292. /* Set tx_rate if using host-based TX rate control */
  2293. if (!local->fw_tx_rate_control)
  2294. local->ap->last_tx_rate = meta->rate =
  2295. ap_update_sta_tx_rate(sta, local->dev);
  2296. if (local->iw_mode != IW_MODE_MASTER)
  2297. goto out;
  2298. if (!(sta->flags & WLAN_STA_PS))
  2299. goto out;
  2300. if (meta->flags & HOSTAP_TX_FLAGS_ADD_MOREDATA) {
  2301. /* indicate to STA that more frames follow */
  2302. hdr->frame_control |=
  2303. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  2304. }
  2305. if (meta->flags & HOSTAP_TX_FLAGS_BUFFERED_FRAME) {
  2306. /* packet was already buffered and now send due to
  2307. * PS poll, so do not rebuffer it */
  2308. goto out;
  2309. }
  2310. if (skb_queue_len(&sta->tx_buf) >= STA_MAX_TX_BUFFER) {
  2311. PDEBUG(DEBUG_PS, "%s: No more space in STA (%pM)'s"
  2312. "PS mode buffer\n",
  2313. local->dev->name, sta->addr);
  2314. /* Make sure that TIM is set for the station (it might not be
  2315. * after AP wlan hw reset). */
  2316. /* FIX: should fix hw reset to restore bits based on STA
  2317. * buffer state.. */
  2318. hostap_set_tim(local, sta->aid, 1);
  2319. sta->flags |= WLAN_STA_TIM;
  2320. ret = AP_TX_DROP;
  2321. goto out;
  2322. }
  2323. /* STA in PS mode, buffer frame for later delivery */
  2324. set_tim = skb_queue_empty(&sta->tx_buf);
  2325. skb_queue_tail(&sta->tx_buf, skb);
  2326. /* FIX: could save RX time to skb and expire buffered frames after
  2327. * some time if STA does not poll for them */
  2328. if (set_tim) {
  2329. if (sta->flags & WLAN_STA_TIM)
  2330. PDEBUG(DEBUG_PS2, "Re-setting TIM for aid %d\n",
  2331. sta->aid);
  2332. hostap_set_tim(local, sta->aid, 1);
  2333. sta->flags |= WLAN_STA_TIM;
  2334. }
  2335. ret = AP_TX_BUFFERED;
  2336. out:
  2337. if (sta != NULL) {
  2338. if (ret == AP_TX_CONTINUE ||
  2339. ret == AP_TX_CONTINUE_NOT_AUTHORIZED) {
  2340. sta->tx_packets++;
  2341. sta->tx_bytes += skb->len;
  2342. sta->last_tx = jiffies;
  2343. }
  2344. if ((ret == AP_TX_CONTINUE ||
  2345. ret == AP_TX_CONTINUE_NOT_AUTHORIZED) &&
  2346. sta->crypt && tx->host_encrypt) {
  2347. tx->crypt = sta->crypt;
  2348. tx->sta_ptr = sta; /* hostap_handle_sta_release() will
  2349. * be called to release sta info
  2350. * later */
  2351. } else
  2352. atomic_dec(&sta->users);
  2353. }
  2354. return ret;
  2355. }
  2356. void hostap_handle_sta_release(void *ptr)
  2357. {
  2358. struct sta_info *sta = ptr;
  2359. atomic_dec(&sta->users);
  2360. }
  2361. /* Called only as a tasklet (software IRQ) */
  2362. void hostap_handle_sta_tx_exc(local_info_t *local, struct sk_buff *skb)
  2363. {
  2364. struct sta_info *sta;
  2365. struct ieee80211_hdr *hdr;
  2366. struct hostap_skb_tx_data *meta;
  2367. hdr = (struct ieee80211_hdr *) skb->data;
  2368. meta = (struct hostap_skb_tx_data *) skb->cb;
  2369. spin_lock(&local->ap->sta_table_lock);
  2370. sta = ap_get_sta(local->ap, hdr->addr1);
  2371. if (!sta) {
  2372. spin_unlock(&local->ap->sta_table_lock);
  2373. PDEBUG(DEBUG_AP, "%s: Could not find STA %pM"
  2374. " for this TX error (@%lu)\n",
  2375. local->dev->name, hdr->addr1, jiffies);
  2376. return;
  2377. }
  2378. sta->tx_since_last_failure = 0;
  2379. sta->tx_consecutive_exc++;
  2380. if (sta->tx_consecutive_exc >= WLAN_RATE_DECREASE_THRESHOLD &&
  2381. sta->tx_rate_idx > 0 && meta->rate <= sta->tx_rate) {
  2382. /* use next lower rate */
  2383. int old, rate;
  2384. old = rate = sta->tx_rate_idx;
  2385. while (rate > 0) {
  2386. rate--;
  2387. if (ap_tx_rate_ok(rate, sta, local)) {
  2388. sta->tx_rate_idx = rate;
  2389. break;
  2390. }
  2391. }
  2392. if (old != sta->tx_rate_idx) {
  2393. switch (sta->tx_rate_idx) {
  2394. case 0: sta->tx_rate = 10; break;
  2395. case 1: sta->tx_rate = 20; break;
  2396. case 2: sta->tx_rate = 55; break;
  2397. case 3: sta->tx_rate = 110; break;
  2398. default: sta->tx_rate = 0; break;
  2399. }
  2400. PDEBUG(DEBUG_AP,
  2401. "%s: STA %pM TX rate lowered to %d\n",
  2402. local->dev->name, sta->addr, sta->tx_rate);
  2403. }
  2404. sta->tx_consecutive_exc = 0;
  2405. }
  2406. spin_unlock(&local->ap->sta_table_lock);
  2407. }
  2408. static void hostap_update_sta_ps2(local_info_t *local, struct sta_info *sta,
  2409. int pwrmgt, int type, int stype)
  2410. {
  2411. if (pwrmgt && !(sta->flags & WLAN_STA_PS)) {
  2412. sta->flags |= WLAN_STA_PS;
  2413. PDEBUG(DEBUG_PS2, "STA %pM changed to use PS "
  2414. "mode (type=0x%02X, stype=0x%02X)\n",
  2415. sta->addr, type >> 2, stype >> 4);
  2416. } else if (!pwrmgt && (sta->flags & WLAN_STA_PS)) {
  2417. sta->flags &= ~WLAN_STA_PS;
  2418. PDEBUG(DEBUG_PS2, "STA %pM changed to not use "
  2419. "PS mode (type=0x%02X, stype=0x%02X)\n",
  2420. sta->addr, type >> 2, stype >> 4);
  2421. if (type != IEEE80211_FTYPE_CTL ||
  2422. stype != IEEE80211_STYPE_PSPOLL)
  2423. schedule_packet_send(local, sta);
  2424. }
  2425. }
  2426. /* Called only as a tasklet (software IRQ). Called for each RX frame to update
  2427. * STA power saving state. pwrmgt is a flag from 802.11 frame_control field. */
  2428. int hostap_update_sta_ps(local_info_t *local, struct ieee80211_hdr *hdr)
  2429. {
  2430. struct sta_info *sta;
  2431. u16 fc;
  2432. spin_lock(&local->ap->sta_table_lock);
  2433. sta = ap_get_sta(local->ap, hdr->addr2);
  2434. if (sta)
  2435. atomic_inc(&sta->users);
  2436. spin_unlock(&local->ap->sta_table_lock);
  2437. if (!sta)
  2438. return -1;
  2439. fc = le16_to_cpu(hdr->frame_control);
  2440. hostap_update_sta_ps2(local, sta, fc & IEEE80211_FCTL_PM,
  2441. fc & IEEE80211_FCTL_FTYPE,
  2442. fc & IEEE80211_FCTL_STYPE);
  2443. atomic_dec(&sta->users);
  2444. return 0;
  2445. }
  2446. /* Called only as a tasklet (software IRQ). Called for each RX frame after
  2447. * getting RX header and payload from hardware. */
  2448. ap_rx_ret hostap_handle_sta_rx(local_info_t *local, struct net_device *dev,
  2449. struct sk_buff *skb,
  2450. struct hostap_80211_rx_status *rx_stats,
  2451. int wds)
  2452. {
  2453. int ret;
  2454. struct sta_info *sta;
  2455. u16 fc, type, stype;
  2456. struct ieee80211_hdr *hdr;
  2457. if (local->ap == NULL)
  2458. return AP_RX_CONTINUE;
  2459. hdr = (struct ieee80211_hdr *) skb->data;
  2460. fc = le16_to_cpu(hdr->frame_control);
  2461. type = fc & IEEE80211_FCTL_FTYPE;
  2462. stype = fc & IEEE80211_FCTL_STYPE;
  2463. spin_lock(&local->ap->sta_table_lock);
  2464. sta = ap_get_sta(local->ap, hdr->addr2);
  2465. if (sta)
  2466. atomic_inc(&sta->users);
  2467. spin_unlock(&local->ap->sta_table_lock);
  2468. if (sta && !(sta->flags & WLAN_STA_AUTHORIZED))
  2469. ret = AP_RX_CONTINUE_NOT_AUTHORIZED;
  2470. else
  2471. ret = AP_RX_CONTINUE;
  2472. if (fc & IEEE80211_FCTL_TODS) {
  2473. if (!wds && (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))) {
  2474. if (local->hostapd) {
  2475. prism2_rx_80211(local->apdev, skb, rx_stats,
  2476. PRISM2_RX_NON_ASSOC);
  2477. #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
  2478. } else {
  2479. printk(KERN_DEBUG "%s: dropped received packet"
  2480. " from non-associated STA %pM"
  2481. " (type=0x%02x, subtype=0x%02x)\n",
  2482. dev->name, hdr->addr2,
  2483. type >> 2, stype >> 4);
  2484. hostap_rx(dev, skb, rx_stats);
  2485. #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
  2486. }
  2487. ret = AP_RX_EXIT;
  2488. goto out;
  2489. }
  2490. } else if (fc & IEEE80211_FCTL_FROMDS) {
  2491. if (!wds) {
  2492. /* FromDS frame - not for us; probably
  2493. * broadcast/multicast in another BSS - drop */
  2494. if (ether_addr_equal(hdr->addr1, dev->dev_addr)) {
  2495. printk(KERN_DEBUG "Odd.. FromDS packet "
  2496. "received with own BSSID\n");
  2497. hostap_dump_rx_80211(dev->name, skb, rx_stats);
  2498. }
  2499. ret = AP_RX_DROP;
  2500. goto out;
  2501. }
  2502. } else if (stype == IEEE80211_STYPE_NULLFUNC && sta == NULL &&
  2503. ether_addr_equal(hdr->addr1, dev->dev_addr)) {
  2504. if (local->hostapd) {
  2505. prism2_rx_80211(local->apdev, skb, rx_stats,
  2506. PRISM2_RX_NON_ASSOC);
  2507. #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
  2508. } else {
  2509. /* At least Lucent f/w seems to send data::nullfunc
  2510. * frames with no ToDS flag when the current AP returns
  2511. * after being unavailable for some time. Speed up
  2512. * re-association by informing the station about it not
  2513. * being associated. */
  2514. printk(KERN_DEBUG "%s: rejected received nullfunc frame"
  2515. " without ToDS from not associated STA %pM\n",
  2516. dev->name, hdr->addr2);
  2517. hostap_rx(dev, skb, rx_stats);
  2518. #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
  2519. }
  2520. ret = AP_RX_EXIT;
  2521. goto out;
  2522. } else if (stype == IEEE80211_STYPE_NULLFUNC) {
  2523. /* At least Lucent cards seem to send periodic nullfunc
  2524. * frames with ToDS. Let these through to update SQ
  2525. * stats and PS state. Nullfunc frames do not contain
  2526. * any data and they will be dropped below. */
  2527. } else {
  2528. /* If BSSID (Addr3) is foreign, this frame is a normal
  2529. * broadcast frame from an IBSS network. Drop it silently.
  2530. * If BSSID is own, report the dropping of this frame. */
  2531. if (ether_addr_equal(hdr->addr3, dev->dev_addr)) {
  2532. printk(KERN_DEBUG "%s: dropped received packet from %pM"
  2533. " with no ToDS flag "
  2534. "(type=0x%02x, subtype=0x%02x)\n", dev->name,
  2535. hdr->addr2, type >> 2, stype >> 4);
  2536. hostap_dump_rx_80211(dev->name, skb, rx_stats);
  2537. }
  2538. ret = AP_RX_DROP;
  2539. goto out;
  2540. }
  2541. if (sta) {
  2542. hostap_update_sta_ps2(local, sta, fc & IEEE80211_FCTL_PM,
  2543. type, stype);
  2544. sta->rx_packets++;
  2545. sta->rx_bytes += skb->len;
  2546. sta->last_rx = jiffies;
  2547. }
  2548. if (local->ap->nullfunc_ack && stype == IEEE80211_STYPE_NULLFUNC &&
  2549. fc & IEEE80211_FCTL_TODS) {
  2550. if (local->hostapd) {
  2551. prism2_rx_80211(local->apdev, skb, rx_stats,
  2552. PRISM2_RX_NULLFUNC_ACK);
  2553. #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
  2554. } else {
  2555. /* some STA f/w's seem to require control::ACK frame
  2556. * for data::nullfunc, but Prism2 f/w 0.8.0 (at least
  2557. * from Compaq) does not send this.. Try to generate
  2558. * ACK for these frames from the host driver to make
  2559. * power saving work with, e.g., Lucent WaveLAN f/w */
  2560. hostap_rx(dev, skb, rx_stats);
  2561. #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
  2562. }
  2563. ret = AP_RX_EXIT;
  2564. goto out;
  2565. }
  2566. out:
  2567. if (sta)
  2568. atomic_dec(&sta->users);
  2569. return ret;
  2570. }
  2571. /* Called only as a tasklet (software IRQ) */
  2572. int hostap_handle_sta_crypto(local_info_t *local,
  2573. struct ieee80211_hdr *hdr,
  2574. struct lib80211_crypt_data **crypt,
  2575. void **sta_ptr)
  2576. {
  2577. struct sta_info *sta;
  2578. spin_lock(&local->ap->sta_table_lock);
  2579. sta = ap_get_sta(local->ap, hdr->addr2);
  2580. if (sta)
  2581. atomic_inc(&sta->users);
  2582. spin_unlock(&local->ap->sta_table_lock);
  2583. if (!sta)
  2584. return -1;
  2585. if (sta->crypt) {
  2586. *crypt = sta->crypt;
  2587. *sta_ptr = sta;
  2588. /* hostap_handle_sta_release() will be called to release STA
  2589. * info */
  2590. } else
  2591. atomic_dec(&sta->users);
  2592. return 0;
  2593. }
  2594. /* Called only as a tasklet (software IRQ) */
  2595. int hostap_is_sta_assoc(struct ap_data *ap, u8 *sta_addr)
  2596. {
  2597. struct sta_info *sta;
  2598. int ret = 0;
  2599. spin_lock(&ap->sta_table_lock);
  2600. sta = ap_get_sta(ap, sta_addr);
  2601. if (sta != NULL && (sta->flags & WLAN_STA_ASSOC) && !sta->ap)
  2602. ret = 1;
  2603. spin_unlock(&ap->sta_table_lock);
  2604. return ret;
  2605. }
  2606. /* Called only as a tasklet (software IRQ) */
  2607. int hostap_is_sta_authorized(struct ap_data *ap, u8 *sta_addr)
  2608. {
  2609. struct sta_info *sta;
  2610. int ret = 0;
  2611. spin_lock(&ap->sta_table_lock);
  2612. sta = ap_get_sta(ap, sta_addr);
  2613. if (sta != NULL && (sta->flags & WLAN_STA_ASSOC) && !sta->ap &&
  2614. ((sta->flags & WLAN_STA_AUTHORIZED) ||
  2615. ap->local->ieee_802_1x == 0))
  2616. ret = 1;
  2617. spin_unlock(&ap->sta_table_lock);
  2618. return ret;
  2619. }
  2620. /* Called only as a tasklet (software IRQ) */
  2621. int hostap_add_sta(struct ap_data *ap, u8 *sta_addr)
  2622. {
  2623. struct sta_info *sta;
  2624. int ret = 1;
  2625. if (!ap)
  2626. return -1;
  2627. spin_lock(&ap->sta_table_lock);
  2628. sta = ap_get_sta(ap, sta_addr);
  2629. if (sta)
  2630. ret = 0;
  2631. spin_unlock(&ap->sta_table_lock);
  2632. if (ret == 1) {
  2633. sta = ap_add_sta(ap, sta_addr);
  2634. if (!sta)
  2635. return -1;
  2636. sta->flags = WLAN_STA_AUTH | WLAN_STA_ASSOC;
  2637. sta->ap = 1;
  2638. memset(sta->supported_rates, 0, sizeof(sta->supported_rates));
  2639. /* No way of knowing which rates are supported since we did not
  2640. * get supported rates element from beacon/assoc req. Assume
  2641. * that remote end supports all 802.11b rates. */
  2642. sta->supported_rates[0] = 0x82;
  2643. sta->supported_rates[1] = 0x84;
  2644. sta->supported_rates[2] = 0x0b;
  2645. sta->supported_rates[3] = 0x16;
  2646. sta->tx_supp_rates = WLAN_RATE_1M | WLAN_RATE_2M |
  2647. WLAN_RATE_5M5 | WLAN_RATE_11M;
  2648. sta->tx_rate = 110;
  2649. sta->tx_max_rate = sta->tx_rate_idx = 3;
  2650. }
  2651. return ret;
  2652. }
  2653. /* Called only as a tasklet (software IRQ) */
  2654. int hostap_update_rx_stats(struct ap_data *ap,
  2655. struct ieee80211_hdr *hdr,
  2656. struct hostap_80211_rx_status *rx_stats)
  2657. {
  2658. struct sta_info *sta;
  2659. if (!ap)
  2660. return -1;
  2661. spin_lock(&ap->sta_table_lock);
  2662. sta = ap_get_sta(ap, hdr->addr2);
  2663. if (sta) {
  2664. sta->last_rx_silence = rx_stats->noise;
  2665. sta->last_rx_signal = rx_stats->signal;
  2666. sta->last_rx_rate = rx_stats->rate;
  2667. sta->last_rx_updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
  2668. if (rx_stats->rate == 10)
  2669. sta->rx_count[0]++;
  2670. else if (rx_stats->rate == 20)
  2671. sta->rx_count[1]++;
  2672. else if (rx_stats->rate == 55)
  2673. sta->rx_count[2]++;
  2674. else if (rx_stats->rate == 110)
  2675. sta->rx_count[3]++;
  2676. }
  2677. spin_unlock(&ap->sta_table_lock);
  2678. return sta ? 0 : -1;
  2679. }
  2680. void hostap_update_rates(local_info_t *local)
  2681. {
  2682. struct sta_info *sta;
  2683. struct ap_data *ap = local->ap;
  2684. if (!ap)
  2685. return;
  2686. spin_lock_bh(&ap->sta_table_lock);
  2687. list_for_each_entry(sta, &ap->sta_list, list) {
  2688. prism2_check_tx_rates(sta);
  2689. }
  2690. spin_unlock_bh(&ap->sta_table_lock);
  2691. }
  2692. void * ap_crypt_get_ptrs(struct ap_data *ap, u8 *addr, int permanent,
  2693. struct lib80211_crypt_data ***crypt)
  2694. {
  2695. struct sta_info *sta;
  2696. spin_lock_bh(&ap->sta_table_lock);
  2697. sta = ap_get_sta(ap, addr);
  2698. if (sta)
  2699. atomic_inc(&sta->users);
  2700. spin_unlock_bh(&ap->sta_table_lock);
  2701. if (!sta && permanent)
  2702. sta = ap_add_sta(ap, addr);
  2703. if (!sta)
  2704. return NULL;
  2705. if (permanent)
  2706. sta->flags |= WLAN_STA_PERM;
  2707. *crypt = &sta->crypt;
  2708. return sta;
  2709. }
  2710. void hostap_add_wds_links(local_info_t *local)
  2711. {
  2712. struct ap_data *ap = local->ap;
  2713. struct sta_info *sta;
  2714. spin_lock_bh(&ap->sta_table_lock);
  2715. list_for_each_entry(sta, &ap->sta_list, list) {
  2716. if (sta->ap)
  2717. hostap_wds_link_oper(local, sta->addr, WDS_ADD);
  2718. }
  2719. spin_unlock_bh(&ap->sta_table_lock);
  2720. schedule_work(&local->ap->wds_oper_queue);
  2721. }
  2722. void hostap_wds_link_oper(local_info_t *local, u8 *addr, wds_oper_type type)
  2723. {
  2724. struct wds_oper_data *entry;
  2725. entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
  2726. if (!entry)
  2727. return;
  2728. memcpy(entry->addr, addr, ETH_ALEN);
  2729. entry->type = type;
  2730. spin_lock_bh(&local->lock);
  2731. entry->next = local->ap->wds_oper_entries;
  2732. local->ap->wds_oper_entries = entry;
  2733. spin_unlock_bh(&local->lock);
  2734. schedule_work(&local->ap->wds_oper_queue);
  2735. }
  2736. EXPORT_SYMBOL(hostap_init_data);
  2737. EXPORT_SYMBOL(hostap_init_ap_proc);
  2738. EXPORT_SYMBOL(hostap_free_data);
  2739. EXPORT_SYMBOL(hostap_check_sta_fw_version);
  2740. EXPORT_SYMBOL(hostap_handle_sta_tx_exc);
  2741. #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
  2742. #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */