hostap_ap.c 85 KB

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