hostap_ap.c 86 KB

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