rx.c 84 KB

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