hostap_ap.c 86 KB

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