hostap_ap.c 86 KB

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