hostap_ap.c 86 KB

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