hostap_ap.c 86 KB

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