hostap_ap.c 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339
  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 "hostap_wlan.h"
  27. #include "hostap.h"
  28. #include "hostap_ap.h"
  29. static int other_ap_policy[MAX_PARM_DEVICES] = { AP_OTHER_AP_SKIP_ALL,
  30. DEF_INTS };
  31. module_param_array(other_ap_policy, int, NULL, 0444);
  32. MODULE_PARM_DESC(other_ap_policy, "Other AP beacon monitoring policy (0-3)");
  33. static int ap_max_inactivity[MAX_PARM_DEVICES] = { AP_MAX_INACTIVITY_SEC,
  34. DEF_INTS };
  35. module_param_array(ap_max_inactivity, int, NULL, 0444);
  36. MODULE_PARM_DESC(ap_max_inactivity, "AP timeout (in seconds) for station "
  37. "inactivity");
  38. static int ap_bridge_packets[MAX_PARM_DEVICES] = { 1, DEF_INTS };
  39. module_param_array(ap_bridge_packets, int, NULL, 0444);
  40. MODULE_PARM_DESC(ap_bridge_packets, "Bridge packets directly between "
  41. "stations");
  42. static int autom_ap_wds[MAX_PARM_DEVICES] = { 0, DEF_INTS };
  43. module_param_array(autom_ap_wds, int, NULL, 0444);
  44. MODULE_PARM_DESC(autom_ap_wds, "Add WDS connections to other APs "
  45. "automatically");
  46. static struct sta_info* ap_get_sta(struct ap_data *ap, u8 *sta);
  47. static void hostap_event_expired_sta(struct net_device *dev,
  48. struct sta_info *sta);
  49. static void handle_add_proc_queue(struct work_struct *work);
  50. #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
  51. static void handle_wds_oper_queue(struct work_struct *work);
  52. static void prism2_send_mgmt(struct net_device *dev,
  53. u16 type_subtype, char *body,
  54. int body_len, u8 *addr, u16 tx_cb_idx);
  55. #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
  56. #ifndef PRISM2_NO_PROCFS_DEBUG
  57. static int ap_debug_proc_show(struct seq_file *m, void *v)
  58. {
  59. struct ap_data *ap = m->private;
  60. seq_printf(m, "BridgedUnicastFrames=%u\n", ap->bridged_unicast);
  61. seq_printf(m, "BridgedMulticastFrames=%u\n", ap->bridged_multicast);
  62. seq_printf(m, "max_inactivity=%u\n", ap->max_inactivity / HZ);
  63. seq_printf(m, "bridge_packets=%u\n", ap->bridge_packets);
  64. seq_printf(m, "nullfunc_ack=%u\n", ap->nullfunc_ack);
  65. seq_printf(m, "autom_ap_wds=%u\n", ap->autom_ap_wds);
  66. seq_printf(m, "auth_algs=%u\n", ap->local->auth_algs);
  67. seq_printf(m, "tx_drop_nonassoc=%u\n", ap->tx_drop_nonassoc);
  68. return 0;
  69. }
  70. static int ap_debug_proc_open(struct inode *inode, struct file *file)
  71. {
  72. return single_open(file, ap_debug_proc_show, PDE_DATA(inode));
  73. }
  74. static const struct file_operations ap_debug_proc_fops = {
  75. .open = ap_debug_proc_open,
  76. .read = seq_read,
  77. .llseek = seq_lseek,
  78. .release = single_release,
  79. };
  80. #endif /* PRISM2_NO_PROCFS_DEBUG */
  81. static void ap_sta_hash_add(struct ap_data *ap, struct sta_info *sta)
  82. {
  83. sta->hnext = ap->sta_hash[STA_HASH(sta->addr)];
  84. ap->sta_hash[STA_HASH(sta->addr)] = sta;
  85. }
  86. static void ap_sta_hash_del(struct ap_data *ap, struct sta_info *sta)
  87. {
  88. struct sta_info *s;
  89. s = ap->sta_hash[STA_HASH(sta->addr)];
  90. if (s == NULL) return;
  91. if (memcmp(s->addr, sta->addr, ETH_ALEN) == 0) {
  92. ap->sta_hash[STA_HASH(sta->addr)] = s->hnext;
  93. return;
  94. }
  95. while (s->hnext != NULL && memcmp(s->hnext->addr, sta->addr, ETH_ALEN)
  96. != 0)
  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 && sta->u.sta.challenge)
  124. kfree(sta->u.sta.challenge);
  125. del_timer(&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. memset(addr, 0xff, ETH_ALEN);
  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 (memcmp(entry->addr, mac, ETH_ALEN) == 0) {
  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 (memcmp(entry->addr, mac, ETH_ALEN) == 0) {
  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 && memcmp(s->addr, sta, ETH_ALEN) != 0)
  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 = (struct ieee80211_hdr *) skb_put(skb, hdrlen);
  824. if (body)
  825. memcpy(skb_put(skb, body_len), body, body_len);
  826. memset(hdr, 0, hdrlen);
  827. /* FIX: ctrl::ack sending used special HFA384X_TX_CTRL_802_11
  828. * tx_control instead of using local->tx_control */
  829. memcpy(hdr->addr1, addr, ETH_ALEN); /* DA / RA */
  830. if (ieee80211_is_data(hdr->frame_control)) {
  831. fc |= IEEE80211_FCTL_FROMDS;
  832. memcpy(hdr->addr2, dev->dev_addr, ETH_ALEN); /* BSSID */
  833. memcpy(hdr->addr3, dev->dev_addr, ETH_ALEN); /* SA */
  834. } else if (ieee80211_is_ctl(hdr->frame_control)) {
  835. /* control:ACK does not have addr2 or addr3 */
  836. memset(hdr->addr2, 0, ETH_ALEN);
  837. memset(hdr->addr3, 0, ETH_ALEN);
  838. } else {
  839. memcpy(hdr->addr2, dev->dev_addr, ETH_ALEN); /* SA */
  840. memcpy(hdr->addr3, dev->dev_addr, ETH_ALEN); /* BSSID */
  841. }
  842. hdr->frame_control = cpu_to_le16(fc);
  843. meta = (struct hostap_skb_tx_data *) skb->cb;
  844. memset(meta, 0, sizeof(*meta));
  845. meta->magic = HOSTAP_SKB_TX_DATA_MAGIC;
  846. meta->iface = iface;
  847. meta->tx_cb_idx = tx_cb_idx;
  848. skb->dev = dev;
  849. skb_reset_mac_header(skb);
  850. skb_reset_network_header(skb);
  851. dev_queue_xmit(skb);
  852. }
  853. #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
  854. static int prism2_sta_proc_show(struct seq_file *m, void *v)
  855. {
  856. struct sta_info *sta = m->private;
  857. int i;
  858. /* FIX: possible race condition.. the STA data could have just expired,
  859. * but proc entry was still here so that the read could have started;
  860. * some locking should be done here.. */
  861. seq_printf(m,
  862. "%s=%pM\nusers=%d\naid=%d\n"
  863. "flags=0x%04x%s%s%s%s%s%s%s\n"
  864. "capability=0x%02x\nlisten_interval=%d\nsupported_rates=",
  865. sta->ap ? "AP" : "STA",
  866. sta->addr, atomic_read(&sta->users), sta->aid,
  867. sta->flags,
  868. sta->flags & WLAN_STA_AUTH ? " AUTH" : "",
  869. sta->flags & WLAN_STA_ASSOC ? " ASSOC" : "",
  870. sta->flags & WLAN_STA_PS ? " PS" : "",
  871. sta->flags & WLAN_STA_TIM ? " TIM" : "",
  872. sta->flags & WLAN_STA_PERM ? " PERM" : "",
  873. sta->flags & WLAN_STA_AUTHORIZED ? " AUTHORIZED" : "",
  874. sta->flags & WLAN_STA_PENDING_POLL ? " POLL" : "",
  875. sta->capability, sta->listen_interval);
  876. /* supported_rates: 500 kbit/s units with msb ignored */
  877. for (i = 0; i < sizeof(sta->supported_rates); i++)
  878. if (sta->supported_rates[i] != 0)
  879. seq_printf(m, "%d%sMbps ",
  880. (sta->supported_rates[i] & 0x7f) / 2,
  881. sta->supported_rates[i] & 1 ? ".5" : "");
  882. seq_printf(m,
  883. "\njiffies=%lu\nlast_auth=%lu\nlast_assoc=%lu\n"
  884. "last_rx=%lu\nlast_tx=%lu\nrx_packets=%lu\n"
  885. "tx_packets=%lu\n"
  886. "rx_bytes=%lu\ntx_bytes=%lu\nbuffer_count=%d\n"
  887. "last_rx: silence=%d dBm signal=%d dBm rate=%d%s Mbps\n"
  888. "tx_rate=%d\ntx[1M]=%d\ntx[2M]=%d\ntx[5.5M]=%d\n"
  889. "tx[11M]=%d\n"
  890. "rx[1M]=%d\nrx[2M]=%d\nrx[5.5M]=%d\nrx[11M]=%d\n",
  891. jiffies, sta->last_auth, sta->last_assoc, sta->last_rx,
  892. sta->last_tx,
  893. sta->rx_packets, sta->tx_packets, sta->rx_bytes,
  894. sta->tx_bytes, skb_queue_len(&sta->tx_buf),
  895. sta->last_rx_silence,
  896. sta->last_rx_signal, sta->last_rx_rate / 10,
  897. sta->last_rx_rate % 10 ? ".5" : "",
  898. sta->tx_rate, sta->tx_count[0], sta->tx_count[1],
  899. sta->tx_count[2], sta->tx_count[3], sta->rx_count[0],
  900. sta->rx_count[1], sta->rx_count[2], sta->rx_count[3]);
  901. if (sta->crypt && sta->crypt->ops && sta->crypt->ops->print_stats)
  902. sta->crypt->ops->print_stats(m, sta->crypt->priv);
  903. #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
  904. if (sta->ap) {
  905. if (sta->u.ap.channel >= 0)
  906. seq_printf(m, "channel=%d\n", sta->u.ap.channel);
  907. seq_puts(m, "ssid=");
  908. for (i = 0; i < sta->u.ap.ssid_len; i++) {
  909. if (sta->u.ap.ssid[i] >= 32 && sta->u.ap.ssid[i] < 127)
  910. seq_putc(m, sta->u.ap.ssid[i]);
  911. else
  912. seq_printf(m, "<%02x>", sta->u.ap.ssid[i]);
  913. }
  914. seq_putc(m, '\n');
  915. }
  916. #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
  917. return 0;
  918. }
  919. static int prism2_sta_proc_open(struct inode *inode, struct file *file)
  920. {
  921. return single_open(file, prism2_sta_proc_show, PDE_DATA(inode));
  922. }
  923. static const struct file_operations prism2_sta_proc_fops = {
  924. .open = prism2_sta_proc_open,
  925. .read = seq_read,
  926. .llseek = seq_lseek,
  927. .release = single_release,
  928. };
  929. static void handle_add_proc_queue(struct work_struct *work)
  930. {
  931. struct ap_data *ap = container_of(work, struct ap_data,
  932. add_sta_proc_queue);
  933. struct sta_info *sta;
  934. char name[20];
  935. struct add_sta_proc_data *entry, *prev;
  936. entry = ap->add_sta_proc_entries;
  937. ap->add_sta_proc_entries = NULL;
  938. while (entry) {
  939. spin_lock_bh(&ap->sta_table_lock);
  940. sta = ap_get_sta(ap, entry->addr);
  941. if (sta)
  942. atomic_inc(&sta->users);
  943. spin_unlock_bh(&ap->sta_table_lock);
  944. if (sta) {
  945. sprintf(name, "%pM", sta->addr);
  946. sta->proc = proc_create_data(
  947. name, 0, ap->proc,
  948. &prism2_sta_proc_fops, sta);
  949. atomic_dec(&sta->users);
  950. }
  951. prev = entry;
  952. entry = entry->next;
  953. kfree(prev);
  954. }
  955. }
  956. static struct sta_info * ap_add_sta(struct ap_data *ap, u8 *addr)
  957. {
  958. struct sta_info *sta;
  959. sta = kzalloc(sizeof(struct sta_info), GFP_ATOMIC);
  960. if (sta == NULL) {
  961. PDEBUG(DEBUG_AP, "AP: kmalloc failed\n");
  962. return NULL;
  963. }
  964. /* initialize STA info data */
  965. sta->local = ap->local;
  966. skb_queue_head_init(&sta->tx_buf);
  967. memcpy(sta->addr, addr, ETH_ALEN);
  968. atomic_inc(&sta->users);
  969. spin_lock_bh(&ap->sta_table_lock);
  970. list_add(&sta->list, &ap->sta_list);
  971. ap->num_sta++;
  972. ap_sta_hash_add(ap, sta);
  973. spin_unlock_bh(&ap->sta_table_lock);
  974. if (ap->proc) {
  975. struct add_sta_proc_data *entry;
  976. /* schedule a non-interrupt context process to add a procfs
  977. * entry for the STA since procfs code use GFP_KERNEL */
  978. entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
  979. if (entry) {
  980. memcpy(entry->addr, sta->addr, ETH_ALEN);
  981. entry->next = ap->add_sta_proc_entries;
  982. ap->add_sta_proc_entries = entry;
  983. schedule_work(&ap->add_sta_proc_queue);
  984. } else
  985. printk(KERN_DEBUG "Failed to add STA proc data\n");
  986. }
  987. #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
  988. init_timer(&sta->timer);
  989. sta->timer.expires = jiffies + ap->max_inactivity;
  990. sta->timer.data = (unsigned long) sta;
  991. sta->timer.function = ap_handle_timer;
  992. if (!ap->local->hostapd)
  993. add_timer(&sta->timer);
  994. #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
  995. return sta;
  996. }
  997. static int ap_tx_rate_ok(int rateidx, struct sta_info *sta,
  998. local_info_t *local)
  999. {
  1000. if (rateidx > sta->tx_max_rate ||
  1001. !(sta->tx_supp_rates & (1 << rateidx)))
  1002. return 0;
  1003. if (local->tx_rate_control != 0 &&
  1004. !(local->tx_rate_control & (1 << rateidx)))
  1005. return 0;
  1006. return 1;
  1007. }
  1008. static void prism2_check_tx_rates(struct sta_info *sta)
  1009. {
  1010. int i;
  1011. sta->tx_supp_rates = 0;
  1012. for (i = 0; i < sizeof(sta->supported_rates); i++) {
  1013. if ((sta->supported_rates[i] & 0x7f) == 2)
  1014. sta->tx_supp_rates |= WLAN_RATE_1M;
  1015. if ((sta->supported_rates[i] & 0x7f) == 4)
  1016. sta->tx_supp_rates |= WLAN_RATE_2M;
  1017. if ((sta->supported_rates[i] & 0x7f) == 11)
  1018. sta->tx_supp_rates |= WLAN_RATE_5M5;
  1019. if ((sta->supported_rates[i] & 0x7f) == 22)
  1020. sta->tx_supp_rates |= WLAN_RATE_11M;
  1021. }
  1022. sta->tx_max_rate = sta->tx_rate = sta->tx_rate_idx = 0;
  1023. if (sta->tx_supp_rates & WLAN_RATE_1M) {
  1024. sta->tx_max_rate = 0;
  1025. if (ap_tx_rate_ok(0, sta, sta->local)) {
  1026. sta->tx_rate = 10;
  1027. sta->tx_rate_idx = 0;
  1028. }
  1029. }
  1030. if (sta->tx_supp_rates & WLAN_RATE_2M) {
  1031. sta->tx_max_rate = 1;
  1032. if (ap_tx_rate_ok(1, sta, sta->local)) {
  1033. sta->tx_rate = 20;
  1034. sta->tx_rate_idx = 1;
  1035. }
  1036. }
  1037. if (sta->tx_supp_rates & WLAN_RATE_5M5) {
  1038. sta->tx_max_rate = 2;
  1039. if (ap_tx_rate_ok(2, sta, sta->local)) {
  1040. sta->tx_rate = 55;
  1041. sta->tx_rate_idx = 2;
  1042. }
  1043. }
  1044. if (sta->tx_supp_rates & WLAN_RATE_11M) {
  1045. sta->tx_max_rate = 3;
  1046. if (ap_tx_rate_ok(3, sta, sta->local)) {
  1047. sta->tx_rate = 110;
  1048. sta->tx_rate_idx = 3;
  1049. }
  1050. }
  1051. }
  1052. #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
  1053. static void ap_crypt_init(struct ap_data *ap)
  1054. {
  1055. ap->crypt = lib80211_get_crypto_ops("WEP");
  1056. if (ap->crypt) {
  1057. if (ap->crypt->init) {
  1058. ap->crypt_priv = ap->crypt->init(0);
  1059. if (ap->crypt_priv == NULL)
  1060. ap->crypt = NULL;
  1061. else {
  1062. u8 key[WEP_KEY_LEN];
  1063. get_random_bytes(key, WEP_KEY_LEN);
  1064. ap->crypt->set_key(key, WEP_KEY_LEN, NULL,
  1065. ap->crypt_priv);
  1066. }
  1067. }
  1068. }
  1069. if (ap->crypt == NULL) {
  1070. printk(KERN_WARNING "AP could not initialize WEP: load module "
  1071. "lib80211_crypt_wep.ko\n");
  1072. }
  1073. }
  1074. /* Generate challenge data for shared key authentication. IEEE 802.11 specifies
  1075. * that WEP algorithm is used for generating challenge. This should be unique,
  1076. * but otherwise there is not really need for randomness etc. Initialize WEP
  1077. * with pseudo random key and then use increasing IV to get unique challenge
  1078. * streams.
  1079. *
  1080. * Called only as a scheduled task for pending AP frames.
  1081. */
  1082. static char * ap_auth_make_challenge(struct ap_data *ap)
  1083. {
  1084. char *tmpbuf;
  1085. struct sk_buff *skb;
  1086. if (ap->crypt == NULL) {
  1087. ap_crypt_init(ap);
  1088. if (ap->crypt == NULL)
  1089. return NULL;
  1090. }
  1091. tmpbuf = kmalloc(WLAN_AUTH_CHALLENGE_LEN, GFP_ATOMIC);
  1092. if (tmpbuf == NULL) {
  1093. PDEBUG(DEBUG_AP, "AP: kmalloc failed for challenge\n");
  1094. return NULL;
  1095. }
  1096. skb = dev_alloc_skb(WLAN_AUTH_CHALLENGE_LEN +
  1097. ap->crypt->extra_mpdu_prefix_len +
  1098. ap->crypt->extra_mpdu_postfix_len);
  1099. if (skb == NULL) {
  1100. kfree(tmpbuf);
  1101. return NULL;
  1102. }
  1103. skb_reserve(skb, ap->crypt->extra_mpdu_prefix_len);
  1104. memset(skb_put(skb, WLAN_AUTH_CHALLENGE_LEN), 0,
  1105. WLAN_AUTH_CHALLENGE_LEN);
  1106. if (ap->crypt->encrypt_mpdu(skb, 0, ap->crypt_priv)) {
  1107. dev_kfree_skb(skb);
  1108. kfree(tmpbuf);
  1109. return NULL;
  1110. }
  1111. skb_copy_from_linear_data_offset(skb, ap->crypt->extra_mpdu_prefix_len,
  1112. tmpbuf, WLAN_AUTH_CHALLENGE_LEN);
  1113. dev_kfree_skb(skb);
  1114. return tmpbuf;
  1115. }
  1116. /* Called only as a scheduled task for pending AP frames. */
  1117. static void handle_authen(local_info_t *local, struct sk_buff *skb,
  1118. struct hostap_80211_rx_status *rx_stats)
  1119. {
  1120. struct net_device *dev = local->dev;
  1121. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1122. size_t hdrlen;
  1123. struct ap_data *ap = local->ap;
  1124. char body[8 + WLAN_AUTH_CHALLENGE_LEN], *challenge = NULL;
  1125. int len, olen;
  1126. u16 auth_alg, auth_transaction, status_code;
  1127. __le16 *pos;
  1128. u16 resp = WLAN_STATUS_SUCCESS;
  1129. struct sta_info *sta = NULL;
  1130. struct lib80211_crypt_data *crypt;
  1131. char *txt = "";
  1132. len = skb->len - IEEE80211_MGMT_HDR_LEN;
  1133. hdrlen = hostap_80211_get_hdrlen(hdr->frame_control);
  1134. if (len < 6) {
  1135. PDEBUG(DEBUG_AP, "%s: handle_authen - too short payload "
  1136. "(len=%d) from %pM\n", dev->name, len, hdr->addr2);
  1137. return;
  1138. }
  1139. spin_lock_bh(&local->ap->sta_table_lock);
  1140. sta = ap_get_sta(local->ap, hdr->addr2);
  1141. if (sta)
  1142. atomic_inc(&sta->users);
  1143. spin_unlock_bh(&local->ap->sta_table_lock);
  1144. if (sta && sta->crypt)
  1145. crypt = sta->crypt;
  1146. else {
  1147. int idx = 0;
  1148. if (skb->len >= hdrlen + 3)
  1149. idx = skb->data[hdrlen + 3] >> 6;
  1150. crypt = local->crypt_info.crypt[idx];
  1151. }
  1152. pos = (__le16 *) (skb->data + IEEE80211_MGMT_HDR_LEN);
  1153. auth_alg = __le16_to_cpu(*pos);
  1154. pos++;
  1155. auth_transaction = __le16_to_cpu(*pos);
  1156. pos++;
  1157. status_code = __le16_to_cpu(*pos);
  1158. pos++;
  1159. if (memcmp(dev->dev_addr, hdr->addr2, ETH_ALEN) == 0 ||
  1160. ap_control_mac_deny(&ap->mac_restrictions, hdr->addr2)) {
  1161. txt = "authentication denied";
  1162. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  1163. goto fail;
  1164. }
  1165. if (((local->auth_algs & PRISM2_AUTH_OPEN) &&
  1166. auth_alg == WLAN_AUTH_OPEN) ||
  1167. ((local->auth_algs & PRISM2_AUTH_SHARED_KEY) &&
  1168. crypt && auth_alg == WLAN_AUTH_SHARED_KEY)) {
  1169. } else {
  1170. txt = "unsupported algorithm";
  1171. resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
  1172. goto fail;
  1173. }
  1174. if (len >= 8) {
  1175. u8 *u = (u8 *) pos;
  1176. if (*u == WLAN_EID_CHALLENGE) {
  1177. if (*(u + 1) != WLAN_AUTH_CHALLENGE_LEN) {
  1178. txt = "invalid challenge len";
  1179. resp = WLAN_STATUS_CHALLENGE_FAIL;
  1180. goto fail;
  1181. }
  1182. if (len - 8 < WLAN_AUTH_CHALLENGE_LEN) {
  1183. txt = "challenge underflow";
  1184. resp = WLAN_STATUS_CHALLENGE_FAIL;
  1185. goto fail;
  1186. }
  1187. challenge = (char *) (u + 2);
  1188. }
  1189. }
  1190. if (sta && sta->ap) {
  1191. if (time_after(jiffies, sta->u.ap.last_beacon +
  1192. (10 * sta->listen_interval * HZ) / 1024)) {
  1193. PDEBUG(DEBUG_AP, "%s: no beacons received for a while,"
  1194. " assuming AP %pM is now STA\n",
  1195. dev->name, sta->addr);
  1196. sta->ap = 0;
  1197. sta->flags = 0;
  1198. sta->u.sta.challenge = NULL;
  1199. } else {
  1200. txt = "AP trying to authenticate?";
  1201. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  1202. goto fail;
  1203. }
  1204. }
  1205. if ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 1) ||
  1206. (auth_alg == WLAN_AUTH_SHARED_KEY &&
  1207. (auth_transaction == 1 ||
  1208. (auth_transaction == 3 && sta != NULL &&
  1209. sta->u.sta.challenge != NULL)))) {
  1210. } else {
  1211. txt = "unknown authentication transaction number";
  1212. resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
  1213. goto fail;
  1214. }
  1215. if (sta == NULL) {
  1216. txt = "new STA";
  1217. if (local->ap->num_sta >= MAX_STA_COUNT) {
  1218. /* FIX: might try to remove some old STAs first? */
  1219. txt = "no more room for new STAs";
  1220. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  1221. goto fail;
  1222. }
  1223. sta = ap_add_sta(local->ap, hdr->addr2);
  1224. if (sta == NULL) {
  1225. txt = "ap_add_sta failed";
  1226. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  1227. goto fail;
  1228. }
  1229. }
  1230. switch (auth_alg) {
  1231. case WLAN_AUTH_OPEN:
  1232. txt = "authOK";
  1233. /* IEEE 802.11 standard is not completely clear about
  1234. * whether STA is considered authenticated after
  1235. * authentication OK frame has been send or after it
  1236. * has been ACKed. In order to reduce interoperability
  1237. * issues, mark the STA authenticated before ACK. */
  1238. sta->flags |= WLAN_STA_AUTH;
  1239. break;
  1240. case WLAN_AUTH_SHARED_KEY:
  1241. if (auth_transaction == 1) {
  1242. if (sta->u.sta.challenge == NULL) {
  1243. sta->u.sta.challenge =
  1244. ap_auth_make_challenge(local->ap);
  1245. if (sta->u.sta.challenge == NULL) {
  1246. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  1247. goto fail;
  1248. }
  1249. }
  1250. } else {
  1251. if (sta->u.sta.challenge == NULL ||
  1252. challenge == NULL ||
  1253. memcmp(sta->u.sta.challenge, challenge,
  1254. WLAN_AUTH_CHALLENGE_LEN) != 0 ||
  1255. !ieee80211_has_protected(hdr->frame_control)) {
  1256. txt = "challenge response incorrect";
  1257. resp = WLAN_STATUS_CHALLENGE_FAIL;
  1258. goto fail;
  1259. }
  1260. txt = "challenge OK - authOK";
  1261. /* IEEE 802.11 standard is not completely clear about
  1262. * whether STA is considered authenticated after
  1263. * authentication OK frame has been send or after it
  1264. * has been ACKed. In order to reduce interoperability
  1265. * issues, mark the STA authenticated before ACK. */
  1266. sta->flags |= WLAN_STA_AUTH;
  1267. kfree(sta->u.sta.challenge);
  1268. sta->u.sta.challenge = NULL;
  1269. }
  1270. break;
  1271. }
  1272. fail:
  1273. pos = (__le16 *) body;
  1274. *pos = cpu_to_le16(auth_alg);
  1275. pos++;
  1276. *pos = cpu_to_le16(auth_transaction + 1);
  1277. pos++;
  1278. *pos = cpu_to_le16(resp); /* status_code */
  1279. pos++;
  1280. olen = 6;
  1281. if (resp == WLAN_STATUS_SUCCESS && sta != NULL &&
  1282. sta->u.sta.challenge != NULL &&
  1283. auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 1) {
  1284. u8 *tmp = (u8 *) pos;
  1285. *tmp++ = WLAN_EID_CHALLENGE;
  1286. *tmp++ = WLAN_AUTH_CHALLENGE_LEN;
  1287. pos++;
  1288. memcpy(pos, sta->u.sta.challenge, WLAN_AUTH_CHALLENGE_LEN);
  1289. olen += 2 + WLAN_AUTH_CHALLENGE_LEN;
  1290. }
  1291. prism2_send_mgmt(dev, IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_AUTH,
  1292. body, olen, hdr->addr2, ap->tx_callback_auth);
  1293. if (sta) {
  1294. sta->last_rx = jiffies;
  1295. atomic_dec(&sta->users);
  1296. }
  1297. if (resp) {
  1298. PDEBUG(DEBUG_AP, "%s: %pM auth (alg=%d "
  1299. "trans#=%d stat=%d len=%d fc=%04x) ==> %d (%s)\n",
  1300. dev->name, hdr->addr2,
  1301. auth_alg, auth_transaction, status_code, len,
  1302. le16_to_cpu(hdr->frame_control), resp, txt);
  1303. }
  1304. }
  1305. /* Called only as a scheduled task for pending AP frames. */
  1306. static void handle_assoc(local_info_t *local, struct sk_buff *skb,
  1307. struct hostap_80211_rx_status *rx_stats, int reassoc)
  1308. {
  1309. struct net_device *dev = local->dev;
  1310. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1311. char body[12], *p, *lpos;
  1312. int len, left;
  1313. __le16 *pos;
  1314. u16 resp = WLAN_STATUS_SUCCESS;
  1315. struct sta_info *sta = NULL;
  1316. int send_deauth = 0;
  1317. char *txt = "";
  1318. u8 prev_ap[ETH_ALEN];
  1319. left = len = skb->len - IEEE80211_MGMT_HDR_LEN;
  1320. if (len < (reassoc ? 10 : 4)) {
  1321. PDEBUG(DEBUG_AP, "%s: handle_assoc - too short payload "
  1322. "(len=%d, reassoc=%d) from %pM\n",
  1323. dev->name, len, reassoc, hdr->addr2);
  1324. return;
  1325. }
  1326. spin_lock_bh(&local->ap->sta_table_lock);
  1327. sta = ap_get_sta(local->ap, hdr->addr2);
  1328. if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
  1329. spin_unlock_bh(&local->ap->sta_table_lock);
  1330. txt = "trying to associate before authentication";
  1331. send_deauth = 1;
  1332. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  1333. sta = NULL; /* do not decrement sta->users */
  1334. goto fail;
  1335. }
  1336. atomic_inc(&sta->users);
  1337. spin_unlock_bh(&local->ap->sta_table_lock);
  1338. pos = (__le16 *) (skb->data + IEEE80211_MGMT_HDR_LEN);
  1339. sta->capability = __le16_to_cpu(*pos);
  1340. pos++; left -= 2;
  1341. sta->listen_interval = __le16_to_cpu(*pos);
  1342. pos++; left -= 2;
  1343. if (reassoc) {
  1344. memcpy(prev_ap, pos, ETH_ALEN);
  1345. pos++; pos++; pos++; left -= 6;
  1346. } else
  1347. memset(prev_ap, 0, ETH_ALEN);
  1348. if (left >= 2) {
  1349. unsigned int ileft;
  1350. unsigned char *u = (unsigned char *) pos;
  1351. if (*u == WLAN_EID_SSID) {
  1352. u++; left--;
  1353. ileft = *u;
  1354. u++; left--;
  1355. if (ileft > left || ileft > MAX_SSID_LEN) {
  1356. txt = "SSID overflow";
  1357. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  1358. goto fail;
  1359. }
  1360. if (ileft != strlen(local->essid) ||
  1361. memcmp(local->essid, u, ileft) != 0) {
  1362. txt = "not our SSID";
  1363. resp = WLAN_STATUS_ASSOC_DENIED_UNSPEC;
  1364. goto fail;
  1365. }
  1366. u += ileft;
  1367. left -= ileft;
  1368. }
  1369. if (left >= 2 && *u == WLAN_EID_SUPP_RATES) {
  1370. u++; left--;
  1371. ileft = *u;
  1372. u++; left--;
  1373. if (ileft > left || ileft == 0 ||
  1374. ileft > WLAN_SUPP_RATES_MAX) {
  1375. txt = "SUPP_RATES len error";
  1376. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  1377. goto fail;
  1378. }
  1379. memset(sta->supported_rates, 0,
  1380. sizeof(sta->supported_rates));
  1381. memcpy(sta->supported_rates, u, ileft);
  1382. prism2_check_tx_rates(sta);
  1383. u += ileft;
  1384. left -= ileft;
  1385. }
  1386. if (left > 0) {
  1387. PDEBUG(DEBUG_AP, "%s: assoc from %pM"
  1388. " with extra data (%d bytes) [",
  1389. dev->name, hdr->addr2, left);
  1390. while (left > 0) {
  1391. PDEBUG2(DEBUG_AP, "<%02x>", *u);
  1392. u++; left--;
  1393. }
  1394. PDEBUG2(DEBUG_AP, "]\n");
  1395. }
  1396. } else {
  1397. txt = "frame underflow";
  1398. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  1399. goto fail;
  1400. }
  1401. /* get a unique AID */
  1402. if (sta->aid > 0)
  1403. txt = "OK, old AID";
  1404. else {
  1405. spin_lock_bh(&local->ap->sta_table_lock);
  1406. for (sta->aid = 1; sta->aid <= MAX_AID_TABLE_SIZE; sta->aid++)
  1407. if (local->ap->sta_aid[sta->aid - 1] == NULL)
  1408. break;
  1409. if (sta->aid > MAX_AID_TABLE_SIZE) {
  1410. sta->aid = 0;
  1411. spin_unlock_bh(&local->ap->sta_table_lock);
  1412. resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
  1413. txt = "no room for more AIDs";
  1414. } else {
  1415. local->ap->sta_aid[sta->aid - 1] = sta;
  1416. spin_unlock_bh(&local->ap->sta_table_lock);
  1417. txt = "OK, new AID";
  1418. }
  1419. }
  1420. fail:
  1421. pos = (__le16 *) body;
  1422. if (send_deauth) {
  1423. *pos = cpu_to_le16(WLAN_REASON_STA_REQ_ASSOC_WITHOUT_AUTH);
  1424. pos++;
  1425. } else {
  1426. /* FIX: CF-Pollable and CF-PollReq should be set to match the
  1427. * values in beacons/probe responses */
  1428. /* FIX: how about privacy and WEP? */
  1429. /* capability */
  1430. *pos = cpu_to_le16(WLAN_CAPABILITY_ESS);
  1431. pos++;
  1432. /* status_code */
  1433. *pos = cpu_to_le16(resp);
  1434. pos++;
  1435. *pos = cpu_to_le16((sta && sta->aid > 0 ? sta->aid : 0) |
  1436. BIT(14) | BIT(15)); /* AID */
  1437. pos++;
  1438. /* Supported rates (Information element) */
  1439. p = (char *) pos;
  1440. *p++ = WLAN_EID_SUPP_RATES;
  1441. lpos = p;
  1442. *p++ = 0; /* len */
  1443. if (local->tx_rate_control & WLAN_RATE_1M) {
  1444. *p++ = local->basic_rates & WLAN_RATE_1M ? 0x82 : 0x02;
  1445. (*lpos)++;
  1446. }
  1447. if (local->tx_rate_control & WLAN_RATE_2M) {
  1448. *p++ = local->basic_rates & WLAN_RATE_2M ? 0x84 : 0x04;
  1449. (*lpos)++;
  1450. }
  1451. if (local->tx_rate_control & WLAN_RATE_5M5) {
  1452. *p++ = local->basic_rates & WLAN_RATE_5M5 ?
  1453. 0x8b : 0x0b;
  1454. (*lpos)++;
  1455. }
  1456. if (local->tx_rate_control & WLAN_RATE_11M) {
  1457. *p++ = local->basic_rates & WLAN_RATE_11M ?
  1458. 0x96 : 0x16;
  1459. (*lpos)++;
  1460. }
  1461. pos = (__le16 *) p;
  1462. }
  1463. prism2_send_mgmt(dev, IEEE80211_FTYPE_MGMT |
  1464. (send_deauth ? IEEE80211_STYPE_DEAUTH :
  1465. (reassoc ? IEEE80211_STYPE_REASSOC_RESP :
  1466. IEEE80211_STYPE_ASSOC_RESP)),
  1467. body, (u8 *) pos - (u8 *) body,
  1468. hdr->addr2,
  1469. send_deauth ? 0 : local->ap->tx_callback_assoc);
  1470. if (sta) {
  1471. if (resp == WLAN_STATUS_SUCCESS) {
  1472. sta->last_rx = jiffies;
  1473. /* STA will be marked associated from TX callback, if
  1474. * AssocResp is ACKed */
  1475. }
  1476. atomic_dec(&sta->users);
  1477. }
  1478. #if 0
  1479. PDEBUG(DEBUG_AP, "%s: %pM %sassoc (len=%d "
  1480. "prev_ap=%pM) => %d(%d) (%s)\n",
  1481. dev->name,
  1482. hdr->addr2,
  1483. reassoc ? "re" : "", len,
  1484. prev_ap,
  1485. resp, send_deauth, txt);
  1486. #endif
  1487. }
  1488. /* Called only as a scheduled task for pending AP frames. */
  1489. static void handle_deauth(local_info_t *local, struct sk_buff *skb,
  1490. struct hostap_80211_rx_status *rx_stats)
  1491. {
  1492. struct net_device *dev = local->dev;
  1493. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1494. char *body = (char *) (skb->data + IEEE80211_MGMT_HDR_LEN);
  1495. int len;
  1496. u16 reason_code;
  1497. __le16 *pos;
  1498. struct sta_info *sta = NULL;
  1499. len = skb->len - IEEE80211_MGMT_HDR_LEN;
  1500. if (len < 2) {
  1501. printk("handle_deauth - too short payload (len=%d)\n", len);
  1502. return;
  1503. }
  1504. pos = (__le16 *) body;
  1505. reason_code = le16_to_cpu(*pos);
  1506. PDEBUG(DEBUG_AP, "%s: deauthentication: %pM len=%d, "
  1507. "reason_code=%d\n", dev->name, hdr->addr2,
  1508. len, reason_code);
  1509. spin_lock_bh(&local->ap->sta_table_lock);
  1510. sta = ap_get_sta(local->ap, hdr->addr2);
  1511. if (sta != NULL) {
  1512. if ((sta->flags & WLAN_STA_ASSOC) && !sta->ap)
  1513. hostap_event_expired_sta(local->dev, sta);
  1514. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
  1515. }
  1516. spin_unlock_bh(&local->ap->sta_table_lock);
  1517. if (sta == NULL) {
  1518. printk("%s: deauthentication from %pM, "
  1519. "reason_code=%d, but STA not authenticated\n", dev->name,
  1520. hdr->addr2, reason_code);
  1521. }
  1522. }
  1523. /* Called only as a scheduled task for pending AP frames. */
  1524. static void handle_disassoc(local_info_t *local, struct sk_buff *skb,
  1525. struct hostap_80211_rx_status *rx_stats)
  1526. {
  1527. struct net_device *dev = local->dev;
  1528. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1529. char *body = skb->data + IEEE80211_MGMT_HDR_LEN;
  1530. int len;
  1531. u16 reason_code;
  1532. __le16 *pos;
  1533. struct sta_info *sta = NULL;
  1534. len = skb->len - IEEE80211_MGMT_HDR_LEN;
  1535. if (len < 2) {
  1536. printk("handle_disassoc - too short payload (len=%d)\n", len);
  1537. return;
  1538. }
  1539. pos = (__le16 *) body;
  1540. reason_code = le16_to_cpu(*pos);
  1541. PDEBUG(DEBUG_AP, "%s: disassociation: %pM len=%d, "
  1542. "reason_code=%d\n", dev->name, hdr->addr2,
  1543. len, reason_code);
  1544. spin_lock_bh(&local->ap->sta_table_lock);
  1545. sta = ap_get_sta(local->ap, hdr->addr2);
  1546. if (sta != NULL) {
  1547. if ((sta->flags & WLAN_STA_ASSOC) && !sta->ap)
  1548. hostap_event_expired_sta(local->dev, sta);
  1549. sta->flags &= ~WLAN_STA_ASSOC;
  1550. }
  1551. spin_unlock_bh(&local->ap->sta_table_lock);
  1552. if (sta == NULL) {
  1553. printk("%s: disassociation from %pM, "
  1554. "reason_code=%d, but STA not authenticated\n",
  1555. dev->name, hdr->addr2, reason_code);
  1556. }
  1557. }
  1558. /* Called only as a scheduled task for pending AP frames. */
  1559. static void ap_handle_data_nullfunc(local_info_t *local,
  1560. struct ieee80211_hdr *hdr)
  1561. {
  1562. struct net_device *dev = local->dev;
  1563. /* some STA f/w's seem to require control::ACK frame for
  1564. * data::nullfunc, but at least Prism2 station f/w version 0.8.0 does
  1565. * not send this..
  1566. * send control::ACK for the data::nullfunc */
  1567. printk(KERN_DEBUG "Sending control::ACK for data::nullfunc\n");
  1568. prism2_send_mgmt(dev, IEEE80211_FTYPE_CTL | IEEE80211_STYPE_ACK,
  1569. NULL, 0, hdr->addr2, 0);
  1570. }
  1571. /* Called only as a scheduled task for pending AP frames. */
  1572. static void ap_handle_dropped_data(local_info_t *local,
  1573. struct ieee80211_hdr *hdr)
  1574. {
  1575. struct net_device *dev = local->dev;
  1576. struct sta_info *sta;
  1577. __le16 reason;
  1578. spin_lock_bh(&local->ap->sta_table_lock);
  1579. sta = ap_get_sta(local->ap, hdr->addr2);
  1580. if (sta)
  1581. atomic_inc(&sta->users);
  1582. spin_unlock_bh(&local->ap->sta_table_lock);
  1583. if (sta != NULL && (sta->flags & WLAN_STA_ASSOC)) {
  1584. PDEBUG(DEBUG_AP, "ap_handle_dropped_data: STA is now okay?\n");
  1585. atomic_dec(&sta->users);
  1586. return;
  1587. }
  1588. reason = cpu_to_le16(WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
  1589. prism2_send_mgmt(dev, IEEE80211_FTYPE_MGMT |
  1590. ((sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) ?
  1591. IEEE80211_STYPE_DEAUTH : IEEE80211_STYPE_DISASSOC),
  1592. (char *) &reason, sizeof(reason), hdr->addr2, 0);
  1593. if (sta)
  1594. atomic_dec(&sta->users);
  1595. }
  1596. #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
  1597. /* Called only as a scheduled task for pending AP frames. */
  1598. static void pspoll_send_buffered(local_info_t *local, struct sta_info *sta,
  1599. struct sk_buff *skb)
  1600. {
  1601. struct hostap_skb_tx_data *meta;
  1602. if (!(sta->flags & WLAN_STA_PS)) {
  1603. /* Station has moved to non-PS mode, so send all buffered
  1604. * frames using normal device queue. */
  1605. dev_queue_xmit(skb);
  1606. return;
  1607. }
  1608. /* add a flag for hostap_handle_sta_tx() to know that this skb should
  1609. * be passed through even though STA is using PS */
  1610. meta = (struct hostap_skb_tx_data *) skb->cb;
  1611. meta->flags |= HOSTAP_TX_FLAGS_BUFFERED_FRAME;
  1612. if (!skb_queue_empty(&sta->tx_buf)) {
  1613. /* indicate to STA that more frames follow */
  1614. meta->flags |= HOSTAP_TX_FLAGS_ADD_MOREDATA;
  1615. }
  1616. dev_queue_xmit(skb);
  1617. }
  1618. /* Called only as a scheduled task for pending AP frames. */
  1619. static void handle_pspoll(local_info_t *local,
  1620. struct ieee80211_hdr *hdr,
  1621. struct hostap_80211_rx_status *rx_stats)
  1622. {
  1623. struct net_device *dev = local->dev;
  1624. struct sta_info *sta;
  1625. u16 aid;
  1626. struct sk_buff *skb;
  1627. PDEBUG(DEBUG_PS2, "handle_pspoll: BSSID=%pM, TA=%pM PWRMGT=%d\n",
  1628. hdr->addr1, hdr->addr2, !!ieee80211_has_pm(hdr->frame_control));
  1629. if (memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN)) {
  1630. PDEBUG(DEBUG_AP,
  1631. "handle_pspoll - addr1(BSSID)=%pM not own MAC\n",
  1632. hdr->addr1);
  1633. return;
  1634. }
  1635. aid = le16_to_cpu(hdr->duration_id);
  1636. if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14))) {
  1637. PDEBUG(DEBUG_PS, " PSPOLL and AID[15:14] not set\n");
  1638. return;
  1639. }
  1640. aid &= ~(BIT(15) | BIT(14));
  1641. if (aid == 0 || aid > MAX_AID_TABLE_SIZE) {
  1642. PDEBUG(DEBUG_PS, " invalid aid=%d\n", aid);
  1643. return;
  1644. }
  1645. PDEBUG(DEBUG_PS2, " aid=%d\n", aid);
  1646. spin_lock_bh(&local->ap->sta_table_lock);
  1647. sta = ap_get_sta(local->ap, hdr->addr2);
  1648. if (sta)
  1649. atomic_inc(&sta->users);
  1650. spin_unlock_bh(&local->ap->sta_table_lock);
  1651. if (sta == NULL) {
  1652. PDEBUG(DEBUG_PS, " STA not found\n");
  1653. return;
  1654. }
  1655. if (sta->aid != aid) {
  1656. PDEBUG(DEBUG_PS, " received aid=%i does not match with "
  1657. "assoc.aid=%d\n", aid, sta->aid);
  1658. return;
  1659. }
  1660. /* FIX: todo:
  1661. * - add timeout for buffering (clear aid in TIM vector if buffer timed
  1662. * out (expiry time must be longer than ListenInterval for
  1663. * the corresponding STA; "8802-11: 11.2.1.9 AP aging function"
  1664. * - what to do, if buffered, pspolled, and sent frame is not ACKed by
  1665. * sta; store buffer for later use and leave TIM aid bit set? use
  1666. * TX event to check whether frame was ACKed?
  1667. */
  1668. while ((skb = skb_dequeue(&sta->tx_buf)) != NULL) {
  1669. /* send buffered frame .. */
  1670. PDEBUG(DEBUG_PS2, "Sending buffered frame to STA after PS POLL"
  1671. " (buffer_count=%d)\n", skb_queue_len(&sta->tx_buf));
  1672. pspoll_send_buffered(local, sta, skb);
  1673. if (sta->flags & WLAN_STA_PS) {
  1674. /* send only one buffered packet per PS Poll */
  1675. /* FIX: should ignore further PS Polls until the
  1676. * buffered packet that was just sent is acknowledged
  1677. * (Tx or TxExc event) */
  1678. break;
  1679. }
  1680. }
  1681. if (skb_queue_empty(&sta->tx_buf)) {
  1682. /* try to clear aid from TIM */
  1683. if (!(sta->flags & WLAN_STA_TIM))
  1684. PDEBUG(DEBUG_PS2, "Re-unsetting TIM for aid %d\n",
  1685. aid);
  1686. hostap_set_tim(local, aid, 0);
  1687. sta->flags &= ~WLAN_STA_TIM;
  1688. }
  1689. atomic_dec(&sta->users);
  1690. }
  1691. #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
  1692. static void handle_wds_oper_queue(struct work_struct *work)
  1693. {
  1694. struct ap_data *ap = container_of(work, struct ap_data,
  1695. wds_oper_queue);
  1696. local_info_t *local = ap->local;
  1697. struct wds_oper_data *entry, *prev;
  1698. spin_lock_bh(&local->lock);
  1699. entry = local->ap->wds_oper_entries;
  1700. local->ap->wds_oper_entries = NULL;
  1701. spin_unlock_bh(&local->lock);
  1702. while (entry) {
  1703. PDEBUG(DEBUG_AP, "%s: %s automatic WDS connection "
  1704. "to AP %pM\n",
  1705. local->dev->name,
  1706. entry->type == WDS_ADD ? "adding" : "removing",
  1707. entry->addr);
  1708. if (entry->type == WDS_ADD)
  1709. prism2_wds_add(local, entry->addr, 0);
  1710. else if (entry->type == WDS_DEL)
  1711. prism2_wds_del(local, entry->addr, 0, 1);
  1712. prev = entry;
  1713. entry = entry->next;
  1714. kfree(prev);
  1715. }
  1716. }
  1717. /* Called only as a scheduled task for pending AP frames. */
  1718. static void handle_beacon(local_info_t *local, struct sk_buff *skb,
  1719. struct hostap_80211_rx_status *rx_stats)
  1720. {
  1721. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1722. char *body = skb->data + IEEE80211_MGMT_HDR_LEN;
  1723. int len, left;
  1724. u16 beacon_int, capability;
  1725. __le16 *pos;
  1726. char *ssid = NULL;
  1727. unsigned char *supp_rates = NULL;
  1728. int ssid_len = 0, supp_rates_len = 0;
  1729. struct sta_info *sta = NULL;
  1730. int new_sta = 0, channel = -1;
  1731. len = skb->len - IEEE80211_MGMT_HDR_LEN;
  1732. if (len < 8 + 2 + 2) {
  1733. printk(KERN_DEBUG "handle_beacon - too short payload "
  1734. "(len=%d)\n", len);
  1735. return;
  1736. }
  1737. pos = (__le16 *) body;
  1738. left = len;
  1739. /* Timestamp (8 octets) */
  1740. pos += 4; left -= 8;
  1741. /* Beacon interval (2 octets) */
  1742. beacon_int = le16_to_cpu(*pos);
  1743. pos++; left -= 2;
  1744. /* Capability information (2 octets) */
  1745. capability = le16_to_cpu(*pos);
  1746. pos++; left -= 2;
  1747. if (local->ap->ap_policy != AP_OTHER_AP_EVEN_IBSS &&
  1748. capability & WLAN_CAPABILITY_IBSS)
  1749. return;
  1750. if (left >= 2) {
  1751. unsigned int ileft;
  1752. unsigned char *u = (unsigned char *) pos;
  1753. if (*u == WLAN_EID_SSID) {
  1754. u++; left--;
  1755. ileft = *u;
  1756. u++; left--;
  1757. if (ileft > left || ileft > MAX_SSID_LEN) {
  1758. PDEBUG(DEBUG_AP, "SSID: overflow\n");
  1759. return;
  1760. }
  1761. if (local->ap->ap_policy == AP_OTHER_AP_SAME_SSID &&
  1762. (ileft != strlen(local->essid) ||
  1763. memcmp(local->essid, u, ileft) != 0)) {
  1764. /* not our SSID */
  1765. return;
  1766. }
  1767. ssid = u;
  1768. ssid_len = ileft;
  1769. u += ileft;
  1770. left -= ileft;
  1771. }
  1772. if (*u == WLAN_EID_SUPP_RATES) {
  1773. u++; left--;
  1774. ileft = *u;
  1775. u++; left--;
  1776. if (ileft > left || ileft == 0 || ileft > 8) {
  1777. PDEBUG(DEBUG_AP, " - SUPP_RATES len error\n");
  1778. return;
  1779. }
  1780. supp_rates = u;
  1781. supp_rates_len = ileft;
  1782. u += ileft;
  1783. left -= ileft;
  1784. }
  1785. if (*u == WLAN_EID_DS_PARAMS) {
  1786. u++; left--;
  1787. ileft = *u;
  1788. u++; left--;
  1789. if (ileft > left || ileft != 1) {
  1790. PDEBUG(DEBUG_AP, " - DS_PARAMS len error\n");
  1791. return;
  1792. }
  1793. channel = *u;
  1794. u += ileft;
  1795. left -= ileft;
  1796. }
  1797. }
  1798. spin_lock_bh(&local->ap->sta_table_lock);
  1799. sta = ap_get_sta(local->ap, hdr->addr2);
  1800. if (sta != NULL)
  1801. atomic_inc(&sta->users);
  1802. spin_unlock_bh(&local->ap->sta_table_lock);
  1803. if (sta == NULL) {
  1804. /* add new AP */
  1805. new_sta = 1;
  1806. sta = ap_add_sta(local->ap, hdr->addr2);
  1807. if (sta == NULL) {
  1808. printk(KERN_INFO "prism2: kmalloc failed for AP "
  1809. "data structure\n");
  1810. return;
  1811. }
  1812. hostap_event_new_sta(local->dev, sta);
  1813. /* mark APs authentication and associated for pseudo ad-hoc
  1814. * style communication */
  1815. sta->flags = WLAN_STA_AUTH | WLAN_STA_ASSOC;
  1816. if (local->ap->autom_ap_wds) {
  1817. hostap_wds_link_oper(local, sta->addr, WDS_ADD);
  1818. }
  1819. }
  1820. sta->ap = 1;
  1821. if (ssid) {
  1822. sta->u.ap.ssid_len = ssid_len;
  1823. memcpy(sta->u.ap.ssid, ssid, ssid_len);
  1824. sta->u.ap.ssid[ssid_len] = '\0';
  1825. } else {
  1826. sta->u.ap.ssid_len = 0;
  1827. sta->u.ap.ssid[0] = '\0';
  1828. }
  1829. sta->u.ap.channel = channel;
  1830. sta->rx_packets++;
  1831. sta->rx_bytes += len;
  1832. sta->u.ap.last_beacon = sta->last_rx = jiffies;
  1833. sta->capability = capability;
  1834. sta->listen_interval = beacon_int;
  1835. atomic_dec(&sta->users);
  1836. if (new_sta) {
  1837. memset(sta->supported_rates, 0, sizeof(sta->supported_rates));
  1838. memcpy(sta->supported_rates, supp_rates, supp_rates_len);
  1839. prism2_check_tx_rates(sta);
  1840. }
  1841. }
  1842. #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
  1843. /* Called only as a tasklet. */
  1844. static void handle_ap_item(local_info_t *local, struct sk_buff *skb,
  1845. struct hostap_80211_rx_status *rx_stats)
  1846. {
  1847. #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
  1848. struct net_device *dev = local->dev;
  1849. #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
  1850. u16 fc, type, stype;
  1851. struct ieee80211_hdr *hdr;
  1852. /* FIX: should give skb->len to handler functions and check that the
  1853. * buffer is long enough */
  1854. hdr = (struct ieee80211_hdr *) skb->data;
  1855. fc = le16_to_cpu(hdr->frame_control);
  1856. type = fc & IEEE80211_FCTL_FTYPE;
  1857. stype = fc & IEEE80211_FCTL_STYPE;
  1858. #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
  1859. if (!local->hostapd && type == IEEE80211_FTYPE_DATA) {
  1860. PDEBUG(DEBUG_AP, "handle_ap_item - data frame\n");
  1861. if (!(fc & IEEE80211_FCTL_TODS) ||
  1862. (fc & IEEE80211_FCTL_FROMDS)) {
  1863. if (stype == IEEE80211_STYPE_NULLFUNC) {
  1864. /* no ToDS nullfunc seems to be used to check
  1865. * AP association; so send reject message to
  1866. * speed up re-association */
  1867. ap_handle_dropped_data(local, hdr);
  1868. goto done;
  1869. }
  1870. PDEBUG(DEBUG_AP, " not ToDS frame (fc=0x%04x)\n",
  1871. fc);
  1872. goto done;
  1873. }
  1874. if (memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN)) {
  1875. PDEBUG(DEBUG_AP, "handle_ap_item - addr1(BSSID)=%pM"
  1876. " not own MAC\n", hdr->addr1);
  1877. goto done;
  1878. }
  1879. if (local->ap->nullfunc_ack &&
  1880. stype == IEEE80211_STYPE_NULLFUNC)
  1881. ap_handle_data_nullfunc(local, hdr);
  1882. else
  1883. ap_handle_dropped_data(local, hdr);
  1884. goto done;
  1885. }
  1886. if (type == IEEE80211_FTYPE_MGMT && stype == IEEE80211_STYPE_BEACON) {
  1887. handle_beacon(local, skb, rx_stats);
  1888. goto done;
  1889. }
  1890. #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
  1891. if (type == IEEE80211_FTYPE_CTL && stype == IEEE80211_STYPE_PSPOLL) {
  1892. handle_pspoll(local, hdr, rx_stats);
  1893. goto done;
  1894. }
  1895. if (local->hostapd) {
  1896. PDEBUG(DEBUG_AP, "Unknown frame in AP queue: type=0x%02x "
  1897. "subtype=0x%02x\n", type, stype);
  1898. goto done;
  1899. }
  1900. #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
  1901. if (type != IEEE80211_FTYPE_MGMT) {
  1902. PDEBUG(DEBUG_AP, "handle_ap_item - not a management frame?\n");
  1903. goto done;
  1904. }
  1905. if (memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN)) {
  1906. PDEBUG(DEBUG_AP, "handle_ap_item - addr1(DA)=%pM"
  1907. " not own MAC\n", hdr->addr1);
  1908. goto done;
  1909. }
  1910. if (memcmp(hdr->addr3, dev->dev_addr, ETH_ALEN)) {
  1911. PDEBUG(DEBUG_AP, "handle_ap_item - addr3(BSSID)=%pM"
  1912. " not own MAC\n", hdr->addr3);
  1913. goto done;
  1914. }
  1915. switch (stype) {
  1916. case IEEE80211_STYPE_ASSOC_REQ:
  1917. handle_assoc(local, skb, rx_stats, 0);
  1918. break;
  1919. case IEEE80211_STYPE_ASSOC_RESP:
  1920. PDEBUG(DEBUG_AP, "==> ASSOC RESP (ignored)\n");
  1921. break;
  1922. case IEEE80211_STYPE_REASSOC_REQ:
  1923. handle_assoc(local, skb, rx_stats, 1);
  1924. break;
  1925. case IEEE80211_STYPE_REASSOC_RESP:
  1926. PDEBUG(DEBUG_AP, "==> REASSOC RESP (ignored)\n");
  1927. break;
  1928. case IEEE80211_STYPE_ATIM:
  1929. PDEBUG(DEBUG_AP, "==> ATIM (ignored)\n");
  1930. break;
  1931. case IEEE80211_STYPE_DISASSOC:
  1932. handle_disassoc(local, skb, rx_stats);
  1933. break;
  1934. case IEEE80211_STYPE_AUTH:
  1935. handle_authen(local, skb, rx_stats);
  1936. break;
  1937. case IEEE80211_STYPE_DEAUTH:
  1938. handle_deauth(local, skb, rx_stats);
  1939. break;
  1940. default:
  1941. PDEBUG(DEBUG_AP, "Unknown mgmt frame subtype 0x%02x\n",
  1942. stype >> 4);
  1943. break;
  1944. }
  1945. #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
  1946. done:
  1947. dev_kfree_skb(skb);
  1948. }
  1949. /* Called only as a tasklet (software IRQ) */
  1950. void hostap_rx(struct net_device *dev, struct sk_buff *skb,
  1951. struct hostap_80211_rx_status *rx_stats)
  1952. {
  1953. struct hostap_interface *iface;
  1954. local_info_t *local;
  1955. struct ieee80211_hdr *hdr;
  1956. iface = netdev_priv(dev);
  1957. local = iface->local;
  1958. if (skb->len < 16)
  1959. goto drop;
  1960. dev->stats.rx_packets++;
  1961. hdr = (struct ieee80211_hdr *) skb->data;
  1962. if (local->ap->ap_policy == AP_OTHER_AP_SKIP_ALL &&
  1963. ieee80211_is_beacon(hdr->frame_control))
  1964. goto drop;
  1965. skb->protocol = cpu_to_be16(ETH_P_HOSTAP);
  1966. handle_ap_item(local, skb, rx_stats);
  1967. return;
  1968. drop:
  1969. dev_kfree_skb(skb);
  1970. }
  1971. /* Called only as a tasklet (software IRQ) */
  1972. static void schedule_packet_send(local_info_t *local, struct sta_info *sta)
  1973. {
  1974. struct sk_buff *skb;
  1975. struct ieee80211_hdr *hdr;
  1976. struct hostap_80211_rx_status rx_stats;
  1977. if (skb_queue_empty(&sta->tx_buf))
  1978. return;
  1979. skb = dev_alloc_skb(16);
  1980. if (skb == NULL) {
  1981. printk(KERN_DEBUG "%s: schedule_packet_send: skb alloc "
  1982. "failed\n", local->dev->name);
  1983. return;
  1984. }
  1985. hdr = (struct ieee80211_hdr *) skb_put(skb, 16);
  1986. /* Generate a fake pspoll frame to start packet delivery */
  1987. hdr->frame_control = cpu_to_le16(
  1988. IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL);
  1989. memcpy(hdr->addr1, local->dev->dev_addr, ETH_ALEN);
  1990. memcpy(hdr->addr2, sta->addr, ETH_ALEN);
  1991. hdr->duration_id = cpu_to_le16(sta->aid | BIT(15) | BIT(14));
  1992. PDEBUG(DEBUG_PS2,
  1993. "%s: Scheduling buffered packet delivery for STA %pM\n",
  1994. local->dev->name, sta->addr);
  1995. skb->dev = local->dev;
  1996. memset(&rx_stats, 0, sizeof(rx_stats));
  1997. hostap_rx(local->dev, skb, &rx_stats);
  1998. }
  1999. int prism2_ap_get_sta_qual(local_info_t *local, struct sockaddr addr[],
  2000. struct iw_quality qual[], int buf_size,
  2001. int aplist)
  2002. {
  2003. struct ap_data *ap = local->ap;
  2004. struct list_head *ptr;
  2005. int count = 0;
  2006. spin_lock_bh(&ap->sta_table_lock);
  2007. for (ptr = ap->sta_list.next; ptr != NULL && ptr != &ap->sta_list;
  2008. ptr = ptr->next) {
  2009. struct sta_info *sta = (struct sta_info *) ptr;
  2010. if (aplist && !sta->ap)
  2011. continue;
  2012. addr[count].sa_family = ARPHRD_ETHER;
  2013. memcpy(addr[count].sa_data, sta->addr, ETH_ALEN);
  2014. if (sta->last_rx_silence == 0)
  2015. qual[count].qual = sta->last_rx_signal < 27 ?
  2016. 0 : (sta->last_rx_signal - 27) * 92 / 127;
  2017. else
  2018. qual[count].qual = sta->last_rx_signal -
  2019. sta->last_rx_silence - 35;
  2020. qual[count].level = HFA384X_LEVEL_TO_dBm(sta->last_rx_signal);
  2021. qual[count].noise = HFA384X_LEVEL_TO_dBm(sta->last_rx_silence);
  2022. qual[count].updated = sta->last_rx_updated;
  2023. sta->last_rx_updated = IW_QUAL_DBM;
  2024. count++;
  2025. if (count >= buf_size)
  2026. break;
  2027. }
  2028. spin_unlock_bh(&ap->sta_table_lock);
  2029. return count;
  2030. }
  2031. /* Translate our list of Access Points & Stations to a card independent
  2032. * format that the Wireless Tools will understand - Jean II */
  2033. int prism2_ap_translate_scan(struct net_device *dev,
  2034. struct iw_request_info *info, char *buffer)
  2035. {
  2036. struct hostap_interface *iface;
  2037. local_info_t *local;
  2038. struct ap_data *ap;
  2039. struct list_head *ptr;
  2040. struct iw_event iwe;
  2041. char *current_ev = buffer;
  2042. char *end_buf = buffer + IW_SCAN_MAX_DATA;
  2043. #if !defined(PRISM2_NO_KERNEL_IEEE80211_MGMT)
  2044. char buf[64];
  2045. #endif
  2046. iface = netdev_priv(dev);
  2047. local = iface->local;
  2048. ap = local->ap;
  2049. spin_lock_bh(&ap->sta_table_lock);
  2050. for (ptr = ap->sta_list.next; ptr != NULL && ptr != &ap->sta_list;
  2051. ptr = ptr->next) {
  2052. struct sta_info *sta = (struct sta_info *) ptr;
  2053. /* First entry *MUST* be the AP MAC address */
  2054. memset(&iwe, 0, sizeof(iwe));
  2055. iwe.cmd = SIOCGIWAP;
  2056. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  2057. memcpy(iwe.u.ap_addr.sa_data, sta->addr, ETH_ALEN);
  2058. iwe.len = IW_EV_ADDR_LEN;
  2059. current_ev = iwe_stream_add_event(info, current_ev, end_buf,
  2060. &iwe, IW_EV_ADDR_LEN);
  2061. /* Use the mode to indicate if it's a station or
  2062. * an Access Point */
  2063. memset(&iwe, 0, sizeof(iwe));
  2064. iwe.cmd = SIOCGIWMODE;
  2065. if (sta->ap)
  2066. iwe.u.mode = IW_MODE_MASTER;
  2067. else
  2068. iwe.u.mode = IW_MODE_INFRA;
  2069. iwe.len = IW_EV_UINT_LEN;
  2070. current_ev = iwe_stream_add_event(info, current_ev, end_buf,
  2071. &iwe, IW_EV_UINT_LEN);
  2072. /* Some quality */
  2073. memset(&iwe, 0, sizeof(iwe));
  2074. iwe.cmd = IWEVQUAL;
  2075. if (sta->last_rx_silence == 0)
  2076. iwe.u.qual.qual = sta->last_rx_signal < 27 ?
  2077. 0 : (sta->last_rx_signal - 27) * 92 / 127;
  2078. else
  2079. iwe.u.qual.qual = sta->last_rx_signal -
  2080. sta->last_rx_silence - 35;
  2081. iwe.u.qual.level = HFA384X_LEVEL_TO_dBm(sta->last_rx_signal);
  2082. iwe.u.qual.noise = HFA384X_LEVEL_TO_dBm(sta->last_rx_silence);
  2083. iwe.u.qual.updated = sta->last_rx_updated;
  2084. iwe.len = IW_EV_QUAL_LEN;
  2085. current_ev = iwe_stream_add_event(info, current_ev, end_buf,
  2086. &iwe, IW_EV_QUAL_LEN);
  2087. #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
  2088. if (sta->ap) {
  2089. memset(&iwe, 0, sizeof(iwe));
  2090. iwe.cmd = SIOCGIWESSID;
  2091. iwe.u.data.length = sta->u.ap.ssid_len;
  2092. iwe.u.data.flags = 1;
  2093. current_ev = iwe_stream_add_point(info, current_ev,
  2094. end_buf, &iwe,
  2095. sta->u.ap.ssid);
  2096. memset(&iwe, 0, sizeof(iwe));
  2097. iwe.cmd = SIOCGIWENCODE;
  2098. if (sta->capability & WLAN_CAPABILITY_PRIVACY)
  2099. iwe.u.data.flags =
  2100. IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
  2101. else
  2102. iwe.u.data.flags = IW_ENCODE_DISABLED;
  2103. current_ev = iwe_stream_add_point(info, current_ev,
  2104. end_buf, &iwe,
  2105. sta->u.ap.ssid);
  2106. if (sta->u.ap.channel > 0 &&
  2107. sta->u.ap.channel <= FREQ_COUNT) {
  2108. memset(&iwe, 0, sizeof(iwe));
  2109. iwe.cmd = SIOCGIWFREQ;
  2110. iwe.u.freq.m = freq_list[sta->u.ap.channel - 1]
  2111. * 100000;
  2112. iwe.u.freq.e = 1;
  2113. current_ev = iwe_stream_add_event(
  2114. info, current_ev, end_buf, &iwe,
  2115. IW_EV_FREQ_LEN);
  2116. }
  2117. memset(&iwe, 0, sizeof(iwe));
  2118. iwe.cmd = IWEVCUSTOM;
  2119. sprintf(buf, "beacon_interval=%d",
  2120. sta->listen_interval);
  2121. iwe.u.data.length = strlen(buf);
  2122. current_ev = iwe_stream_add_point(info, current_ev,
  2123. end_buf, &iwe, buf);
  2124. }
  2125. #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
  2126. sta->last_rx_updated = IW_QUAL_DBM;
  2127. /* To be continued, we should make good use of IWEVCUSTOM */
  2128. }
  2129. spin_unlock_bh(&ap->sta_table_lock);
  2130. return current_ev - buffer;
  2131. }
  2132. static int prism2_hostapd_add_sta(struct ap_data *ap,
  2133. struct prism2_hostapd_param *param)
  2134. {
  2135. struct sta_info *sta;
  2136. spin_lock_bh(&ap->sta_table_lock);
  2137. sta = ap_get_sta(ap, param->sta_addr);
  2138. if (sta)
  2139. atomic_inc(&sta->users);
  2140. spin_unlock_bh(&ap->sta_table_lock);
  2141. if (sta == NULL) {
  2142. sta = ap_add_sta(ap, param->sta_addr);
  2143. if (sta == NULL)
  2144. return -1;
  2145. }
  2146. if (!(sta->flags & WLAN_STA_ASSOC) && !sta->ap && sta->local)
  2147. hostap_event_new_sta(sta->local->dev, sta);
  2148. sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
  2149. sta->last_rx = jiffies;
  2150. sta->aid = param->u.add_sta.aid;
  2151. sta->capability = param->u.add_sta.capability;
  2152. sta->tx_supp_rates = param->u.add_sta.tx_supp_rates;
  2153. if (sta->tx_supp_rates & WLAN_RATE_1M)
  2154. sta->supported_rates[0] = 2;
  2155. if (sta->tx_supp_rates & WLAN_RATE_2M)
  2156. sta->supported_rates[1] = 4;
  2157. if (sta->tx_supp_rates & WLAN_RATE_5M5)
  2158. sta->supported_rates[2] = 11;
  2159. if (sta->tx_supp_rates & WLAN_RATE_11M)
  2160. sta->supported_rates[3] = 22;
  2161. prism2_check_tx_rates(sta);
  2162. atomic_dec(&sta->users);
  2163. return 0;
  2164. }
  2165. static int prism2_hostapd_remove_sta(struct ap_data *ap,
  2166. struct prism2_hostapd_param *param)
  2167. {
  2168. struct sta_info *sta;
  2169. spin_lock_bh(&ap->sta_table_lock);
  2170. sta = ap_get_sta(ap, param->sta_addr);
  2171. if (sta) {
  2172. ap_sta_hash_del(ap, sta);
  2173. list_del(&sta->list);
  2174. }
  2175. spin_unlock_bh(&ap->sta_table_lock);
  2176. if (!sta)
  2177. return -ENOENT;
  2178. if ((sta->flags & WLAN_STA_ASSOC) && !sta->ap && sta->local)
  2179. hostap_event_expired_sta(sta->local->dev, sta);
  2180. ap_free_sta(ap, sta);
  2181. return 0;
  2182. }
  2183. static int prism2_hostapd_get_info_sta(struct ap_data *ap,
  2184. struct prism2_hostapd_param *param)
  2185. {
  2186. struct sta_info *sta;
  2187. spin_lock_bh(&ap->sta_table_lock);
  2188. sta = ap_get_sta(ap, param->sta_addr);
  2189. if (sta)
  2190. atomic_inc(&sta->users);
  2191. spin_unlock_bh(&ap->sta_table_lock);
  2192. if (!sta)
  2193. return -ENOENT;
  2194. param->u.get_info_sta.inactive_sec = (jiffies - sta->last_rx) / HZ;
  2195. atomic_dec(&sta->users);
  2196. return 1;
  2197. }
  2198. static int prism2_hostapd_set_flags_sta(struct ap_data *ap,
  2199. struct prism2_hostapd_param *param)
  2200. {
  2201. struct sta_info *sta;
  2202. spin_lock_bh(&ap->sta_table_lock);
  2203. sta = ap_get_sta(ap, param->sta_addr);
  2204. if (sta) {
  2205. sta->flags |= param->u.set_flags_sta.flags_or;
  2206. sta->flags &= param->u.set_flags_sta.flags_and;
  2207. }
  2208. spin_unlock_bh(&ap->sta_table_lock);
  2209. if (!sta)
  2210. return -ENOENT;
  2211. return 0;
  2212. }
  2213. static int prism2_hostapd_sta_clear_stats(struct ap_data *ap,
  2214. struct prism2_hostapd_param *param)
  2215. {
  2216. struct sta_info *sta;
  2217. int rate;
  2218. spin_lock_bh(&ap->sta_table_lock);
  2219. sta = ap_get_sta(ap, param->sta_addr);
  2220. if (sta) {
  2221. sta->rx_packets = sta->tx_packets = 0;
  2222. sta->rx_bytes = sta->tx_bytes = 0;
  2223. for (rate = 0; rate < WLAN_RATE_COUNT; rate++) {
  2224. sta->tx_count[rate] = 0;
  2225. sta->rx_count[rate] = 0;
  2226. }
  2227. }
  2228. spin_unlock_bh(&ap->sta_table_lock);
  2229. if (!sta)
  2230. return -ENOENT;
  2231. return 0;
  2232. }
  2233. int prism2_hostapd(struct ap_data *ap, struct prism2_hostapd_param *param)
  2234. {
  2235. switch (param->cmd) {
  2236. case PRISM2_HOSTAPD_FLUSH:
  2237. ap_control_kickall(ap);
  2238. return 0;
  2239. case PRISM2_HOSTAPD_ADD_STA:
  2240. return prism2_hostapd_add_sta(ap, param);
  2241. case PRISM2_HOSTAPD_REMOVE_STA:
  2242. return prism2_hostapd_remove_sta(ap, param);
  2243. case PRISM2_HOSTAPD_GET_INFO_STA:
  2244. return prism2_hostapd_get_info_sta(ap, param);
  2245. case PRISM2_HOSTAPD_SET_FLAGS_STA:
  2246. return prism2_hostapd_set_flags_sta(ap, param);
  2247. case PRISM2_HOSTAPD_STA_CLEAR_STATS:
  2248. return prism2_hostapd_sta_clear_stats(ap, param);
  2249. default:
  2250. printk(KERN_WARNING "prism2_hostapd: unknown cmd=%d\n",
  2251. param->cmd);
  2252. return -EOPNOTSUPP;
  2253. }
  2254. }
  2255. /* Update station info for host-based TX rate control and return current
  2256. * TX rate */
  2257. static int ap_update_sta_tx_rate(struct sta_info *sta, struct net_device *dev)
  2258. {
  2259. int ret = sta->tx_rate;
  2260. struct hostap_interface *iface;
  2261. local_info_t *local;
  2262. iface = netdev_priv(dev);
  2263. local = iface->local;
  2264. sta->tx_count[sta->tx_rate_idx]++;
  2265. sta->tx_since_last_failure++;
  2266. sta->tx_consecutive_exc = 0;
  2267. if (sta->tx_since_last_failure >= WLAN_RATE_UPDATE_COUNT &&
  2268. sta->tx_rate_idx < sta->tx_max_rate) {
  2269. /* use next higher rate */
  2270. int old_rate, new_rate;
  2271. old_rate = new_rate = sta->tx_rate_idx;
  2272. while (new_rate < sta->tx_max_rate) {
  2273. new_rate++;
  2274. if (ap_tx_rate_ok(new_rate, sta, local)) {
  2275. sta->tx_rate_idx = new_rate;
  2276. break;
  2277. }
  2278. }
  2279. if (old_rate != sta->tx_rate_idx) {
  2280. switch (sta->tx_rate_idx) {
  2281. case 0: sta->tx_rate = 10; break;
  2282. case 1: sta->tx_rate = 20; break;
  2283. case 2: sta->tx_rate = 55; break;
  2284. case 3: sta->tx_rate = 110; break;
  2285. default: sta->tx_rate = 0; break;
  2286. }
  2287. PDEBUG(DEBUG_AP, "%s: STA %pM TX rate raised to %d\n",
  2288. dev->name, sta->addr, sta->tx_rate);
  2289. }
  2290. sta->tx_since_last_failure = 0;
  2291. }
  2292. return ret;
  2293. }
  2294. /* Called only from software IRQ. Called for each TX frame prior possible
  2295. * encryption and transmit. */
  2296. ap_tx_ret hostap_handle_sta_tx(local_info_t *local, struct hostap_tx_data *tx)
  2297. {
  2298. struct sta_info *sta = NULL;
  2299. struct sk_buff *skb = tx->skb;
  2300. int set_tim, ret;
  2301. struct ieee80211_hdr *hdr;
  2302. struct hostap_skb_tx_data *meta;
  2303. meta = (struct hostap_skb_tx_data *) skb->cb;
  2304. ret = AP_TX_CONTINUE;
  2305. if (local->ap == NULL || skb->len < 10 ||
  2306. meta->iface->type == HOSTAP_INTERFACE_STA)
  2307. goto out;
  2308. hdr = (struct ieee80211_hdr *) skb->data;
  2309. if (hdr->addr1[0] & 0x01) {
  2310. /* broadcast/multicast frame - no AP related processing */
  2311. if (local->ap->num_sta <= 0)
  2312. ret = AP_TX_DROP;
  2313. goto out;
  2314. }
  2315. /* unicast packet - check whether destination STA is associated */
  2316. spin_lock(&local->ap->sta_table_lock);
  2317. sta = ap_get_sta(local->ap, hdr->addr1);
  2318. if (sta)
  2319. atomic_inc(&sta->users);
  2320. spin_unlock(&local->ap->sta_table_lock);
  2321. if (local->iw_mode == IW_MODE_MASTER && sta == NULL &&
  2322. !(meta->flags & HOSTAP_TX_FLAGS_WDS) &&
  2323. meta->iface->type != HOSTAP_INTERFACE_MASTER &&
  2324. meta->iface->type != HOSTAP_INTERFACE_AP) {
  2325. #if 0
  2326. /* This can happen, e.g., when wlan0 is added to a bridge and
  2327. * bridging code does not know which port is the correct target
  2328. * for a unicast frame. In this case, the packet is send to all
  2329. * ports of the bridge. Since this is a valid scenario, do not
  2330. * print out any errors here. */
  2331. if (net_ratelimit()) {
  2332. printk(KERN_DEBUG "AP: drop packet to non-associated "
  2333. "STA %pM\n", hdr->addr1);
  2334. }
  2335. #endif
  2336. local->ap->tx_drop_nonassoc++;
  2337. ret = AP_TX_DROP;
  2338. goto out;
  2339. }
  2340. if (sta == NULL)
  2341. goto out;
  2342. if (!(sta->flags & WLAN_STA_AUTHORIZED))
  2343. ret = AP_TX_CONTINUE_NOT_AUTHORIZED;
  2344. /* Set tx_rate if using host-based TX rate control */
  2345. if (!local->fw_tx_rate_control)
  2346. local->ap->last_tx_rate = meta->rate =
  2347. ap_update_sta_tx_rate(sta, local->dev);
  2348. if (local->iw_mode != IW_MODE_MASTER)
  2349. goto out;
  2350. if (!(sta->flags & WLAN_STA_PS))
  2351. goto out;
  2352. if (meta->flags & HOSTAP_TX_FLAGS_ADD_MOREDATA) {
  2353. /* indicate to STA that more frames follow */
  2354. hdr->frame_control |=
  2355. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  2356. }
  2357. if (meta->flags & HOSTAP_TX_FLAGS_BUFFERED_FRAME) {
  2358. /* packet was already buffered and now send due to
  2359. * PS poll, so do not rebuffer it */
  2360. goto out;
  2361. }
  2362. if (skb_queue_len(&sta->tx_buf) >= STA_MAX_TX_BUFFER) {
  2363. PDEBUG(DEBUG_PS, "%s: No more space in STA (%pM)'s"
  2364. "PS mode buffer\n",
  2365. local->dev->name, sta->addr);
  2366. /* Make sure that TIM is set for the station (it might not be
  2367. * after AP wlan hw reset). */
  2368. /* FIX: should fix hw reset to restore bits based on STA
  2369. * buffer state.. */
  2370. hostap_set_tim(local, sta->aid, 1);
  2371. sta->flags |= WLAN_STA_TIM;
  2372. ret = AP_TX_DROP;
  2373. goto out;
  2374. }
  2375. /* STA in PS mode, buffer frame for later delivery */
  2376. set_tim = skb_queue_empty(&sta->tx_buf);
  2377. skb_queue_tail(&sta->tx_buf, skb);
  2378. /* FIX: could save RX time to skb and expire buffered frames after
  2379. * some time if STA does not poll for them */
  2380. if (set_tim) {
  2381. if (sta->flags & WLAN_STA_TIM)
  2382. PDEBUG(DEBUG_PS2, "Re-setting TIM for aid %d\n",
  2383. sta->aid);
  2384. hostap_set_tim(local, sta->aid, 1);
  2385. sta->flags |= WLAN_STA_TIM;
  2386. }
  2387. ret = AP_TX_BUFFERED;
  2388. out:
  2389. if (sta != NULL) {
  2390. if (ret == AP_TX_CONTINUE ||
  2391. ret == AP_TX_CONTINUE_NOT_AUTHORIZED) {
  2392. sta->tx_packets++;
  2393. sta->tx_bytes += skb->len;
  2394. sta->last_tx = jiffies;
  2395. }
  2396. if ((ret == AP_TX_CONTINUE ||
  2397. ret == AP_TX_CONTINUE_NOT_AUTHORIZED) &&
  2398. sta->crypt && tx->host_encrypt) {
  2399. tx->crypt = sta->crypt;
  2400. tx->sta_ptr = sta; /* hostap_handle_sta_release() will
  2401. * be called to release sta info
  2402. * later */
  2403. } else
  2404. atomic_dec(&sta->users);
  2405. }
  2406. return ret;
  2407. }
  2408. void hostap_handle_sta_release(void *ptr)
  2409. {
  2410. struct sta_info *sta = ptr;
  2411. atomic_dec(&sta->users);
  2412. }
  2413. /* Called only as a tasklet (software IRQ) */
  2414. void hostap_handle_sta_tx_exc(local_info_t *local, struct sk_buff *skb)
  2415. {
  2416. struct sta_info *sta;
  2417. struct ieee80211_hdr *hdr;
  2418. struct hostap_skb_tx_data *meta;
  2419. hdr = (struct ieee80211_hdr *) skb->data;
  2420. meta = (struct hostap_skb_tx_data *) skb->cb;
  2421. spin_lock(&local->ap->sta_table_lock);
  2422. sta = ap_get_sta(local->ap, hdr->addr1);
  2423. if (!sta) {
  2424. spin_unlock(&local->ap->sta_table_lock);
  2425. PDEBUG(DEBUG_AP, "%s: Could not find STA %pM"
  2426. " for this TX error (@%lu)\n",
  2427. local->dev->name, hdr->addr1, jiffies);
  2428. return;
  2429. }
  2430. sta->tx_since_last_failure = 0;
  2431. sta->tx_consecutive_exc++;
  2432. if (sta->tx_consecutive_exc >= WLAN_RATE_DECREASE_THRESHOLD &&
  2433. sta->tx_rate_idx > 0 && meta->rate <= sta->tx_rate) {
  2434. /* use next lower rate */
  2435. int old, rate;
  2436. old = rate = sta->tx_rate_idx;
  2437. while (rate > 0) {
  2438. rate--;
  2439. if (ap_tx_rate_ok(rate, sta, local)) {
  2440. sta->tx_rate_idx = rate;
  2441. break;
  2442. }
  2443. }
  2444. if (old != sta->tx_rate_idx) {
  2445. switch (sta->tx_rate_idx) {
  2446. case 0: sta->tx_rate = 10; break;
  2447. case 1: sta->tx_rate = 20; break;
  2448. case 2: sta->tx_rate = 55; break;
  2449. case 3: sta->tx_rate = 110; break;
  2450. default: sta->tx_rate = 0; break;
  2451. }
  2452. PDEBUG(DEBUG_AP,
  2453. "%s: STA %pM TX rate lowered to %d\n",
  2454. local->dev->name, sta->addr, sta->tx_rate);
  2455. }
  2456. sta->tx_consecutive_exc = 0;
  2457. }
  2458. spin_unlock(&local->ap->sta_table_lock);
  2459. }
  2460. static void hostap_update_sta_ps2(local_info_t *local, struct sta_info *sta,
  2461. int pwrmgt, int type, int stype)
  2462. {
  2463. if (pwrmgt && !(sta->flags & WLAN_STA_PS)) {
  2464. sta->flags |= WLAN_STA_PS;
  2465. PDEBUG(DEBUG_PS2, "STA %pM changed to use PS "
  2466. "mode (type=0x%02X, stype=0x%02X)\n",
  2467. sta->addr, type >> 2, stype >> 4);
  2468. } else if (!pwrmgt && (sta->flags & WLAN_STA_PS)) {
  2469. sta->flags &= ~WLAN_STA_PS;
  2470. PDEBUG(DEBUG_PS2, "STA %pM changed to not use "
  2471. "PS mode (type=0x%02X, stype=0x%02X)\n",
  2472. sta->addr, type >> 2, stype >> 4);
  2473. if (type != IEEE80211_FTYPE_CTL ||
  2474. stype != IEEE80211_STYPE_PSPOLL)
  2475. schedule_packet_send(local, sta);
  2476. }
  2477. }
  2478. /* Called only as a tasklet (software IRQ). Called for each RX frame to update
  2479. * STA power saving state. pwrmgt is a flag from 802.11 frame_control field. */
  2480. int hostap_update_sta_ps(local_info_t *local, struct ieee80211_hdr *hdr)
  2481. {
  2482. struct sta_info *sta;
  2483. u16 fc;
  2484. spin_lock(&local->ap->sta_table_lock);
  2485. sta = ap_get_sta(local->ap, hdr->addr2);
  2486. if (sta)
  2487. atomic_inc(&sta->users);
  2488. spin_unlock(&local->ap->sta_table_lock);
  2489. if (!sta)
  2490. return -1;
  2491. fc = le16_to_cpu(hdr->frame_control);
  2492. hostap_update_sta_ps2(local, sta, fc & IEEE80211_FCTL_PM,
  2493. fc & IEEE80211_FCTL_FTYPE,
  2494. fc & IEEE80211_FCTL_STYPE);
  2495. atomic_dec(&sta->users);
  2496. return 0;
  2497. }
  2498. /* Called only as a tasklet (software IRQ). Called for each RX frame after
  2499. * getting RX header and payload from hardware. */
  2500. ap_rx_ret hostap_handle_sta_rx(local_info_t *local, struct net_device *dev,
  2501. struct sk_buff *skb,
  2502. struct hostap_80211_rx_status *rx_stats,
  2503. int wds)
  2504. {
  2505. int ret;
  2506. struct sta_info *sta;
  2507. u16 fc, type, stype;
  2508. struct ieee80211_hdr *hdr;
  2509. if (local->ap == NULL)
  2510. return AP_RX_CONTINUE;
  2511. hdr = (struct ieee80211_hdr *) skb->data;
  2512. fc = le16_to_cpu(hdr->frame_control);
  2513. type = fc & IEEE80211_FCTL_FTYPE;
  2514. stype = fc & IEEE80211_FCTL_STYPE;
  2515. spin_lock(&local->ap->sta_table_lock);
  2516. sta = ap_get_sta(local->ap, hdr->addr2);
  2517. if (sta)
  2518. atomic_inc(&sta->users);
  2519. spin_unlock(&local->ap->sta_table_lock);
  2520. if (sta && !(sta->flags & WLAN_STA_AUTHORIZED))
  2521. ret = AP_RX_CONTINUE_NOT_AUTHORIZED;
  2522. else
  2523. ret = AP_RX_CONTINUE;
  2524. if (fc & IEEE80211_FCTL_TODS) {
  2525. if (!wds && (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))) {
  2526. if (local->hostapd) {
  2527. prism2_rx_80211(local->apdev, skb, rx_stats,
  2528. PRISM2_RX_NON_ASSOC);
  2529. #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
  2530. } else {
  2531. printk(KERN_DEBUG "%s: dropped received packet"
  2532. " from non-associated STA %pM"
  2533. " (type=0x%02x, subtype=0x%02x)\n",
  2534. dev->name, hdr->addr2,
  2535. type >> 2, stype >> 4);
  2536. hostap_rx(dev, skb, rx_stats);
  2537. #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
  2538. }
  2539. ret = AP_RX_EXIT;
  2540. goto out;
  2541. }
  2542. } else if (fc & IEEE80211_FCTL_FROMDS) {
  2543. if (!wds) {
  2544. /* FromDS frame - not for us; probably
  2545. * broadcast/multicast in another BSS - drop */
  2546. if (memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) == 0) {
  2547. printk(KERN_DEBUG "Odd.. FromDS packet "
  2548. "received with own BSSID\n");
  2549. hostap_dump_rx_80211(dev->name, skb, rx_stats);
  2550. }
  2551. ret = AP_RX_DROP;
  2552. goto out;
  2553. }
  2554. } else if (stype == IEEE80211_STYPE_NULLFUNC && sta == NULL &&
  2555. memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) == 0) {
  2556. if (local->hostapd) {
  2557. prism2_rx_80211(local->apdev, skb, rx_stats,
  2558. PRISM2_RX_NON_ASSOC);
  2559. #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
  2560. } else {
  2561. /* At least Lucent f/w seems to send data::nullfunc
  2562. * frames with no ToDS flag when the current AP returns
  2563. * after being unavailable for some time. Speed up
  2564. * re-association by informing the station about it not
  2565. * being associated. */
  2566. printk(KERN_DEBUG "%s: rejected received nullfunc frame"
  2567. " without ToDS from not associated STA %pM\n",
  2568. dev->name, hdr->addr2);
  2569. hostap_rx(dev, skb, rx_stats);
  2570. #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
  2571. }
  2572. ret = AP_RX_EXIT;
  2573. goto out;
  2574. } else if (stype == IEEE80211_STYPE_NULLFUNC) {
  2575. /* At least Lucent cards seem to send periodic nullfunc
  2576. * frames with ToDS. Let these through to update SQ
  2577. * stats and PS state. Nullfunc frames do not contain
  2578. * any data and they will be dropped below. */
  2579. } else {
  2580. /* If BSSID (Addr3) is foreign, this frame is a normal
  2581. * broadcast frame from an IBSS network. Drop it silently.
  2582. * If BSSID is own, report the dropping of this frame. */
  2583. if (memcmp(hdr->addr3, dev->dev_addr, ETH_ALEN) == 0) {
  2584. printk(KERN_DEBUG "%s: dropped received packet from %pM"
  2585. " with no ToDS flag "
  2586. "(type=0x%02x, subtype=0x%02x)\n", dev->name,
  2587. hdr->addr2, type >> 2, stype >> 4);
  2588. hostap_dump_rx_80211(dev->name, skb, rx_stats);
  2589. }
  2590. ret = AP_RX_DROP;
  2591. goto out;
  2592. }
  2593. if (sta) {
  2594. hostap_update_sta_ps2(local, sta, fc & IEEE80211_FCTL_PM,
  2595. type, stype);
  2596. sta->rx_packets++;
  2597. sta->rx_bytes += skb->len;
  2598. sta->last_rx = jiffies;
  2599. }
  2600. if (local->ap->nullfunc_ack && stype == IEEE80211_STYPE_NULLFUNC &&
  2601. fc & IEEE80211_FCTL_TODS) {
  2602. if (local->hostapd) {
  2603. prism2_rx_80211(local->apdev, skb, rx_stats,
  2604. PRISM2_RX_NULLFUNC_ACK);
  2605. #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
  2606. } else {
  2607. /* some STA f/w's seem to require control::ACK frame
  2608. * for data::nullfunc, but Prism2 f/w 0.8.0 (at least
  2609. * from Compaq) does not send this.. Try to generate
  2610. * ACK for these frames from the host driver to make
  2611. * power saving work with, e.g., Lucent WaveLAN f/w */
  2612. hostap_rx(dev, skb, rx_stats);
  2613. #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
  2614. }
  2615. ret = AP_RX_EXIT;
  2616. goto out;
  2617. }
  2618. out:
  2619. if (sta)
  2620. atomic_dec(&sta->users);
  2621. return ret;
  2622. }
  2623. /* Called only as a tasklet (software IRQ) */
  2624. int hostap_handle_sta_crypto(local_info_t *local,
  2625. struct ieee80211_hdr *hdr,
  2626. struct lib80211_crypt_data **crypt,
  2627. void **sta_ptr)
  2628. {
  2629. struct sta_info *sta;
  2630. spin_lock(&local->ap->sta_table_lock);
  2631. sta = ap_get_sta(local->ap, hdr->addr2);
  2632. if (sta)
  2633. atomic_inc(&sta->users);
  2634. spin_unlock(&local->ap->sta_table_lock);
  2635. if (!sta)
  2636. return -1;
  2637. if (sta->crypt) {
  2638. *crypt = sta->crypt;
  2639. *sta_ptr = sta;
  2640. /* hostap_handle_sta_release() will be called to release STA
  2641. * info */
  2642. } else
  2643. atomic_dec(&sta->users);
  2644. return 0;
  2645. }
  2646. /* Called only as a tasklet (software IRQ) */
  2647. int hostap_is_sta_assoc(struct ap_data *ap, u8 *sta_addr)
  2648. {
  2649. struct sta_info *sta;
  2650. int ret = 0;
  2651. spin_lock(&ap->sta_table_lock);
  2652. sta = ap_get_sta(ap, sta_addr);
  2653. if (sta != NULL && (sta->flags & WLAN_STA_ASSOC) && !sta->ap)
  2654. ret = 1;
  2655. spin_unlock(&ap->sta_table_lock);
  2656. return ret;
  2657. }
  2658. /* Called only as a tasklet (software IRQ) */
  2659. int hostap_is_sta_authorized(struct ap_data *ap, u8 *sta_addr)
  2660. {
  2661. struct sta_info *sta;
  2662. int ret = 0;
  2663. spin_lock(&ap->sta_table_lock);
  2664. sta = ap_get_sta(ap, sta_addr);
  2665. if (sta != NULL && (sta->flags & WLAN_STA_ASSOC) && !sta->ap &&
  2666. ((sta->flags & WLAN_STA_AUTHORIZED) ||
  2667. ap->local->ieee_802_1x == 0))
  2668. ret = 1;
  2669. spin_unlock(&ap->sta_table_lock);
  2670. return ret;
  2671. }
  2672. /* Called only as a tasklet (software IRQ) */
  2673. int hostap_add_sta(struct ap_data *ap, u8 *sta_addr)
  2674. {
  2675. struct sta_info *sta;
  2676. int ret = 1;
  2677. if (!ap)
  2678. return -1;
  2679. spin_lock(&ap->sta_table_lock);
  2680. sta = ap_get_sta(ap, sta_addr);
  2681. if (sta)
  2682. ret = 0;
  2683. spin_unlock(&ap->sta_table_lock);
  2684. if (ret == 1) {
  2685. sta = ap_add_sta(ap, sta_addr);
  2686. if (!sta)
  2687. return -1;
  2688. sta->flags = WLAN_STA_AUTH | WLAN_STA_ASSOC;
  2689. sta->ap = 1;
  2690. memset(sta->supported_rates, 0, sizeof(sta->supported_rates));
  2691. /* No way of knowing which rates are supported since we did not
  2692. * get supported rates element from beacon/assoc req. Assume
  2693. * that remote end supports all 802.11b rates. */
  2694. sta->supported_rates[0] = 0x82;
  2695. sta->supported_rates[1] = 0x84;
  2696. sta->supported_rates[2] = 0x0b;
  2697. sta->supported_rates[3] = 0x16;
  2698. sta->tx_supp_rates = WLAN_RATE_1M | WLAN_RATE_2M |
  2699. WLAN_RATE_5M5 | WLAN_RATE_11M;
  2700. sta->tx_rate = 110;
  2701. sta->tx_max_rate = sta->tx_rate_idx = 3;
  2702. }
  2703. return ret;
  2704. }
  2705. /* Called only as a tasklet (software IRQ) */
  2706. int hostap_update_rx_stats(struct ap_data *ap,
  2707. struct ieee80211_hdr *hdr,
  2708. struct hostap_80211_rx_status *rx_stats)
  2709. {
  2710. struct sta_info *sta;
  2711. if (!ap)
  2712. return -1;
  2713. spin_lock(&ap->sta_table_lock);
  2714. sta = ap_get_sta(ap, hdr->addr2);
  2715. if (sta) {
  2716. sta->last_rx_silence = rx_stats->noise;
  2717. sta->last_rx_signal = rx_stats->signal;
  2718. sta->last_rx_rate = rx_stats->rate;
  2719. sta->last_rx_updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
  2720. if (rx_stats->rate == 10)
  2721. sta->rx_count[0]++;
  2722. else if (rx_stats->rate == 20)
  2723. sta->rx_count[1]++;
  2724. else if (rx_stats->rate == 55)
  2725. sta->rx_count[2]++;
  2726. else if (rx_stats->rate == 110)
  2727. sta->rx_count[3]++;
  2728. }
  2729. spin_unlock(&ap->sta_table_lock);
  2730. return sta ? 0 : -1;
  2731. }
  2732. void hostap_update_rates(local_info_t *local)
  2733. {
  2734. struct sta_info *sta;
  2735. struct ap_data *ap = local->ap;
  2736. if (!ap)
  2737. return;
  2738. spin_lock_bh(&ap->sta_table_lock);
  2739. list_for_each_entry(sta, &ap->sta_list, list) {
  2740. prism2_check_tx_rates(sta);
  2741. }
  2742. spin_unlock_bh(&ap->sta_table_lock);
  2743. }
  2744. void * ap_crypt_get_ptrs(struct ap_data *ap, u8 *addr, int permanent,
  2745. struct lib80211_crypt_data ***crypt)
  2746. {
  2747. struct sta_info *sta;
  2748. spin_lock_bh(&ap->sta_table_lock);
  2749. sta = ap_get_sta(ap, addr);
  2750. if (sta)
  2751. atomic_inc(&sta->users);
  2752. spin_unlock_bh(&ap->sta_table_lock);
  2753. if (!sta && permanent)
  2754. sta = ap_add_sta(ap, addr);
  2755. if (!sta)
  2756. return NULL;
  2757. if (permanent)
  2758. sta->flags |= WLAN_STA_PERM;
  2759. *crypt = &sta->crypt;
  2760. return sta;
  2761. }
  2762. void hostap_add_wds_links(local_info_t *local)
  2763. {
  2764. struct ap_data *ap = local->ap;
  2765. struct sta_info *sta;
  2766. spin_lock_bh(&ap->sta_table_lock);
  2767. list_for_each_entry(sta, &ap->sta_list, list) {
  2768. if (sta->ap)
  2769. hostap_wds_link_oper(local, sta->addr, WDS_ADD);
  2770. }
  2771. spin_unlock_bh(&ap->sta_table_lock);
  2772. schedule_work(&local->ap->wds_oper_queue);
  2773. }
  2774. void hostap_wds_link_oper(local_info_t *local, u8 *addr, wds_oper_type type)
  2775. {
  2776. struct wds_oper_data *entry;
  2777. entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
  2778. if (!entry)
  2779. return;
  2780. memcpy(entry->addr, addr, ETH_ALEN);
  2781. entry->type = type;
  2782. spin_lock_bh(&local->lock);
  2783. entry->next = local->ap->wds_oper_entries;
  2784. local->ap->wds_oper_entries = entry;
  2785. spin_unlock_bh(&local->lock);
  2786. schedule_work(&local->ap->wds_oper_queue);
  2787. }
  2788. EXPORT_SYMBOL(hostap_init_data);
  2789. EXPORT_SYMBOL(hostap_init_ap_proc);
  2790. EXPORT_SYMBOL(hostap_free_data);
  2791. EXPORT_SYMBOL(hostap_check_sta_fw_version);
  2792. EXPORT_SYMBOL(hostap_handle_sta_tx_exc);
  2793. #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
  2794. #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */