rx.c 86 KB

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