hostap_ap.c 86 KB

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