hostap_ap.c 85 KB

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