hostap_ap.c 86 KB

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