hostap_ap.c 86 KB

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