hostap_ap.c 85 KB

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