hostap_ap.c 86 KB

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