hostap_ap.c 86 KB

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