rtllib_rx.c 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787
  1. /*
  2. * Original code based Host AP (software wireless LAN access point) driver
  3. * for Intersil Prism2/2.5/3 - hostap.o module, common routines
  4. *
  5. * Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen
  6. * <jkmaline@cc.hut.fi>
  7. * Copyright (c) 2002-2003, Jouni Malinen <jkmaline@cc.hut.fi>
  8. * Copyright (c) 2004, Intel Corporation
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation. See README and COPYING for
  13. * more details.
  14. ******************************************************************************
  15. Few modifications for Realtek's Wi-Fi drivers by
  16. Andrea Merello <andrea.merello@gmail.com>
  17. A special thanks goes to Realtek for their support !
  18. ******************************************************************************/
  19. #include <linux/compiler.h>
  20. #include <linux/errno.h>
  21. #include <linux/if_arp.h>
  22. #include <linux/in6.h>
  23. #include <linux/in.h>
  24. #include <linux/ip.h>
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/pci.h>
  29. #include <linux/proc_fs.h>
  30. #include <linux/skbuff.h>
  31. #include <linux/slab.h>
  32. #include <linux/tcp.h>
  33. #include <linux/types.h>
  34. #include <linux/wireless.h>
  35. #include <linux/etherdevice.h>
  36. #include <linux/uaccess.h>
  37. #include <linux/ctype.h>
  38. #include "rtllib.h"
  39. #include "dot11d.h"
  40. static void rtllib_rx_mgt(struct rtllib_device *ieee, struct sk_buff *skb,
  41. struct rtllib_rx_stats *stats);
  42. static inline void rtllib_monitor_rx(struct rtllib_device *ieee,
  43. struct sk_buff *skb,
  44. struct rtllib_rx_stats *rx_status,
  45. size_t hdr_length)
  46. {
  47. skb->dev = ieee->dev;
  48. skb_reset_mac_header(skb);
  49. skb_pull(skb, hdr_length);
  50. skb->pkt_type = PACKET_OTHERHOST;
  51. skb->protocol = htons(ETH_P_80211_RAW);
  52. memset(skb->cb, 0, sizeof(skb->cb));
  53. netif_rx(skb);
  54. }
  55. /* Called only as a tasklet (software IRQ) */
  56. static struct rtllib_frag_entry *
  57. rtllib_frag_cache_find(struct rtllib_device *ieee, unsigned int seq,
  58. unsigned int frag, u8 tid, u8 *src, u8 *dst)
  59. {
  60. struct rtllib_frag_entry *entry;
  61. int i;
  62. for (i = 0; i < RTLLIB_FRAG_CACHE_LEN; i++) {
  63. entry = &ieee->frag_cache[tid][i];
  64. if (entry->skb != NULL &&
  65. time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
  66. netdev_dbg(ieee->dev,
  67. "expiring fragment cache entry seq=%u last_frag=%u\n",
  68. entry->seq, entry->last_frag);
  69. dev_kfree_skb_any(entry->skb);
  70. entry->skb = NULL;
  71. }
  72. if (entry->skb != NULL && entry->seq == seq &&
  73. (entry->last_frag + 1 == frag || frag == -1) &&
  74. memcmp(entry->src_addr, src, ETH_ALEN) == 0 &&
  75. memcmp(entry->dst_addr, dst, ETH_ALEN) == 0)
  76. return entry;
  77. }
  78. return NULL;
  79. }
  80. /* Called only as a tasklet (software IRQ) */
  81. static struct sk_buff *
  82. rtllib_frag_cache_get(struct rtllib_device *ieee,
  83. struct rtllib_hdr_4addr *hdr)
  84. {
  85. struct sk_buff *skb = NULL;
  86. u16 fc = le16_to_cpu(hdr->frame_ctl);
  87. u16 sc = le16_to_cpu(hdr->seq_ctl);
  88. unsigned int frag = WLAN_GET_SEQ_FRAG(sc);
  89. unsigned int seq = WLAN_GET_SEQ_SEQ(sc);
  90. struct rtllib_frag_entry *entry;
  91. struct rtllib_hdr_3addrqos *hdr_3addrqos;
  92. struct rtllib_hdr_4addrqos *hdr_4addrqos;
  93. u8 tid;
  94. if (((fc & RTLLIB_FCTL_DSTODS) == RTLLIB_FCTL_DSTODS) &&
  95. RTLLIB_QOS_HAS_SEQ(fc)) {
  96. hdr_4addrqos = (struct rtllib_hdr_4addrqos *)hdr;
  97. tid = le16_to_cpu(hdr_4addrqos->qos_ctl) & RTLLIB_QCTL_TID;
  98. tid = UP2AC(tid);
  99. tid++;
  100. } else if (RTLLIB_QOS_HAS_SEQ(fc)) {
  101. hdr_3addrqos = (struct rtllib_hdr_3addrqos *)hdr;
  102. tid = le16_to_cpu(hdr_3addrqos->qos_ctl) & RTLLIB_QCTL_TID;
  103. tid = UP2AC(tid);
  104. tid++;
  105. } else {
  106. tid = 0;
  107. }
  108. if (frag == 0) {
  109. /* Reserve enough space to fit maximum frame length */
  110. skb = dev_alloc_skb(ieee->dev->mtu +
  111. sizeof(struct rtllib_hdr_4addr) +
  112. 8 /* LLC */ +
  113. 2 /* alignment */ +
  114. 8 /* WEP */ +
  115. ETH_ALEN /* WDS */ +
  116. /* QOS Control */
  117. (RTLLIB_QOS_HAS_SEQ(fc) ? 2 : 0));
  118. if (skb == NULL)
  119. return NULL;
  120. entry = &ieee->frag_cache[tid][ieee->frag_next_idx[tid]];
  121. ieee->frag_next_idx[tid]++;
  122. if (ieee->frag_next_idx[tid] >= RTLLIB_FRAG_CACHE_LEN)
  123. ieee->frag_next_idx[tid] = 0;
  124. if (entry->skb != NULL)
  125. dev_kfree_skb_any(entry->skb);
  126. entry->first_frag_time = jiffies;
  127. entry->seq = seq;
  128. entry->last_frag = frag;
  129. entry->skb = skb;
  130. ether_addr_copy(entry->src_addr, hdr->addr2);
  131. ether_addr_copy(entry->dst_addr, hdr->addr1);
  132. } else {
  133. /* received a fragment of a frame for which the head fragment
  134. * should have already been received
  135. */
  136. entry = rtllib_frag_cache_find(ieee, seq, frag, tid, hdr->addr2,
  137. hdr->addr1);
  138. if (entry != NULL) {
  139. entry->last_frag = frag;
  140. skb = entry->skb;
  141. }
  142. }
  143. return skb;
  144. }
  145. /* Called only as a tasklet (software IRQ) */
  146. static int rtllib_frag_cache_invalidate(struct rtllib_device *ieee,
  147. struct rtllib_hdr_4addr *hdr)
  148. {
  149. u16 fc = le16_to_cpu(hdr->frame_ctl);
  150. u16 sc = le16_to_cpu(hdr->seq_ctl);
  151. unsigned int seq = WLAN_GET_SEQ_SEQ(sc);
  152. struct rtllib_frag_entry *entry;
  153. struct rtllib_hdr_3addrqos *hdr_3addrqos;
  154. struct rtllib_hdr_4addrqos *hdr_4addrqos;
  155. u8 tid;
  156. if (((fc & RTLLIB_FCTL_DSTODS) == RTLLIB_FCTL_DSTODS) &&
  157. RTLLIB_QOS_HAS_SEQ(fc)) {
  158. hdr_4addrqos = (struct rtllib_hdr_4addrqos *)hdr;
  159. tid = le16_to_cpu(hdr_4addrqos->qos_ctl) & RTLLIB_QCTL_TID;
  160. tid = UP2AC(tid);
  161. tid++;
  162. } else if (RTLLIB_QOS_HAS_SEQ(fc)) {
  163. hdr_3addrqos = (struct rtllib_hdr_3addrqos *)hdr;
  164. tid = le16_to_cpu(hdr_3addrqos->qos_ctl) & RTLLIB_QCTL_TID;
  165. tid = UP2AC(tid);
  166. tid++;
  167. } else {
  168. tid = 0;
  169. }
  170. entry = rtllib_frag_cache_find(ieee, seq, -1, tid, hdr->addr2,
  171. hdr->addr1);
  172. if (entry == NULL) {
  173. netdev_dbg(ieee->dev,
  174. "Couldn't invalidate fragment cache entry (seq=%u)\n",
  175. seq);
  176. return -1;
  177. }
  178. entry->skb = NULL;
  179. return 0;
  180. }
  181. /* rtllib_rx_frame_mgtmt
  182. *
  183. * Responsible for handling management control frames
  184. *
  185. * Called by rtllib_rx
  186. */
  187. static inline int
  188. rtllib_rx_frame_mgmt(struct rtllib_device *ieee, struct sk_buff *skb,
  189. struct rtllib_rx_stats *rx_stats, u16 type,
  190. u16 stype)
  191. {
  192. /* On the struct stats definition there is written that
  193. * this is not mandatory.... but seems that the probe
  194. * response parser uses it
  195. */
  196. struct rtllib_hdr_3addr *hdr = (struct rtllib_hdr_3addr *)skb->data;
  197. rx_stats->len = skb->len;
  198. rtllib_rx_mgt(ieee, skb, rx_stats);
  199. if ((memcmp(hdr->addr1, ieee->dev->dev_addr, ETH_ALEN))) {
  200. dev_kfree_skb_any(skb);
  201. return 0;
  202. }
  203. rtllib_rx_frame_softmac(ieee, skb, rx_stats, type, stype);
  204. dev_kfree_skb_any(skb);
  205. return 0;
  206. }
  207. /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation
  208. * Ethernet-II snap header (RFC1042 for most EtherTypes)
  209. */
  210. static unsigned char rfc1042_header[] = {
  211. 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00
  212. };
  213. /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
  214. static unsigned char bridge_tunnel_header[] = {
  215. 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8
  216. };
  217. /* No encapsulation header if EtherType < 0x600 (=length) */
  218. /* Called by rtllib_rx_frame_decrypt */
  219. static int rtllib_is_eapol_frame(struct rtllib_device *ieee,
  220. struct sk_buff *skb, size_t hdrlen)
  221. {
  222. struct net_device *dev = ieee->dev;
  223. u16 fc, ethertype;
  224. struct rtllib_hdr_4addr *hdr;
  225. u8 *pos;
  226. if (skb->len < 24)
  227. return 0;
  228. hdr = (struct rtllib_hdr_4addr *) skb->data;
  229. fc = le16_to_cpu(hdr->frame_ctl);
  230. /* check that the frame is unicast frame to us */
  231. if ((fc & (RTLLIB_FCTL_TODS | RTLLIB_FCTL_FROMDS)) ==
  232. RTLLIB_FCTL_TODS &&
  233. memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) == 0 &&
  234. memcmp(hdr->addr3, dev->dev_addr, ETH_ALEN) == 0) {
  235. /* ToDS frame with own addr BSSID and DA */
  236. } else if ((fc & (RTLLIB_FCTL_TODS | RTLLIB_FCTL_FROMDS)) ==
  237. RTLLIB_FCTL_FROMDS &&
  238. memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) == 0) {
  239. /* FromDS frame with own addr as DA */
  240. } else
  241. return 0;
  242. if (skb->len < 24 + 8)
  243. return 0;
  244. /* check for port access entity Ethernet type */
  245. pos = skb->data + hdrlen;
  246. ethertype = (pos[6] << 8) | pos[7];
  247. if (ethertype == ETH_P_PAE)
  248. return 1;
  249. return 0;
  250. }
  251. /* Called only as a tasklet (software IRQ), by rtllib_rx */
  252. static inline int
  253. rtllib_rx_frame_decrypt(struct rtllib_device *ieee, struct sk_buff *skb,
  254. struct lib80211_crypt_data *crypt)
  255. {
  256. struct rtllib_hdr_4addr *hdr;
  257. int res, hdrlen;
  258. if (crypt == NULL || crypt->ops->decrypt_mpdu == NULL)
  259. return 0;
  260. if (ieee->hwsec_active) {
  261. struct cb_desc *tcb_desc = (struct cb_desc *)
  262. (skb->cb + MAX_DEV_ADDR_SIZE);
  263. tcb_desc->bHwSec = 1;
  264. if (ieee->need_sw_enc)
  265. tcb_desc->bHwSec = 0;
  266. }
  267. hdr = (struct rtllib_hdr_4addr *) skb->data;
  268. hdrlen = rtllib_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
  269. atomic_inc(&crypt->refcnt);
  270. res = crypt->ops->decrypt_mpdu(skb, hdrlen, crypt->priv);
  271. atomic_dec(&crypt->refcnt);
  272. if (res < 0) {
  273. netdev_dbg(ieee->dev, "decryption failed (SA= %pM) res=%d\n",
  274. hdr->addr2, res);
  275. if (res == -2)
  276. netdev_dbg(ieee->dev,
  277. "Decryption failed ICV mismatch (key %d)\n",
  278. skb->data[hdrlen + 3] >> 6);
  279. return -1;
  280. }
  281. return res;
  282. }
  283. /* Called only as a tasklet (software IRQ), by rtllib_rx */
  284. static inline int
  285. rtllib_rx_frame_decrypt_msdu(struct rtllib_device *ieee, struct sk_buff *skb,
  286. int keyidx, struct lib80211_crypt_data *crypt)
  287. {
  288. struct rtllib_hdr_4addr *hdr;
  289. int res, hdrlen;
  290. if (crypt == NULL || crypt->ops->decrypt_msdu == NULL)
  291. return 0;
  292. if (ieee->hwsec_active) {
  293. struct cb_desc *tcb_desc = (struct cb_desc *)
  294. (skb->cb + MAX_DEV_ADDR_SIZE);
  295. tcb_desc->bHwSec = 1;
  296. if (ieee->need_sw_enc)
  297. tcb_desc->bHwSec = 0;
  298. }
  299. hdr = (struct rtllib_hdr_4addr *) skb->data;
  300. hdrlen = rtllib_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
  301. atomic_inc(&crypt->refcnt);
  302. res = crypt->ops->decrypt_msdu(skb, keyidx, hdrlen, crypt->priv);
  303. atomic_dec(&crypt->refcnt);
  304. if (res < 0) {
  305. netdev_dbg(ieee->dev,
  306. "MSDU decryption/MIC verification failed (SA= %pM keyidx=%d)\n",
  307. hdr->addr2, keyidx);
  308. return -1;
  309. }
  310. return 0;
  311. }
  312. /* this function is stolen from ipw2200 driver*/
  313. #define IEEE_PACKET_RETRY_TIME (5*HZ)
  314. static int is_duplicate_packet(struct rtllib_device *ieee,
  315. struct rtllib_hdr_4addr *header)
  316. {
  317. u16 fc = le16_to_cpu(header->frame_ctl);
  318. u16 sc = le16_to_cpu(header->seq_ctl);
  319. u16 seq = WLAN_GET_SEQ_SEQ(sc);
  320. u16 frag = WLAN_GET_SEQ_FRAG(sc);
  321. u16 *last_seq, *last_frag;
  322. unsigned long *last_time;
  323. struct rtllib_hdr_3addrqos *hdr_3addrqos;
  324. struct rtllib_hdr_4addrqos *hdr_4addrqos;
  325. u8 tid;
  326. if (((fc & RTLLIB_FCTL_DSTODS) == RTLLIB_FCTL_DSTODS) &&
  327. RTLLIB_QOS_HAS_SEQ(fc)) {
  328. hdr_4addrqos = (struct rtllib_hdr_4addrqos *)header;
  329. tid = le16_to_cpu(hdr_4addrqos->qos_ctl) & RTLLIB_QCTL_TID;
  330. tid = UP2AC(tid);
  331. tid++;
  332. } else if (RTLLIB_QOS_HAS_SEQ(fc)) {
  333. hdr_3addrqos = (struct rtllib_hdr_3addrqos *)header;
  334. tid = le16_to_cpu(hdr_3addrqos->qos_ctl) & RTLLIB_QCTL_TID;
  335. tid = UP2AC(tid);
  336. tid++;
  337. } else {
  338. tid = 0;
  339. }
  340. switch (ieee->iw_mode) {
  341. case IW_MODE_ADHOC:
  342. {
  343. struct list_head *p;
  344. struct ieee_ibss_seq *entry = NULL;
  345. u8 *mac = header->addr2;
  346. int index = mac[5] % IEEE_IBSS_MAC_HASH_SIZE;
  347. list_for_each(p, &ieee->ibss_mac_hash[index]) {
  348. entry = list_entry(p, struct ieee_ibss_seq, list);
  349. if (!memcmp(entry->mac, mac, ETH_ALEN))
  350. break;
  351. }
  352. if (p == &ieee->ibss_mac_hash[index]) {
  353. entry = kmalloc(sizeof(struct ieee_ibss_seq),
  354. GFP_ATOMIC);
  355. if (!entry)
  356. return 0;
  357. ether_addr_copy(entry->mac, mac);
  358. entry->seq_num[tid] = seq;
  359. entry->frag_num[tid] = frag;
  360. entry->packet_time[tid] = jiffies;
  361. list_add(&entry->list, &ieee->ibss_mac_hash[index]);
  362. return 0;
  363. }
  364. last_seq = &entry->seq_num[tid];
  365. last_frag = &entry->frag_num[tid];
  366. last_time = &entry->packet_time[tid];
  367. break;
  368. }
  369. case IW_MODE_INFRA:
  370. last_seq = &ieee->last_rxseq_num[tid];
  371. last_frag = &ieee->last_rxfrag_num[tid];
  372. last_time = &ieee->last_packet_time[tid];
  373. break;
  374. default:
  375. return 0;
  376. }
  377. if ((*last_seq == seq) &&
  378. time_after(*last_time + IEEE_PACKET_RETRY_TIME, jiffies)) {
  379. if (*last_frag == frag)
  380. goto drop;
  381. if (*last_frag + 1 != frag)
  382. /* out-of-order fragment */
  383. goto drop;
  384. } else
  385. *last_seq = seq;
  386. *last_frag = frag;
  387. *last_time = jiffies;
  388. return 0;
  389. drop:
  390. return 1;
  391. }
  392. static bool AddReorderEntry(struct rx_ts_record *pTS,
  393. struct rx_reorder_entry *pReorderEntry)
  394. {
  395. struct list_head *pList = &pTS->RxPendingPktList;
  396. while (pList->next != &pTS->RxPendingPktList) {
  397. if (SN_LESS(pReorderEntry->SeqNum, ((struct rx_reorder_entry *)
  398. list_entry(pList->next, struct rx_reorder_entry,
  399. List))->SeqNum))
  400. pList = pList->next;
  401. else if (SN_EQUAL(pReorderEntry->SeqNum,
  402. ((struct rx_reorder_entry *)list_entry(pList->next,
  403. struct rx_reorder_entry, List))->SeqNum))
  404. return false;
  405. else
  406. break;
  407. }
  408. pReorderEntry->List.next = pList->next;
  409. pReorderEntry->List.next->prev = &pReorderEntry->List;
  410. pReorderEntry->List.prev = pList;
  411. pList->next = &pReorderEntry->List;
  412. return true;
  413. }
  414. void rtllib_indicate_packets(struct rtllib_device *ieee,
  415. struct rtllib_rxb **prxbIndicateArray, u8 index)
  416. {
  417. struct net_device_stats *stats = &ieee->stats;
  418. u8 i = 0, j = 0;
  419. u16 ethertype;
  420. for (j = 0; j < index; j++) {
  421. struct rtllib_rxb *prxb = prxbIndicateArray[j];
  422. for (i = 0; i < prxb->nr_subframes; i++) {
  423. struct sk_buff *sub_skb = prxb->subframes[i];
  424. /* convert hdr + possible LLC headers into Ethernet header */
  425. ethertype = (sub_skb->data[6] << 8) | sub_skb->data[7];
  426. if (sub_skb->len >= 8 &&
  427. ((memcmp(sub_skb->data, rfc1042_header,
  428. SNAP_SIZE) == 0 &&
  429. ethertype != ETH_P_AARP &&
  430. ethertype != ETH_P_IPX) ||
  431. memcmp(sub_skb->data, bridge_tunnel_header,
  432. SNAP_SIZE) == 0)) {
  433. /* remove RFC1042 or Bridge-Tunnel encapsulation
  434. * and replace EtherType
  435. */
  436. skb_pull(sub_skb, SNAP_SIZE);
  437. memcpy(skb_push(sub_skb, ETH_ALEN), prxb->src, ETH_ALEN);
  438. memcpy(skb_push(sub_skb, ETH_ALEN), prxb->dst, ETH_ALEN);
  439. } else {
  440. u16 len;
  441. /* Leave Ethernet header part of hdr and full payload */
  442. len = sub_skb->len;
  443. memcpy(skb_push(sub_skb, 2), &len, 2);
  444. memcpy(skb_push(sub_skb, ETH_ALEN), prxb->src, ETH_ALEN);
  445. memcpy(skb_push(sub_skb, ETH_ALEN), prxb->dst, ETH_ALEN);
  446. }
  447. /* Indicate the packets to upper layer */
  448. if (sub_skb) {
  449. stats->rx_packets++;
  450. stats->rx_bytes += sub_skb->len;
  451. memset(sub_skb->cb, 0, sizeof(sub_skb->cb));
  452. sub_skb->protocol = eth_type_trans(sub_skb,
  453. ieee->dev);
  454. sub_skb->dev = ieee->dev;
  455. sub_skb->dev->stats.rx_packets++;
  456. sub_skb->dev->stats.rx_bytes += sub_skb->len;
  457. /* 802.11 crc not sufficient */
  458. sub_skb->ip_summed = CHECKSUM_NONE;
  459. ieee->last_rx_ps_time = jiffies;
  460. netif_rx(sub_skb);
  461. }
  462. }
  463. kfree(prxb);
  464. prxb = NULL;
  465. }
  466. }
  467. void rtllib_FlushRxTsPendingPkts(struct rtllib_device *ieee,
  468. struct rx_ts_record *pTS)
  469. {
  470. struct rx_reorder_entry *pRxReorderEntry;
  471. u8 RfdCnt = 0;
  472. del_timer_sync(&pTS->RxPktPendingTimer);
  473. while (!list_empty(&pTS->RxPendingPktList)) {
  474. if (RfdCnt >= REORDER_WIN_SIZE) {
  475. netdev_info(ieee->dev,
  476. "-------------->%s() error! RfdCnt >= REORDER_WIN_SIZE\n",
  477. __func__);
  478. break;
  479. }
  480. pRxReorderEntry = (struct rx_reorder_entry *)
  481. list_entry(pTS->RxPendingPktList.prev,
  482. struct rx_reorder_entry, List);
  483. netdev_dbg(ieee->dev, "%s(): Indicate SeqNum %d!\n", __func__,
  484. pRxReorderEntry->SeqNum);
  485. list_del_init(&pRxReorderEntry->List);
  486. ieee->RfdArray[RfdCnt] = pRxReorderEntry->prxb;
  487. RfdCnt = RfdCnt + 1;
  488. list_add_tail(&pRxReorderEntry->List,
  489. &ieee->RxReorder_Unused_List);
  490. }
  491. rtllib_indicate_packets(ieee, ieee->RfdArray, RfdCnt);
  492. pTS->RxIndicateSeq = 0xffff;
  493. }
  494. static void RxReorderIndicatePacket(struct rtllib_device *ieee,
  495. struct rtllib_rxb *prxb,
  496. struct rx_ts_record *pTS, u16 SeqNum)
  497. {
  498. struct rt_hi_throughput *pHTInfo = ieee->pHTInfo;
  499. struct rx_reorder_entry *pReorderEntry = NULL;
  500. u8 WinSize = pHTInfo->RxReorderWinSize;
  501. u16 WinEnd = 0;
  502. u8 index = 0;
  503. bool bMatchWinStart = false, bPktInBuf = false;
  504. unsigned long flags;
  505. netdev_dbg(ieee->dev,
  506. "%s(): Seq is %d, pTS->RxIndicateSeq is %d, WinSize is %d\n",
  507. __func__, SeqNum, pTS->RxIndicateSeq, WinSize);
  508. spin_lock_irqsave(&(ieee->reorder_spinlock), flags);
  509. WinEnd = (pTS->RxIndicateSeq + WinSize - 1) % 4096;
  510. /* Rx Reorder initialize condition.*/
  511. if (pTS->RxIndicateSeq == 0xffff)
  512. pTS->RxIndicateSeq = SeqNum;
  513. /* Drop out the packet which SeqNum is smaller than WinStart */
  514. if (SN_LESS(SeqNum, pTS->RxIndicateSeq)) {
  515. netdev_dbg(ieee->dev,
  516. "Packet Drop! IndicateSeq: %d, NewSeq: %d\n",
  517. pTS->RxIndicateSeq, SeqNum);
  518. pHTInfo->RxReorderDropCounter++;
  519. {
  520. int i;
  521. for (i = 0; i < prxb->nr_subframes; i++)
  522. dev_kfree_skb(prxb->subframes[i]);
  523. kfree(prxb);
  524. prxb = NULL;
  525. }
  526. spin_unlock_irqrestore(&(ieee->reorder_spinlock), flags);
  527. return;
  528. }
  529. /* Sliding window manipulation. Conditions includes:
  530. * 1. Incoming SeqNum is equal to WinStart =>Window shift 1
  531. * 2. Incoming SeqNum is larger than the WinEnd => Window shift N
  532. */
  533. if (SN_EQUAL(SeqNum, pTS->RxIndicateSeq)) {
  534. pTS->RxIndicateSeq = (pTS->RxIndicateSeq + 1) % 4096;
  535. bMatchWinStart = true;
  536. } else if (SN_LESS(WinEnd, SeqNum)) {
  537. if (SeqNum >= (WinSize - 1))
  538. pTS->RxIndicateSeq = SeqNum + 1 - WinSize;
  539. else
  540. pTS->RxIndicateSeq = 4095 -
  541. (WinSize - (SeqNum + 1)) + 1;
  542. netdev_dbg(ieee->dev,
  543. "Window Shift! IndicateSeq: %d, NewSeq: %d\n",
  544. pTS->RxIndicateSeq, SeqNum);
  545. }
  546. /* Indication process.
  547. * After Packet dropping and Sliding Window shifting as above, we can
  548. * now just indicate the packets with the SeqNum smaller than latest
  549. * WinStart and struct buffer other packets.
  550. *
  551. * For Rx Reorder condition:
  552. * 1. All packets with SeqNum smaller than WinStart => Indicate
  553. * 2. All packets with SeqNum larger than or equal to
  554. * WinStart => Buffer it.
  555. */
  556. if (bMatchWinStart) {
  557. /* Current packet is going to be indicated.*/
  558. netdev_dbg(ieee->dev,
  559. "Packets indication! IndicateSeq: %d, NewSeq: %d\n",
  560. pTS->RxIndicateSeq, SeqNum);
  561. ieee->prxbIndicateArray[0] = prxb;
  562. index = 1;
  563. } else {
  564. /* Current packet is going to be inserted into pending list.*/
  565. if (!list_empty(&ieee->RxReorder_Unused_List)) {
  566. pReorderEntry = (struct rx_reorder_entry *)
  567. list_entry(ieee->RxReorder_Unused_List.next,
  568. struct rx_reorder_entry, List);
  569. list_del_init(&pReorderEntry->List);
  570. /* Make a reorder entry and insert
  571. * into a the packet list.
  572. */
  573. pReorderEntry->SeqNum = SeqNum;
  574. pReorderEntry->prxb = prxb;
  575. if (!AddReorderEntry(pTS, pReorderEntry)) {
  576. int i;
  577. netdev_dbg(ieee->dev,
  578. "%s(): Duplicate packet is dropped. IndicateSeq: %d, NewSeq: %d\n",
  579. __func__, pTS->RxIndicateSeq,
  580. SeqNum);
  581. list_add_tail(&pReorderEntry->List,
  582. &ieee->RxReorder_Unused_List);
  583. for (i = 0; i < prxb->nr_subframes; i++)
  584. dev_kfree_skb(prxb->subframes[i]);
  585. kfree(prxb);
  586. prxb = NULL;
  587. } else {
  588. netdev_dbg(ieee->dev,
  589. "Pkt insert into struct buffer. IndicateSeq: %d, NewSeq: %d\n",
  590. pTS->RxIndicateSeq, SeqNum);
  591. }
  592. } else {
  593. /* Packets are dropped if there are not enough reorder
  594. * entries. This part should be modified!! We can just
  595. * indicate all the packets in struct buffer and get
  596. * reorder entries.
  597. */
  598. netdev_err(ieee->dev,
  599. "%s(): There is no reorder entry! Packet is dropped!\n",
  600. __func__);
  601. {
  602. int i;
  603. for (i = 0; i < prxb->nr_subframes; i++)
  604. dev_kfree_skb(prxb->subframes[i]);
  605. kfree(prxb);
  606. prxb = NULL;
  607. }
  608. }
  609. }
  610. /* Check if there is any packet need indicate.*/
  611. while (!list_empty(&pTS->RxPendingPktList)) {
  612. netdev_dbg(ieee->dev, "%s(): start RREORDER indicate\n",
  613. __func__);
  614. pReorderEntry = (struct rx_reorder_entry *)
  615. list_entry(pTS->RxPendingPktList.prev,
  616. struct rx_reorder_entry,
  617. List);
  618. if (SN_LESS(pReorderEntry->SeqNum, pTS->RxIndicateSeq) ||
  619. SN_EQUAL(pReorderEntry->SeqNum, pTS->RxIndicateSeq)) {
  620. /* This protect struct buffer from overflow. */
  621. if (index >= REORDER_WIN_SIZE) {
  622. netdev_err(ieee->dev,
  623. "%s(): Buffer overflow!\n",
  624. __func__);
  625. bPktInBuf = true;
  626. break;
  627. }
  628. list_del_init(&pReorderEntry->List);
  629. if (SN_EQUAL(pReorderEntry->SeqNum, pTS->RxIndicateSeq))
  630. pTS->RxIndicateSeq = (pTS->RxIndicateSeq + 1) %
  631. 4096;
  632. ieee->prxbIndicateArray[index] = pReorderEntry->prxb;
  633. netdev_dbg(ieee->dev, "%s(): Indicate SeqNum %d!\n",
  634. __func__, pReorderEntry->SeqNum);
  635. index++;
  636. list_add_tail(&pReorderEntry->List,
  637. &ieee->RxReorder_Unused_List);
  638. } else {
  639. bPktInBuf = true;
  640. break;
  641. }
  642. }
  643. /* Handling pending timer. Set this timer to prevent from long time
  644. * Rx buffering.
  645. */
  646. if (index > 0) {
  647. if (timer_pending(&pTS->RxPktPendingTimer))
  648. del_timer_sync(&pTS->RxPktPendingTimer);
  649. pTS->RxTimeoutIndicateSeq = 0xffff;
  650. if (index > REORDER_WIN_SIZE) {
  651. netdev_err(ieee->dev,
  652. "%s(): Rx Reorder struct buffer full!\n",
  653. __func__);
  654. spin_unlock_irqrestore(&(ieee->reorder_spinlock),
  655. flags);
  656. return;
  657. }
  658. rtllib_indicate_packets(ieee, ieee->prxbIndicateArray, index);
  659. bPktInBuf = false;
  660. }
  661. if (bPktInBuf && pTS->RxTimeoutIndicateSeq == 0xffff) {
  662. netdev_dbg(ieee->dev, "%s(): SET rx timeout timer\n", __func__);
  663. pTS->RxTimeoutIndicateSeq = pTS->RxIndicateSeq;
  664. mod_timer(&pTS->RxPktPendingTimer, jiffies +
  665. msecs_to_jiffies(pHTInfo->RxReorderPendingTime));
  666. }
  667. spin_unlock_irqrestore(&(ieee->reorder_spinlock), flags);
  668. }
  669. static u8 parse_subframe(struct rtllib_device *ieee, struct sk_buff *skb,
  670. struct rtllib_rx_stats *rx_stats,
  671. struct rtllib_rxb *rxb, u8 *src, u8 *dst)
  672. {
  673. struct rtllib_hdr_3addr *hdr = (struct rtllib_hdr_3addr *)skb->data;
  674. u16 fc = le16_to_cpu(hdr->frame_ctl);
  675. u16 LLCOffset = sizeof(struct rtllib_hdr_3addr);
  676. u16 ChkLength;
  677. bool bIsAggregateFrame = false;
  678. u16 nSubframe_Length;
  679. u8 nPadding_Length = 0;
  680. u16 SeqNum = 0;
  681. struct sk_buff *sub_skb;
  682. u8 *data_ptr;
  683. /* just for debug purpose */
  684. SeqNum = WLAN_GET_SEQ_SEQ(le16_to_cpu(hdr->seq_ctl));
  685. if ((RTLLIB_QOS_HAS_SEQ(fc)) &&
  686. (((union frameqos *)(skb->data + RTLLIB_3ADDR_LEN))->field.reserved))
  687. bIsAggregateFrame = true;
  688. if (RTLLIB_QOS_HAS_SEQ(fc))
  689. LLCOffset += 2;
  690. if (rx_stats->bContainHTC)
  691. LLCOffset += sHTCLng;
  692. ChkLength = LLCOffset;
  693. if (skb->len <= ChkLength)
  694. return 0;
  695. skb_pull(skb, LLCOffset);
  696. ieee->bIsAggregateFrame = bIsAggregateFrame;
  697. if (!bIsAggregateFrame) {
  698. rxb->nr_subframes = 1;
  699. /* altered by clark 3/30/2010
  700. * The struct buffer size of the skb indicated to upper layer
  701. * must be less than 5000, or the defraged IP datagram
  702. * in the IP layer will exceed "ipfrag_high_tresh" and be
  703. * discarded. so there must not use the function
  704. * "skb_copy" and "skb_clone" for "skb".
  705. */
  706. /* Allocate new skb for releasing to upper layer */
  707. sub_skb = dev_alloc_skb(RTLLIB_SKBBUFFER_SIZE);
  708. if (!sub_skb)
  709. return 0;
  710. skb_reserve(sub_skb, 12);
  711. data_ptr = (u8 *)skb_put(sub_skb, skb->len);
  712. memcpy(data_ptr, skb->data, skb->len);
  713. sub_skb->dev = ieee->dev;
  714. rxb->subframes[0] = sub_skb;
  715. memcpy(rxb->src, src, ETH_ALEN);
  716. memcpy(rxb->dst, dst, ETH_ALEN);
  717. rxb->subframes[0]->dev = ieee->dev;
  718. return 1;
  719. }
  720. rxb->nr_subframes = 0;
  721. memcpy(rxb->src, src, ETH_ALEN);
  722. memcpy(rxb->dst, dst, ETH_ALEN);
  723. while (skb->len > ETHERNET_HEADER_SIZE) {
  724. /* Offset 12 denote 2 mac address */
  725. nSubframe_Length = *((u16 *)(skb->data + 12));
  726. nSubframe_Length = (nSubframe_Length >> 8) +
  727. (nSubframe_Length << 8);
  728. if (skb->len < (ETHERNET_HEADER_SIZE + nSubframe_Length)) {
  729. netdev_info(ieee->dev,
  730. "%s: A-MSDU parse error!! pRfd->nTotalSubframe : %d\n",
  731. __func__, rxb->nr_subframes);
  732. netdev_info(ieee->dev,
  733. "%s: A-MSDU parse error!! Subframe Length: %d\n",
  734. __func__, nSubframe_Length);
  735. netdev_info(ieee->dev,
  736. "nRemain_Length is %d and nSubframe_Length is : %d\n",
  737. skb->len, nSubframe_Length);
  738. netdev_info(ieee->dev,
  739. "The Packet SeqNum is %d\n",
  740. SeqNum);
  741. return 0;
  742. }
  743. /* move the data point to data content */
  744. skb_pull(skb, ETHERNET_HEADER_SIZE);
  745. /* altered by clark 3/30/2010
  746. * The struct buffer size of the skb indicated to upper layer
  747. * must be less than 5000, or the defraged IP datagram
  748. * in the IP layer will exceed "ipfrag_high_tresh" and be
  749. * discarded. so there must not use the function
  750. * "skb_copy" and "skb_clone" for "skb".
  751. */
  752. /* Allocate new skb for releasing to upper layer */
  753. sub_skb = dev_alloc_skb(nSubframe_Length + 12);
  754. if (!sub_skb)
  755. return 0;
  756. skb_reserve(sub_skb, 12);
  757. data_ptr = (u8 *)skb_put(sub_skb, nSubframe_Length);
  758. memcpy(data_ptr, skb->data, nSubframe_Length);
  759. sub_skb->dev = ieee->dev;
  760. rxb->subframes[rxb->nr_subframes++] = sub_skb;
  761. if (rxb->nr_subframes >= MAX_SUBFRAME_COUNT) {
  762. netdev_dbg(ieee->dev,
  763. "ParseSubframe(): Too many Subframes! Packets dropped!\n");
  764. break;
  765. }
  766. skb_pull(skb, nSubframe_Length);
  767. if (skb->len != 0) {
  768. nPadding_Length = 4 - ((nSubframe_Length +
  769. ETHERNET_HEADER_SIZE) % 4);
  770. if (nPadding_Length == 4)
  771. nPadding_Length = 0;
  772. if (skb->len < nPadding_Length)
  773. return 0;
  774. skb_pull(skb, nPadding_Length);
  775. }
  776. }
  777. return rxb->nr_subframes;
  778. }
  779. static size_t rtllib_rx_get_hdrlen(struct rtllib_device *ieee,
  780. struct sk_buff *skb,
  781. struct rtllib_rx_stats *rx_stats)
  782. {
  783. struct rtllib_hdr_4addr *hdr = (struct rtllib_hdr_4addr *)skb->data;
  784. u16 fc = le16_to_cpu(hdr->frame_ctl);
  785. size_t hdrlen = 0;
  786. hdrlen = rtllib_get_hdrlen(fc);
  787. if (HTCCheck(ieee, skb->data)) {
  788. if (net_ratelimit())
  789. netdev_info(ieee->dev, "%s: find HTCControl!\n",
  790. __func__);
  791. hdrlen += 4;
  792. rx_stats->bContainHTC = true;
  793. }
  794. if (RTLLIB_QOS_HAS_SEQ(fc))
  795. rx_stats->bIsQosData = true;
  796. return hdrlen;
  797. }
  798. static int rtllib_rx_check_duplicate(struct rtllib_device *ieee,
  799. struct sk_buff *skb, u8 multicast)
  800. {
  801. struct rtllib_hdr_4addr *hdr = (struct rtllib_hdr_4addr *)skb->data;
  802. u16 fc, sc;
  803. u8 frag, type, stype;
  804. fc = le16_to_cpu(hdr->frame_ctl);
  805. type = WLAN_FC_GET_TYPE(fc);
  806. stype = WLAN_FC_GET_STYPE(fc);
  807. sc = le16_to_cpu(hdr->seq_ctl);
  808. frag = WLAN_GET_SEQ_FRAG(sc);
  809. if ((ieee->pHTInfo->bCurRxReorderEnable == false) ||
  810. !ieee->current_network.qos_data.active ||
  811. !IsDataFrame(skb->data) ||
  812. IsLegacyDataFrame(skb->data)) {
  813. if (!((type == RTLLIB_FTYPE_MGMT) &&
  814. (stype == RTLLIB_STYPE_BEACON))) {
  815. if (is_duplicate_packet(ieee, hdr))
  816. return -1;
  817. }
  818. } else {
  819. struct rx_ts_record *pRxTS = NULL;
  820. if (GetTs(ieee, (struct ts_common_info **) &pRxTS, hdr->addr2,
  821. (u8)Frame_QoSTID((u8 *)(skb->data)), RX_DIR, true)) {
  822. if ((fc & (1<<11)) && (frag == pRxTS->RxLastFragNum) &&
  823. (WLAN_GET_SEQ_SEQ(sc) == pRxTS->RxLastSeqNum))
  824. return -1;
  825. pRxTS->RxLastFragNum = frag;
  826. pRxTS->RxLastSeqNum = WLAN_GET_SEQ_SEQ(sc);
  827. } else {
  828. netdev_warn(ieee->dev, "%s(): No TS! Skip the check!\n",
  829. __func__);
  830. return -1;
  831. }
  832. }
  833. return 0;
  834. }
  835. static void rtllib_rx_extract_addr(struct rtllib_device *ieee,
  836. struct rtllib_hdr_4addr *hdr, u8 *dst,
  837. u8 *src, u8 *bssid)
  838. {
  839. u16 fc = le16_to_cpu(hdr->frame_ctl);
  840. switch (fc & (RTLLIB_FCTL_FROMDS | RTLLIB_FCTL_TODS)) {
  841. case RTLLIB_FCTL_FROMDS:
  842. ether_addr_copy(dst, hdr->addr1);
  843. ether_addr_copy(src, hdr->addr3);
  844. ether_addr_copy(bssid, hdr->addr2);
  845. break;
  846. case RTLLIB_FCTL_TODS:
  847. ether_addr_copy(dst, hdr->addr3);
  848. ether_addr_copy(src, hdr->addr2);
  849. ether_addr_copy(bssid, hdr->addr1);
  850. break;
  851. case RTLLIB_FCTL_FROMDS | RTLLIB_FCTL_TODS:
  852. ether_addr_copy(dst, hdr->addr3);
  853. ether_addr_copy(src, hdr->addr4);
  854. ether_addr_copy(bssid, ieee->current_network.bssid);
  855. break;
  856. case 0:
  857. ether_addr_copy(dst, hdr->addr1);
  858. ether_addr_copy(src, hdr->addr2);
  859. ether_addr_copy(bssid, hdr->addr3);
  860. break;
  861. }
  862. }
  863. static int rtllib_rx_data_filter(struct rtllib_device *ieee, u16 fc,
  864. u8 *dst, u8 *src, u8 *bssid, u8 *addr2)
  865. {
  866. u8 type, stype;
  867. type = WLAN_FC_GET_TYPE(fc);
  868. stype = WLAN_FC_GET_STYPE(fc);
  869. /* Filter frames from different BSS */
  870. if (((fc & RTLLIB_FCTL_DSTODS) != RTLLIB_FCTL_DSTODS) &&
  871. !ether_addr_equal(ieee->current_network.bssid, bssid) &&
  872. !is_zero_ether_addr(ieee->current_network.bssid)) {
  873. return -1;
  874. }
  875. /* Filter packets sent by an STA that will be forwarded by AP */
  876. if (ieee->IntelPromiscuousModeInfo.bPromiscuousOn &&
  877. ieee->IntelPromiscuousModeInfo.bFilterSourceStationFrame) {
  878. if ((fc & RTLLIB_FCTL_TODS) && !(fc & RTLLIB_FCTL_FROMDS) &&
  879. !ether_addr_equal(dst, ieee->current_network.bssid) &&
  880. ether_addr_equal(bssid, ieee->current_network.bssid)) {
  881. return -1;
  882. }
  883. }
  884. /* Nullfunc frames may have PS-bit set, so they must be passed to
  885. * hostap_handle_sta_rx() before being dropped here.
  886. */
  887. if (!ieee->IntelPromiscuousModeInfo.bPromiscuousOn) {
  888. if (stype != RTLLIB_STYPE_DATA &&
  889. stype != RTLLIB_STYPE_DATA_CFACK &&
  890. stype != RTLLIB_STYPE_DATA_CFPOLL &&
  891. stype != RTLLIB_STYPE_DATA_CFACKPOLL &&
  892. stype != RTLLIB_STYPE_QOS_DATA) {
  893. if (stype != RTLLIB_STYPE_NULLFUNC)
  894. netdev_dbg(ieee->dev,
  895. "RX: dropped data frame with no data (type=0x%02x, subtype=0x%02x)\n",
  896. type, stype);
  897. return -1;
  898. }
  899. }
  900. if (ieee->iw_mode != IW_MODE_MESH) {
  901. /* packets from our adapter are dropped (echo) */
  902. if (!memcmp(src, ieee->dev->dev_addr, ETH_ALEN))
  903. return -1;
  904. /* {broad,multi}cast packets to our BSS go through */
  905. if (is_multicast_ether_addr(dst)) {
  906. if (memcmp(bssid, ieee->current_network.bssid,
  907. ETH_ALEN))
  908. return -1;
  909. }
  910. }
  911. return 0;
  912. }
  913. static int rtllib_rx_get_crypt(struct rtllib_device *ieee, struct sk_buff *skb,
  914. struct lib80211_crypt_data **crypt, size_t hdrlen)
  915. {
  916. struct rtllib_hdr_4addr *hdr = (struct rtllib_hdr_4addr *)skb->data;
  917. u16 fc = le16_to_cpu(hdr->frame_ctl);
  918. int idx = 0;
  919. if (ieee->host_decrypt) {
  920. if (skb->len >= hdrlen + 3)
  921. idx = skb->data[hdrlen + 3] >> 6;
  922. *crypt = ieee->crypt_info.crypt[idx];
  923. /* allow NULL decrypt to indicate an station specific override
  924. * for default encryption
  925. */
  926. if (*crypt && ((*crypt)->ops == NULL ||
  927. (*crypt)->ops->decrypt_mpdu == NULL))
  928. *crypt = NULL;
  929. if (!*crypt && (fc & RTLLIB_FCTL_WEP)) {
  930. /* This seems to be triggered by some (multicast?)
  931. * frames from other than current BSS, so just drop the
  932. * frames silently instead of filling system log with
  933. * these reports.
  934. */
  935. netdev_dbg(ieee->dev,
  936. "Decryption failed (not set) (SA= %pM)\n",
  937. hdr->addr2);
  938. return -1;
  939. }
  940. }
  941. return 0;
  942. }
  943. static int rtllib_rx_decrypt(struct rtllib_device *ieee, struct sk_buff *skb,
  944. struct rtllib_rx_stats *rx_stats,
  945. struct lib80211_crypt_data *crypt, size_t hdrlen)
  946. {
  947. struct rtllib_hdr_4addr *hdr;
  948. int keyidx = 0;
  949. u16 fc, sc;
  950. u8 frag;
  951. hdr = (struct rtllib_hdr_4addr *)skb->data;
  952. fc = le16_to_cpu(hdr->frame_ctl);
  953. sc = le16_to_cpu(hdr->seq_ctl);
  954. frag = WLAN_GET_SEQ_FRAG(sc);
  955. if ((!rx_stats->Decrypted))
  956. ieee->need_sw_enc = 1;
  957. else
  958. ieee->need_sw_enc = 0;
  959. keyidx = rtllib_rx_frame_decrypt(ieee, skb, crypt);
  960. if (ieee->host_decrypt && (fc & RTLLIB_FCTL_WEP) && (keyidx < 0)) {
  961. netdev_info(ieee->dev, "%s: decrypt frame error\n", __func__);
  962. return -1;
  963. }
  964. hdr = (struct rtllib_hdr_4addr *) skb->data;
  965. if ((frag != 0 || (fc & RTLLIB_FCTL_MOREFRAGS))) {
  966. int flen;
  967. struct sk_buff *frag_skb = rtllib_frag_cache_get(ieee, hdr);
  968. netdev_dbg(ieee->dev, "Rx Fragment received (%u)\n", frag);
  969. if (!frag_skb) {
  970. netdev_dbg(ieee->dev,
  971. "Rx cannot get skb from fragment cache (morefrag=%d seq=%u frag=%u)\n",
  972. (fc & RTLLIB_FCTL_MOREFRAGS) != 0,
  973. WLAN_GET_SEQ_SEQ(sc), frag);
  974. return -1;
  975. }
  976. flen = skb->len;
  977. if (frag != 0)
  978. flen -= hdrlen;
  979. if (frag_skb->tail + flen > frag_skb->end) {
  980. netdev_warn(ieee->dev,
  981. "%s: host decrypted and reassembled frame did not fit skb\n",
  982. __func__);
  983. rtllib_frag_cache_invalidate(ieee, hdr);
  984. return -1;
  985. }
  986. if (frag == 0) {
  987. /* copy first fragment (including full headers) into
  988. * beginning of the fragment cache skb
  989. */
  990. memcpy(skb_put(frag_skb, flen), skb->data, flen);
  991. } else {
  992. /* append frame payload to the end of the fragment
  993. * cache skb
  994. */
  995. memcpy(skb_put(frag_skb, flen), skb->data + hdrlen,
  996. flen);
  997. }
  998. dev_kfree_skb_any(skb);
  999. skb = NULL;
  1000. if (fc & RTLLIB_FCTL_MOREFRAGS) {
  1001. /* more fragments expected - leave the skb in fragment
  1002. * cache for now; it will be delivered to upper layers
  1003. * after all fragments have been received
  1004. */
  1005. return -2;
  1006. }
  1007. /* this was the last fragment and the frame will be
  1008. * delivered, so remove skb from fragment cache
  1009. */
  1010. skb = frag_skb;
  1011. hdr = (struct rtllib_hdr_4addr *) skb->data;
  1012. rtllib_frag_cache_invalidate(ieee, hdr);
  1013. }
  1014. /* skb: hdr + (possible reassembled) full MSDU payload; possibly still
  1015. * encrypted/authenticated
  1016. */
  1017. if (ieee->host_decrypt && (fc & RTLLIB_FCTL_WEP) &&
  1018. rtllib_rx_frame_decrypt_msdu(ieee, skb, keyidx, crypt)) {
  1019. netdev_info(ieee->dev, "%s: ==>decrypt msdu error\n", __func__);
  1020. return -1;
  1021. }
  1022. hdr = (struct rtllib_hdr_4addr *) skb->data;
  1023. if (crypt && !(fc & RTLLIB_FCTL_WEP) && !ieee->open_wep) {
  1024. if (/*ieee->ieee802_1x &&*/
  1025. rtllib_is_eapol_frame(ieee, skb, hdrlen)) {
  1026. /* pass unencrypted EAPOL frames even if encryption is
  1027. * configured
  1028. */
  1029. struct eapol *eap = (struct eapol *)(skb->data +
  1030. 24);
  1031. netdev_dbg(ieee->dev,
  1032. "RX: IEEE 802.1X EAPOL frame: %s\n",
  1033. eap_get_type(eap->type));
  1034. } else {
  1035. netdev_dbg(ieee->dev,
  1036. "encryption configured, but RX frame not encrypted (SA= %pM)\n",
  1037. hdr->addr2);
  1038. return -1;
  1039. }
  1040. }
  1041. if (crypt && !(fc & RTLLIB_FCTL_WEP) &&
  1042. rtllib_is_eapol_frame(ieee, skb, hdrlen)) {
  1043. struct eapol *eap = (struct eapol *)(skb->data + 24);
  1044. netdev_dbg(ieee->dev, "RX: IEEE 802.1X EAPOL frame: %s\n",
  1045. eap_get_type(eap->type));
  1046. }
  1047. if (crypt && !(fc & RTLLIB_FCTL_WEP) && !ieee->open_wep &&
  1048. !rtllib_is_eapol_frame(ieee, skb, hdrlen)) {
  1049. netdev_dbg(ieee->dev,
  1050. "dropped unencrypted RX data frame from %pM (drop_unencrypted=1)\n",
  1051. hdr->addr2);
  1052. return -1;
  1053. }
  1054. if (rtllib_is_eapol_frame(ieee, skb, hdrlen))
  1055. netdev_warn(ieee->dev, "RX: IEEE802.1X EAPOL frame!\n");
  1056. return 0;
  1057. }
  1058. static void rtllib_rx_check_leave_lps(struct rtllib_device *ieee, u8 unicast,
  1059. u8 nr_subframes)
  1060. {
  1061. if (unicast) {
  1062. if (ieee->state == RTLLIB_LINKED) {
  1063. if (((ieee->LinkDetectInfo.NumRxUnicastOkInPeriod +
  1064. ieee->LinkDetectInfo.NumTxOkInPeriod) > 8) ||
  1065. (ieee->LinkDetectInfo.NumRxUnicastOkInPeriod > 2)) {
  1066. if (ieee->LeisurePSLeave)
  1067. ieee->LeisurePSLeave(ieee->dev);
  1068. }
  1069. }
  1070. }
  1071. ieee->last_rx_ps_time = jiffies;
  1072. }
  1073. static void rtllib_rx_indicate_pkt_legacy(struct rtllib_device *ieee,
  1074. struct rtllib_rx_stats *rx_stats,
  1075. struct rtllib_rxb *rxb,
  1076. u8 *dst,
  1077. u8 *src)
  1078. {
  1079. struct net_device *dev = ieee->dev;
  1080. u16 ethertype;
  1081. int i = 0;
  1082. if (rxb == NULL) {
  1083. netdev_info(dev, "%s: rxb is NULL!!\n", __func__);
  1084. return;
  1085. }
  1086. for (i = 0; i < rxb->nr_subframes; i++) {
  1087. struct sk_buff *sub_skb = rxb->subframes[i];
  1088. if (sub_skb) {
  1089. /* convert hdr + possible LLC headers
  1090. * into Ethernet header
  1091. */
  1092. ethertype = (sub_skb->data[6] << 8) | sub_skb->data[7];
  1093. if (sub_skb->len >= 8 &&
  1094. ((memcmp(sub_skb->data, rfc1042_header, SNAP_SIZE) == 0 &&
  1095. ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
  1096. memcmp(sub_skb->data, bridge_tunnel_header, SNAP_SIZE) == 0)) {
  1097. /* remove RFC1042 or Bridge-Tunnel encapsulation
  1098. * and replace EtherType
  1099. */
  1100. skb_pull(sub_skb, SNAP_SIZE);
  1101. ether_addr_copy(skb_push(sub_skb, ETH_ALEN),
  1102. src);
  1103. ether_addr_copy(skb_push(sub_skb, ETH_ALEN),
  1104. dst);
  1105. } else {
  1106. u16 len;
  1107. /* Leave Ethernet header part of hdr
  1108. * and full payload
  1109. */
  1110. len = sub_skb->len;
  1111. memcpy(skb_push(sub_skb, 2), &len, 2);
  1112. ether_addr_copy(skb_push(sub_skb, ETH_ALEN),
  1113. src);
  1114. ether_addr_copy(skb_push(sub_skb, ETH_ALEN),
  1115. dst);
  1116. }
  1117. ieee->stats.rx_packets++;
  1118. ieee->stats.rx_bytes += sub_skb->len;
  1119. if (is_multicast_ether_addr(dst))
  1120. ieee->stats.multicast++;
  1121. /* Indicate the packets to upper layer */
  1122. memset(sub_skb->cb, 0, sizeof(sub_skb->cb));
  1123. sub_skb->protocol = eth_type_trans(sub_skb, dev);
  1124. sub_skb->dev = dev;
  1125. sub_skb->dev->stats.rx_packets++;
  1126. sub_skb->dev->stats.rx_bytes += sub_skb->len;
  1127. /* 802.11 crc not sufficient */
  1128. sub_skb->ip_summed = CHECKSUM_NONE;
  1129. netif_rx(sub_skb);
  1130. }
  1131. }
  1132. kfree(rxb);
  1133. }
  1134. static int rtllib_rx_InfraAdhoc(struct rtllib_device *ieee, struct sk_buff *skb,
  1135. struct rtllib_rx_stats *rx_stats)
  1136. {
  1137. struct net_device *dev = ieee->dev;
  1138. struct rtllib_hdr_4addr *hdr = (struct rtllib_hdr_4addr *)skb->data;
  1139. struct lib80211_crypt_data *crypt = NULL;
  1140. struct rtllib_rxb *rxb = NULL;
  1141. struct rx_ts_record *pTS = NULL;
  1142. u16 fc, sc, SeqNum = 0;
  1143. u8 type, stype, multicast = 0, unicast = 0, nr_subframes = 0, TID = 0;
  1144. u8 *payload;
  1145. u8 dst[ETH_ALEN];
  1146. u8 src[ETH_ALEN];
  1147. u8 bssid[ETH_ALEN] = {0};
  1148. size_t hdrlen = 0;
  1149. bool bToOtherSTA = false;
  1150. int ret = 0, i = 0;
  1151. hdr = (struct rtllib_hdr_4addr *)skb->data;
  1152. fc = le16_to_cpu(hdr->frame_ctl);
  1153. type = WLAN_FC_GET_TYPE(fc);
  1154. stype = WLAN_FC_GET_STYPE(fc);
  1155. sc = le16_to_cpu(hdr->seq_ctl);
  1156. /*Filter pkt not to me*/
  1157. multicast = is_multicast_ether_addr(hdr->addr1);
  1158. unicast = !multicast;
  1159. if (unicast && !ether_addr_equal(dev->dev_addr, hdr->addr1)) {
  1160. if (ieee->bNetPromiscuousMode)
  1161. bToOtherSTA = true;
  1162. else
  1163. goto rx_dropped;
  1164. }
  1165. /*Filter pkt has too small length */
  1166. hdrlen = rtllib_rx_get_hdrlen(ieee, skb, rx_stats);
  1167. if (skb->len < hdrlen) {
  1168. netdev_info(dev,
  1169. "%s():ERR!!! skb->len is smaller than hdrlen\n",
  1170. __func__);
  1171. goto rx_dropped;
  1172. }
  1173. /* Filter Duplicate pkt */
  1174. ret = rtllib_rx_check_duplicate(ieee, skb, multicast);
  1175. if (ret < 0)
  1176. goto rx_dropped;
  1177. /* Filter CTRL Frame */
  1178. if (type == RTLLIB_FTYPE_CTL)
  1179. goto rx_dropped;
  1180. /* Filter MGNT Frame */
  1181. if (type == RTLLIB_FTYPE_MGMT) {
  1182. if (bToOtherSTA)
  1183. goto rx_dropped;
  1184. if (rtllib_rx_frame_mgmt(ieee, skb, rx_stats, type, stype))
  1185. goto rx_dropped;
  1186. else
  1187. goto rx_exit;
  1188. }
  1189. /* Filter WAPI DATA Frame */
  1190. /* Update statstics for AP roaming */
  1191. if (!bToOtherSTA) {
  1192. ieee->LinkDetectInfo.NumRecvDataInPeriod++;
  1193. ieee->LinkDetectInfo.NumRxOkInPeriod++;
  1194. }
  1195. dev->last_rx = jiffies;
  1196. /* Data frame - extract src/dst addresses */
  1197. rtllib_rx_extract_addr(ieee, hdr, dst, src, bssid);
  1198. /* Filter Data frames */
  1199. ret = rtllib_rx_data_filter(ieee, fc, dst, src, bssid, hdr->addr2);
  1200. if (ret < 0)
  1201. goto rx_dropped;
  1202. if (skb->len == hdrlen)
  1203. goto rx_dropped;
  1204. /* Send pspoll based on moredata */
  1205. if ((ieee->iw_mode == IW_MODE_INFRA) &&
  1206. (ieee->sta_sleep == LPS_IS_SLEEP) &&
  1207. (ieee->polling) && (!bToOtherSTA)) {
  1208. if (WLAN_FC_MORE_DATA(fc)) {
  1209. /* more data bit is set, let's request a new frame
  1210. * from the AP
  1211. */
  1212. rtllib_sta_ps_send_pspoll_frame(ieee);
  1213. } else {
  1214. ieee->polling = false;
  1215. }
  1216. }
  1217. /* Get crypt if encrypted */
  1218. ret = rtllib_rx_get_crypt(ieee, skb, &crypt, hdrlen);
  1219. if (ret == -1)
  1220. goto rx_dropped;
  1221. /* Decrypt data frame (including reassemble) */
  1222. ret = rtllib_rx_decrypt(ieee, skb, rx_stats, crypt, hdrlen);
  1223. if (ret == -1)
  1224. goto rx_dropped;
  1225. else if (ret == -2)
  1226. goto rx_exit;
  1227. /* Get TS for Rx Reorder */
  1228. hdr = (struct rtllib_hdr_4addr *) skb->data;
  1229. if (ieee->current_network.qos_data.active && IsQoSDataFrame(skb->data)
  1230. && !is_multicast_ether_addr(hdr->addr1)
  1231. && (!bToOtherSTA)) {
  1232. TID = Frame_QoSTID(skb->data);
  1233. SeqNum = WLAN_GET_SEQ_SEQ(sc);
  1234. GetTs(ieee, (struct ts_common_info **) &pTS, hdr->addr2, TID,
  1235. RX_DIR, true);
  1236. if (TID != 0 && TID != 3)
  1237. ieee->bis_any_nonbepkts = true;
  1238. }
  1239. /* Parse rx data frame (For AMSDU) */
  1240. /* skb: hdr + (possible reassembled) full plaintext payload */
  1241. payload = skb->data + hdrlen;
  1242. rxb = kmalloc(sizeof(struct rtllib_rxb), GFP_ATOMIC);
  1243. if (rxb == NULL)
  1244. goto rx_dropped;
  1245. /* to parse amsdu packets */
  1246. /* qos data packets & reserved bit is 1 */
  1247. if (parse_subframe(ieee, skb, rx_stats, rxb, src, dst) == 0) {
  1248. /* only to free rxb, and not submit the packets
  1249. * to upper layer
  1250. */
  1251. for (i = 0; i < rxb->nr_subframes; i++)
  1252. dev_kfree_skb(rxb->subframes[i]);
  1253. kfree(rxb);
  1254. rxb = NULL;
  1255. goto rx_dropped;
  1256. }
  1257. /* Update WAPI PN */
  1258. /* Check if leave LPS */
  1259. if (!bToOtherSTA) {
  1260. if (ieee->bIsAggregateFrame)
  1261. nr_subframes = rxb->nr_subframes;
  1262. else
  1263. nr_subframes = 1;
  1264. if (unicast)
  1265. ieee->LinkDetectInfo.NumRxUnicastOkInPeriod += nr_subframes;
  1266. rtllib_rx_check_leave_lps(ieee, unicast, nr_subframes);
  1267. }
  1268. /* Indicate packets to upper layer or Rx Reorder */
  1269. if (ieee->pHTInfo->bCurRxReorderEnable == false || pTS == NULL ||
  1270. bToOtherSTA)
  1271. rtllib_rx_indicate_pkt_legacy(ieee, rx_stats, rxb, dst, src);
  1272. else
  1273. RxReorderIndicatePacket(ieee, rxb, pTS, SeqNum);
  1274. dev_kfree_skb(skb);
  1275. rx_exit:
  1276. return 1;
  1277. rx_dropped:
  1278. ieee->stats.rx_dropped++;
  1279. /* Returning 0 indicates to caller that we have not handled the SKB--
  1280. * so it is still allocated and can be used again by underlying
  1281. * hardware as a DMA target
  1282. */
  1283. return 0;
  1284. }
  1285. static int rtllib_rx_Master(struct rtllib_device *ieee, struct sk_buff *skb,
  1286. struct rtllib_rx_stats *rx_stats)
  1287. {
  1288. return 0;
  1289. }
  1290. static int rtllib_rx_Monitor(struct rtllib_device *ieee, struct sk_buff *skb,
  1291. struct rtllib_rx_stats *rx_stats)
  1292. {
  1293. struct rtllib_hdr_4addr *hdr = (struct rtllib_hdr_4addr *)skb->data;
  1294. u16 fc = le16_to_cpu(hdr->frame_ctl);
  1295. size_t hdrlen = rtllib_get_hdrlen(fc);
  1296. if (skb->len < hdrlen) {
  1297. netdev_info(ieee->dev,
  1298. "%s():ERR!!! skb->len is smaller than hdrlen\n",
  1299. __func__);
  1300. return 0;
  1301. }
  1302. if (HTCCheck(ieee, skb->data)) {
  1303. if (net_ratelimit())
  1304. netdev_info(ieee->dev, "%s: Find HTCControl!\n",
  1305. __func__);
  1306. hdrlen += 4;
  1307. }
  1308. rtllib_monitor_rx(ieee, skb, rx_stats, hdrlen);
  1309. ieee->stats.rx_packets++;
  1310. ieee->stats.rx_bytes += skb->len;
  1311. return 1;
  1312. }
  1313. static int rtllib_rx_Mesh(struct rtllib_device *ieee, struct sk_buff *skb,
  1314. struct rtllib_rx_stats *rx_stats)
  1315. {
  1316. return 0;
  1317. }
  1318. /* All received frames are sent to this function. @skb contains the frame in
  1319. * IEEE 802.11 format, i.e., in the format it was sent over air.
  1320. * This function is called only as a tasklet (software IRQ).
  1321. */
  1322. int rtllib_rx(struct rtllib_device *ieee, struct sk_buff *skb,
  1323. struct rtllib_rx_stats *rx_stats)
  1324. {
  1325. int ret = 0;
  1326. if (!ieee || !skb || !rx_stats) {
  1327. pr_info("%s: Input parameters NULL!\n", __func__);
  1328. goto rx_dropped;
  1329. }
  1330. if (skb->len < 10) {
  1331. netdev_info(ieee->dev, "%s: SKB length < 10\n", __func__);
  1332. goto rx_dropped;
  1333. }
  1334. switch (ieee->iw_mode) {
  1335. case IW_MODE_ADHOC:
  1336. case IW_MODE_INFRA:
  1337. ret = rtllib_rx_InfraAdhoc(ieee, skb, rx_stats);
  1338. break;
  1339. case IW_MODE_MASTER:
  1340. case IW_MODE_REPEAT:
  1341. ret = rtllib_rx_Master(ieee, skb, rx_stats);
  1342. break;
  1343. case IW_MODE_MONITOR:
  1344. ret = rtllib_rx_Monitor(ieee, skb, rx_stats);
  1345. break;
  1346. case IW_MODE_MESH:
  1347. ret = rtllib_rx_Mesh(ieee, skb, rx_stats);
  1348. break;
  1349. default:
  1350. netdev_info(ieee->dev, "%s: ERR iw mode!!!\n", __func__);
  1351. break;
  1352. }
  1353. return ret;
  1354. rx_dropped:
  1355. if (ieee)
  1356. ieee->stats.rx_dropped++;
  1357. return 0;
  1358. }
  1359. EXPORT_SYMBOL(rtllib_rx);
  1360. static u8 qos_oui[QOS_OUI_LEN] = { 0x00, 0x50, 0xF2 };
  1361. /* Make ther structure we read from the beacon packet has the right values */
  1362. static int rtllib_verify_qos_info(struct rtllib_qos_information_element
  1363. *info_element, int sub_type)
  1364. {
  1365. if (info_element->qui_subtype != sub_type)
  1366. return -1;
  1367. if (memcmp(info_element->qui, qos_oui, QOS_OUI_LEN))
  1368. return -1;
  1369. if (info_element->qui_type != QOS_OUI_TYPE)
  1370. return -1;
  1371. if (info_element->version != QOS_VERSION_1)
  1372. return -1;
  1373. return 0;
  1374. }
  1375. /* Parse a QoS parameter element */
  1376. static int rtllib_read_qos_param_element(struct rtllib_qos_parameter_info
  1377. *element_param,
  1378. struct rtllib_info_element
  1379. *info_element)
  1380. {
  1381. int ret = 0;
  1382. u16 size = sizeof(struct rtllib_qos_parameter_info) - 2;
  1383. if ((info_element == NULL) || (element_param == NULL))
  1384. return -1;
  1385. if (info_element->id == QOS_ELEMENT_ID && info_element->len == size) {
  1386. memcpy(element_param->info_element.qui, info_element->data,
  1387. info_element->len);
  1388. element_param->info_element.elementID = info_element->id;
  1389. element_param->info_element.length = info_element->len;
  1390. } else
  1391. ret = -1;
  1392. if (ret == 0)
  1393. ret = rtllib_verify_qos_info(&element_param->info_element,
  1394. QOS_OUI_PARAM_SUB_TYPE);
  1395. return ret;
  1396. }
  1397. /* Parse a QoS information element */
  1398. static int rtllib_read_qos_info_element(struct rtllib_qos_information_element
  1399. *element_info,
  1400. struct rtllib_info_element
  1401. *info_element)
  1402. {
  1403. int ret = 0;
  1404. u16 size = sizeof(struct rtllib_qos_information_element) - 2;
  1405. if (element_info == NULL)
  1406. return -1;
  1407. if (info_element == NULL)
  1408. return -1;
  1409. if ((info_element->id == QOS_ELEMENT_ID) &&
  1410. (info_element->len == size)) {
  1411. memcpy(element_info->qui, info_element->data,
  1412. info_element->len);
  1413. element_info->elementID = info_element->id;
  1414. element_info->length = info_element->len;
  1415. } else
  1416. ret = -1;
  1417. if (ret == 0)
  1418. ret = rtllib_verify_qos_info(element_info,
  1419. QOS_OUI_INFO_SUB_TYPE);
  1420. return ret;
  1421. }
  1422. /* Write QoS parameters from the ac parameters. */
  1423. static int rtllib_qos_convert_ac_to_parameters(struct rtllib_qos_parameter_info *param_elm,
  1424. struct rtllib_qos_data *qos_data)
  1425. {
  1426. struct rtllib_qos_ac_parameter *ac_params;
  1427. struct rtllib_qos_parameters *qos_param = &(qos_data->parameters);
  1428. int i;
  1429. u8 aci;
  1430. u8 acm;
  1431. qos_data->wmm_acm = 0;
  1432. for (i = 0; i < QOS_QUEUE_NUM; i++) {
  1433. ac_params = &(param_elm->ac_params_record[i]);
  1434. aci = (ac_params->aci_aifsn & 0x60) >> 5;
  1435. acm = (ac_params->aci_aifsn & 0x10) >> 4;
  1436. if (aci >= QOS_QUEUE_NUM)
  1437. continue;
  1438. switch (aci) {
  1439. case 1:
  1440. /* BIT(0) | BIT(3) */
  1441. if (acm)
  1442. qos_data->wmm_acm |= (0x01<<0)|(0x01<<3);
  1443. break;
  1444. case 2:
  1445. /* BIT(4) | BIT(5) */
  1446. if (acm)
  1447. qos_data->wmm_acm |= (0x01<<4)|(0x01<<5);
  1448. break;
  1449. case 3:
  1450. /* BIT(6) | BIT(7) */
  1451. if (acm)
  1452. qos_data->wmm_acm |= (0x01<<6)|(0x01<<7);
  1453. break;
  1454. case 0:
  1455. default:
  1456. /* BIT(1) | BIT(2) */
  1457. if (acm)
  1458. qos_data->wmm_acm |= (0x01<<1)|(0x01<<2);
  1459. break;
  1460. }
  1461. qos_param->aifs[aci] = (ac_params->aci_aifsn) & 0x0f;
  1462. /* WMM spec P.11: The minimum value for AIFSN shall be 2 */
  1463. qos_param->aifs[aci] = max_t(u8, qos_param->aifs[aci], 2);
  1464. qos_param->cw_min[aci] = cpu_to_le16(ac_params->ecw_min_max &
  1465. 0x0F);
  1466. qos_param->cw_max[aci] = cpu_to_le16((ac_params->ecw_min_max &
  1467. 0xF0) >> 4);
  1468. qos_param->flag[aci] =
  1469. (ac_params->aci_aifsn & 0x10) ? 0x01 : 0x00;
  1470. qos_param->tx_op_limit[aci] = ac_params->tx_op_limit;
  1471. }
  1472. return 0;
  1473. }
  1474. /* we have a generic data element which it may contain QoS information or
  1475. * parameters element. check the information element length to decide
  1476. * which type to read
  1477. */
  1478. static int rtllib_parse_qos_info_param_IE(struct rtllib_device *ieee,
  1479. struct rtllib_info_element
  1480. *info_element,
  1481. struct rtllib_network *network)
  1482. {
  1483. int rc = 0;
  1484. struct rtllib_qos_information_element qos_info_element;
  1485. rc = rtllib_read_qos_info_element(&qos_info_element, info_element);
  1486. if (rc == 0) {
  1487. network->qos_data.param_count = qos_info_element.ac_info & 0x0F;
  1488. network->flags |= NETWORK_HAS_QOS_INFORMATION;
  1489. } else {
  1490. struct rtllib_qos_parameter_info param_element;
  1491. rc = rtllib_read_qos_param_element(&param_element,
  1492. info_element);
  1493. if (rc == 0) {
  1494. rtllib_qos_convert_ac_to_parameters(&param_element,
  1495. &(network->qos_data));
  1496. network->flags |= NETWORK_HAS_QOS_PARAMETERS;
  1497. network->qos_data.param_count =
  1498. param_element.info_element.ac_info & 0x0F;
  1499. }
  1500. }
  1501. if (rc == 0) {
  1502. netdev_dbg(ieee->dev, "QoS is supported\n");
  1503. network->qos_data.supported = 1;
  1504. }
  1505. return rc;
  1506. }
  1507. static const char *get_info_element_string(u16 id)
  1508. {
  1509. switch (id) {
  1510. case MFIE_TYPE_SSID:
  1511. return "SSID";
  1512. case MFIE_TYPE_RATES:
  1513. return "RATES";
  1514. case MFIE_TYPE_FH_SET:
  1515. return "FH_SET";
  1516. case MFIE_TYPE_DS_SET:
  1517. return "DS_SET";
  1518. case MFIE_TYPE_CF_SET:
  1519. return "CF_SET";
  1520. case MFIE_TYPE_TIM:
  1521. return "TIM";
  1522. case MFIE_TYPE_IBSS_SET:
  1523. return "IBSS_SET";
  1524. case MFIE_TYPE_COUNTRY:
  1525. return "COUNTRY";
  1526. case MFIE_TYPE_HOP_PARAMS:
  1527. return "HOP_PARAMS";
  1528. case MFIE_TYPE_HOP_TABLE:
  1529. return "HOP_TABLE";
  1530. case MFIE_TYPE_REQUEST:
  1531. return "REQUEST";
  1532. case MFIE_TYPE_CHALLENGE:
  1533. return "CHALLENGE";
  1534. case MFIE_TYPE_POWER_CONSTRAINT:
  1535. return "POWER_CONSTRAINT";
  1536. case MFIE_TYPE_POWER_CAPABILITY:
  1537. return "POWER_CAPABILITY";
  1538. case MFIE_TYPE_TPC_REQUEST:
  1539. return "TPC_REQUEST";
  1540. case MFIE_TYPE_TPC_REPORT:
  1541. return "TPC_REPORT";
  1542. case MFIE_TYPE_SUPP_CHANNELS:
  1543. return "SUPP_CHANNELS";
  1544. case MFIE_TYPE_CSA:
  1545. return "CSA";
  1546. case MFIE_TYPE_MEASURE_REQUEST:
  1547. return "MEASURE_REQUEST";
  1548. case MFIE_TYPE_MEASURE_REPORT:
  1549. return "MEASURE_REPORT";
  1550. case MFIE_TYPE_QUIET:
  1551. return "QUIET";
  1552. case MFIE_TYPE_IBSS_DFS:
  1553. return "IBSS_DFS";
  1554. case MFIE_TYPE_RSN:
  1555. return "RSN";
  1556. case MFIE_TYPE_RATES_EX:
  1557. return "RATES_EX";
  1558. case MFIE_TYPE_GENERIC:
  1559. return "GENERIC";
  1560. case MFIE_TYPE_QOS_PARAMETER:
  1561. return "QOS_PARAMETER";
  1562. default:
  1563. return "UNKNOWN";
  1564. }
  1565. }
  1566. static inline void rtllib_extract_country_ie(
  1567. struct rtllib_device *ieee,
  1568. struct rtllib_info_element *info_element,
  1569. struct rtllib_network *network,
  1570. u8 *addr2)
  1571. {
  1572. if (IS_DOT11D_ENABLE(ieee)) {
  1573. if (info_element->len != 0) {
  1574. memcpy(network->CountryIeBuf, info_element->data,
  1575. info_element->len);
  1576. network->CountryIeLen = info_element->len;
  1577. if (!IS_COUNTRY_IE_VALID(ieee)) {
  1578. if (rtllib_act_scanning(ieee, false) &&
  1579. ieee->FirstIe_InScan)
  1580. netdev_info(ieee->dev,
  1581. "Received beacon ContryIE, SSID: <%s>\n",
  1582. network->ssid);
  1583. Dot11d_UpdateCountryIe(ieee, addr2,
  1584. info_element->len,
  1585. info_element->data);
  1586. }
  1587. }
  1588. if (IS_EQUAL_CIE_SRC(ieee, addr2))
  1589. UPDATE_CIE_WATCHDOG(ieee);
  1590. }
  1591. }
  1592. static void rtllib_parse_mife_generic(struct rtllib_device *ieee,
  1593. struct rtllib_info_element *info_element,
  1594. struct rtllib_network *network,
  1595. u16 *tmp_htcap_len,
  1596. u16 *tmp_htinfo_len)
  1597. {
  1598. u16 ht_realtek_agg_len = 0;
  1599. u8 ht_realtek_agg_buf[MAX_IE_LEN];
  1600. if (!rtllib_parse_qos_info_param_IE(ieee, info_element, network))
  1601. return;
  1602. if (info_element->len >= 4 &&
  1603. info_element->data[0] == 0x00 &&
  1604. info_element->data[1] == 0x50 &&
  1605. info_element->data[2] == 0xf2 &&
  1606. info_element->data[3] == 0x01) {
  1607. network->wpa_ie_len = min(info_element->len + 2,
  1608. MAX_WPA_IE_LEN);
  1609. memcpy(network->wpa_ie, info_element, network->wpa_ie_len);
  1610. return;
  1611. }
  1612. if (info_element->len == 7 &&
  1613. info_element->data[0] == 0x00 &&
  1614. info_element->data[1] == 0xe0 &&
  1615. info_element->data[2] == 0x4c &&
  1616. info_element->data[3] == 0x01 &&
  1617. info_element->data[4] == 0x02)
  1618. network->Turbo_Enable = 1;
  1619. if (*tmp_htcap_len == 0) {
  1620. if (info_element->len >= 4 &&
  1621. info_element->data[0] == 0x00 &&
  1622. info_element->data[1] == 0x90 &&
  1623. info_element->data[2] == 0x4c &&
  1624. info_element->data[3] == 0x033) {
  1625. *tmp_htcap_len = min_t(u8, info_element->len,
  1626. MAX_IE_LEN);
  1627. if (*tmp_htcap_len != 0) {
  1628. network->bssht.bdHTSpecVer = HT_SPEC_VER_EWC;
  1629. network->bssht.bdHTCapLen = min_t(u16, *tmp_htcap_len, sizeof(network->bssht.bdHTCapBuf));
  1630. memcpy(network->bssht.bdHTCapBuf,
  1631. info_element->data,
  1632. network->bssht.bdHTCapLen);
  1633. }
  1634. }
  1635. if (*tmp_htcap_len != 0) {
  1636. network->bssht.bdSupportHT = true;
  1637. network->bssht.bdHT1R = ((((struct ht_capab_ele *)(network->bssht.bdHTCapBuf))->MCS[1]) == 0);
  1638. } else {
  1639. network->bssht.bdSupportHT = false;
  1640. network->bssht.bdHT1R = false;
  1641. }
  1642. }
  1643. if (*tmp_htinfo_len == 0) {
  1644. if (info_element->len >= 4 &&
  1645. info_element->data[0] == 0x00 &&
  1646. info_element->data[1] == 0x90 &&
  1647. info_element->data[2] == 0x4c &&
  1648. info_element->data[3] == 0x034) {
  1649. *tmp_htinfo_len = min_t(u8, info_element->len,
  1650. MAX_IE_LEN);
  1651. if (*tmp_htinfo_len != 0) {
  1652. network->bssht.bdHTSpecVer = HT_SPEC_VER_EWC;
  1653. network->bssht.bdHTInfoLen = min_t(u16, *tmp_htinfo_len, sizeof(network->bssht.bdHTInfoBuf));
  1654. memcpy(network->bssht.bdHTInfoBuf,
  1655. info_element->data,
  1656. network->bssht.bdHTInfoLen);
  1657. }
  1658. }
  1659. }
  1660. if (network->bssht.bdSupportHT) {
  1661. if (info_element->len >= 4 &&
  1662. info_element->data[0] == 0x00 &&
  1663. info_element->data[1] == 0xe0 &&
  1664. info_element->data[2] == 0x4c &&
  1665. info_element->data[3] == 0x02) {
  1666. ht_realtek_agg_len = min_t(u8, info_element->len,
  1667. MAX_IE_LEN);
  1668. memcpy(ht_realtek_agg_buf, info_element->data,
  1669. info_element->len);
  1670. }
  1671. if (ht_realtek_agg_len >= 5) {
  1672. network->realtek_cap_exit = true;
  1673. network->bssht.bdRT2RTAggregation = true;
  1674. if ((ht_realtek_agg_buf[4] == 1) &&
  1675. (ht_realtek_agg_buf[5] & 0x02))
  1676. network->bssht.bdRT2RTLongSlotTime = true;
  1677. if ((ht_realtek_agg_buf[4] == 1) &&
  1678. (ht_realtek_agg_buf[5] & RT_HT_CAP_USE_92SE))
  1679. network->bssht.RT2RT_HT_Mode |= RT_HT_CAP_USE_92SE;
  1680. }
  1681. }
  1682. if (ht_realtek_agg_len >= 5) {
  1683. if ((ht_realtek_agg_buf[5] & RT_HT_CAP_USE_SOFTAP))
  1684. network->bssht.RT2RT_HT_Mode |= RT_HT_CAP_USE_SOFTAP;
  1685. }
  1686. if ((info_element->len >= 3 &&
  1687. info_element->data[0] == 0x00 &&
  1688. info_element->data[1] == 0x05 &&
  1689. info_element->data[2] == 0xb5) ||
  1690. (info_element->len >= 3 &&
  1691. info_element->data[0] == 0x00 &&
  1692. info_element->data[1] == 0x0a &&
  1693. info_element->data[2] == 0xf7) ||
  1694. (info_element->len >= 3 &&
  1695. info_element->data[0] == 0x00 &&
  1696. info_element->data[1] == 0x10 &&
  1697. info_element->data[2] == 0x18)) {
  1698. network->broadcom_cap_exist = true;
  1699. }
  1700. if (info_element->len >= 3 &&
  1701. info_element->data[0] == 0x00 &&
  1702. info_element->data[1] == 0x0c &&
  1703. info_element->data[2] == 0x43)
  1704. network->ralink_cap_exist = true;
  1705. if ((info_element->len >= 3 &&
  1706. info_element->data[0] == 0x00 &&
  1707. info_element->data[1] == 0x03 &&
  1708. info_element->data[2] == 0x7f) ||
  1709. (info_element->len >= 3 &&
  1710. info_element->data[0] == 0x00 &&
  1711. info_element->data[1] == 0x13 &&
  1712. info_element->data[2] == 0x74))
  1713. network->atheros_cap_exist = true;
  1714. if ((info_element->len >= 3 &&
  1715. info_element->data[0] == 0x00 &&
  1716. info_element->data[1] == 0x50 &&
  1717. info_element->data[2] == 0x43))
  1718. network->marvell_cap_exist = true;
  1719. if (info_element->len >= 3 &&
  1720. info_element->data[0] == 0x00 &&
  1721. info_element->data[1] == 0x40 &&
  1722. info_element->data[2] == 0x96)
  1723. network->cisco_cap_exist = true;
  1724. if (info_element->len >= 3 &&
  1725. info_element->data[0] == 0x00 &&
  1726. info_element->data[1] == 0x0a &&
  1727. info_element->data[2] == 0xf5)
  1728. network->airgo_cap_exist = true;
  1729. if (info_element->len > 4 &&
  1730. info_element->data[0] == 0x00 &&
  1731. info_element->data[1] == 0x40 &&
  1732. info_element->data[2] == 0x96 &&
  1733. info_element->data[3] == 0x01) {
  1734. if (info_element->len == 6) {
  1735. memcpy(network->CcxRmState, &info_element[4], 2);
  1736. if (network->CcxRmState[0] != 0)
  1737. network->bCcxRmEnable = true;
  1738. else
  1739. network->bCcxRmEnable = false;
  1740. network->MBssidMask = network->CcxRmState[1] & 0x07;
  1741. if (network->MBssidMask != 0) {
  1742. network->bMBssidValid = true;
  1743. network->MBssidMask = 0xff <<
  1744. (network->MBssidMask);
  1745. ether_addr_copy(network->MBssid,
  1746. network->bssid);
  1747. network->MBssid[5] &= network->MBssidMask;
  1748. } else {
  1749. network->bMBssidValid = false;
  1750. }
  1751. } else {
  1752. network->bCcxRmEnable = false;
  1753. }
  1754. }
  1755. if (info_element->len > 4 &&
  1756. info_element->data[0] == 0x00 &&
  1757. info_element->data[1] == 0x40 &&
  1758. info_element->data[2] == 0x96 &&
  1759. info_element->data[3] == 0x03) {
  1760. if (info_element->len == 5) {
  1761. network->bWithCcxVerNum = true;
  1762. network->BssCcxVerNumber = info_element->data[4];
  1763. } else {
  1764. network->bWithCcxVerNum = false;
  1765. network->BssCcxVerNumber = 0;
  1766. }
  1767. }
  1768. if (info_element->len > 4 &&
  1769. info_element->data[0] == 0x00 &&
  1770. info_element->data[1] == 0x50 &&
  1771. info_element->data[2] == 0xf2 &&
  1772. info_element->data[3] == 0x04) {
  1773. netdev_dbg(ieee->dev, "MFIE_TYPE_WZC: %d bytes\n",
  1774. info_element->len);
  1775. network->wzc_ie_len = min(info_element->len+2, MAX_WZC_IE_LEN);
  1776. memcpy(network->wzc_ie, info_element, network->wzc_ie_len);
  1777. }
  1778. }
  1779. static void rtllib_parse_mfie_ht_cap(struct rtllib_info_element *info_element,
  1780. struct rtllib_network *network,
  1781. u16 *tmp_htcap_len)
  1782. {
  1783. struct bss_ht *ht = &network->bssht;
  1784. *tmp_htcap_len = min_t(u8, info_element->len, MAX_IE_LEN);
  1785. if (*tmp_htcap_len != 0) {
  1786. ht->bdHTSpecVer = HT_SPEC_VER_EWC;
  1787. ht->bdHTCapLen = min_t(u16, *tmp_htcap_len,
  1788. sizeof(ht->bdHTCapBuf));
  1789. memcpy(ht->bdHTCapBuf, info_element->data, ht->bdHTCapLen);
  1790. ht->bdSupportHT = true;
  1791. ht->bdHT1R = ((((struct ht_capab_ele *)
  1792. ht->bdHTCapBuf))->MCS[1]) == 0;
  1793. ht->bdBandWidth = (enum ht_channel_width)
  1794. (((struct ht_capab_ele *)
  1795. (ht->bdHTCapBuf))->ChlWidth);
  1796. } else {
  1797. ht->bdSupportHT = false;
  1798. ht->bdHT1R = false;
  1799. ht->bdBandWidth = HT_CHANNEL_WIDTH_20;
  1800. }
  1801. }
  1802. int rtllib_parse_info_param(struct rtllib_device *ieee,
  1803. struct rtllib_info_element *info_element,
  1804. u16 length,
  1805. struct rtllib_network *network,
  1806. struct rtllib_rx_stats *stats)
  1807. {
  1808. u8 i;
  1809. short offset;
  1810. u16 tmp_htcap_len = 0;
  1811. u16 tmp_htinfo_len = 0;
  1812. char rates_str[64];
  1813. char *p;
  1814. while (length >= sizeof(*info_element)) {
  1815. if (sizeof(*info_element) + info_element->len > length) {
  1816. netdev_dbg(ieee->dev,
  1817. "Info elem: parse failed: info_element->len + 2 > left : info_element->len+2=%zd left=%d, id=%d.\n",
  1818. info_element->len + sizeof(*info_element),
  1819. length, info_element->id);
  1820. /* We stop processing but don't return an error here
  1821. * because some misbehaviour APs break this rule. ie.
  1822. * Orinoco AP1000.
  1823. */
  1824. break;
  1825. }
  1826. switch (info_element->id) {
  1827. case MFIE_TYPE_SSID:
  1828. if (rtllib_is_empty_essid(info_element->data,
  1829. info_element->len)) {
  1830. network->flags |= NETWORK_EMPTY_ESSID;
  1831. break;
  1832. }
  1833. network->ssid_len = min(info_element->len,
  1834. (u8) IW_ESSID_MAX_SIZE);
  1835. memcpy(network->ssid, info_element->data,
  1836. network->ssid_len);
  1837. if (network->ssid_len < IW_ESSID_MAX_SIZE)
  1838. memset(network->ssid + network->ssid_len, 0,
  1839. IW_ESSID_MAX_SIZE - network->ssid_len);
  1840. netdev_dbg(ieee->dev, "MFIE_TYPE_SSID: '%s' len=%d.\n",
  1841. network->ssid, network->ssid_len);
  1842. break;
  1843. case MFIE_TYPE_RATES:
  1844. p = rates_str;
  1845. network->rates_len = min(info_element->len,
  1846. MAX_RATES_LENGTH);
  1847. for (i = 0; i < network->rates_len; i++) {
  1848. network->rates[i] = info_element->data[i];
  1849. p += snprintf(p, sizeof(rates_str) -
  1850. (p - rates_str), "%02X ",
  1851. network->rates[i]);
  1852. if (rtllib_is_ofdm_rate
  1853. (info_element->data[i])) {
  1854. network->flags |= NETWORK_HAS_OFDM;
  1855. if (info_element->data[i] &
  1856. RTLLIB_BASIC_RATE_MASK)
  1857. network->flags &=
  1858. ~NETWORK_HAS_CCK;
  1859. }
  1860. if (rtllib_is_cck_rate
  1861. (info_element->data[i])) {
  1862. network->flags |= NETWORK_HAS_CCK;
  1863. }
  1864. }
  1865. netdev_dbg(ieee->dev, "MFIE_TYPE_RATES: '%s' (%d)\n",
  1866. rates_str, network->rates_len);
  1867. break;
  1868. case MFIE_TYPE_RATES_EX:
  1869. p = rates_str;
  1870. network->rates_ex_len = min(info_element->len,
  1871. MAX_RATES_EX_LENGTH);
  1872. for (i = 0; i < network->rates_ex_len; i++) {
  1873. network->rates_ex[i] = info_element->data[i];
  1874. p += snprintf(p, sizeof(rates_str) -
  1875. (p - rates_str), "%02X ",
  1876. network->rates_ex[i]);
  1877. if (rtllib_is_ofdm_rate
  1878. (info_element->data[i])) {
  1879. network->flags |= NETWORK_HAS_OFDM;
  1880. if (info_element->data[i] &
  1881. RTLLIB_BASIC_RATE_MASK)
  1882. network->flags &=
  1883. ~NETWORK_HAS_CCK;
  1884. }
  1885. }
  1886. netdev_dbg(ieee->dev, "MFIE_TYPE_RATES_EX: '%s' (%d)\n",
  1887. rates_str, network->rates_ex_len);
  1888. break;
  1889. case MFIE_TYPE_DS_SET:
  1890. netdev_dbg(ieee->dev, "MFIE_TYPE_DS_SET: %d\n",
  1891. info_element->data[0]);
  1892. network->channel = info_element->data[0];
  1893. break;
  1894. case MFIE_TYPE_FH_SET:
  1895. netdev_dbg(ieee->dev, "MFIE_TYPE_FH_SET: ignored\n");
  1896. break;
  1897. case MFIE_TYPE_CF_SET:
  1898. netdev_dbg(ieee->dev, "MFIE_TYPE_CF_SET: ignored\n");
  1899. break;
  1900. case MFIE_TYPE_TIM:
  1901. if (info_element->len < 4)
  1902. break;
  1903. network->tim.tim_count = info_element->data[0];
  1904. network->tim.tim_period = info_element->data[1];
  1905. network->dtim_period = info_element->data[1];
  1906. if (ieee->state != RTLLIB_LINKED)
  1907. break;
  1908. network->last_dtim_sta_time = jiffies;
  1909. network->dtim_data = RTLLIB_DTIM_VALID;
  1910. if (info_element->data[2] & 1)
  1911. network->dtim_data |= RTLLIB_DTIM_MBCAST;
  1912. offset = (info_element->data[2] >> 1)*2;
  1913. if (ieee->assoc_id < 8*offset ||
  1914. ieee->assoc_id > 8*(offset + info_element->len - 3))
  1915. break;
  1916. offset = (ieee->assoc_id / 8) - offset;
  1917. if (info_element->data[3 + offset] &
  1918. (1 << (ieee->assoc_id % 8)))
  1919. network->dtim_data |= RTLLIB_DTIM_UCAST;
  1920. network->listen_interval = network->dtim_period;
  1921. break;
  1922. case MFIE_TYPE_ERP:
  1923. network->erp_value = info_element->data[0];
  1924. network->flags |= NETWORK_HAS_ERP_VALUE;
  1925. netdev_dbg(ieee->dev, "MFIE_TYPE_ERP_SET: %d\n",
  1926. network->erp_value);
  1927. break;
  1928. case MFIE_TYPE_IBSS_SET:
  1929. network->atim_window = info_element->data[0];
  1930. netdev_dbg(ieee->dev, "MFIE_TYPE_IBSS_SET: %d\n",
  1931. network->atim_window);
  1932. break;
  1933. case MFIE_TYPE_CHALLENGE:
  1934. netdev_dbg(ieee->dev, "MFIE_TYPE_CHALLENGE: ignored\n");
  1935. break;
  1936. case MFIE_TYPE_GENERIC:
  1937. netdev_dbg(ieee->dev, "MFIE_TYPE_GENERIC: %d bytes\n",
  1938. info_element->len);
  1939. rtllib_parse_mife_generic(ieee, info_element, network,
  1940. &tmp_htcap_len,
  1941. &tmp_htinfo_len);
  1942. break;
  1943. case MFIE_TYPE_RSN:
  1944. netdev_dbg(ieee->dev, "MFIE_TYPE_RSN: %d bytes\n",
  1945. info_element->len);
  1946. network->rsn_ie_len = min(info_element->len + 2,
  1947. MAX_WPA_IE_LEN);
  1948. memcpy(network->rsn_ie, info_element,
  1949. network->rsn_ie_len);
  1950. break;
  1951. case MFIE_TYPE_HT_CAP:
  1952. netdev_dbg(ieee->dev, "MFIE_TYPE_HT_CAP: %d bytes\n",
  1953. info_element->len);
  1954. rtllib_parse_mfie_ht_cap(info_element, network,
  1955. &tmp_htcap_len);
  1956. break;
  1957. case MFIE_TYPE_HT_INFO:
  1958. netdev_dbg(ieee->dev, "MFIE_TYPE_HT_INFO: %d bytes\n",
  1959. info_element->len);
  1960. tmp_htinfo_len = min_t(u8, info_element->len,
  1961. MAX_IE_LEN);
  1962. if (tmp_htinfo_len) {
  1963. network->bssht.bdHTSpecVer = HT_SPEC_VER_IEEE;
  1964. network->bssht.bdHTInfoLen = tmp_htinfo_len >
  1965. sizeof(network->bssht.bdHTInfoBuf) ?
  1966. sizeof(network->bssht.bdHTInfoBuf) :
  1967. tmp_htinfo_len;
  1968. memcpy(network->bssht.bdHTInfoBuf,
  1969. info_element->data,
  1970. network->bssht.bdHTInfoLen);
  1971. }
  1972. break;
  1973. case MFIE_TYPE_AIRONET:
  1974. netdev_dbg(ieee->dev, "MFIE_TYPE_AIRONET: %d bytes\n",
  1975. info_element->len);
  1976. if (info_element->len > IE_CISCO_FLAG_POSITION) {
  1977. network->bWithAironetIE = true;
  1978. if ((info_element->data[IE_CISCO_FLAG_POSITION]
  1979. & SUPPORT_CKIP_MIC) ||
  1980. (info_element->data[IE_CISCO_FLAG_POSITION]
  1981. & SUPPORT_CKIP_PK))
  1982. network->bCkipSupported = true;
  1983. else
  1984. network->bCkipSupported = false;
  1985. } else {
  1986. network->bWithAironetIE = false;
  1987. network->bCkipSupported = false;
  1988. }
  1989. break;
  1990. case MFIE_TYPE_QOS_PARAMETER:
  1991. netdev_err(ieee->dev,
  1992. "QoS Error need to parse QOS_PARAMETER IE\n");
  1993. break;
  1994. case MFIE_TYPE_COUNTRY:
  1995. netdev_dbg(ieee->dev, "MFIE_TYPE_COUNTRY: %d bytes\n",
  1996. info_element->len);
  1997. rtllib_extract_country_ie(ieee, info_element, network,
  1998. network->bssid);
  1999. break;
  2000. /* TODO */
  2001. default:
  2002. netdev_dbg(ieee->dev,
  2003. "Unsupported info element: %s (%d)\n",
  2004. get_info_element_string(info_element->id),
  2005. info_element->id);
  2006. break;
  2007. }
  2008. length -= sizeof(*info_element) + info_element->len;
  2009. info_element =
  2010. (struct rtllib_info_element *)&info_element->
  2011. data[info_element->len];
  2012. }
  2013. if (!network->atheros_cap_exist && !network->broadcom_cap_exist &&
  2014. !network->cisco_cap_exist && !network->ralink_cap_exist &&
  2015. !network->bssht.bdRT2RTAggregation)
  2016. network->unknown_cap_exist = true;
  2017. else
  2018. network->unknown_cap_exist = false;
  2019. return 0;
  2020. }
  2021. static long rtllib_translate_todbm(u8 signal_strength_index)
  2022. {
  2023. long signal_power;
  2024. signal_power = (long)((signal_strength_index + 1) >> 1);
  2025. signal_power -= 95;
  2026. return signal_power;
  2027. }
  2028. static inline int rtllib_network_init(
  2029. struct rtllib_device *ieee,
  2030. struct rtllib_probe_response *beacon,
  2031. struct rtllib_network *network,
  2032. struct rtllib_rx_stats *stats)
  2033. {
  2034. memset(&network->qos_data, 0, sizeof(struct rtllib_qos_data));
  2035. /* Pull out fixed field data */
  2036. ether_addr_copy(network->bssid, beacon->header.addr3);
  2037. network->capability = le16_to_cpu(beacon->capability);
  2038. network->last_scanned = jiffies;
  2039. network->time_stamp[0] = beacon->time_stamp[0];
  2040. network->time_stamp[1] = beacon->time_stamp[1];
  2041. network->beacon_interval = le16_to_cpu(beacon->beacon_interval);
  2042. /* Where to pull this? beacon->listen_interval;*/
  2043. network->listen_interval = 0x0A;
  2044. network->rates_len = network->rates_ex_len = 0;
  2045. network->ssid_len = 0;
  2046. network->hidden_ssid_len = 0;
  2047. memset(network->hidden_ssid, 0, sizeof(network->hidden_ssid));
  2048. network->flags = 0;
  2049. network->atim_window = 0;
  2050. network->erp_value = (network->capability & WLAN_CAPABILITY_IBSS) ?
  2051. 0x3 : 0x0;
  2052. network->berp_info_valid = false;
  2053. network->broadcom_cap_exist = false;
  2054. network->ralink_cap_exist = false;
  2055. network->atheros_cap_exist = false;
  2056. network->cisco_cap_exist = false;
  2057. network->unknown_cap_exist = false;
  2058. network->realtek_cap_exit = false;
  2059. network->marvell_cap_exist = false;
  2060. network->airgo_cap_exist = false;
  2061. network->Turbo_Enable = 0;
  2062. network->SignalStrength = stats->SignalStrength;
  2063. network->RSSI = stats->SignalStrength;
  2064. network->CountryIeLen = 0;
  2065. memset(network->CountryIeBuf, 0, MAX_IE_LEN);
  2066. HTInitializeBssDesc(&network->bssht);
  2067. if (stats->freq == RTLLIB_52GHZ_BAND) {
  2068. /* for A band (No DS info) */
  2069. network->channel = stats->received_channel;
  2070. } else
  2071. network->flags |= NETWORK_HAS_CCK;
  2072. network->wpa_ie_len = 0;
  2073. network->rsn_ie_len = 0;
  2074. network->wzc_ie_len = 0;
  2075. if (rtllib_parse_info_param(ieee,
  2076. beacon->info_element,
  2077. (stats->len - sizeof(*beacon)),
  2078. network,
  2079. stats))
  2080. return 1;
  2081. network->mode = 0;
  2082. if (stats->freq == RTLLIB_52GHZ_BAND)
  2083. network->mode = IEEE_A;
  2084. else {
  2085. if (network->flags & NETWORK_HAS_OFDM)
  2086. network->mode |= IEEE_G;
  2087. if (network->flags & NETWORK_HAS_CCK)
  2088. network->mode |= IEEE_B;
  2089. }
  2090. if (network->mode == 0) {
  2091. netdev_dbg(ieee->dev, "Filtered out '%s (%pM)' network.\n",
  2092. escape_essid(network->ssid, network->ssid_len),
  2093. network->bssid);
  2094. return 1;
  2095. }
  2096. if (network->bssht.bdSupportHT) {
  2097. if (network->mode == IEEE_A)
  2098. network->mode = IEEE_N_5G;
  2099. else if (network->mode & (IEEE_G | IEEE_B))
  2100. network->mode = IEEE_N_24G;
  2101. }
  2102. if (rtllib_is_empty_essid(network->ssid, network->ssid_len))
  2103. network->flags |= NETWORK_EMPTY_ESSID;
  2104. stats->signal = 30 + (stats->SignalStrength * 70) / 100;
  2105. stats->noise = rtllib_translate_todbm((u8)(100-stats->signal)) - 25;
  2106. memcpy(&network->stats, stats, sizeof(network->stats));
  2107. return 0;
  2108. }
  2109. static inline int is_same_network(struct rtllib_network *src,
  2110. struct rtllib_network *dst, u8 ssidbroad)
  2111. {
  2112. /* A network is only a duplicate if the channel, BSSID, ESSID
  2113. * and the capability field (in particular IBSS and BSS) all match.
  2114. * We treat all <hidden> with the same BSSID and channel
  2115. * as one network
  2116. */
  2117. return (((src->ssid_len == dst->ssid_len) || (!ssidbroad)) &&
  2118. (src->channel == dst->channel) &&
  2119. !memcmp(src->bssid, dst->bssid, ETH_ALEN) &&
  2120. (!memcmp(src->ssid, dst->ssid, src->ssid_len) ||
  2121. (!ssidbroad)) &&
  2122. ((src->capability & WLAN_CAPABILITY_IBSS) ==
  2123. (dst->capability & WLAN_CAPABILITY_IBSS)) &&
  2124. ((src->capability & WLAN_CAPABILITY_ESS) ==
  2125. (dst->capability & WLAN_CAPABILITY_ESS)));
  2126. }
  2127. static inline void update_network(struct rtllib_device *ieee,
  2128. struct rtllib_network *dst,
  2129. struct rtllib_network *src)
  2130. {
  2131. int qos_active;
  2132. u8 old_param;
  2133. memcpy(&dst->stats, &src->stats, sizeof(struct rtllib_rx_stats));
  2134. dst->capability = src->capability;
  2135. memcpy(dst->rates, src->rates, src->rates_len);
  2136. dst->rates_len = src->rates_len;
  2137. memcpy(dst->rates_ex, src->rates_ex, src->rates_ex_len);
  2138. dst->rates_ex_len = src->rates_ex_len;
  2139. if (src->ssid_len > 0) {
  2140. if (dst->ssid_len == 0) {
  2141. memset(dst->hidden_ssid, 0, sizeof(dst->hidden_ssid));
  2142. dst->hidden_ssid_len = src->ssid_len;
  2143. memcpy(dst->hidden_ssid, src->ssid, src->ssid_len);
  2144. } else {
  2145. memset(dst->ssid, 0, dst->ssid_len);
  2146. dst->ssid_len = src->ssid_len;
  2147. memcpy(dst->ssid, src->ssid, src->ssid_len);
  2148. }
  2149. }
  2150. dst->mode = src->mode;
  2151. dst->flags = src->flags;
  2152. dst->time_stamp[0] = src->time_stamp[0];
  2153. dst->time_stamp[1] = src->time_stamp[1];
  2154. if (src->flags & NETWORK_HAS_ERP_VALUE) {
  2155. dst->erp_value = src->erp_value;
  2156. dst->berp_info_valid = src->berp_info_valid = true;
  2157. }
  2158. dst->beacon_interval = src->beacon_interval;
  2159. dst->listen_interval = src->listen_interval;
  2160. dst->atim_window = src->atim_window;
  2161. dst->dtim_period = src->dtim_period;
  2162. dst->dtim_data = src->dtim_data;
  2163. dst->last_dtim_sta_time = src->last_dtim_sta_time;
  2164. memcpy(&dst->tim, &src->tim, sizeof(struct rtllib_tim_parameters));
  2165. dst->bssht.bdSupportHT = src->bssht.bdSupportHT;
  2166. dst->bssht.bdRT2RTAggregation = src->bssht.bdRT2RTAggregation;
  2167. dst->bssht.bdHTCapLen = src->bssht.bdHTCapLen;
  2168. memcpy(dst->bssht.bdHTCapBuf, src->bssht.bdHTCapBuf,
  2169. src->bssht.bdHTCapLen);
  2170. dst->bssht.bdHTInfoLen = src->bssht.bdHTInfoLen;
  2171. memcpy(dst->bssht.bdHTInfoBuf, src->bssht.bdHTInfoBuf,
  2172. src->bssht.bdHTInfoLen);
  2173. dst->bssht.bdHTSpecVer = src->bssht.bdHTSpecVer;
  2174. dst->bssht.bdRT2RTLongSlotTime = src->bssht.bdRT2RTLongSlotTime;
  2175. dst->broadcom_cap_exist = src->broadcom_cap_exist;
  2176. dst->ralink_cap_exist = src->ralink_cap_exist;
  2177. dst->atheros_cap_exist = src->atheros_cap_exist;
  2178. dst->realtek_cap_exit = src->realtek_cap_exit;
  2179. dst->marvell_cap_exist = src->marvell_cap_exist;
  2180. dst->cisco_cap_exist = src->cisco_cap_exist;
  2181. dst->airgo_cap_exist = src->airgo_cap_exist;
  2182. dst->unknown_cap_exist = src->unknown_cap_exist;
  2183. memcpy(dst->wpa_ie, src->wpa_ie, src->wpa_ie_len);
  2184. dst->wpa_ie_len = src->wpa_ie_len;
  2185. memcpy(dst->rsn_ie, src->rsn_ie, src->rsn_ie_len);
  2186. dst->rsn_ie_len = src->rsn_ie_len;
  2187. memcpy(dst->wzc_ie, src->wzc_ie, src->wzc_ie_len);
  2188. dst->wzc_ie_len = src->wzc_ie_len;
  2189. dst->last_scanned = jiffies;
  2190. /* qos related parameters */
  2191. qos_active = dst->qos_data.active;
  2192. old_param = dst->qos_data.param_count;
  2193. dst->qos_data.supported = src->qos_data.supported;
  2194. if (dst->flags & NETWORK_HAS_QOS_PARAMETERS)
  2195. memcpy(&dst->qos_data, &src->qos_data,
  2196. sizeof(struct rtllib_qos_data));
  2197. if (dst->qos_data.supported == 1) {
  2198. if (dst->ssid_len)
  2199. netdev_dbg(ieee->dev,
  2200. "QoS the network %s is QoS supported\n",
  2201. dst->ssid);
  2202. else
  2203. netdev_dbg(ieee->dev,
  2204. "QoS the network is QoS supported\n");
  2205. }
  2206. dst->qos_data.active = qos_active;
  2207. dst->qos_data.old_param_count = old_param;
  2208. dst->wmm_info = src->wmm_info;
  2209. if (src->wmm_param[0].ac_aci_acm_aifsn ||
  2210. src->wmm_param[1].ac_aci_acm_aifsn ||
  2211. src->wmm_param[2].ac_aci_acm_aifsn ||
  2212. src->wmm_param[3].ac_aci_acm_aifsn)
  2213. memcpy(dst->wmm_param, src->wmm_param, WME_AC_PRAM_LEN);
  2214. dst->SignalStrength = src->SignalStrength;
  2215. dst->RSSI = src->RSSI;
  2216. dst->Turbo_Enable = src->Turbo_Enable;
  2217. dst->CountryIeLen = src->CountryIeLen;
  2218. memcpy(dst->CountryIeBuf, src->CountryIeBuf, src->CountryIeLen);
  2219. dst->bWithAironetIE = src->bWithAironetIE;
  2220. dst->bCkipSupported = src->bCkipSupported;
  2221. memcpy(dst->CcxRmState, src->CcxRmState, 2);
  2222. dst->bCcxRmEnable = src->bCcxRmEnable;
  2223. dst->MBssidMask = src->MBssidMask;
  2224. dst->bMBssidValid = src->bMBssidValid;
  2225. memcpy(dst->MBssid, src->MBssid, 6);
  2226. dst->bWithCcxVerNum = src->bWithCcxVerNum;
  2227. dst->BssCcxVerNumber = src->BssCcxVerNumber;
  2228. }
  2229. static inline int is_beacon(u16 fc)
  2230. {
  2231. return (WLAN_FC_GET_STYPE(fc) == RTLLIB_STYPE_BEACON);
  2232. }
  2233. static int IsPassiveChannel(struct rtllib_device *rtllib, u8 channel)
  2234. {
  2235. if (channel > MAX_CHANNEL_NUMBER) {
  2236. netdev_info(rtllib->dev, "%s(): Invalid Channel\n", __func__);
  2237. return 0;
  2238. }
  2239. if (rtllib->active_channel_map[channel] == 2)
  2240. return 1;
  2241. return 0;
  2242. }
  2243. int rtllib_legal_channel(struct rtllib_device *rtllib, u8 channel)
  2244. {
  2245. if (channel > MAX_CHANNEL_NUMBER) {
  2246. netdev_info(rtllib->dev, "%s(): Invalid Channel\n", __func__);
  2247. return 0;
  2248. }
  2249. if (rtllib->active_channel_map[channel] > 0)
  2250. return 1;
  2251. return 0;
  2252. }
  2253. EXPORT_SYMBOL(rtllib_legal_channel);
  2254. static inline void rtllib_process_probe_response(
  2255. struct rtllib_device *ieee,
  2256. struct rtllib_probe_response *beacon,
  2257. struct rtllib_rx_stats *stats)
  2258. {
  2259. struct rtllib_network *target;
  2260. struct rtllib_network *oldest = NULL;
  2261. struct rtllib_info_element *info_element = &beacon->info_element[0];
  2262. unsigned long flags;
  2263. short renew;
  2264. struct rtllib_network *network = kzalloc(sizeof(struct rtllib_network),
  2265. GFP_ATOMIC);
  2266. u16 frame_ctl = le16_to_cpu(beacon->header.frame_ctl);
  2267. if (!network)
  2268. return;
  2269. netdev_dbg(ieee->dev,
  2270. "'%s' ( %pM ): %c%c%c%c %c%c%c%c-%c%c%c%c %c%c%c%c\n",
  2271. escape_essid(info_element->data, info_element->len),
  2272. beacon->header.addr3,
  2273. (le16_to_cpu(beacon->capability) & (1<<0xf)) ? '1' : '0',
  2274. (le16_to_cpu(beacon->capability) & (1<<0xe)) ? '1' : '0',
  2275. (le16_to_cpu(beacon->capability) & (1<<0xd)) ? '1' : '0',
  2276. (le16_to_cpu(beacon->capability) & (1<<0xc)) ? '1' : '0',
  2277. (le16_to_cpu(beacon->capability) & (1<<0xb)) ? '1' : '0',
  2278. (le16_to_cpu(beacon->capability) & (1<<0xa)) ? '1' : '0',
  2279. (le16_to_cpu(beacon->capability) & (1<<0x9)) ? '1' : '0',
  2280. (le16_to_cpu(beacon->capability) & (1<<0x8)) ? '1' : '0',
  2281. (le16_to_cpu(beacon->capability) & (1<<0x7)) ? '1' : '0',
  2282. (le16_to_cpu(beacon->capability) & (1<<0x6)) ? '1' : '0',
  2283. (le16_to_cpu(beacon->capability) & (1<<0x5)) ? '1' : '0',
  2284. (le16_to_cpu(beacon->capability) & (1<<0x4)) ? '1' : '0',
  2285. (le16_to_cpu(beacon->capability) & (1<<0x3)) ? '1' : '0',
  2286. (le16_to_cpu(beacon->capability) & (1<<0x2)) ? '1' : '0',
  2287. (le16_to_cpu(beacon->capability) & (1<<0x1)) ? '1' : '0',
  2288. (le16_to_cpu(beacon->capability) & (1<<0x0)) ? '1' : '0');
  2289. if (rtllib_network_init(ieee, beacon, network, stats)) {
  2290. netdev_dbg(ieee->dev, "Dropped '%s' ( %pM) via %s.\n",
  2291. escape_essid(info_element->data, info_element->len),
  2292. beacon->header.addr3,
  2293. is_beacon(frame_ctl) ? "BEACON" : "PROBE RESPONSE");
  2294. goto free_network;
  2295. }
  2296. if (!rtllib_legal_channel(ieee, network->channel))
  2297. goto free_network;
  2298. if (WLAN_FC_GET_STYPE(frame_ctl) == RTLLIB_STYPE_PROBE_RESP) {
  2299. if (IsPassiveChannel(ieee, network->channel)) {
  2300. netdev_info(ieee->dev,
  2301. "GetScanInfo(): For Global Domain, filter probe response at channel(%d).\n",
  2302. network->channel);
  2303. goto free_network;
  2304. }
  2305. }
  2306. /* The network parsed correctly -- so now we scan our known networks
  2307. * to see if we can find it in our list.
  2308. *
  2309. * NOTE: This search is definitely not optimized. Once its doing
  2310. * the "right thing" we'll optimize it for efficiency if
  2311. * necessary
  2312. */
  2313. /* Search for this entry in the list and update it if it is
  2314. * already there.
  2315. */
  2316. spin_lock_irqsave(&ieee->lock, flags);
  2317. if (is_same_network(&ieee->current_network, network,
  2318. (network->ssid_len ? 1 : 0))) {
  2319. update_network(ieee, &ieee->current_network, network);
  2320. if ((ieee->current_network.mode == IEEE_N_24G ||
  2321. ieee->current_network.mode == IEEE_G)
  2322. && ieee->current_network.berp_info_valid) {
  2323. if (ieee->current_network.erp_value & ERP_UseProtection)
  2324. ieee->current_network.buseprotection = true;
  2325. else
  2326. ieee->current_network.buseprotection = false;
  2327. }
  2328. if (is_beacon(frame_ctl)) {
  2329. if (ieee->state >= RTLLIB_LINKED)
  2330. ieee->LinkDetectInfo.NumRecvBcnInPeriod++;
  2331. }
  2332. }
  2333. list_for_each_entry(target, &ieee->network_list, list) {
  2334. if (is_same_network(target, network,
  2335. (target->ssid_len ? 1 : 0)))
  2336. break;
  2337. if ((oldest == NULL) ||
  2338. (target->last_scanned < oldest->last_scanned))
  2339. oldest = target;
  2340. }
  2341. /* If we didn't find a match, then get a new network slot to initialize
  2342. * with this beacon's information
  2343. */
  2344. if (&target->list == &ieee->network_list) {
  2345. if (list_empty(&ieee->network_free_list)) {
  2346. /* If there are no more slots, expire the oldest */
  2347. list_del(&oldest->list);
  2348. target = oldest;
  2349. netdev_dbg(ieee->dev,
  2350. "Expired '%s' ( %pM) from network list.\n",
  2351. escape_essid(target->ssid, target->ssid_len),
  2352. target->bssid);
  2353. } else {
  2354. /* Otherwise just pull from the free list */
  2355. target = list_entry(ieee->network_free_list.next,
  2356. struct rtllib_network, list);
  2357. list_del(ieee->network_free_list.next);
  2358. }
  2359. netdev_dbg(ieee->dev, "Adding '%s' ( %pM) via %s.\n",
  2360. escape_essid(network->ssid, network->ssid_len),
  2361. network->bssid,
  2362. is_beacon(frame_ctl) ? "BEACON" : "PROBE RESPONSE");
  2363. memcpy(target, network, sizeof(*target));
  2364. list_add_tail(&target->list, &ieee->network_list);
  2365. if (ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE)
  2366. rtllib_softmac_new_net(ieee, network);
  2367. } else {
  2368. netdev_dbg(ieee->dev, "Updating '%s' ( %pM) via %s.\n",
  2369. escape_essid(target->ssid, target->ssid_len),
  2370. target->bssid,
  2371. is_beacon(frame_ctl) ? "BEACON" : "PROBE RESPONSE");
  2372. /* we have an entry and we are going to update it. But this
  2373. * entry may be already expired. In this case we do the same
  2374. * as we found a new net and call the new_net handler
  2375. */
  2376. renew = !time_after(target->last_scanned + ieee->scan_age,
  2377. jiffies);
  2378. if ((!target->ssid_len) &&
  2379. (((network->ssid_len > 0) && (target->hidden_ssid_len == 0))
  2380. || ((ieee->current_network.ssid_len == network->ssid_len) &&
  2381. (strncmp(ieee->current_network.ssid, network->ssid,
  2382. network->ssid_len) == 0) &&
  2383. (ieee->state == RTLLIB_NOLINK))))
  2384. renew = 1;
  2385. update_network(ieee, target, network);
  2386. if (renew && (ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE))
  2387. rtllib_softmac_new_net(ieee, network);
  2388. }
  2389. spin_unlock_irqrestore(&ieee->lock, flags);
  2390. if (is_beacon(frame_ctl) &&
  2391. is_same_network(&ieee->current_network, network,
  2392. (network->ssid_len ? 1 : 0)) &&
  2393. (ieee->state == RTLLIB_LINKED)) {
  2394. if (ieee->handle_beacon != NULL)
  2395. ieee->handle_beacon(ieee->dev, beacon,
  2396. &ieee->current_network);
  2397. }
  2398. free_network:
  2399. kfree(network);
  2400. }
  2401. static void rtllib_rx_mgt(struct rtllib_device *ieee,
  2402. struct sk_buff *skb,
  2403. struct rtllib_rx_stats *stats)
  2404. {
  2405. struct rtllib_hdr_4addr *header = (struct rtllib_hdr_4addr *)skb->data;
  2406. if ((WLAN_FC_GET_STYPE(le16_to_cpu(header->frame_ctl)) !=
  2407. RTLLIB_STYPE_PROBE_RESP) &&
  2408. (WLAN_FC_GET_STYPE(le16_to_cpu(header->frame_ctl)) !=
  2409. RTLLIB_STYPE_BEACON))
  2410. ieee->last_rx_ps_time = jiffies;
  2411. switch (WLAN_FC_GET_STYPE(le16_to_cpu(header->frame_ctl))) {
  2412. case RTLLIB_STYPE_BEACON:
  2413. netdev_dbg(ieee->dev, "received BEACON (%d)\n",
  2414. WLAN_FC_GET_STYPE(le16_to_cpu(header->frame_ctl)));
  2415. rtllib_process_probe_response(
  2416. ieee, (struct rtllib_probe_response *)header,
  2417. stats);
  2418. if (ieee->sta_sleep || (ieee->ps != RTLLIB_PS_DISABLED &&
  2419. ieee->iw_mode == IW_MODE_INFRA &&
  2420. ieee->state == RTLLIB_LINKED))
  2421. tasklet_schedule(&ieee->ps_task);
  2422. break;
  2423. case RTLLIB_STYPE_PROBE_RESP:
  2424. netdev_dbg(ieee->dev, "received PROBE RESPONSE (%d)\n",
  2425. WLAN_FC_GET_STYPE(le16_to_cpu(header->frame_ctl)));
  2426. rtllib_process_probe_response(ieee,
  2427. (struct rtllib_probe_response *)header, stats);
  2428. break;
  2429. case RTLLIB_STYPE_PROBE_REQ:
  2430. netdev_dbg(ieee->dev, "received PROBE RESQUEST (%d)\n",
  2431. WLAN_FC_GET_STYPE(le16_to_cpu(header->frame_ctl)));
  2432. if ((ieee->softmac_features & IEEE_SOFTMAC_PROBERS) &&
  2433. ((ieee->iw_mode == IW_MODE_ADHOC ||
  2434. ieee->iw_mode == IW_MODE_MASTER) &&
  2435. ieee->state == RTLLIB_LINKED))
  2436. rtllib_rx_probe_rq(ieee, skb);
  2437. break;
  2438. }
  2439. }