rx.c 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2005-2006, Devicescape Software, Inc.
  4. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  5. * Copyright 2007-2010 Johannes Berg <johannes@sipsolutions.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/jiffies.h>
  12. #include <linux/slab.h>
  13. #include <linux/kernel.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/etherdevice.h>
  17. #include <linux/rcupdate.h>
  18. #include <linux/export.h>
  19. #include <net/mac80211.h>
  20. #include <net/ieee80211_radiotap.h>
  21. #include "ieee80211_i.h"
  22. #include "driver-ops.h"
  23. #include "led.h"
  24. #include "mesh.h"
  25. #include "wep.h"
  26. #include "wpa.h"
  27. #include "tkip.h"
  28. #include "wme.h"
  29. #include "rate.h"
  30. /*
  31. * monitor mode reception
  32. *
  33. * This function cleans up the SKB, i.e. it removes all the stuff
  34. * only useful for monitoring.
  35. */
  36. static struct sk_buff *remove_monitor_info(struct ieee80211_local *local,
  37. struct sk_buff *skb)
  38. {
  39. if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS) {
  40. if (likely(skb->len > FCS_LEN))
  41. __pskb_trim(skb, skb->len - FCS_LEN);
  42. else {
  43. /* driver bug */
  44. WARN_ON(1);
  45. dev_kfree_skb(skb);
  46. skb = NULL;
  47. }
  48. }
  49. return skb;
  50. }
  51. static inline int should_drop_frame(struct sk_buff *skb,
  52. int present_fcs_len)
  53. {
  54. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  55. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  56. if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
  57. return 1;
  58. if (unlikely(skb->len < 16 + present_fcs_len))
  59. return 1;
  60. if (ieee80211_is_ctl(hdr->frame_control) &&
  61. !ieee80211_is_pspoll(hdr->frame_control) &&
  62. !ieee80211_is_back_req(hdr->frame_control))
  63. return 1;
  64. return 0;
  65. }
  66. static int
  67. ieee80211_rx_radiotap_len(struct ieee80211_local *local,
  68. struct ieee80211_rx_status *status)
  69. {
  70. int len;
  71. /* always present fields */
  72. len = sizeof(struct ieee80211_radiotap_header) + 9;
  73. if (status->flag & RX_FLAG_MACTIME_MPDU)
  74. len += 8;
  75. if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
  76. len += 1;
  77. if (len & 1) /* padding for RX_FLAGS if necessary */
  78. len++;
  79. if (status->flag & RX_FLAG_HT) /* HT info */
  80. len += 3;
  81. return len;
  82. }
  83. /*
  84. * ieee80211_add_rx_radiotap_header - add radiotap header
  85. *
  86. * add a radiotap header containing all the fields which the hardware provided.
  87. */
  88. static void
  89. ieee80211_add_rx_radiotap_header(struct ieee80211_local *local,
  90. struct sk_buff *skb,
  91. struct ieee80211_rate *rate,
  92. int rtap_len)
  93. {
  94. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  95. struct ieee80211_radiotap_header *rthdr;
  96. unsigned char *pos;
  97. u16 rx_flags = 0;
  98. rthdr = (struct ieee80211_radiotap_header *)skb_push(skb, rtap_len);
  99. memset(rthdr, 0, rtap_len);
  100. /* radiotap header, set always present flags */
  101. rthdr->it_present =
  102. cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  103. (1 << IEEE80211_RADIOTAP_CHANNEL) |
  104. (1 << IEEE80211_RADIOTAP_ANTENNA) |
  105. (1 << IEEE80211_RADIOTAP_RX_FLAGS));
  106. rthdr->it_len = cpu_to_le16(rtap_len);
  107. pos = (unsigned char *)(rthdr+1);
  108. /* the order of the following fields is important */
  109. /* IEEE80211_RADIOTAP_TSFT */
  110. if (status->flag & RX_FLAG_MACTIME_MPDU) {
  111. put_unaligned_le64(status->mactime, pos);
  112. rthdr->it_present |=
  113. cpu_to_le32(1 << IEEE80211_RADIOTAP_TSFT);
  114. pos += 8;
  115. }
  116. /* IEEE80211_RADIOTAP_FLAGS */
  117. if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
  118. *pos |= IEEE80211_RADIOTAP_F_FCS;
  119. if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
  120. *pos |= IEEE80211_RADIOTAP_F_BADFCS;
  121. if (status->flag & RX_FLAG_SHORTPRE)
  122. *pos |= IEEE80211_RADIOTAP_F_SHORTPRE;
  123. pos++;
  124. /* IEEE80211_RADIOTAP_RATE */
  125. if (!rate || status->flag & RX_FLAG_HT) {
  126. /*
  127. * Without rate information don't add it. If we have,
  128. * MCS information is a separate field in radiotap,
  129. * added below. The byte here is needed as padding
  130. * for the channel though, so initialise it to 0.
  131. */
  132. *pos = 0;
  133. } else {
  134. rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
  135. *pos = rate->bitrate / 5;
  136. }
  137. pos++;
  138. /* IEEE80211_RADIOTAP_CHANNEL */
  139. put_unaligned_le16(status->freq, pos);
  140. pos += 2;
  141. if (status->band == IEEE80211_BAND_5GHZ)
  142. put_unaligned_le16(IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ,
  143. pos);
  144. else if (status->flag & RX_FLAG_HT)
  145. put_unaligned_le16(IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ,
  146. pos);
  147. else if (rate && rate->flags & IEEE80211_RATE_ERP_G)
  148. put_unaligned_le16(IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ,
  149. pos);
  150. else if (rate)
  151. put_unaligned_le16(IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ,
  152. pos);
  153. else
  154. put_unaligned_le16(IEEE80211_CHAN_2GHZ, pos);
  155. pos += 2;
  156. /* IEEE80211_RADIOTAP_DBM_ANTSIGNAL */
  157. if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM) {
  158. *pos = status->signal;
  159. rthdr->it_present |=
  160. cpu_to_le32(1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
  161. pos++;
  162. }
  163. /* IEEE80211_RADIOTAP_LOCK_QUALITY is missing */
  164. /* IEEE80211_RADIOTAP_ANTENNA */
  165. *pos = status->antenna;
  166. pos++;
  167. /* IEEE80211_RADIOTAP_DB_ANTNOISE is not used */
  168. /* IEEE80211_RADIOTAP_RX_FLAGS */
  169. /* ensure 2 byte alignment for the 2 byte field as required */
  170. if ((pos - (u8 *)rthdr) & 1)
  171. pos++;
  172. if (status->flag & RX_FLAG_FAILED_PLCP_CRC)
  173. rx_flags |= IEEE80211_RADIOTAP_F_RX_BADPLCP;
  174. put_unaligned_le16(rx_flags, pos);
  175. pos += 2;
  176. if (status->flag & RX_FLAG_HT) {
  177. rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_MCS);
  178. *pos++ = IEEE80211_RADIOTAP_MCS_HAVE_MCS |
  179. IEEE80211_RADIOTAP_MCS_HAVE_GI |
  180. IEEE80211_RADIOTAP_MCS_HAVE_BW;
  181. *pos = 0;
  182. if (status->flag & RX_FLAG_SHORT_GI)
  183. *pos |= IEEE80211_RADIOTAP_MCS_SGI;
  184. if (status->flag & RX_FLAG_40MHZ)
  185. *pos |= IEEE80211_RADIOTAP_MCS_BW_40;
  186. pos++;
  187. *pos++ = status->rate_idx;
  188. }
  189. }
  190. /*
  191. * This function copies a received frame to all monitor interfaces and
  192. * returns a cleaned-up SKB that no longer includes the FCS nor the
  193. * radiotap header the driver might have added.
  194. */
  195. static struct sk_buff *
  196. ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
  197. struct ieee80211_rate *rate)
  198. {
  199. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(origskb);
  200. struct ieee80211_sub_if_data *sdata;
  201. int needed_headroom = 0;
  202. struct sk_buff *skb, *skb2;
  203. struct net_device *prev_dev = NULL;
  204. int present_fcs_len = 0;
  205. /*
  206. * First, we may need to make a copy of the skb because
  207. * (1) we need to modify it for radiotap (if not present), and
  208. * (2) the other RX handlers will modify the skb we got.
  209. *
  210. * We don't need to, of course, if we aren't going to return
  211. * the SKB because it has a bad FCS/PLCP checksum.
  212. */
  213. /* room for the radiotap header based on driver features */
  214. needed_headroom = ieee80211_rx_radiotap_len(local, status);
  215. if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
  216. present_fcs_len = FCS_LEN;
  217. /* make sure hdr->frame_control is on the linear part */
  218. if (!pskb_may_pull(origskb, 2)) {
  219. dev_kfree_skb(origskb);
  220. return NULL;
  221. }
  222. if (!local->monitors) {
  223. if (should_drop_frame(origskb, present_fcs_len)) {
  224. dev_kfree_skb(origskb);
  225. return NULL;
  226. }
  227. return remove_monitor_info(local, origskb);
  228. }
  229. if (should_drop_frame(origskb, present_fcs_len)) {
  230. /* only need to expand headroom if necessary */
  231. skb = origskb;
  232. origskb = NULL;
  233. /*
  234. * This shouldn't trigger often because most devices have an
  235. * RX header they pull before we get here, and that should
  236. * be big enough for our radiotap information. We should
  237. * probably export the length to drivers so that we can have
  238. * them allocate enough headroom to start with.
  239. */
  240. if (skb_headroom(skb) < needed_headroom &&
  241. pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
  242. dev_kfree_skb(skb);
  243. return NULL;
  244. }
  245. } else {
  246. /*
  247. * Need to make a copy and possibly remove radiotap header
  248. * and FCS from the original.
  249. */
  250. skb = skb_copy_expand(origskb, needed_headroom, 0, GFP_ATOMIC);
  251. origskb = remove_monitor_info(local, origskb);
  252. if (!skb)
  253. return origskb;
  254. }
  255. /* prepend radiotap information */
  256. ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom);
  257. skb_reset_mac_header(skb);
  258. skb->ip_summed = CHECKSUM_UNNECESSARY;
  259. skb->pkt_type = PACKET_OTHERHOST;
  260. skb->protocol = htons(ETH_P_802_2);
  261. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  262. if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
  263. continue;
  264. if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES)
  265. continue;
  266. if (!ieee80211_sdata_running(sdata))
  267. continue;
  268. if (prev_dev) {
  269. skb2 = skb_clone(skb, GFP_ATOMIC);
  270. if (skb2) {
  271. skb2->dev = prev_dev;
  272. netif_receive_skb(skb2);
  273. }
  274. }
  275. prev_dev = sdata->dev;
  276. sdata->dev->stats.rx_packets++;
  277. sdata->dev->stats.rx_bytes += skb->len;
  278. }
  279. if (prev_dev) {
  280. skb->dev = prev_dev;
  281. netif_receive_skb(skb);
  282. } else
  283. dev_kfree_skb(skb);
  284. return origskb;
  285. }
  286. static void ieee80211_parse_qos(struct ieee80211_rx_data *rx)
  287. {
  288. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  289. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  290. int tid, seqno_idx, security_idx;
  291. /* does the frame have a qos control field? */
  292. if (ieee80211_is_data_qos(hdr->frame_control)) {
  293. u8 *qc = ieee80211_get_qos_ctl(hdr);
  294. /* frame has qos control */
  295. tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
  296. if (*qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT)
  297. status->rx_flags |= IEEE80211_RX_AMSDU;
  298. seqno_idx = tid;
  299. security_idx = tid;
  300. } else {
  301. /*
  302. * IEEE 802.11-2007, 7.1.3.4.1 ("Sequence Number field"):
  303. *
  304. * Sequence numbers for management frames, QoS data
  305. * frames with a broadcast/multicast address in the
  306. * Address 1 field, and all non-QoS data frames sent
  307. * by QoS STAs are assigned using an additional single
  308. * modulo-4096 counter, [...]
  309. *
  310. * We also use that counter for non-QoS STAs.
  311. */
  312. seqno_idx = NUM_RX_DATA_QUEUES;
  313. security_idx = 0;
  314. if (ieee80211_is_mgmt(hdr->frame_control))
  315. security_idx = NUM_RX_DATA_QUEUES;
  316. tid = 0;
  317. }
  318. rx->seqno_idx = seqno_idx;
  319. rx->security_idx = security_idx;
  320. /* Set skb->priority to 1d tag if highest order bit of TID is not set.
  321. * For now, set skb->priority to 0 for other cases. */
  322. rx->skb->priority = (tid > 7) ? 0 : tid;
  323. }
  324. /**
  325. * DOC: Packet alignment
  326. *
  327. * Drivers always need to pass packets that are aligned to two-byte boundaries
  328. * to the stack.
  329. *
  330. * Additionally, should, if possible, align the payload data in a way that
  331. * guarantees that the contained IP header is aligned to a four-byte
  332. * boundary. In the case of regular frames, this simply means aligning the
  333. * payload to a four-byte boundary (because either the IP header is directly
  334. * contained, or IV/RFC1042 headers that have a length divisible by four are
  335. * in front of it). If the payload data is not properly aligned and the
  336. * architecture doesn't support efficient unaligned operations, mac80211
  337. * will align the data.
  338. *
  339. * With A-MSDU frames, however, the payload data address must yield two modulo
  340. * four because there are 14-byte 802.3 headers within the A-MSDU frames that
  341. * push the IP header further back to a multiple of four again. Thankfully, the
  342. * specs were sane enough this time around to require padding each A-MSDU
  343. * subframe to a length that is a multiple of four.
  344. *
  345. * Padding like Atheros hardware adds which is between the 802.11 header and
  346. * the payload is not supported, the driver is required to move the 802.11
  347. * header to be directly in front of the payload in that case.
  348. */
  349. static void ieee80211_verify_alignment(struct ieee80211_rx_data *rx)
  350. {
  351. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  352. WARN_ONCE((unsigned long)rx->skb->data & 1,
  353. "unaligned packet at 0x%p\n", rx->skb->data);
  354. #endif
  355. }
  356. /* rx handlers */
  357. static ieee80211_rx_result debug_noinline
  358. ieee80211_rx_h_passive_scan(struct ieee80211_rx_data *rx)
  359. {
  360. struct ieee80211_local *local = rx->local;
  361. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  362. struct sk_buff *skb = rx->skb;
  363. if (likely(!(status->rx_flags & IEEE80211_RX_IN_SCAN) &&
  364. !local->sched_scanning))
  365. return RX_CONTINUE;
  366. if (test_bit(SCAN_HW_SCANNING, &local->scanning) ||
  367. test_bit(SCAN_SW_SCANNING, &local->scanning) ||
  368. local->sched_scanning)
  369. return ieee80211_scan_rx(rx->sdata, skb);
  370. /* scanning finished during invoking of handlers */
  371. I802_DEBUG_INC(local->rx_handlers_drop_passive_scan);
  372. return RX_DROP_UNUSABLE;
  373. }
  374. static int ieee80211_is_unicast_robust_mgmt_frame(struct sk_buff *skb)
  375. {
  376. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  377. if (skb->len < 24 || is_multicast_ether_addr(hdr->addr1))
  378. return 0;
  379. return ieee80211_is_robust_mgmt_frame(hdr);
  380. }
  381. static int ieee80211_is_multicast_robust_mgmt_frame(struct sk_buff *skb)
  382. {
  383. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  384. if (skb->len < 24 || !is_multicast_ether_addr(hdr->addr1))
  385. return 0;
  386. return ieee80211_is_robust_mgmt_frame(hdr);
  387. }
  388. /* Get the BIP key index from MMIE; return -1 if this is not a BIP frame */
  389. static int ieee80211_get_mmie_keyidx(struct sk_buff *skb)
  390. {
  391. struct ieee80211_mgmt *hdr = (struct ieee80211_mgmt *) skb->data;
  392. struct ieee80211_mmie *mmie;
  393. if (skb->len < 24 + sizeof(*mmie) ||
  394. !is_multicast_ether_addr(hdr->da))
  395. return -1;
  396. if (!ieee80211_is_robust_mgmt_frame((struct ieee80211_hdr *) hdr))
  397. return -1; /* not a robust management frame */
  398. mmie = (struct ieee80211_mmie *)
  399. (skb->data + skb->len - sizeof(*mmie));
  400. if (mmie->element_id != WLAN_EID_MMIE ||
  401. mmie->length != sizeof(*mmie) - 2)
  402. return -1;
  403. return le16_to_cpu(mmie->key_id);
  404. }
  405. static ieee80211_rx_result
  406. ieee80211_rx_mesh_check(struct ieee80211_rx_data *rx)
  407. {
  408. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  409. char *dev_addr = rx->sdata->vif.addr;
  410. if (ieee80211_is_data(hdr->frame_control)) {
  411. if (is_multicast_ether_addr(hdr->addr1)) {
  412. if (ieee80211_has_tods(hdr->frame_control) ||
  413. !ieee80211_has_fromds(hdr->frame_control))
  414. return RX_DROP_MONITOR;
  415. if (memcmp(hdr->addr3, dev_addr, ETH_ALEN) == 0)
  416. return RX_DROP_MONITOR;
  417. } else {
  418. if (!ieee80211_has_a4(hdr->frame_control))
  419. return RX_DROP_MONITOR;
  420. if (memcmp(hdr->addr4, dev_addr, ETH_ALEN) == 0)
  421. return RX_DROP_MONITOR;
  422. }
  423. }
  424. /* If there is not an established peer link and this is not a peer link
  425. * establisment frame, beacon or probe, drop the frame.
  426. */
  427. if (!rx->sta || sta_plink_state(rx->sta) != NL80211_PLINK_ESTAB) {
  428. struct ieee80211_mgmt *mgmt;
  429. if (!ieee80211_is_mgmt(hdr->frame_control))
  430. return RX_DROP_MONITOR;
  431. if (ieee80211_is_action(hdr->frame_control)) {
  432. u8 category;
  433. mgmt = (struct ieee80211_mgmt *)hdr;
  434. category = mgmt->u.action.category;
  435. if (category != WLAN_CATEGORY_MESH_ACTION &&
  436. category != WLAN_CATEGORY_SELF_PROTECTED)
  437. return RX_DROP_MONITOR;
  438. return RX_CONTINUE;
  439. }
  440. if (ieee80211_is_probe_req(hdr->frame_control) ||
  441. ieee80211_is_probe_resp(hdr->frame_control) ||
  442. ieee80211_is_beacon(hdr->frame_control) ||
  443. ieee80211_is_auth(hdr->frame_control))
  444. return RX_CONTINUE;
  445. return RX_DROP_MONITOR;
  446. }
  447. return RX_CONTINUE;
  448. }
  449. #define SEQ_MODULO 0x1000
  450. #define SEQ_MASK 0xfff
  451. static inline int seq_less(u16 sq1, u16 sq2)
  452. {
  453. return ((sq1 - sq2) & SEQ_MASK) > (SEQ_MODULO >> 1);
  454. }
  455. static inline u16 seq_inc(u16 sq)
  456. {
  457. return (sq + 1) & SEQ_MASK;
  458. }
  459. static inline u16 seq_sub(u16 sq1, u16 sq2)
  460. {
  461. return (sq1 - sq2) & SEQ_MASK;
  462. }
  463. static void ieee80211_release_reorder_frame(struct ieee80211_hw *hw,
  464. struct tid_ampdu_rx *tid_agg_rx,
  465. int index)
  466. {
  467. struct ieee80211_local *local = hw_to_local(hw);
  468. struct sk_buff *skb = tid_agg_rx->reorder_buf[index];
  469. struct ieee80211_rx_status *status;
  470. lockdep_assert_held(&tid_agg_rx->reorder_lock);
  471. if (!skb)
  472. goto no_frame;
  473. /* release the frame from the reorder ring buffer */
  474. tid_agg_rx->stored_mpdu_num--;
  475. tid_agg_rx->reorder_buf[index] = NULL;
  476. status = IEEE80211_SKB_RXCB(skb);
  477. status->rx_flags |= IEEE80211_RX_DEFERRED_RELEASE;
  478. skb_queue_tail(&local->rx_skb_queue, skb);
  479. no_frame:
  480. tid_agg_rx->head_seq_num = seq_inc(tid_agg_rx->head_seq_num);
  481. }
  482. static void ieee80211_release_reorder_frames(struct ieee80211_hw *hw,
  483. struct tid_ampdu_rx *tid_agg_rx,
  484. u16 head_seq_num)
  485. {
  486. int index;
  487. lockdep_assert_held(&tid_agg_rx->reorder_lock);
  488. while (seq_less(tid_agg_rx->head_seq_num, head_seq_num)) {
  489. index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn) %
  490. tid_agg_rx->buf_size;
  491. ieee80211_release_reorder_frame(hw, tid_agg_rx, index);
  492. }
  493. }
  494. /*
  495. * Timeout (in jiffies) for skb's that are waiting in the RX reorder buffer. If
  496. * the skb was added to the buffer longer than this time ago, the earlier
  497. * frames that have not yet been received are assumed to be lost and the skb
  498. * can be released for processing. This may also release other skb's from the
  499. * reorder buffer if there are no additional gaps between the frames.
  500. *
  501. * Callers must hold tid_agg_rx->reorder_lock.
  502. */
  503. #define HT_RX_REORDER_BUF_TIMEOUT (HZ / 10)
  504. static void ieee80211_sta_reorder_release(struct ieee80211_hw *hw,
  505. struct tid_ampdu_rx *tid_agg_rx)
  506. {
  507. int index, j;
  508. lockdep_assert_held(&tid_agg_rx->reorder_lock);
  509. /* release the buffer until next missing frame */
  510. index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn) %
  511. tid_agg_rx->buf_size;
  512. if (!tid_agg_rx->reorder_buf[index] &&
  513. tid_agg_rx->stored_mpdu_num > 1) {
  514. /*
  515. * No buffers ready to be released, but check whether any
  516. * frames in the reorder buffer have timed out.
  517. */
  518. int skipped = 1;
  519. for (j = (index + 1) % tid_agg_rx->buf_size; j != index;
  520. j = (j + 1) % tid_agg_rx->buf_size) {
  521. if (!tid_agg_rx->reorder_buf[j]) {
  522. skipped++;
  523. continue;
  524. }
  525. if (skipped &&
  526. !time_after(jiffies, tid_agg_rx->reorder_time[j] +
  527. HT_RX_REORDER_BUF_TIMEOUT))
  528. goto set_release_timer;
  529. #ifdef CONFIG_MAC80211_HT_DEBUG
  530. if (net_ratelimit())
  531. wiphy_debug(hw->wiphy,
  532. "release an RX reorder frame due to timeout on earlier frames\n");
  533. #endif
  534. ieee80211_release_reorder_frame(hw, tid_agg_rx, j);
  535. /*
  536. * Increment the head seq# also for the skipped slots.
  537. */
  538. tid_agg_rx->head_seq_num =
  539. (tid_agg_rx->head_seq_num + skipped) & SEQ_MASK;
  540. skipped = 0;
  541. }
  542. } else while (tid_agg_rx->reorder_buf[index]) {
  543. ieee80211_release_reorder_frame(hw, tid_agg_rx, index);
  544. index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn) %
  545. tid_agg_rx->buf_size;
  546. }
  547. if (tid_agg_rx->stored_mpdu_num) {
  548. j = index = seq_sub(tid_agg_rx->head_seq_num,
  549. tid_agg_rx->ssn) % tid_agg_rx->buf_size;
  550. for (; j != (index - 1) % tid_agg_rx->buf_size;
  551. j = (j + 1) % tid_agg_rx->buf_size) {
  552. if (tid_agg_rx->reorder_buf[j])
  553. break;
  554. }
  555. set_release_timer:
  556. mod_timer(&tid_agg_rx->reorder_timer,
  557. tid_agg_rx->reorder_time[j] + 1 +
  558. HT_RX_REORDER_BUF_TIMEOUT);
  559. } else {
  560. del_timer(&tid_agg_rx->reorder_timer);
  561. }
  562. }
  563. /*
  564. * As this function belongs to the RX path it must be under
  565. * rcu_read_lock protection. It returns false if the frame
  566. * can be processed immediately, true if it was consumed.
  567. */
  568. static bool ieee80211_sta_manage_reorder_buf(struct ieee80211_hw *hw,
  569. struct tid_ampdu_rx *tid_agg_rx,
  570. struct sk_buff *skb)
  571. {
  572. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  573. u16 sc = le16_to_cpu(hdr->seq_ctrl);
  574. u16 mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
  575. u16 head_seq_num, buf_size;
  576. int index;
  577. bool ret = true;
  578. spin_lock(&tid_agg_rx->reorder_lock);
  579. buf_size = tid_agg_rx->buf_size;
  580. head_seq_num = tid_agg_rx->head_seq_num;
  581. /* frame with out of date sequence number */
  582. if (seq_less(mpdu_seq_num, head_seq_num)) {
  583. dev_kfree_skb(skb);
  584. goto out;
  585. }
  586. /*
  587. * If frame the sequence number exceeds our buffering window
  588. * size release some previous frames to make room for this one.
  589. */
  590. if (!seq_less(mpdu_seq_num, head_seq_num + buf_size)) {
  591. head_seq_num = seq_inc(seq_sub(mpdu_seq_num, buf_size));
  592. /* release stored frames up to new head to stack */
  593. ieee80211_release_reorder_frames(hw, tid_agg_rx, head_seq_num);
  594. }
  595. /* Now the new frame is always in the range of the reordering buffer */
  596. index = seq_sub(mpdu_seq_num, tid_agg_rx->ssn) % tid_agg_rx->buf_size;
  597. /* check if we already stored this frame */
  598. if (tid_agg_rx->reorder_buf[index]) {
  599. dev_kfree_skb(skb);
  600. goto out;
  601. }
  602. /*
  603. * If the current MPDU is in the right order and nothing else
  604. * is stored we can process it directly, no need to buffer it.
  605. * If it is first but there's something stored, we may be able
  606. * to release frames after this one.
  607. */
  608. if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
  609. tid_agg_rx->stored_mpdu_num == 0) {
  610. tid_agg_rx->head_seq_num = seq_inc(tid_agg_rx->head_seq_num);
  611. ret = false;
  612. goto out;
  613. }
  614. /* put the frame in the reordering buffer */
  615. tid_agg_rx->reorder_buf[index] = skb;
  616. tid_agg_rx->reorder_time[index] = jiffies;
  617. tid_agg_rx->stored_mpdu_num++;
  618. ieee80211_sta_reorder_release(hw, tid_agg_rx);
  619. out:
  620. spin_unlock(&tid_agg_rx->reorder_lock);
  621. return ret;
  622. }
  623. /*
  624. * Reorder MPDUs from A-MPDUs, keeping them on a buffer. Returns
  625. * true if the MPDU was buffered, false if it should be processed.
  626. */
  627. static void ieee80211_rx_reorder_ampdu(struct ieee80211_rx_data *rx)
  628. {
  629. struct sk_buff *skb = rx->skb;
  630. struct ieee80211_local *local = rx->local;
  631. struct ieee80211_hw *hw = &local->hw;
  632. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  633. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  634. struct sta_info *sta = rx->sta;
  635. struct tid_ampdu_rx *tid_agg_rx;
  636. u16 sc;
  637. u8 tid, ack_policy;
  638. if (!ieee80211_is_data_qos(hdr->frame_control))
  639. goto dont_reorder;
  640. /*
  641. * filter the QoS data rx stream according to
  642. * STA/TID and check if this STA/TID is on aggregation
  643. */
  644. if (!sta)
  645. goto dont_reorder;
  646. ack_policy = *ieee80211_get_qos_ctl(hdr) &
  647. IEEE80211_QOS_CTL_ACK_POLICY_MASK;
  648. tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
  649. tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
  650. if (!tid_agg_rx)
  651. goto dont_reorder;
  652. /* qos null data frames are excluded */
  653. if (unlikely(hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_NULLFUNC)))
  654. goto dont_reorder;
  655. /* not part of a BA session */
  656. if (ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_BLOCKACK &&
  657. ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_NORMAL)
  658. goto dont_reorder;
  659. /* not actually part of this BA session */
  660. if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
  661. goto dont_reorder;
  662. /* new, potentially un-ordered, ampdu frame - process it */
  663. /* reset session timer */
  664. if (tid_agg_rx->timeout)
  665. mod_timer(&tid_agg_rx->session_timer,
  666. TU_TO_EXP_TIME(tid_agg_rx->timeout));
  667. /* if this mpdu is fragmented - terminate rx aggregation session */
  668. sc = le16_to_cpu(hdr->seq_ctrl);
  669. if (sc & IEEE80211_SCTL_FRAG) {
  670. skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
  671. skb_queue_tail(&rx->sdata->skb_queue, skb);
  672. ieee80211_queue_work(&local->hw, &rx->sdata->work);
  673. return;
  674. }
  675. /*
  676. * No locking needed -- we will only ever process one
  677. * RX packet at a time, and thus own tid_agg_rx. All
  678. * other code manipulating it needs to (and does) make
  679. * sure that we cannot get to it any more before doing
  680. * anything with it.
  681. */
  682. if (ieee80211_sta_manage_reorder_buf(hw, tid_agg_rx, skb))
  683. return;
  684. dont_reorder:
  685. skb_queue_tail(&local->rx_skb_queue, skb);
  686. }
  687. static ieee80211_rx_result debug_noinline
  688. ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
  689. {
  690. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  691. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  692. /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */
  693. if (rx->sta && !is_multicast_ether_addr(hdr->addr1)) {
  694. if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
  695. rx->sta->last_seq_ctrl[rx->seqno_idx] ==
  696. hdr->seq_ctrl)) {
  697. if (status->rx_flags & IEEE80211_RX_RA_MATCH) {
  698. rx->local->dot11FrameDuplicateCount++;
  699. rx->sta->num_duplicates++;
  700. }
  701. return RX_DROP_UNUSABLE;
  702. } else
  703. rx->sta->last_seq_ctrl[rx->seqno_idx] = hdr->seq_ctrl;
  704. }
  705. if (unlikely(rx->skb->len < 16)) {
  706. I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
  707. return RX_DROP_MONITOR;
  708. }
  709. /* Drop disallowed frame classes based on STA auth/assoc state;
  710. * IEEE 802.11, Chap 5.5.
  711. *
  712. * mac80211 filters only based on association state, i.e. it drops
  713. * Class 3 frames from not associated stations. hostapd sends
  714. * deauth/disassoc frames when needed. In addition, hostapd is
  715. * responsible for filtering on both auth and assoc states.
  716. */
  717. if (ieee80211_vif_is_mesh(&rx->sdata->vif))
  718. return ieee80211_rx_mesh_check(rx);
  719. if (unlikely((ieee80211_is_data(hdr->frame_control) ||
  720. ieee80211_is_pspoll(hdr->frame_control)) &&
  721. rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  722. rx->sdata->vif.type != NL80211_IFTYPE_WDS &&
  723. (!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_ASSOC)))) {
  724. if (rx->sta && rx->sta->dummy &&
  725. ieee80211_is_data_present(hdr->frame_control)) {
  726. u16 ethertype;
  727. u8 *payload;
  728. payload = rx->skb->data +
  729. ieee80211_hdrlen(hdr->frame_control);
  730. ethertype = (payload[6] << 8) | payload[7];
  731. if (cpu_to_be16(ethertype) ==
  732. rx->sdata->control_port_protocol)
  733. return RX_CONTINUE;
  734. }
  735. if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
  736. cfg80211_rx_spurious_frame(rx->sdata->dev,
  737. hdr->addr2,
  738. GFP_ATOMIC))
  739. return RX_DROP_UNUSABLE;
  740. return RX_DROP_MONITOR;
  741. }
  742. return RX_CONTINUE;
  743. }
  744. static ieee80211_rx_result debug_noinline
  745. ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
  746. {
  747. struct sk_buff *skb = rx->skb;
  748. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  749. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  750. int keyidx;
  751. int hdrlen;
  752. ieee80211_rx_result result = RX_DROP_UNUSABLE;
  753. struct ieee80211_key *sta_ptk = NULL;
  754. int mmie_keyidx = -1;
  755. __le16 fc;
  756. /*
  757. * Key selection 101
  758. *
  759. * There are four types of keys:
  760. * - GTK (group keys)
  761. * - IGTK (group keys for management frames)
  762. * - PTK (pairwise keys)
  763. * - STK (station-to-station pairwise keys)
  764. *
  765. * When selecting a key, we have to distinguish between multicast
  766. * (including broadcast) and unicast frames, the latter can only
  767. * use PTKs and STKs while the former always use GTKs and IGTKs.
  768. * Unless, of course, actual WEP keys ("pre-RSNA") are used, then
  769. * unicast frames can also use key indices like GTKs. Hence, if we
  770. * don't have a PTK/STK we check the key index for a WEP key.
  771. *
  772. * Note that in a regular BSS, multicast frames are sent by the
  773. * AP only, associated stations unicast the frame to the AP first
  774. * which then multicasts it on their behalf.
  775. *
  776. * There is also a slight problem in IBSS mode: GTKs are negotiated
  777. * with each station, that is something we don't currently handle.
  778. * The spec seems to expect that one negotiates the same key with
  779. * every station but there's no such requirement; VLANs could be
  780. * possible.
  781. */
  782. /*
  783. * No point in finding a key and decrypting if the frame is neither
  784. * addressed to us nor a multicast frame.
  785. */
  786. if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
  787. return RX_CONTINUE;
  788. /* start without a key */
  789. rx->key = NULL;
  790. if (rx->sta)
  791. sta_ptk = rcu_dereference(rx->sta->ptk);
  792. fc = hdr->frame_control;
  793. if (!ieee80211_has_protected(fc))
  794. mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
  795. if (!is_multicast_ether_addr(hdr->addr1) && sta_ptk) {
  796. rx->key = sta_ptk;
  797. if ((status->flag & RX_FLAG_DECRYPTED) &&
  798. (status->flag & RX_FLAG_IV_STRIPPED))
  799. return RX_CONTINUE;
  800. /* Skip decryption if the frame is not protected. */
  801. if (!ieee80211_has_protected(fc))
  802. return RX_CONTINUE;
  803. } else if (mmie_keyidx >= 0) {
  804. /* Broadcast/multicast robust management frame / BIP */
  805. if ((status->flag & RX_FLAG_DECRYPTED) &&
  806. (status->flag & RX_FLAG_IV_STRIPPED))
  807. return RX_CONTINUE;
  808. if (mmie_keyidx < NUM_DEFAULT_KEYS ||
  809. mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
  810. return RX_DROP_MONITOR; /* unexpected BIP keyidx */
  811. if (rx->sta)
  812. rx->key = rcu_dereference(rx->sta->gtk[mmie_keyidx]);
  813. if (!rx->key)
  814. rx->key = rcu_dereference(rx->sdata->keys[mmie_keyidx]);
  815. } else if (!ieee80211_has_protected(fc)) {
  816. /*
  817. * The frame was not protected, so skip decryption. However, we
  818. * need to set rx->key if there is a key that could have been
  819. * used so that the frame may be dropped if encryption would
  820. * have been expected.
  821. */
  822. struct ieee80211_key *key = NULL;
  823. struct ieee80211_sub_if_data *sdata = rx->sdata;
  824. int i;
  825. if (ieee80211_is_mgmt(fc) &&
  826. is_multicast_ether_addr(hdr->addr1) &&
  827. (key = rcu_dereference(rx->sdata->default_mgmt_key)))
  828. rx->key = key;
  829. else {
  830. if (rx->sta) {
  831. for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
  832. key = rcu_dereference(rx->sta->gtk[i]);
  833. if (key)
  834. break;
  835. }
  836. }
  837. if (!key) {
  838. for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
  839. key = rcu_dereference(sdata->keys[i]);
  840. if (key)
  841. break;
  842. }
  843. }
  844. if (key)
  845. rx->key = key;
  846. }
  847. return RX_CONTINUE;
  848. } else {
  849. u8 keyid;
  850. /*
  851. * The device doesn't give us the IV so we won't be
  852. * able to look up the key. That's ok though, we
  853. * don't need to decrypt the frame, we just won't
  854. * be able to keep statistics accurate.
  855. * Except for key threshold notifications, should
  856. * we somehow allow the driver to tell us which key
  857. * the hardware used if this flag is set?
  858. */
  859. if ((status->flag & RX_FLAG_DECRYPTED) &&
  860. (status->flag & RX_FLAG_IV_STRIPPED))
  861. return RX_CONTINUE;
  862. hdrlen = ieee80211_hdrlen(fc);
  863. if (rx->skb->len < 8 + hdrlen)
  864. return RX_DROP_UNUSABLE; /* TODO: count this? */
  865. /*
  866. * no need to call ieee80211_wep_get_keyidx,
  867. * it verifies a bunch of things we've done already
  868. */
  869. skb_copy_bits(rx->skb, hdrlen + 3, &keyid, 1);
  870. keyidx = keyid >> 6;
  871. /* check per-station GTK first, if multicast packet */
  872. if (is_multicast_ether_addr(hdr->addr1) && rx->sta)
  873. rx->key = rcu_dereference(rx->sta->gtk[keyidx]);
  874. /* if not found, try default key */
  875. if (!rx->key) {
  876. rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
  877. /*
  878. * RSNA-protected unicast frames should always be
  879. * sent with pairwise or station-to-station keys,
  880. * but for WEP we allow using a key index as well.
  881. */
  882. if (rx->key &&
  883. rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP40 &&
  884. rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP104 &&
  885. !is_multicast_ether_addr(hdr->addr1))
  886. rx->key = NULL;
  887. }
  888. }
  889. if (rx->key) {
  890. if (unlikely(rx->key->flags & KEY_FLAG_TAINTED))
  891. return RX_DROP_MONITOR;
  892. rx->key->tx_rx_count++;
  893. /* TODO: add threshold stuff again */
  894. } else {
  895. return RX_DROP_MONITOR;
  896. }
  897. if (skb_linearize(rx->skb))
  898. return RX_DROP_UNUSABLE;
  899. /* the hdr variable is invalid now! */
  900. switch (rx->key->conf.cipher) {
  901. case WLAN_CIPHER_SUITE_WEP40:
  902. case WLAN_CIPHER_SUITE_WEP104:
  903. /* Check for weak IVs if possible */
  904. if (rx->sta && ieee80211_is_data(fc) &&
  905. (!(status->flag & RX_FLAG_IV_STRIPPED) ||
  906. !(status->flag & RX_FLAG_DECRYPTED)) &&
  907. ieee80211_wep_is_weak_iv(rx->skb, rx->key))
  908. rx->sta->wep_weak_iv_count++;
  909. result = ieee80211_crypto_wep_decrypt(rx);
  910. break;
  911. case WLAN_CIPHER_SUITE_TKIP:
  912. result = ieee80211_crypto_tkip_decrypt(rx);
  913. break;
  914. case WLAN_CIPHER_SUITE_CCMP:
  915. result = ieee80211_crypto_ccmp_decrypt(rx);
  916. break;
  917. case WLAN_CIPHER_SUITE_AES_CMAC:
  918. result = ieee80211_crypto_aes_cmac_decrypt(rx);
  919. break;
  920. default:
  921. /*
  922. * We can reach here only with HW-only algorithms
  923. * but why didn't it decrypt the frame?!
  924. */
  925. return RX_DROP_UNUSABLE;
  926. }
  927. /* either the frame has been decrypted or will be dropped */
  928. status->flag |= RX_FLAG_DECRYPTED;
  929. return result;
  930. }
  931. static ieee80211_rx_result debug_noinline
  932. ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
  933. {
  934. struct ieee80211_local *local;
  935. struct ieee80211_hdr *hdr;
  936. struct sk_buff *skb;
  937. local = rx->local;
  938. skb = rx->skb;
  939. hdr = (struct ieee80211_hdr *) skb->data;
  940. if (!local->pspolling)
  941. return RX_CONTINUE;
  942. if (!ieee80211_has_fromds(hdr->frame_control))
  943. /* this is not from AP */
  944. return RX_CONTINUE;
  945. if (!ieee80211_is_data(hdr->frame_control))
  946. return RX_CONTINUE;
  947. if (!ieee80211_has_moredata(hdr->frame_control)) {
  948. /* AP has no more frames buffered for us */
  949. local->pspolling = false;
  950. return RX_CONTINUE;
  951. }
  952. /* more data bit is set, let's request a new frame from the AP */
  953. ieee80211_send_pspoll(local, rx->sdata);
  954. return RX_CONTINUE;
  955. }
  956. static void ap_sta_ps_start(struct sta_info *sta)
  957. {
  958. struct ieee80211_sub_if_data *sdata = sta->sdata;
  959. struct ieee80211_local *local = sdata->local;
  960. atomic_inc(&sdata->bss->num_sta_ps);
  961. set_sta_flag(sta, WLAN_STA_PS_STA);
  962. if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
  963. drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta);
  964. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  965. printk(KERN_DEBUG "%s: STA %pM aid %d enters power save mode\n",
  966. sdata->name, sta->sta.addr, sta->sta.aid);
  967. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  968. }
  969. static void ap_sta_ps_end(struct sta_info *sta)
  970. {
  971. struct ieee80211_sub_if_data *sdata = sta->sdata;
  972. atomic_dec(&sdata->bss->num_sta_ps);
  973. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  974. printk(KERN_DEBUG "%s: STA %pM aid %d exits power save mode\n",
  975. sdata->name, sta->sta.addr, sta->sta.aid);
  976. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  977. if (test_sta_flag(sta, WLAN_STA_PS_DRIVER)) {
  978. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  979. printk(KERN_DEBUG "%s: STA %pM aid %d driver-ps-blocked\n",
  980. sdata->name, sta->sta.addr, sta->sta.aid);
  981. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  982. return;
  983. }
  984. ieee80211_sta_ps_deliver_wakeup(sta);
  985. }
  986. int ieee80211_sta_ps_transition(struct ieee80211_sta *sta, bool start)
  987. {
  988. struct sta_info *sta_inf = container_of(sta, struct sta_info, sta);
  989. bool in_ps;
  990. WARN_ON(!(sta_inf->local->hw.flags & IEEE80211_HW_AP_LINK_PS));
  991. /* Don't let the same PS state be set twice */
  992. in_ps = test_sta_flag(sta_inf, WLAN_STA_PS_STA);
  993. if ((start && in_ps) || (!start && !in_ps))
  994. return -EINVAL;
  995. if (start)
  996. ap_sta_ps_start(sta_inf);
  997. else
  998. ap_sta_ps_end(sta_inf);
  999. return 0;
  1000. }
  1001. EXPORT_SYMBOL(ieee80211_sta_ps_transition);
  1002. static ieee80211_rx_result debug_noinline
  1003. ieee80211_rx_h_uapsd_and_pspoll(struct ieee80211_rx_data *rx)
  1004. {
  1005. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1006. struct ieee80211_hdr *hdr = (void *)rx->skb->data;
  1007. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1008. int tid, ac;
  1009. if (!rx->sta || !(status->rx_flags & IEEE80211_RX_RA_MATCH))
  1010. return RX_CONTINUE;
  1011. if (sdata->vif.type != NL80211_IFTYPE_AP &&
  1012. sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
  1013. return RX_CONTINUE;
  1014. /*
  1015. * The device handles station powersave, so don't do anything about
  1016. * uAPSD and PS-Poll frames (the latter shouldn't even come up from
  1017. * it to mac80211 since they're handled.)
  1018. */
  1019. if (sdata->local->hw.flags & IEEE80211_HW_AP_LINK_PS)
  1020. return RX_CONTINUE;
  1021. /*
  1022. * Don't do anything if the station isn't already asleep. In
  1023. * the uAPSD case, the station will probably be marked asleep,
  1024. * in the PS-Poll case the station must be confused ...
  1025. */
  1026. if (!test_sta_flag(rx->sta, WLAN_STA_PS_STA))
  1027. return RX_CONTINUE;
  1028. if (unlikely(ieee80211_is_pspoll(hdr->frame_control))) {
  1029. if (!test_sta_flag(rx->sta, WLAN_STA_SP)) {
  1030. if (!test_sta_flag(rx->sta, WLAN_STA_PS_DRIVER))
  1031. ieee80211_sta_ps_deliver_poll_response(rx->sta);
  1032. else
  1033. set_sta_flag(rx->sta, WLAN_STA_PSPOLL);
  1034. }
  1035. /* Free PS Poll skb here instead of returning RX_DROP that would
  1036. * count as an dropped frame. */
  1037. dev_kfree_skb(rx->skb);
  1038. return RX_QUEUED;
  1039. } else if (!ieee80211_has_morefrags(hdr->frame_control) &&
  1040. !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
  1041. ieee80211_has_pm(hdr->frame_control) &&
  1042. (ieee80211_is_data_qos(hdr->frame_control) ||
  1043. ieee80211_is_qos_nullfunc(hdr->frame_control))) {
  1044. tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
  1045. ac = ieee802_1d_to_ac[tid & 7];
  1046. /*
  1047. * If this AC is not trigger-enabled do nothing.
  1048. *
  1049. * NB: This could/should check a separate bitmap of trigger-
  1050. * enabled queues, but for now we only implement uAPSD w/o
  1051. * TSPEC changes to the ACs, so they're always the same.
  1052. */
  1053. if (!(rx->sta->sta.uapsd_queues & BIT(ac)))
  1054. return RX_CONTINUE;
  1055. /* if we are in a service period, do nothing */
  1056. if (test_sta_flag(rx->sta, WLAN_STA_SP))
  1057. return RX_CONTINUE;
  1058. if (!test_sta_flag(rx->sta, WLAN_STA_PS_DRIVER))
  1059. ieee80211_sta_ps_deliver_uapsd(rx->sta);
  1060. else
  1061. set_sta_flag(rx->sta, WLAN_STA_UAPSD);
  1062. }
  1063. return RX_CONTINUE;
  1064. }
  1065. static ieee80211_rx_result debug_noinline
  1066. ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
  1067. {
  1068. struct sta_info *sta = rx->sta;
  1069. struct sk_buff *skb = rx->skb;
  1070. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  1071. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1072. if (!sta)
  1073. return RX_CONTINUE;
  1074. /*
  1075. * Update last_rx only for IBSS packets which are for the current
  1076. * BSSID to avoid keeping the current IBSS network alive in cases
  1077. * where other STAs start using different BSSID.
  1078. */
  1079. if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  1080. u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
  1081. NL80211_IFTYPE_ADHOC);
  1082. if (compare_ether_addr(bssid, rx->sdata->u.ibss.bssid) == 0) {
  1083. sta->last_rx = jiffies;
  1084. if (ieee80211_is_data(hdr->frame_control)) {
  1085. sta->last_rx_rate_idx = status->rate_idx;
  1086. sta->last_rx_rate_flag = status->flag;
  1087. }
  1088. }
  1089. } else if (!is_multicast_ether_addr(hdr->addr1)) {
  1090. /*
  1091. * Mesh beacons will update last_rx when if they are found to
  1092. * match the current local configuration when processed.
  1093. */
  1094. sta->last_rx = jiffies;
  1095. if (ieee80211_is_data(hdr->frame_control)) {
  1096. sta->last_rx_rate_idx = status->rate_idx;
  1097. sta->last_rx_rate_flag = status->flag;
  1098. }
  1099. }
  1100. if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
  1101. return RX_CONTINUE;
  1102. if (rx->sdata->vif.type == NL80211_IFTYPE_STATION)
  1103. ieee80211_sta_rx_notify(rx->sdata, hdr);
  1104. sta->rx_fragments++;
  1105. sta->rx_bytes += rx->skb->len;
  1106. sta->last_signal = status->signal;
  1107. ewma_add(&sta->avg_signal, -status->signal);
  1108. /*
  1109. * Change STA power saving mode only at the end of a frame
  1110. * exchange sequence.
  1111. */
  1112. if (!(sta->local->hw.flags & IEEE80211_HW_AP_LINK_PS) &&
  1113. !ieee80211_has_morefrags(hdr->frame_control) &&
  1114. !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
  1115. (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
  1116. rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)) {
  1117. if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
  1118. /*
  1119. * Ignore doze->wake transitions that are
  1120. * indicated by non-data frames, the standard
  1121. * is unclear here, but for example going to
  1122. * PS mode and then scanning would cause a
  1123. * doze->wake transition for the probe request,
  1124. * and that is clearly undesirable.
  1125. */
  1126. if (ieee80211_is_data(hdr->frame_control) &&
  1127. !ieee80211_has_pm(hdr->frame_control))
  1128. ap_sta_ps_end(sta);
  1129. } else {
  1130. if (ieee80211_has_pm(hdr->frame_control))
  1131. ap_sta_ps_start(sta);
  1132. }
  1133. }
  1134. /*
  1135. * Drop (qos-)data::nullfunc frames silently, since they
  1136. * are used only to control station power saving mode.
  1137. */
  1138. if (ieee80211_is_nullfunc(hdr->frame_control) ||
  1139. ieee80211_is_qos_nullfunc(hdr->frame_control)) {
  1140. I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
  1141. /*
  1142. * If we receive a 4-addr nullfunc frame from a STA
  1143. * that was not moved to a 4-addr STA vlan yet send
  1144. * the event to userspace and for older hostapd drop
  1145. * the frame to the monitor interface.
  1146. */
  1147. if (ieee80211_has_a4(hdr->frame_control) &&
  1148. (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
  1149. (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  1150. !rx->sdata->u.vlan.sta))) {
  1151. if (!test_and_set_sta_flag(sta, WLAN_STA_4ADDR_EVENT))
  1152. cfg80211_rx_unexpected_4addr_frame(
  1153. rx->sdata->dev, sta->sta.addr,
  1154. GFP_ATOMIC);
  1155. return RX_DROP_MONITOR;
  1156. }
  1157. /*
  1158. * Update counter and free packet here to avoid
  1159. * counting this as a dropped packed.
  1160. */
  1161. sta->rx_packets++;
  1162. dev_kfree_skb(rx->skb);
  1163. return RX_QUEUED;
  1164. }
  1165. return RX_CONTINUE;
  1166. } /* ieee80211_rx_h_sta_process */
  1167. static inline struct ieee80211_fragment_entry *
  1168. ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
  1169. unsigned int frag, unsigned int seq, int rx_queue,
  1170. struct sk_buff **skb)
  1171. {
  1172. struct ieee80211_fragment_entry *entry;
  1173. int idx;
  1174. idx = sdata->fragment_next;
  1175. entry = &sdata->fragments[sdata->fragment_next++];
  1176. if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
  1177. sdata->fragment_next = 0;
  1178. if (!skb_queue_empty(&entry->skb_list)) {
  1179. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1180. struct ieee80211_hdr *hdr =
  1181. (struct ieee80211_hdr *) entry->skb_list.next->data;
  1182. printk(KERN_DEBUG "%s: RX reassembly removed oldest "
  1183. "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
  1184. "addr1=%pM addr2=%pM\n",
  1185. sdata->name, idx,
  1186. jiffies - entry->first_frag_time, entry->seq,
  1187. entry->last_frag, hdr->addr1, hdr->addr2);
  1188. #endif
  1189. __skb_queue_purge(&entry->skb_list);
  1190. }
  1191. __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
  1192. *skb = NULL;
  1193. entry->first_frag_time = jiffies;
  1194. entry->seq = seq;
  1195. entry->rx_queue = rx_queue;
  1196. entry->last_frag = frag;
  1197. entry->ccmp = 0;
  1198. entry->extra_len = 0;
  1199. return entry;
  1200. }
  1201. static inline struct ieee80211_fragment_entry *
  1202. ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
  1203. unsigned int frag, unsigned int seq,
  1204. int rx_queue, struct ieee80211_hdr *hdr)
  1205. {
  1206. struct ieee80211_fragment_entry *entry;
  1207. int i, idx;
  1208. idx = sdata->fragment_next;
  1209. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
  1210. struct ieee80211_hdr *f_hdr;
  1211. idx--;
  1212. if (idx < 0)
  1213. idx = IEEE80211_FRAGMENT_MAX - 1;
  1214. entry = &sdata->fragments[idx];
  1215. if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
  1216. entry->rx_queue != rx_queue ||
  1217. entry->last_frag + 1 != frag)
  1218. continue;
  1219. f_hdr = (struct ieee80211_hdr *)entry->skb_list.next->data;
  1220. /*
  1221. * Check ftype and addresses are equal, else check next fragment
  1222. */
  1223. if (((hdr->frame_control ^ f_hdr->frame_control) &
  1224. cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
  1225. compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 ||
  1226. compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0)
  1227. continue;
  1228. if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
  1229. __skb_queue_purge(&entry->skb_list);
  1230. continue;
  1231. }
  1232. return entry;
  1233. }
  1234. return NULL;
  1235. }
  1236. static ieee80211_rx_result debug_noinline
  1237. ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
  1238. {
  1239. struct ieee80211_hdr *hdr;
  1240. u16 sc;
  1241. __le16 fc;
  1242. unsigned int frag, seq;
  1243. struct ieee80211_fragment_entry *entry;
  1244. struct sk_buff *skb;
  1245. struct ieee80211_rx_status *status;
  1246. hdr = (struct ieee80211_hdr *)rx->skb->data;
  1247. fc = hdr->frame_control;
  1248. sc = le16_to_cpu(hdr->seq_ctrl);
  1249. frag = sc & IEEE80211_SCTL_FRAG;
  1250. if (likely((!ieee80211_has_morefrags(fc) && frag == 0) ||
  1251. (rx->skb)->len < 24 ||
  1252. is_multicast_ether_addr(hdr->addr1))) {
  1253. /* not fragmented */
  1254. goto out;
  1255. }
  1256. I802_DEBUG_INC(rx->local->rx_handlers_fragments);
  1257. if (skb_linearize(rx->skb))
  1258. return RX_DROP_UNUSABLE;
  1259. /*
  1260. * skb_linearize() might change the skb->data and
  1261. * previously cached variables (in this case, hdr) need to
  1262. * be refreshed with the new data.
  1263. */
  1264. hdr = (struct ieee80211_hdr *)rx->skb->data;
  1265. seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
  1266. if (frag == 0) {
  1267. /* This is the first fragment of a new frame. */
  1268. entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
  1269. rx->seqno_idx, &(rx->skb));
  1270. if (rx->key && rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP &&
  1271. ieee80211_has_protected(fc)) {
  1272. int queue = rx->security_idx;
  1273. /* Store CCMP PN so that we can verify that the next
  1274. * fragment has a sequential PN value. */
  1275. entry->ccmp = 1;
  1276. memcpy(entry->last_pn,
  1277. rx->key->u.ccmp.rx_pn[queue],
  1278. CCMP_PN_LEN);
  1279. }
  1280. return RX_QUEUED;
  1281. }
  1282. /* This is a fragment for a frame that should already be pending in
  1283. * fragment cache. Add this fragment to the end of the pending entry.
  1284. */
  1285. entry = ieee80211_reassemble_find(rx->sdata, frag, seq,
  1286. rx->seqno_idx, hdr);
  1287. if (!entry) {
  1288. I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
  1289. return RX_DROP_MONITOR;
  1290. }
  1291. /* Verify that MPDUs within one MSDU have sequential PN values.
  1292. * (IEEE 802.11i, 8.3.3.4.5) */
  1293. if (entry->ccmp) {
  1294. int i;
  1295. u8 pn[CCMP_PN_LEN], *rpn;
  1296. int queue;
  1297. if (!rx->key || rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP)
  1298. return RX_DROP_UNUSABLE;
  1299. memcpy(pn, entry->last_pn, CCMP_PN_LEN);
  1300. for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
  1301. pn[i]++;
  1302. if (pn[i])
  1303. break;
  1304. }
  1305. queue = rx->security_idx;
  1306. rpn = rx->key->u.ccmp.rx_pn[queue];
  1307. if (memcmp(pn, rpn, CCMP_PN_LEN))
  1308. return RX_DROP_UNUSABLE;
  1309. memcpy(entry->last_pn, pn, CCMP_PN_LEN);
  1310. }
  1311. skb_pull(rx->skb, ieee80211_hdrlen(fc));
  1312. __skb_queue_tail(&entry->skb_list, rx->skb);
  1313. entry->last_frag = frag;
  1314. entry->extra_len += rx->skb->len;
  1315. if (ieee80211_has_morefrags(fc)) {
  1316. rx->skb = NULL;
  1317. return RX_QUEUED;
  1318. }
  1319. rx->skb = __skb_dequeue(&entry->skb_list);
  1320. if (skb_tailroom(rx->skb) < entry->extra_len) {
  1321. I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
  1322. if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
  1323. GFP_ATOMIC))) {
  1324. I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
  1325. __skb_queue_purge(&entry->skb_list);
  1326. return RX_DROP_UNUSABLE;
  1327. }
  1328. }
  1329. while ((skb = __skb_dequeue(&entry->skb_list))) {
  1330. memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
  1331. dev_kfree_skb(skb);
  1332. }
  1333. /* Complete frame has been reassembled - process it now */
  1334. status = IEEE80211_SKB_RXCB(rx->skb);
  1335. status->rx_flags |= IEEE80211_RX_FRAGMENTED;
  1336. out:
  1337. if (rx->sta)
  1338. rx->sta->rx_packets++;
  1339. if (is_multicast_ether_addr(hdr->addr1))
  1340. rx->local->dot11MulticastReceivedFrameCount++;
  1341. else
  1342. ieee80211_led_rx(rx->local);
  1343. return RX_CONTINUE;
  1344. }
  1345. static ieee80211_rx_result debug_noinline
  1346. ieee80211_rx_h_remove_qos_control(struct ieee80211_rx_data *rx)
  1347. {
  1348. u8 *data = rx->skb->data;
  1349. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)data;
  1350. if (!ieee80211_is_data_qos(hdr->frame_control))
  1351. return RX_CONTINUE;
  1352. /* remove the qos control field, update frame type and meta-data */
  1353. memmove(data + IEEE80211_QOS_CTL_LEN, data,
  1354. ieee80211_hdrlen(hdr->frame_control) - IEEE80211_QOS_CTL_LEN);
  1355. hdr = (struct ieee80211_hdr *)skb_pull(rx->skb, IEEE80211_QOS_CTL_LEN);
  1356. /* change frame type to non QOS */
  1357. hdr->frame_control &= ~cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
  1358. return RX_CONTINUE;
  1359. }
  1360. static int
  1361. ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
  1362. {
  1363. if (unlikely(!rx->sta ||
  1364. !test_sta_flag(rx->sta, WLAN_STA_AUTHORIZED)))
  1365. return -EACCES;
  1366. return 0;
  1367. }
  1368. static int
  1369. ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
  1370. {
  1371. struct sk_buff *skb = rx->skb;
  1372. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  1373. /*
  1374. * Pass through unencrypted frames if the hardware has
  1375. * decrypted them already.
  1376. */
  1377. if (status->flag & RX_FLAG_DECRYPTED)
  1378. return 0;
  1379. /* Drop unencrypted frames if key is set. */
  1380. if (unlikely(!ieee80211_has_protected(fc) &&
  1381. !ieee80211_is_nullfunc(fc) &&
  1382. ieee80211_is_data(fc) &&
  1383. (rx->key || rx->sdata->drop_unencrypted)))
  1384. return -EACCES;
  1385. return 0;
  1386. }
  1387. static int
  1388. ieee80211_drop_unencrypted_mgmt(struct ieee80211_rx_data *rx)
  1389. {
  1390. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  1391. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1392. __le16 fc = hdr->frame_control;
  1393. /*
  1394. * Pass through unencrypted frames if the hardware has
  1395. * decrypted them already.
  1396. */
  1397. if (status->flag & RX_FLAG_DECRYPTED)
  1398. return 0;
  1399. if (rx->sta && test_sta_flag(rx->sta, WLAN_STA_MFP)) {
  1400. if (unlikely(!ieee80211_has_protected(fc) &&
  1401. ieee80211_is_unicast_robust_mgmt_frame(rx->skb) &&
  1402. rx->key)) {
  1403. if (ieee80211_is_deauth(fc))
  1404. cfg80211_send_unprot_deauth(rx->sdata->dev,
  1405. rx->skb->data,
  1406. rx->skb->len);
  1407. else if (ieee80211_is_disassoc(fc))
  1408. cfg80211_send_unprot_disassoc(rx->sdata->dev,
  1409. rx->skb->data,
  1410. rx->skb->len);
  1411. return -EACCES;
  1412. }
  1413. /* BIP does not use Protected field, so need to check MMIE */
  1414. if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb) &&
  1415. ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
  1416. if (ieee80211_is_deauth(fc))
  1417. cfg80211_send_unprot_deauth(rx->sdata->dev,
  1418. rx->skb->data,
  1419. rx->skb->len);
  1420. else if (ieee80211_is_disassoc(fc))
  1421. cfg80211_send_unprot_disassoc(rx->sdata->dev,
  1422. rx->skb->data,
  1423. rx->skb->len);
  1424. return -EACCES;
  1425. }
  1426. /*
  1427. * When using MFP, Action frames are not allowed prior to
  1428. * having configured keys.
  1429. */
  1430. if (unlikely(ieee80211_is_action(fc) && !rx->key &&
  1431. ieee80211_is_robust_mgmt_frame(
  1432. (struct ieee80211_hdr *) rx->skb->data)))
  1433. return -EACCES;
  1434. }
  1435. return 0;
  1436. }
  1437. static int
  1438. __ieee80211_data_to_8023(struct ieee80211_rx_data *rx, bool *port_control)
  1439. {
  1440. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1441. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  1442. bool check_port_control = false;
  1443. struct ethhdr *ehdr;
  1444. int ret;
  1445. *port_control = false;
  1446. if (ieee80211_has_a4(hdr->frame_control) &&
  1447. sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta)
  1448. return -1;
  1449. if (sdata->vif.type == NL80211_IFTYPE_STATION &&
  1450. !!sdata->u.mgd.use_4addr != !!ieee80211_has_a4(hdr->frame_control)) {
  1451. if (!sdata->u.mgd.use_4addr)
  1452. return -1;
  1453. else
  1454. check_port_control = true;
  1455. }
  1456. if (is_multicast_ether_addr(hdr->addr1) &&
  1457. sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta)
  1458. return -1;
  1459. ret = ieee80211_data_to_8023(rx->skb, sdata->vif.addr, sdata->vif.type);
  1460. if (ret < 0)
  1461. return ret;
  1462. ehdr = (struct ethhdr *) rx->skb->data;
  1463. if (ehdr->h_proto == rx->sdata->control_port_protocol)
  1464. *port_control = true;
  1465. else if (check_port_control)
  1466. return -1;
  1467. return 0;
  1468. }
  1469. /*
  1470. * requires that rx->skb is a frame with ethernet header
  1471. */
  1472. static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
  1473. {
  1474. static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
  1475. = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
  1476. struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
  1477. /*
  1478. * Allow EAPOL frames to us/the PAE group address regardless
  1479. * of whether the frame was encrypted or not.
  1480. */
  1481. if (ehdr->h_proto == rx->sdata->control_port_protocol &&
  1482. (compare_ether_addr(ehdr->h_dest, rx->sdata->vif.addr) == 0 ||
  1483. compare_ether_addr(ehdr->h_dest, pae_group_addr) == 0))
  1484. return true;
  1485. if (ieee80211_802_1x_port_control(rx) ||
  1486. ieee80211_drop_unencrypted(rx, fc))
  1487. return false;
  1488. return true;
  1489. }
  1490. /*
  1491. * requires that rx->skb is a frame with ethernet header
  1492. */
  1493. static void
  1494. ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
  1495. {
  1496. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1497. struct net_device *dev = sdata->dev;
  1498. struct sk_buff *skb, *xmit_skb;
  1499. struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
  1500. struct sta_info *dsta;
  1501. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1502. skb = rx->skb;
  1503. xmit_skb = NULL;
  1504. if ((sdata->vif.type == NL80211_IFTYPE_AP ||
  1505. sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
  1506. !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
  1507. (status->rx_flags & IEEE80211_RX_RA_MATCH) &&
  1508. (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) {
  1509. if (is_multicast_ether_addr(ehdr->h_dest)) {
  1510. /*
  1511. * send multicast frames both to higher layers in
  1512. * local net stack and back to the wireless medium
  1513. */
  1514. xmit_skb = skb_copy(skb, GFP_ATOMIC);
  1515. if (!xmit_skb && net_ratelimit())
  1516. printk(KERN_DEBUG "%s: failed to clone "
  1517. "multicast frame\n", dev->name);
  1518. } else {
  1519. dsta = sta_info_get(sdata, skb->data);
  1520. if (dsta) {
  1521. /*
  1522. * The destination station is associated to
  1523. * this AP (in this VLAN), so send the frame
  1524. * directly to it and do not pass it to local
  1525. * net stack.
  1526. */
  1527. xmit_skb = skb;
  1528. skb = NULL;
  1529. }
  1530. }
  1531. }
  1532. if (skb) {
  1533. int align __maybe_unused;
  1534. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1535. /*
  1536. * 'align' will only take the values 0 or 2 here
  1537. * since all frames are required to be aligned
  1538. * to 2-byte boundaries when being passed to
  1539. * mac80211. That also explains the __skb_push()
  1540. * below.
  1541. */
  1542. align = ((unsigned long)(skb->data + sizeof(struct ethhdr))) & 3;
  1543. if (align) {
  1544. if (WARN_ON(skb_headroom(skb) < 3)) {
  1545. dev_kfree_skb(skb);
  1546. skb = NULL;
  1547. } else {
  1548. u8 *data = skb->data;
  1549. size_t len = skb_headlen(skb);
  1550. skb->data -= align;
  1551. memmove(skb->data, data, len);
  1552. skb_set_tail_pointer(skb, len);
  1553. }
  1554. }
  1555. #endif
  1556. if (skb) {
  1557. /* deliver to local stack */
  1558. skb->protocol = eth_type_trans(skb, dev);
  1559. memset(skb->cb, 0, sizeof(skb->cb));
  1560. netif_receive_skb(skb);
  1561. }
  1562. }
  1563. if (xmit_skb) {
  1564. /* send to wireless media */
  1565. xmit_skb->protocol = htons(ETH_P_802_3);
  1566. skb_reset_network_header(xmit_skb);
  1567. skb_reset_mac_header(xmit_skb);
  1568. dev_queue_xmit(xmit_skb);
  1569. }
  1570. }
  1571. static ieee80211_rx_result debug_noinline
  1572. ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
  1573. {
  1574. struct net_device *dev = rx->sdata->dev;
  1575. struct sk_buff *skb = rx->skb;
  1576. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1577. __le16 fc = hdr->frame_control;
  1578. struct sk_buff_head frame_list;
  1579. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1580. if (unlikely(!ieee80211_is_data(fc)))
  1581. return RX_CONTINUE;
  1582. if (unlikely(!ieee80211_is_data_present(fc)))
  1583. return RX_DROP_MONITOR;
  1584. if (!(status->rx_flags & IEEE80211_RX_AMSDU))
  1585. return RX_CONTINUE;
  1586. if (ieee80211_has_a4(hdr->frame_control) &&
  1587. rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  1588. !rx->sdata->u.vlan.sta)
  1589. return RX_DROP_UNUSABLE;
  1590. if (is_multicast_ether_addr(hdr->addr1) &&
  1591. ((rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  1592. rx->sdata->u.vlan.sta) ||
  1593. (rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
  1594. rx->sdata->u.mgd.use_4addr)))
  1595. return RX_DROP_UNUSABLE;
  1596. skb->dev = dev;
  1597. __skb_queue_head_init(&frame_list);
  1598. if (skb_linearize(skb))
  1599. return RX_DROP_UNUSABLE;
  1600. ieee80211_amsdu_to_8023s(skb, &frame_list, dev->dev_addr,
  1601. rx->sdata->vif.type,
  1602. rx->local->hw.extra_tx_headroom, true);
  1603. while (!skb_queue_empty(&frame_list)) {
  1604. rx->skb = __skb_dequeue(&frame_list);
  1605. if (!ieee80211_frame_allowed(rx, fc)) {
  1606. dev_kfree_skb(rx->skb);
  1607. continue;
  1608. }
  1609. dev->stats.rx_packets++;
  1610. dev->stats.rx_bytes += rx->skb->len;
  1611. ieee80211_deliver_skb(rx);
  1612. }
  1613. return RX_QUEUED;
  1614. }
  1615. #ifdef CONFIG_MAC80211_MESH
  1616. static ieee80211_rx_result
  1617. ieee80211_rx_h_mesh_fwding(struct ieee80211_rx_data *rx)
  1618. {
  1619. struct ieee80211_hdr *fwd_hdr, *hdr;
  1620. struct ieee80211_tx_info *info;
  1621. struct ieee80211s_hdr *mesh_hdr;
  1622. struct sk_buff *skb = rx->skb, *fwd_skb;
  1623. struct ieee80211_local *local = rx->local;
  1624. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1625. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  1626. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  1627. __le16 reason = cpu_to_le16(WLAN_REASON_MESH_PATH_NOFORWARD);
  1628. u16 q, hdrlen;
  1629. hdr = (struct ieee80211_hdr *) skb->data;
  1630. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  1631. mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
  1632. /* frame is in RMC, don't forward */
  1633. if (ieee80211_is_data(hdr->frame_control) &&
  1634. is_multicast_ether_addr(hdr->addr1) &&
  1635. mesh_rmc_check(hdr->addr3, mesh_hdr, rx->sdata))
  1636. return RX_DROP_MONITOR;
  1637. if (!ieee80211_is_data(hdr->frame_control))
  1638. return RX_CONTINUE;
  1639. if (!mesh_hdr->ttl)
  1640. return RX_DROP_MONITOR;
  1641. if (mesh_hdr->flags & MESH_FLAGS_AE) {
  1642. struct mesh_path *mppath;
  1643. char *proxied_addr;
  1644. char *mpp_addr;
  1645. if (is_multicast_ether_addr(hdr->addr1)) {
  1646. mpp_addr = hdr->addr3;
  1647. proxied_addr = mesh_hdr->eaddr1;
  1648. } else {
  1649. mpp_addr = hdr->addr4;
  1650. proxied_addr = mesh_hdr->eaddr2;
  1651. }
  1652. rcu_read_lock();
  1653. mppath = mpp_path_lookup(proxied_addr, sdata);
  1654. if (!mppath) {
  1655. mpp_path_add(proxied_addr, mpp_addr, sdata);
  1656. } else {
  1657. spin_lock_bh(&mppath->state_lock);
  1658. if (compare_ether_addr(mppath->mpp, mpp_addr) != 0)
  1659. memcpy(mppath->mpp, mpp_addr, ETH_ALEN);
  1660. spin_unlock_bh(&mppath->state_lock);
  1661. }
  1662. rcu_read_unlock();
  1663. }
  1664. /* Frame has reached destination. Don't forward */
  1665. if (!is_multicast_ether_addr(hdr->addr1) &&
  1666. compare_ether_addr(sdata->vif.addr, hdr->addr3) == 0)
  1667. return RX_CONTINUE;
  1668. q = ieee80211_select_queue_80211(local, skb, hdr);
  1669. if (ieee80211_queue_stopped(&local->hw, q)) {
  1670. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_congestion);
  1671. return RX_DROP_MONITOR;
  1672. }
  1673. skb_set_queue_mapping(skb, q);
  1674. if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
  1675. goto out;
  1676. if (!--mesh_hdr->ttl) {
  1677. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_ttl);
  1678. return RX_DROP_MONITOR;
  1679. }
  1680. fwd_skb = skb_copy(skb, GFP_ATOMIC);
  1681. if (!fwd_skb) {
  1682. if (net_ratelimit())
  1683. printk(KERN_DEBUG "%s: failed to clone mesh frame\n",
  1684. sdata->name);
  1685. goto out;
  1686. }
  1687. fwd_hdr = (struct ieee80211_hdr *) fwd_skb->data;
  1688. info = IEEE80211_SKB_CB(fwd_skb);
  1689. memset(info, 0, sizeof(*info));
  1690. info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
  1691. info->control.vif = &rx->sdata->vif;
  1692. info->control.jiffies = jiffies;
  1693. if (is_multicast_ether_addr(fwd_hdr->addr1)) {
  1694. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_mcast);
  1695. memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN);
  1696. } else if (!mesh_nexthop_lookup(fwd_skb, sdata)) {
  1697. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_unicast);
  1698. } else {
  1699. /* unable to resolve next hop */
  1700. mesh_path_error_tx(ifmsh->mshcfg.element_ttl, fwd_hdr->addr3,
  1701. 0, reason, fwd_hdr->addr2, sdata);
  1702. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_no_route);
  1703. return RX_DROP_MONITOR;
  1704. }
  1705. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames);
  1706. ieee80211_add_pending_skb(local, fwd_skb);
  1707. out:
  1708. if (is_multicast_ether_addr(hdr->addr1) ||
  1709. sdata->dev->flags & IFF_PROMISC)
  1710. return RX_CONTINUE;
  1711. else
  1712. return RX_DROP_MONITOR;
  1713. }
  1714. #endif
  1715. static ieee80211_rx_result debug_noinline
  1716. ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
  1717. {
  1718. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1719. struct ieee80211_local *local = rx->local;
  1720. struct net_device *dev = sdata->dev;
  1721. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  1722. __le16 fc = hdr->frame_control;
  1723. bool port_control;
  1724. int err;
  1725. if (unlikely(!ieee80211_is_data(hdr->frame_control)))
  1726. return RX_CONTINUE;
  1727. if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
  1728. return RX_DROP_MONITOR;
  1729. /*
  1730. * Send unexpected-4addr-frame event to hostapd. For older versions,
  1731. * also drop the frame to cooked monitor interfaces.
  1732. */
  1733. if (ieee80211_has_a4(hdr->frame_control) &&
  1734. sdata->vif.type == NL80211_IFTYPE_AP) {
  1735. if (rx->sta &&
  1736. !test_and_set_sta_flag(rx->sta, WLAN_STA_4ADDR_EVENT))
  1737. cfg80211_rx_unexpected_4addr_frame(
  1738. rx->sdata->dev, rx->sta->sta.addr, GFP_ATOMIC);
  1739. return RX_DROP_MONITOR;
  1740. }
  1741. err = __ieee80211_data_to_8023(rx, &port_control);
  1742. if (unlikely(err))
  1743. return RX_DROP_UNUSABLE;
  1744. if (!ieee80211_frame_allowed(rx, fc))
  1745. return RX_DROP_MONITOR;
  1746. if (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  1747. unlikely(port_control) && sdata->bss) {
  1748. sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
  1749. u.ap);
  1750. dev = sdata->dev;
  1751. rx->sdata = sdata;
  1752. }
  1753. rx->skb->dev = dev;
  1754. dev->stats.rx_packets++;
  1755. dev->stats.rx_bytes += rx->skb->len;
  1756. if (local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 &&
  1757. !is_multicast_ether_addr(
  1758. ((struct ethhdr *)rx->skb->data)->h_dest) &&
  1759. (!local->scanning &&
  1760. !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))) {
  1761. mod_timer(&local->dynamic_ps_timer, jiffies +
  1762. msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
  1763. }
  1764. ieee80211_deliver_skb(rx);
  1765. return RX_QUEUED;
  1766. }
  1767. static ieee80211_rx_result debug_noinline
  1768. ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx)
  1769. {
  1770. struct ieee80211_local *local = rx->local;
  1771. struct ieee80211_hw *hw = &local->hw;
  1772. struct sk_buff *skb = rx->skb;
  1773. struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
  1774. struct tid_ampdu_rx *tid_agg_rx;
  1775. u16 start_seq_num;
  1776. u16 tid;
  1777. if (likely(!ieee80211_is_ctl(bar->frame_control)))
  1778. return RX_CONTINUE;
  1779. if (ieee80211_is_back_req(bar->frame_control)) {
  1780. struct {
  1781. __le16 control, start_seq_num;
  1782. } __packed bar_data;
  1783. if (!rx->sta)
  1784. return RX_DROP_MONITOR;
  1785. if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control),
  1786. &bar_data, sizeof(bar_data)))
  1787. return RX_DROP_MONITOR;
  1788. tid = le16_to_cpu(bar_data.control) >> 12;
  1789. tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]);
  1790. if (!tid_agg_rx)
  1791. return RX_DROP_MONITOR;
  1792. start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4;
  1793. /* reset session timer */
  1794. if (tid_agg_rx->timeout)
  1795. mod_timer(&tid_agg_rx->session_timer,
  1796. TU_TO_EXP_TIME(tid_agg_rx->timeout));
  1797. spin_lock(&tid_agg_rx->reorder_lock);
  1798. /* release stored frames up to start of BAR */
  1799. ieee80211_release_reorder_frames(hw, tid_agg_rx, start_seq_num);
  1800. spin_unlock(&tid_agg_rx->reorder_lock);
  1801. kfree_skb(skb);
  1802. return RX_QUEUED;
  1803. }
  1804. /*
  1805. * After this point, we only want management frames,
  1806. * so we can drop all remaining control frames to
  1807. * cooked monitor interfaces.
  1808. */
  1809. return RX_DROP_MONITOR;
  1810. }
  1811. static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
  1812. struct ieee80211_mgmt *mgmt,
  1813. size_t len)
  1814. {
  1815. struct ieee80211_local *local = sdata->local;
  1816. struct sk_buff *skb;
  1817. struct ieee80211_mgmt *resp;
  1818. if (compare_ether_addr(mgmt->da, sdata->vif.addr) != 0) {
  1819. /* Not to own unicast address */
  1820. return;
  1821. }
  1822. if (compare_ether_addr(mgmt->sa, sdata->u.mgd.bssid) != 0 ||
  1823. compare_ether_addr(mgmt->bssid, sdata->u.mgd.bssid) != 0) {
  1824. /* Not from the current AP or not associated yet. */
  1825. return;
  1826. }
  1827. if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
  1828. /* Too short SA Query request frame */
  1829. return;
  1830. }
  1831. skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
  1832. if (skb == NULL)
  1833. return;
  1834. skb_reserve(skb, local->hw.extra_tx_headroom);
  1835. resp = (struct ieee80211_mgmt *) skb_put(skb, 24);
  1836. memset(resp, 0, 24);
  1837. memcpy(resp->da, mgmt->sa, ETH_ALEN);
  1838. memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
  1839. memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
  1840. resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  1841. IEEE80211_STYPE_ACTION);
  1842. skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
  1843. resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
  1844. resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
  1845. memcpy(resp->u.action.u.sa_query.trans_id,
  1846. mgmt->u.action.u.sa_query.trans_id,
  1847. WLAN_SA_QUERY_TR_ID_LEN);
  1848. ieee80211_tx_skb(sdata, skb);
  1849. }
  1850. static ieee80211_rx_result debug_noinline
  1851. ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx)
  1852. {
  1853. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
  1854. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1855. /*
  1856. * From here on, look only at management frames.
  1857. * Data and control frames are already handled,
  1858. * and unknown (reserved) frames are useless.
  1859. */
  1860. if (rx->skb->len < 24)
  1861. return RX_DROP_MONITOR;
  1862. if (!ieee80211_is_mgmt(mgmt->frame_control))
  1863. return RX_DROP_MONITOR;
  1864. if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
  1865. ieee80211_is_beacon(mgmt->frame_control) &&
  1866. !(rx->flags & IEEE80211_RX_BEACON_REPORTED)) {
  1867. struct ieee80211_rx_status *status;
  1868. status = IEEE80211_SKB_RXCB(rx->skb);
  1869. cfg80211_report_obss_beacon(rx->local->hw.wiphy,
  1870. rx->skb->data, rx->skb->len,
  1871. status->freq, GFP_ATOMIC);
  1872. rx->flags |= IEEE80211_RX_BEACON_REPORTED;
  1873. }
  1874. if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
  1875. return RX_DROP_MONITOR;
  1876. if (ieee80211_drop_unencrypted_mgmt(rx))
  1877. return RX_DROP_UNUSABLE;
  1878. return RX_CONTINUE;
  1879. }
  1880. static ieee80211_rx_result debug_noinline
  1881. ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
  1882. {
  1883. struct ieee80211_local *local = rx->local;
  1884. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1885. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
  1886. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1887. int len = rx->skb->len;
  1888. if (!ieee80211_is_action(mgmt->frame_control))
  1889. return RX_CONTINUE;
  1890. /* drop too small frames */
  1891. if (len < IEEE80211_MIN_ACTION_SIZE)
  1892. return RX_DROP_UNUSABLE;
  1893. if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC)
  1894. return RX_DROP_UNUSABLE;
  1895. if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
  1896. return RX_DROP_UNUSABLE;
  1897. switch (mgmt->u.action.category) {
  1898. case WLAN_CATEGORY_HT:
  1899. /* reject HT action frames from stations not supporting HT */
  1900. if (!rx->sta->sta.ht_cap.ht_supported)
  1901. goto invalid;
  1902. if (sdata->vif.type != NL80211_IFTYPE_STATION &&
  1903. sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
  1904. sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  1905. sdata->vif.type != NL80211_IFTYPE_AP &&
  1906. sdata->vif.type != NL80211_IFTYPE_ADHOC)
  1907. break;
  1908. /* verify action & smps_control are present */
  1909. if (len < IEEE80211_MIN_ACTION_SIZE + 2)
  1910. goto invalid;
  1911. switch (mgmt->u.action.u.ht_smps.action) {
  1912. case WLAN_HT_ACTION_SMPS: {
  1913. struct ieee80211_supported_band *sband;
  1914. u8 smps;
  1915. /* convert to HT capability */
  1916. switch (mgmt->u.action.u.ht_smps.smps_control) {
  1917. case WLAN_HT_SMPS_CONTROL_DISABLED:
  1918. smps = WLAN_HT_CAP_SM_PS_DISABLED;
  1919. break;
  1920. case WLAN_HT_SMPS_CONTROL_STATIC:
  1921. smps = WLAN_HT_CAP_SM_PS_STATIC;
  1922. break;
  1923. case WLAN_HT_SMPS_CONTROL_DYNAMIC:
  1924. smps = WLAN_HT_CAP_SM_PS_DYNAMIC;
  1925. break;
  1926. default:
  1927. goto invalid;
  1928. }
  1929. smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
  1930. /* if no change do nothing */
  1931. if ((rx->sta->sta.ht_cap.cap &
  1932. IEEE80211_HT_CAP_SM_PS) == smps)
  1933. goto handled;
  1934. rx->sta->sta.ht_cap.cap &= ~IEEE80211_HT_CAP_SM_PS;
  1935. rx->sta->sta.ht_cap.cap |= smps;
  1936. sband = rx->local->hw.wiphy->bands[status->band];
  1937. rate_control_rate_update(local, sband, rx->sta,
  1938. IEEE80211_RC_SMPS_CHANGED,
  1939. local->_oper_channel_type);
  1940. goto handled;
  1941. }
  1942. default:
  1943. goto invalid;
  1944. }
  1945. break;
  1946. case WLAN_CATEGORY_BACK:
  1947. if (sdata->vif.type != NL80211_IFTYPE_STATION &&
  1948. sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
  1949. sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  1950. sdata->vif.type != NL80211_IFTYPE_AP &&
  1951. sdata->vif.type != NL80211_IFTYPE_ADHOC)
  1952. break;
  1953. /* verify action_code is present */
  1954. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  1955. break;
  1956. switch (mgmt->u.action.u.addba_req.action_code) {
  1957. case WLAN_ACTION_ADDBA_REQ:
  1958. if (len < (IEEE80211_MIN_ACTION_SIZE +
  1959. sizeof(mgmt->u.action.u.addba_req)))
  1960. goto invalid;
  1961. break;
  1962. case WLAN_ACTION_ADDBA_RESP:
  1963. if (len < (IEEE80211_MIN_ACTION_SIZE +
  1964. sizeof(mgmt->u.action.u.addba_resp)))
  1965. goto invalid;
  1966. break;
  1967. case WLAN_ACTION_DELBA:
  1968. if (len < (IEEE80211_MIN_ACTION_SIZE +
  1969. sizeof(mgmt->u.action.u.delba)))
  1970. goto invalid;
  1971. break;
  1972. default:
  1973. goto invalid;
  1974. }
  1975. goto queue;
  1976. case WLAN_CATEGORY_SPECTRUM_MGMT:
  1977. if (local->hw.conf.channel->band != IEEE80211_BAND_5GHZ)
  1978. break;
  1979. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1980. break;
  1981. /* verify action_code is present */
  1982. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  1983. break;
  1984. switch (mgmt->u.action.u.measurement.action_code) {
  1985. case WLAN_ACTION_SPCT_MSR_REQ:
  1986. if (len < (IEEE80211_MIN_ACTION_SIZE +
  1987. sizeof(mgmt->u.action.u.measurement)))
  1988. break;
  1989. ieee80211_process_measurement_req(sdata, mgmt, len);
  1990. goto handled;
  1991. case WLAN_ACTION_SPCT_CHL_SWITCH:
  1992. if (len < (IEEE80211_MIN_ACTION_SIZE +
  1993. sizeof(mgmt->u.action.u.chan_switch)))
  1994. break;
  1995. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1996. break;
  1997. if (memcmp(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN))
  1998. break;
  1999. goto queue;
  2000. }
  2001. break;
  2002. case WLAN_CATEGORY_SA_QUERY:
  2003. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2004. sizeof(mgmt->u.action.u.sa_query)))
  2005. break;
  2006. switch (mgmt->u.action.u.sa_query.action) {
  2007. case WLAN_ACTION_SA_QUERY_REQUEST:
  2008. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  2009. break;
  2010. ieee80211_process_sa_query_req(sdata, mgmt, len);
  2011. goto handled;
  2012. }
  2013. break;
  2014. case WLAN_CATEGORY_SELF_PROTECTED:
  2015. switch (mgmt->u.action.u.self_prot.action_code) {
  2016. case WLAN_SP_MESH_PEERING_OPEN:
  2017. case WLAN_SP_MESH_PEERING_CLOSE:
  2018. case WLAN_SP_MESH_PEERING_CONFIRM:
  2019. if (!ieee80211_vif_is_mesh(&sdata->vif))
  2020. goto invalid;
  2021. if (sdata->u.mesh.security != IEEE80211_MESH_SEC_NONE)
  2022. /* userspace handles this frame */
  2023. break;
  2024. goto queue;
  2025. case WLAN_SP_MGK_INFORM:
  2026. case WLAN_SP_MGK_ACK:
  2027. if (!ieee80211_vif_is_mesh(&sdata->vif))
  2028. goto invalid;
  2029. break;
  2030. }
  2031. break;
  2032. case WLAN_CATEGORY_MESH_ACTION:
  2033. if (!ieee80211_vif_is_mesh(&sdata->vif))
  2034. break;
  2035. if (mesh_action_is_path_sel(mgmt) &&
  2036. (!mesh_path_sel_is_hwmp(sdata)))
  2037. break;
  2038. goto queue;
  2039. }
  2040. return RX_CONTINUE;
  2041. invalid:
  2042. status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM;
  2043. /* will return in the next handlers */
  2044. return RX_CONTINUE;
  2045. handled:
  2046. if (rx->sta)
  2047. rx->sta->rx_packets++;
  2048. dev_kfree_skb(rx->skb);
  2049. return RX_QUEUED;
  2050. queue:
  2051. rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
  2052. skb_queue_tail(&sdata->skb_queue, rx->skb);
  2053. ieee80211_queue_work(&local->hw, &sdata->work);
  2054. if (rx->sta)
  2055. rx->sta->rx_packets++;
  2056. return RX_QUEUED;
  2057. }
  2058. static ieee80211_rx_result debug_noinline
  2059. ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx)
  2060. {
  2061. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  2062. /* skip known-bad action frames and return them in the next handler */
  2063. if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM)
  2064. return RX_CONTINUE;
  2065. /*
  2066. * Getting here means the kernel doesn't know how to handle
  2067. * it, but maybe userspace does ... include returned frames
  2068. * so userspace can register for those to know whether ones
  2069. * it transmitted were processed or returned.
  2070. */
  2071. if (cfg80211_rx_mgmt(rx->sdata->dev, status->freq,
  2072. rx->skb->data, rx->skb->len,
  2073. GFP_ATOMIC)) {
  2074. if (rx->sta)
  2075. rx->sta->rx_packets++;
  2076. dev_kfree_skb(rx->skb);
  2077. return RX_QUEUED;
  2078. }
  2079. return RX_CONTINUE;
  2080. }
  2081. static ieee80211_rx_result debug_noinline
  2082. ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx)
  2083. {
  2084. struct ieee80211_local *local = rx->local;
  2085. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
  2086. struct sk_buff *nskb;
  2087. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2088. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  2089. if (!ieee80211_is_action(mgmt->frame_control))
  2090. return RX_CONTINUE;
  2091. /*
  2092. * For AP mode, hostapd is responsible for handling any action
  2093. * frames that we didn't handle, including returning unknown
  2094. * ones. For all other modes we will return them to the sender,
  2095. * setting the 0x80 bit in the action category, as required by
  2096. * 802.11-2007 7.3.1.11.
  2097. * Newer versions of hostapd shall also use the management frame
  2098. * registration mechanisms, but older ones still use cooked
  2099. * monitor interfaces so push all frames there.
  2100. */
  2101. if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) &&
  2102. (sdata->vif.type == NL80211_IFTYPE_AP ||
  2103. sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
  2104. return RX_DROP_MONITOR;
  2105. /* do not return rejected action frames */
  2106. if (mgmt->u.action.category & 0x80)
  2107. return RX_DROP_UNUSABLE;
  2108. nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
  2109. GFP_ATOMIC);
  2110. if (nskb) {
  2111. struct ieee80211_mgmt *nmgmt = (void *)nskb->data;
  2112. nmgmt->u.action.category |= 0x80;
  2113. memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN);
  2114. memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
  2115. memset(nskb->cb, 0, sizeof(nskb->cb));
  2116. ieee80211_tx_skb(rx->sdata, nskb);
  2117. }
  2118. dev_kfree_skb(rx->skb);
  2119. return RX_QUEUED;
  2120. }
  2121. static ieee80211_rx_result debug_noinline
  2122. ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
  2123. {
  2124. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2125. ieee80211_rx_result rxs;
  2126. struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
  2127. __le16 stype;
  2128. rxs = ieee80211_work_rx_mgmt(rx->sdata, rx->skb);
  2129. if (rxs != RX_CONTINUE)
  2130. return rxs;
  2131. stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
  2132. if (!ieee80211_vif_is_mesh(&sdata->vif) &&
  2133. sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  2134. sdata->vif.type != NL80211_IFTYPE_STATION)
  2135. return RX_DROP_MONITOR;
  2136. switch (stype) {
  2137. case cpu_to_le16(IEEE80211_STYPE_BEACON):
  2138. case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
  2139. /* process for all: mesh, mlme, ibss */
  2140. break;
  2141. case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
  2142. case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
  2143. if (is_multicast_ether_addr(mgmt->da) &&
  2144. !is_broadcast_ether_addr(mgmt->da))
  2145. return RX_DROP_MONITOR;
  2146. /* process only for station */
  2147. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  2148. return RX_DROP_MONITOR;
  2149. break;
  2150. case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
  2151. case cpu_to_le16(IEEE80211_STYPE_AUTH):
  2152. /* process only for ibss */
  2153. if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
  2154. return RX_DROP_MONITOR;
  2155. break;
  2156. default:
  2157. return RX_DROP_MONITOR;
  2158. }
  2159. /* queue up frame and kick off work to process it */
  2160. rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
  2161. skb_queue_tail(&sdata->skb_queue, rx->skb);
  2162. ieee80211_queue_work(&rx->local->hw, &sdata->work);
  2163. if (rx->sta)
  2164. rx->sta->rx_packets++;
  2165. return RX_QUEUED;
  2166. }
  2167. /* TODO: use IEEE80211_RX_FRAGMENTED */
  2168. static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
  2169. struct ieee80211_rate *rate)
  2170. {
  2171. struct ieee80211_sub_if_data *sdata;
  2172. struct ieee80211_local *local = rx->local;
  2173. struct ieee80211_rtap_hdr {
  2174. struct ieee80211_radiotap_header hdr;
  2175. u8 flags;
  2176. u8 rate_or_pad;
  2177. __le16 chan_freq;
  2178. __le16 chan_flags;
  2179. } __packed *rthdr;
  2180. struct sk_buff *skb = rx->skb, *skb2;
  2181. struct net_device *prev_dev = NULL;
  2182. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2183. /*
  2184. * If cooked monitor has been processed already, then
  2185. * don't do it again. If not, set the flag.
  2186. */
  2187. if (rx->flags & IEEE80211_RX_CMNTR)
  2188. goto out_free_skb;
  2189. rx->flags |= IEEE80211_RX_CMNTR;
  2190. /* If there are no cooked monitor interfaces, just free the SKB */
  2191. if (!local->cooked_mntrs)
  2192. goto out_free_skb;
  2193. if (skb_headroom(skb) < sizeof(*rthdr) &&
  2194. pskb_expand_head(skb, sizeof(*rthdr), 0, GFP_ATOMIC))
  2195. goto out_free_skb;
  2196. rthdr = (void *)skb_push(skb, sizeof(*rthdr));
  2197. memset(rthdr, 0, sizeof(*rthdr));
  2198. rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
  2199. rthdr->hdr.it_present =
  2200. cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  2201. (1 << IEEE80211_RADIOTAP_CHANNEL));
  2202. if (rate) {
  2203. rthdr->rate_or_pad = rate->bitrate / 5;
  2204. rthdr->hdr.it_present |=
  2205. cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
  2206. }
  2207. rthdr->chan_freq = cpu_to_le16(status->freq);
  2208. if (status->band == IEEE80211_BAND_5GHZ)
  2209. rthdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_OFDM |
  2210. IEEE80211_CHAN_5GHZ);
  2211. else
  2212. rthdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_DYN |
  2213. IEEE80211_CHAN_2GHZ);
  2214. skb_set_mac_header(skb, 0);
  2215. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2216. skb->pkt_type = PACKET_OTHERHOST;
  2217. skb->protocol = htons(ETH_P_802_2);
  2218. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  2219. if (!ieee80211_sdata_running(sdata))
  2220. continue;
  2221. if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
  2222. !(sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES))
  2223. continue;
  2224. if (prev_dev) {
  2225. skb2 = skb_clone(skb, GFP_ATOMIC);
  2226. if (skb2) {
  2227. skb2->dev = prev_dev;
  2228. netif_receive_skb(skb2);
  2229. }
  2230. }
  2231. prev_dev = sdata->dev;
  2232. sdata->dev->stats.rx_packets++;
  2233. sdata->dev->stats.rx_bytes += skb->len;
  2234. }
  2235. if (prev_dev) {
  2236. skb->dev = prev_dev;
  2237. netif_receive_skb(skb);
  2238. return;
  2239. }
  2240. out_free_skb:
  2241. dev_kfree_skb(skb);
  2242. }
  2243. static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx,
  2244. ieee80211_rx_result res)
  2245. {
  2246. switch (res) {
  2247. case RX_DROP_MONITOR:
  2248. I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
  2249. if (rx->sta)
  2250. rx->sta->rx_dropped++;
  2251. /* fall through */
  2252. case RX_CONTINUE: {
  2253. struct ieee80211_rate *rate = NULL;
  2254. struct ieee80211_supported_band *sband;
  2255. struct ieee80211_rx_status *status;
  2256. status = IEEE80211_SKB_RXCB((rx->skb));
  2257. sband = rx->local->hw.wiphy->bands[status->band];
  2258. if (!(status->flag & RX_FLAG_HT))
  2259. rate = &sband->bitrates[status->rate_idx];
  2260. ieee80211_rx_cooked_monitor(rx, rate);
  2261. break;
  2262. }
  2263. case RX_DROP_UNUSABLE:
  2264. I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
  2265. if (rx->sta)
  2266. rx->sta->rx_dropped++;
  2267. dev_kfree_skb(rx->skb);
  2268. break;
  2269. case RX_QUEUED:
  2270. I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued);
  2271. break;
  2272. }
  2273. }
  2274. static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx)
  2275. {
  2276. ieee80211_rx_result res = RX_DROP_MONITOR;
  2277. struct sk_buff *skb;
  2278. #define CALL_RXH(rxh) \
  2279. do { \
  2280. res = rxh(rx); \
  2281. if (res != RX_CONTINUE) \
  2282. goto rxh_next; \
  2283. } while (0);
  2284. spin_lock(&rx->local->rx_skb_queue.lock);
  2285. if (rx->local->running_rx_handler)
  2286. goto unlock;
  2287. rx->local->running_rx_handler = true;
  2288. while ((skb = __skb_dequeue(&rx->local->rx_skb_queue))) {
  2289. spin_unlock(&rx->local->rx_skb_queue.lock);
  2290. /*
  2291. * all the other fields are valid across frames
  2292. * that belong to an aMPDU since they are on the
  2293. * same TID from the same station
  2294. */
  2295. rx->skb = skb;
  2296. CALL_RXH(ieee80211_rx_h_decrypt)
  2297. CALL_RXH(ieee80211_rx_h_check_more_data)
  2298. CALL_RXH(ieee80211_rx_h_uapsd_and_pspoll)
  2299. CALL_RXH(ieee80211_rx_h_sta_process)
  2300. CALL_RXH(ieee80211_rx_h_defragment)
  2301. CALL_RXH(ieee80211_rx_h_michael_mic_verify)
  2302. /* must be after MMIC verify so header is counted in MPDU mic */
  2303. #ifdef CONFIG_MAC80211_MESH
  2304. if (ieee80211_vif_is_mesh(&rx->sdata->vif))
  2305. CALL_RXH(ieee80211_rx_h_mesh_fwding);
  2306. #endif
  2307. CALL_RXH(ieee80211_rx_h_remove_qos_control)
  2308. CALL_RXH(ieee80211_rx_h_amsdu)
  2309. CALL_RXH(ieee80211_rx_h_data)
  2310. CALL_RXH(ieee80211_rx_h_ctrl);
  2311. CALL_RXH(ieee80211_rx_h_mgmt_check)
  2312. CALL_RXH(ieee80211_rx_h_action)
  2313. CALL_RXH(ieee80211_rx_h_userspace_mgmt)
  2314. CALL_RXH(ieee80211_rx_h_action_return)
  2315. CALL_RXH(ieee80211_rx_h_mgmt)
  2316. rxh_next:
  2317. ieee80211_rx_handlers_result(rx, res);
  2318. spin_lock(&rx->local->rx_skb_queue.lock);
  2319. #undef CALL_RXH
  2320. }
  2321. rx->local->running_rx_handler = false;
  2322. unlock:
  2323. spin_unlock(&rx->local->rx_skb_queue.lock);
  2324. }
  2325. static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
  2326. {
  2327. ieee80211_rx_result res = RX_DROP_MONITOR;
  2328. #define CALL_RXH(rxh) \
  2329. do { \
  2330. res = rxh(rx); \
  2331. if (res != RX_CONTINUE) \
  2332. goto rxh_next; \
  2333. } while (0);
  2334. CALL_RXH(ieee80211_rx_h_passive_scan)
  2335. CALL_RXH(ieee80211_rx_h_check)
  2336. ieee80211_rx_reorder_ampdu(rx);
  2337. ieee80211_rx_handlers(rx);
  2338. return;
  2339. rxh_next:
  2340. ieee80211_rx_handlers_result(rx, res);
  2341. #undef CALL_RXH
  2342. }
  2343. /*
  2344. * This function makes calls into the RX path, therefore
  2345. * it has to be invoked under RCU read lock.
  2346. */
  2347. void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
  2348. {
  2349. struct ieee80211_rx_data rx = {
  2350. .sta = sta,
  2351. .sdata = sta->sdata,
  2352. .local = sta->local,
  2353. /* This is OK -- must be QoS data frame */
  2354. .security_idx = tid,
  2355. .seqno_idx = tid,
  2356. .flags = 0,
  2357. };
  2358. struct tid_ampdu_rx *tid_agg_rx;
  2359. tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
  2360. if (!tid_agg_rx)
  2361. return;
  2362. spin_lock(&tid_agg_rx->reorder_lock);
  2363. ieee80211_sta_reorder_release(&sta->local->hw, tid_agg_rx);
  2364. spin_unlock(&tid_agg_rx->reorder_lock);
  2365. ieee80211_rx_handlers(&rx);
  2366. }
  2367. /* main receive path */
  2368. static int prepare_for_handlers(struct ieee80211_rx_data *rx,
  2369. struct ieee80211_hdr *hdr)
  2370. {
  2371. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2372. struct sk_buff *skb = rx->skb;
  2373. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2374. u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
  2375. int multicast = is_multicast_ether_addr(hdr->addr1);
  2376. switch (sdata->vif.type) {
  2377. case NL80211_IFTYPE_STATION:
  2378. if (!bssid && !sdata->u.mgd.use_4addr)
  2379. return 0;
  2380. if (!multicast &&
  2381. compare_ether_addr(sdata->vif.addr, hdr->addr1) != 0) {
  2382. if (!(sdata->dev->flags & IFF_PROMISC) ||
  2383. sdata->u.mgd.use_4addr)
  2384. return 0;
  2385. status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
  2386. }
  2387. break;
  2388. case NL80211_IFTYPE_ADHOC:
  2389. if (!bssid)
  2390. return 0;
  2391. if (ieee80211_is_beacon(hdr->frame_control)) {
  2392. return 1;
  2393. }
  2394. else if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid)) {
  2395. if (!(status->rx_flags & IEEE80211_RX_IN_SCAN))
  2396. return 0;
  2397. status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
  2398. } else if (!multicast &&
  2399. compare_ether_addr(sdata->vif.addr,
  2400. hdr->addr1) != 0) {
  2401. if (!(sdata->dev->flags & IFF_PROMISC))
  2402. return 0;
  2403. status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
  2404. } else if (!rx->sta) {
  2405. int rate_idx;
  2406. if (status->flag & RX_FLAG_HT)
  2407. rate_idx = 0; /* TODO: HT rates */
  2408. else
  2409. rate_idx = status->rate_idx;
  2410. ieee80211_ibss_rx_no_sta(sdata, bssid, hdr->addr2,
  2411. BIT(rate_idx));
  2412. }
  2413. break;
  2414. case NL80211_IFTYPE_MESH_POINT:
  2415. if (!multicast &&
  2416. compare_ether_addr(sdata->vif.addr,
  2417. hdr->addr1) != 0) {
  2418. if (!(sdata->dev->flags & IFF_PROMISC))
  2419. return 0;
  2420. status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
  2421. }
  2422. break;
  2423. case NL80211_IFTYPE_AP_VLAN:
  2424. case NL80211_IFTYPE_AP:
  2425. if (!bssid) {
  2426. if (compare_ether_addr(sdata->vif.addr,
  2427. hdr->addr1))
  2428. return 0;
  2429. } else if (!ieee80211_bssid_match(bssid,
  2430. sdata->vif.addr)) {
  2431. /*
  2432. * Accept public action frames even when the
  2433. * BSSID doesn't match, this is used for P2P
  2434. * and location updates. Note that mac80211
  2435. * itself never looks at these frames.
  2436. */
  2437. if (!(status->rx_flags & IEEE80211_RX_IN_SCAN) &&
  2438. ieee80211_is_public_action(hdr, skb->len))
  2439. return 1;
  2440. if (!(status->rx_flags & IEEE80211_RX_IN_SCAN) &&
  2441. !ieee80211_is_beacon(hdr->frame_control))
  2442. return 0;
  2443. status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
  2444. }
  2445. break;
  2446. case NL80211_IFTYPE_WDS:
  2447. if (bssid || !ieee80211_is_data(hdr->frame_control))
  2448. return 0;
  2449. if (compare_ether_addr(sdata->u.wds.remote_addr, hdr->addr2))
  2450. return 0;
  2451. break;
  2452. default:
  2453. /* should never get here */
  2454. WARN_ON(1);
  2455. break;
  2456. }
  2457. return 1;
  2458. }
  2459. /*
  2460. * This function returns whether or not the SKB
  2461. * was destined for RX processing or not, which,
  2462. * if consume is true, is equivalent to whether
  2463. * or not the skb was consumed.
  2464. */
  2465. static bool ieee80211_prepare_and_rx_handle(struct ieee80211_rx_data *rx,
  2466. struct sk_buff *skb, bool consume)
  2467. {
  2468. struct ieee80211_local *local = rx->local;
  2469. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2470. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2471. struct ieee80211_hdr *hdr = (void *)skb->data;
  2472. int prepares;
  2473. rx->skb = skb;
  2474. status->rx_flags |= IEEE80211_RX_RA_MATCH;
  2475. prepares = prepare_for_handlers(rx, hdr);
  2476. if (!prepares)
  2477. return false;
  2478. if (!consume) {
  2479. skb = skb_copy(skb, GFP_ATOMIC);
  2480. if (!skb) {
  2481. if (net_ratelimit())
  2482. wiphy_debug(local->hw.wiphy,
  2483. "failed to copy skb for %s\n",
  2484. sdata->name);
  2485. return true;
  2486. }
  2487. rx->skb = skb;
  2488. }
  2489. ieee80211_invoke_rx_handlers(rx);
  2490. return true;
  2491. }
  2492. /*
  2493. * This is the actual Rx frames handler. as it blongs to Rx path it must
  2494. * be called with rcu_read_lock protection.
  2495. */
  2496. static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
  2497. struct sk_buff *skb)
  2498. {
  2499. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2500. struct ieee80211_local *local = hw_to_local(hw);
  2501. struct ieee80211_sub_if_data *sdata;
  2502. struct ieee80211_hdr *hdr;
  2503. __le16 fc;
  2504. struct ieee80211_rx_data rx;
  2505. struct ieee80211_sub_if_data *prev;
  2506. struct sta_info *sta, *tmp, *prev_sta;
  2507. int err = 0;
  2508. fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
  2509. memset(&rx, 0, sizeof(rx));
  2510. rx.skb = skb;
  2511. rx.local = local;
  2512. if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc))
  2513. local->dot11ReceivedFragmentCount++;
  2514. if (unlikely(test_bit(SCAN_HW_SCANNING, &local->scanning) ||
  2515. test_bit(SCAN_SW_SCANNING, &local->scanning)))
  2516. status->rx_flags |= IEEE80211_RX_IN_SCAN;
  2517. if (ieee80211_is_mgmt(fc))
  2518. err = skb_linearize(skb);
  2519. else
  2520. err = !pskb_may_pull(skb, ieee80211_hdrlen(fc));
  2521. if (err) {
  2522. dev_kfree_skb(skb);
  2523. return;
  2524. }
  2525. hdr = (struct ieee80211_hdr *)skb->data;
  2526. ieee80211_parse_qos(&rx);
  2527. ieee80211_verify_alignment(&rx);
  2528. if (ieee80211_is_data(fc)) {
  2529. prev_sta = NULL;
  2530. for_each_sta_info_rx(local, hdr->addr2, sta, tmp) {
  2531. if (!prev_sta) {
  2532. prev_sta = sta;
  2533. continue;
  2534. }
  2535. rx.sta = prev_sta;
  2536. rx.sdata = prev_sta->sdata;
  2537. ieee80211_prepare_and_rx_handle(&rx, skb, false);
  2538. prev_sta = sta;
  2539. }
  2540. if (prev_sta) {
  2541. rx.sta = prev_sta;
  2542. rx.sdata = prev_sta->sdata;
  2543. if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
  2544. return;
  2545. goto out;
  2546. }
  2547. }
  2548. prev = NULL;
  2549. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  2550. if (!ieee80211_sdata_running(sdata))
  2551. continue;
  2552. if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
  2553. sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  2554. continue;
  2555. /*
  2556. * frame is destined for this interface, but if it's
  2557. * not also for the previous one we handle that after
  2558. * the loop to avoid copying the SKB once too much
  2559. */
  2560. if (!prev) {
  2561. prev = sdata;
  2562. continue;
  2563. }
  2564. rx.sta = sta_info_get_bss_rx(prev, hdr->addr2);
  2565. rx.sdata = prev;
  2566. ieee80211_prepare_and_rx_handle(&rx, skb, false);
  2567. prev = sdata;
  2568. }
  2569. if (prev) {
  2570. rx.sta = sta_info_get_bss_rx(prev, hdr->addr2);
  2571. rx.sdata = prev;
  2572. if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
  2573. return;
  2574. }
  2575. out:
  2576. dev_kfree_skb(skb);
  2577. }
  2578. /*
  2579. * This is the receive path handler. It is called by a low level driver when an
  2580. * 802.11 MPDU is received from the hardware.
  2581. */
  2582. void ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb)
  2583. {
  2584. struct ieee80211_local *local = hw_to_local(hw);
  2585. struct ieee80211_rate *rate = NULL;
  2586. struct ieee80211_supported_band *sband;
  2587. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2588. WARN_ON_ONCE(softirq_count() == 0);
  2589. if (WARN_ON(status->band < 0 ||
  2590. status->band >= IEEE80211_NUM_BANDS))
  2591. goto drop;
  2592. sband = local->hw.wiphy->bands[status->band];
  2593. if (WARN_ON(!sband))
  2594. goto drop;
  2595. /*
  2596. * If we're suspending, it is possible although not too likely
  2597. * that we'd be receiving frames after having already partially
  2598. * quiesced the stack. We can't process such frames then since
  2599. * that might, for example, cause stations to be added or other
  2600. * driver callbacks be invoked.
  2601. */
  2602. if (unlikely(local->quiescing || local->suspended))
  2603. goto drop;
  2604. /*
  2605. * The same happens when we're not even started,
  2606. * but that's worth a warning.
  2607. */
  2608. if (WARN_ON(!local->started))
  2609. goto drop;
  2610. if (likely(!(status->flag & RX_FLAG_FAILED_PLCP_CRC))) {
  2611. /*
  2612. * Validate the rate, unless a PLCP error means that
  2613. * we probably can't have a valid rate here anyway.
  2614. */
  2615. if (status->flag & RX_FLAG_HT) {
  2616. /*
  2617. * rate_idx is MCS index, which can be [0-76]
  2618. * as documented on:
  2619. *
  2620. * http://wireless.kernel.org/en/developers/Documentation/ieee80211/802.11n
  2621. *
  2622. * Anything else would be some sort of driver or
  2623. * hardware error. The driver should catch hardware
  2624. * errors.
  2625. */
  2626. if (WARN((status->rate_idx < 0 ||
  2627. status->rate_idx > 76),
  2628. "Rate marked as an HT rate but passed "
  2629. "status->rate_idx is not "
  2630. "an MCS index [0-76]: %d (0x%02x)\n",
  2631. status->rate_idx,
  2632. status->rate_idx))
  2633. goto drop;
  2634. } else {
  2635. if (WARN_ON(status->rate_idx < 0 ||
  2636. status->rate_idx >= sband->n_bitrates))
  2637. goto drop;
  2638. rate = &sband->bitrates[status->rate_idx];
  2639. }
  2640. }
  2641. status->rx_flags = 0;
  2642. /*
  2643. * key references and virtual interfaces are protected using RCU
  2644. * and this requires that we are in a read-side RCU section during
  2645. * receive processing
  2646. */
  2647. rcu_read_lock();
  2648. /*
  2649. * Frames with failed FCS/PLCP checksum are not returned,
  2650. * all other frames are returned without radiotap header
  2651. * if it was previously present.
  2652. * Also, frames with less than 16 bytes are dropped.
  2653. */
  2654. skb = ieee80211_rx_monitor(local, skb, rate);
  2655. if (!skb) {
  2656. rcu_read_unlock();
  2657. return;
  2658. }
  2659. ieee80211_tpt_led_trig_rx(local,
  2660. ((struct ieee80211_hdr *)skb->data)->frame_control,
  2661. skb->len);
  2662. __ieee80211_rx_handle_packet(hw, skb);
  2663. rcu_read_unlock();
  2664. return;
  2665. drop:
  2666. kfree_skb(skb);
  2667. }
  2668. EXPORT_SYMBOL(ieee80211_rx);
  2669. /* This is a version of the rx handler that can be called from hard irq
  2670. * context. Post the skb on the queue and schedule the tasklet */
  2671. void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
  2672. {
  2673. struct ieee80211_local *local = hw_to_local(hw);
  2674. BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
  2675. skb->pkt_type = IEEE80211_RX_MSG;
  2676. skb_queue_tail(&local->skb_queue, skb);
  2677. tasklet_schedule(&local->tasklet);
  2678. }
  2679. EXPORT_SYMBOL(ieee80211_rx_irqsafe);