rx.c 86 KB

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