hostap_ap.c 85 KB

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