hostap_ap.c 86 KB

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