rx.c 86 KB

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