rx.c 82 KB

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