rx.c 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991
  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)
  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. if (ieee80211_has_a4(hdr->frame_control) &&
  1360. sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta)
  1361. return -1;
  1362. if (sdata->vif.type == NL80211_IFTYPE_STATION &&
  1363. !!sdata->u.mgd.use_4addr != !!ieee80211_has_a4(hdr->frame_control)) {
  1364. if (!sdata->u.mgd.use_4addr)
  1365. return -1;
  1366. else
  1367. check_port_control = true;
  1368. }
  1369. if (is_multicast_ether_addr(hdr->addr1) &&
  1370. sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta)
  1371. return -1;
  1372. ret = ieee80211_data_to_8023(rx->skb, sdata->vif.addr, sdata->vif.type);
  1373. if (ret < 0 || !check_port_control)
  1374. return ret;
  1375. ehdr = (struct ethhdr *) rx->skb->data;
  1376. if (ehdr->h_proto != rx->sdata->control_port_protocol)
  1377. return -1;
  1378. return 0;
  1379. }
  1380. /*
  1381. * requires that rx->skb is a frame with ethernet header
  1382. */
  1383. static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
  1384. {
  1385. static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
  1386. = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
  1387. struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
  1388. /*
  1389. * Allow EAPOL frames to us/the PAE group address regardless
  1390. * of whether the frame was encrypted or not.
  1391. */
  1392. if (ehdr->h_proto == rx->sdata->control_port_protocol &&
  1393. (compare_ether_addr(ehdr->h_dest, rx->sdata->vif.addr) == 0 ||
  1394. compare_ether_addr(ehdr->h_dest, pae_group_addr) == 0))
  1395. return true;
  1396. if (ieee80211_802_1x_port_control(rx) ||
  1397. ieee80211_drop_unencrypted(rx, fc))
  1398. return false;
  1399. return true;
  1400. }
  1401. /*
  1402. * requires that rx->skb is a frame with ethernet header
  1403. */
  1404. static void
  1405. ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
  1406. {
  1407. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1408. struct net_device *dev = sdata->dev;
  1409. struct sk_buff *skb, *xmit_skb;
  1410. struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
  1411. struct sta_info *dsta;
  1412. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1413. skb = rx->skb;
  1414. xmit_skb = NULL;
  1415. if ((sdata->vif.type == NL80211_IFTYPE_AP ||
  1416. sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
  1417. !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
  1418. (status->rx_flags & IEEE80211_RX_RA_MATCH) &&
  1419. (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) {
  1420. if (is_multicast_ether_addr(ehdr->h_dest)) {
  1421. /*
  1422. * send multicast frames both to higher layers in
  1423. * local net stack and back to the wireless medium
  1424. */
  1425. xmit_skb = skb_copy(skb, GFP_ATOMIC);
  1426. if (!xmit_skb && net_ratelimit())
  1427. printk(KERN_DEBUG "%s: failed to clone "
  1428. "multicast frame\n", dev->name);
  1429. } else {
  1430. dsta = sta_info_get(sdata, skb->data);
  1431. if (dsta) {
  1432. /*
  1433. * The destination station is associated to
  1434. * this AP (in this VLAN), so send the frame
  1435. * directly to it and do not pass it to local
  1436. * net stack.
  1437. */
  1438. xmit_skb = skb;
  1439. skb = NULL;
  1440. }
  1441. }
  1442. }
  1443. if (skb) {
  1444. int align __maybe_unused;
  1445. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1446. /*
  1447. * 'align' will only take the values 0 or 2 here
  1448. * since all frames are required to be aligned
  1449. * to 2-byte boundaries when being passed to
  1450. * mac80211. That also explains the __skb_push()
  1451. * below.
  1452. */
  1453. align = ((unsigned long)(skb->data + sizeof(struct ethhdr))) & 3;
  1454. if (align) {
  1455. if (WARN_ON(skb_headroom(skb) < 3)) {
  1456. dev_kfree_skb(skb);
  1457. skb = NULL;
  1458. } else {
  1459. u8 *data = skb->data;
  1460. size_t len = skb_headlen(skb);
  1461. skb->data -= align;
  1462. memmove(skb->data, data, len);
  1463. skb_set_tail_pointer(skb, len);
  1464. }
  1465. }
  1466. #endif
  1467. if (skb) {
  1468. /* deliver to local stack */
  1469. skb->protocol = eth_type_trans(skb, dev);
  1470. memset(skb->cb, 0, sizeof(skb->cb));
  1471. netif_receive_skb(skb);
  1472. }
  1473. }
  1474. if (xmit_skb) {
  1475. /* send to wireless media */
  1476. xmit_skb->protocol = htons(ETH_P_802_3);
  1477. skb_reset_network_header(xmit_skb);
  1478. skb_reset_mac_header(xmit_skb);
  1479. dev_queue_xmit(xmit_skb);
  1480. }
  1481. }
  1482. static ieee80211_rx_result debug_noinline
  1483. ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
  1484. {
  1485. struct net_device *dev = rx->sdata->dev;
  1486. struct sk_buff *skb = rx->skb;
  1487. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1488. __le16 fc = hdr->frame_control;
  1489. struct sk_buff_head frame_list;
  1490. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1491. if (unlikely(!ieee80211_is_data(fc)))
  1492. return RX_CONTINUE;
  1493. if (unlikely(!ieee80211_is_data_present(fc)))
  1494. return RX_DROP_MONITOR;
  1495. if (!(status->rx_flags & IEEE80211_RX_AMSDU))
  1496. return RX_CONTINUE;
  1497. if (ieee80211_has_a4(hdr->frame_control) &&
  1498. rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  1499. !rx->sdata->u.vlan.sta)
  1500. return RX_DROP_UNUSABLE;
  1501. if (is_multicast_ether_addr(hdr->addr1) &&
  1502. ((rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  1503. rx->sdata->u.vlan.sta) ||
  1504. (rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
  1505. rx->sdata->u.mgd.use_4addr)))
  1506. return RX_DROP_UNUSABLE;
  1507. skb->dev = dev;
  1508. __skb_queue_head_init(&frame_list);
  1509. if (skb_linearize(skb))
  1510. return RX_DROP_UNUSABLE;
  1511. ieee80211_amsdu_to_8023s(skb, &frame_list, dev->dev_addr,
  1512. rx->sdata->vif.type,
  1513. rx->local->hw.extra_tx_headroom);
  1514. while (!skb_queue_empty(&frame_list)) {
  1515. rx->skb = __skb_dequeue(&frame_list);
  1516. if (!ieee80211_frame_allowed(rx, fc)) {
  1517. dev_kfree_skb(rx->skb);
  1518. continue;
  1519. }
  1520. dev->stats.rx_packets++;
  1521. dev->stats.rx_bytes += rx->skb->len;
  1522. ieee80211_deliver_skb(rx);
  1523. }
  1524. return RX_QUEUED;
  1525. }
  1526. #ifdef CONFIG_MAC80211_MESH
  1527. static ieee80211_rx_result
  1528. ieee80211_rx_h_mesh_fwding(struct ieee80211_rx_data *rx)
  1529. {
  1530. struct ieee80211_hdr *hdr;
  1531. struct ieee80211s_hdr *mesh_hdr;
  1532. unsigned int hdrlen;
  1533. struct sk_buff *skb = rx->skb, *fwd_skb;
  1534. struct ieee80211_local *local = rx->local;
  1535. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1536. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  1537. hdr = (struct ieee80211_hdr *) skb->data;
  1538. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  1539. mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
  1540. if (!ieee80211_is_data(hdr->frame_control))
  1541. return RX_CONTINUE;
  1542. if (!mesh_hdr->ttl)
  1543. /* illegal frame */
  1544. return RX_DROP_MONITOR;
  1545. if (mesh_hdr->flags & MESH_FLAGS_AE) {
  1546. struct mesh_path *mppath;
  1547. char *proxied_addr;
  1548. char *mpp_addr;
  1549. if (is_multicast_ether_addr(hdr->addr1)) {
  1550. mpp_addr = hdr->addr3;
  1551. proxied_addr = mesh_hdr->eaddr1;
  1552. } else {
  1553. mpp_addr = hdr->addr4;
  1554. proxied_addr = mesh_hdr->eaddr2;
  1555. }
  1556. rcu_read_lock();
  1557. mppath = mpp_path_lookup(proxied_addr, sdata);
  1558. if (!mppath) {
  1559. mpp_path_add(proxied_addr, mpp_addr, sdata);
  1560. } else {
  1561. spin_lock_bh(&mppath->state_lock);
  1562. if (compare_ether_addr(mppath->mpp, mpp_addr) != 0)
  1563. memcpy(mppath->mpp, mpp_addr, ETH_ALEN);
  1564. spin_unlock_bh(&mppath->state_lock);
  1565. }
  1566. rcu_read_unlock();
  1567. }
  1568. /* Frame has reached destination. Don't forward */
  1569. if (!is_multicast_ether_addr(hdr->addr1) &&
  1570. compare_ether_addr(sdata->vif.addr, hdr->addr3) == 0)
  1571. return RX_CONTINUE;
  1572. mesh_hdr->ttl--;
  1573. if (status->rx_flags & IEEE80211_RX_RA_MATCH) {
  1574. if (!mesh_hdr->ttl)
  1575. IEEE80211_IFSTA_MESH_CTR_INC(&rx->sdata->u.mesh,
  1576. dropped_frames_ttl);
  1577. else {
  1578. struct ieee80211_hdr *fwd_hdr;
  1579. struct ieee80211_tx_info *info;
  1580. fwd_skb = skb_copy(skb, GFP_ATOMIC);
  1581. if (!fwd_skb && net_ratelimit())
  1582. printk(KERN_DEBUG "%s: failed to clone mesh frame\n",
  1583. sdata->name);
  1584. if (!fwd_skb)
  1585. goto out;
  1586. fwd_hdr = (struct ieee80211_hdr *) fwd_skb->data;
  1587. memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN);
  1588. info = IEEE80211_SKB_CB(fwd_skb);
  1589. memset(info, 0, sizeof(*info));
  1590. info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
  1591. info->control.vif = &rx->sdata->vif;
  1592. skb_set_queue_mapping(skb,
  1593. ieee80211_select_queue(rx->sdata, fwd_skb));
  1594. ieee80211_set_qos_hdr(local, skb);
  1595. if (is_multicast_ether_addr(fwd_hdr->addr1))
  1596. IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.mesh,
  1597. fwded_mcast);
  1598. else {
  1599. int err;
  1600. /*
  1601. * Save TA to addr1 to send TA a path error if a
  1602. * suitable next hop is not found
  1603. */
  1604. memcpy(fwd_hdr->addr1, fwd_hdr->addr2,
  1605. ETH_ALEN);
  1606. err = mesh_nexthop_lookup(fwd_skb, sdata);
  1607. /* Failed to immediately resolve next hop:
  1608. * fwded frame was dropped or will be added
  1609. * later to the pending skb queue. */
  1610. if (err)
  1611. return RX_DROP_MONITOR;
  1612. IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.mesh,
  1613. fwded_unicast);
  1614. }
  1615. IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.mesh,
  1616. fwded_frames);
  1617. ieee80211_add_pending_skb(local, fwd_skb);
  1618. }
  1619. }
  1620. out:
  1621. if (is_multicast_ether_addr(hdr->addr1) ||
  1622. sdata->dev->flags & IFF_PROMISC)
  1623. return RX_CONTINUE;
  1624. else
  1625. return RX_DROP_MONITOR;
  1626. }
  1627. #endif
  1628. static ieee80211_rx_result debug_noinline
  1629. ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
  1630. {
  1631. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1632. struct ieee80211_local *local = rx->local;
  1633. struct net_device *dev = sdata->dev;
  1634. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  1635. __le16 fc = hdr->frame_control;
  1636. int err;
  1637. if (unlikely(!ieee80211_is_data(hdr->frame_control)))
  1638. return RX_CONTINUE;
  1639. if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
  1640. return RX_DROP_MONITOR;
  1641. /*
  1642. * Allow the cooked monitor interface of an AP to see 4-addr frames so
  1643. * that a 4-addr station can be detected and moved into a separate VLAN
  1644. */
  1645. if (ieee80211_has_a4(hdr->frame_control) &&
  1646. sdata->vif.type == NL80211_IFTYPE_AP)
  1647. return RX_DROP_MONITOR;
  1648. err = __ieee80211_data_to_8023(rx);
  1649. if (unlikely(err))
  1650. return RX_DROP_UNUSABLE;
  1651. if (!ieee80211_frame_allowed(rx, fc))
  1652. return RX_DROP_MONITOR;
  1653. rx->skb->dev = dev;
  1654. dev->stats.rx_packets++;
  1655. dev->stats.rx_bytes += rx->skb->len;
  1656. if (local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 &&
  1657. !is_multicast_ether_addr(
  1658. ((struct ethhdr *)rx->skb->data)->h_dest) &&
  1659. (!local->scanning &&
  1660. !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))) {
  1661. mod_timer(&local->dynamic_ps_timer, jiffies +
  1662. msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
  1663. }
  1664. ieee80211_deliver_skb(rx);
  1665. return RX_QUEUED;
  1666. }
  1667. static ieee80211_rx_result debug_noinline
  1668. ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx)
  1669. {
  1670. struct ieee80211_local *local = rx->local;
  1671. struct ieee80211_hw *hw = &local->hw;
  1672. struct sk_buff *skb = rx->skb;
  1673. struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
  1674. struct tid_ampdu_rx *tid_agg_rx;
  1675. u16 start_seq_num;
  1676. u16 tid;
  1677. if (likely(!ieee80211_is_ctl(bar->frame_control)))
  1678. return RX_CONTINUE;
  1679. if (ieee80211_is_back_req(bar->frame_control)) {
  1680. struct {
  1681. __le16 control, start_seq_num;
  1682. } __packed bar_data;
  1683. if (!rx->sta)
  1684. return RX_DROP_MONITOR;
  1685. if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control),
  1686. &bar_data, sizeof(bar_data)))
  1687. return RX_DROP_MONITOR;
  1688. tid = le16_to_cpu(bar_data.control) >> 12;
  1689. tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]);
  1690. if (!tid_agg_rx)
  1691. return RX_DROP_MONITOR;
  1692. start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4;
  1693. /* reset session timer */
  1694. if (tid_agg_rx->timeout)
  1695. mod_timer(&tid_agg_rx->session_timer,
  1696. TU_TO_EXP_TIME(tid_agg_rx->timeout));
  1697. spin_lock(&tid_agg_rx->reorder_lock);
  1698. /* release stored frames up to start of BAR */
  1699. ieee80211_release_reorder_frames(hw, tid_agg_rx, start_seq_num);
  1700. spin_unlock(&tid_agg_rx->reorder_lock);
  1701. kfree_skb(skb);
  1702. return RX_QUEUED;
  1703. }
  1704. /*
  1705. * After this point, we only want management frames,
  1706. * so we can drop all remaining control frames to
  1707. * cooked monitor interfaces.
  1708. */
  1709. return RX_DROP_MONITOR;
  1710. }
  1711. static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
  1712. struct ieee80211_mgmt *mgmt,
  1713. size_t len)
  1714. {
  1715. struct ieee80211_local *local = sdata->local;
  1716. struct sk_buff *skb;
  1717. struct ieee80211_mgmt *resp;
  1718. if (compare_ether_addr(mgmt->da, sdata->vif.addr) != 0) {
  1719. /* Not to own unicast address */
  1720. return;
  1721. }
  1722. if (compare_ether_addr(mgmt->sa, sdata->u.mgd.bssid) != 0 ||
  1723. compare_ether_addr(mgmt->bssid, sdata->u.mgd.bssid) != 0) {
  1724. /* Not from the current AP or not associated yet. */
  1725. return;
  1726. }
  1727. if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
  1728. /* Too short SA Query request frame */
  1729. return;
  1730. }
  1731. skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
  1732. if (skb == NULL)
  1733. return;
  1734. skb_reserve(skb, local->hw.extra_tx_headroom);
  1735. resp = (struct ieee80211_mgmt *) skb_put(skb, 24);
  1736. memset(resp, 0, 24);
  1737. memcpy(resp->da, mgmt->sa, ETH_ALEN);
  1738. memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
  1739. memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
  1740. resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  1741. IEEE80211_STYPE_ACTION);
  1742. skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
  1743. resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
  1744. resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
  1745. memcpy(resp->u.action.u.sa_query.trans_id,
  1746. mgmt->u.action.u.sa_query.trans_id,
  1747. WLAN_SA_QUERY_TR_ID_LEN);
  1748. ieee80211_tx_skb(sdata, skb);
  1749. }
  1750. static ieee80211_rx_result debug_noinline
  1751. ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx)
  1752. {
  1753. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
  1754. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1755. /*
  1756. * From here on, look only at management frames.
  1757. * Data and control frames are already handled,
  1758. * and unknown (reserved) frames are useless.
  1759. */
  1760. if (rx->skb->len < 24)
  1761. return RX_DROP_MONITOR;
  1762. if (!ieee80211_is_mgmt(mgmt->frame_control))
  1763. return RX_DROP_MONITOR;
  1764. if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
  1765. return RX_DROP_MONITOR;
  1766. if (ieee80211_drop_unencrypted_mgmt(rx))
  1767. return RX_DROP_UNUSABLE;
  1768. return RX_CONTINUE;
  1769. }
  1770. static ieee80211_rx_result debug_noinline
  1771. ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
  1772. {
  1773. struct ieee80211_local *local = rx->local;
  1774. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1775. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
  1776. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1777. int len = rx->skb->len;
  1778. if (!ieee80211_is_action(mgmt->frame_control))
  1779. return RX_CONTINUE;
  1780. /* drop too small frames */
  1781. if (len < IEEE80211_MIN_ACTION_SIZE)
  1782. return RX_DROP_UNUSABLE;
  1783. if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC)
  1784. return RX_DROP_UNUSABLE;
  1785. if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
  1786. return RX_DROP_UNUSABLE;
  1787. switch (mgmt->u.action.category) {
  1788. case WLAN_CATEGORY_BACK:
  1789. /*
  1790. * The aggregation code is not prepared to handle
  1791. * anything but STA/AP due to the BSSID handling;
  1792. * IBSS could work in the code but isn't supported
  1793. * by drivers or the standard.
  1794. */
  1795. if (sdata->vif.type != NL80211_IFTYPE_STATION &&
  1796. sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  1797. sdata->vif.type != NL80211_IFTYPE_AP)
  1798. break;
  1799. /* verify action_code is present */
  1800. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  1801. break;
  1802. switch (mgmt->u.action.u.addba_req.action_code) {
  1803. case WLAN_ACTION_ADDBA_REQ:
  1804. if (len < (IEEE80211_MIN_ACTION_SIZE +
  1805. sizeof(mgmt->u.action.u.addba_req)))
  1806. goto invalid;
  1807. break;
  1808. case WLAN_ACTION_ADDBA_RESP:
  1809. if (len < (IEEE80211_MIN_ACTION_SIZE +
  1810. sizeof(mgmt->u.action.u.addba_resp)))
  1811. goto invalid;
  1812. break;
  1813. case WLAN_ACTION_DELBA:
  1814. if (len < (IEEE80211_MIN_ACTION_SIZE +
  1815. sizeof(mgmt->u.action.u.delba)))
  1816. goto invalid;
  1817. break;
  1818. default:
  1819. goto invalid;
  1820. }
  1821. goto queue;
  1822. case WLAN_CATEGORY_SPECTRUM_MGMT:
  1823. if (local->hw.conf.channel->band != IEEE80211_BAND_5GHZ)
  1824. break;
  1825. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1826. break;
  1827. /* verify action_code is present */
  1828. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  1829. break;
  1830. switch (mgmt->u.action.u.measurement.action_code) {
  1831. case WLAN_ACTION_SPCT_MSR_REQ:
  1832. if (len < (IEEE80211_MIN_ACTION_SIZE +
  1833. sizeof(mgmt->u.action.u.measurement)))
  1834. break;
  1835. ieee80211_process_measurement_req(sdata, mgmt, len);
  1836. goto handled;
  1837. case WLAN_ACTION_SPCT_CHL_SWITCH:
  1838. if (len < (IEEE80211_MIN_ACTION_SIZE +
  1839. sizeof(mgmt->u.action.u.chan_switch)))
  1840. break;
  1841. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1842. break;
  1843. if (memcmp(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN))
  1844. break;
  1845. goto queue;
  1846. }
  1847. break;
  1848. case WLAN_CATEGORY_SA_QUERY:
  1849. if (len < (IEEE80211_MIN_ACTION_SIZE +
  1850. sizeof(mgmt->u.action.u.sa_query)))
  1851. break;
  1852. switch (mgmt->u.action.u.sa_query.action) {
  1853. case WLAN_ACTION_SA_QUERY_REQUEST:
  1854. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1855. break;
  1856. ieee80211_process_sa_query_req(sdata, mgmt, len);
  1857. goto handled;
  1858. }
  1859. break;
  1860. case WLAN_CATEGORY_MESH_PLINK:
  1861. if (!ieee80211_vif_is_mesh(&sdata->vif))
  1862. break;
  1863. goto queue;
  1864. case WLAN_CATEGORY_MESH_PATH_SEL:
  1865. if (!mesh_path_sel_is_hwmp(sdata))
  1866. break;
  1867. goto queue;
  1868. }
  1869. return RX_CONTINUE;
  1870. invalid:
  1871. status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM;
  1872. /* will return in the next handlers */
  1873. return RX_CONTINUE;
  1874. handled:
  1875. if (rx->sta)
  1876. rx->sta->rx_packets++;
  1877. dev_kfree_skb(rx->skb);
  1878. return RX_QUEUED;
  1879. queue:
  1880. rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
  1881. skb_queue_tail(&sdata->skb_queue, rx->skb);
  1882. ieee80211_queue_work(&local->hw, &sdata->work);
  1883. if (rx->sta)
  1884. rx->sta->rx_packets++;
  1885. return RX_QUEUED;
  1886. }
  1887. static ieee80211_rx_result debug_noinline
  1888. ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx)
  1889. {
  1890. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1891. /* skip known-bad action frames and return them in the next handler */
  1892. if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM)
  1893. return RX_CONTINUE;
  1894. /*
  1895. * Getting here means the kernel doesn't know how to handle
  1896. * it, but maybe userspace does ... include returned frames
  1897. * so userspace can register for those to know whether ones
  1898. * it transmitted were processed or returned.
  1899. */
  1900. if (cfg80211_rx_mgmt(rx->sdata->dev, status->freq,
  1901. rx->skb->data, rx->skb->len,
  1902. GFP_ATOMIC)) {
  1903. if (rx->sta)
  1904. rx->sta->rx_packets++;
  1905. dev_kfree_skb(rx->skb);
  1906. return RX_QUEUED;
  1907. }
  1908. return RX_CONTINUE;
  1909. }
  1910. static ieee80211_rx_result debug_noinline
  1911. ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx)
  1912. {
  1913. struct ieee80211_local *local = rx->local;
  1914. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
  1915. struct sk_buff *nskb;
  1916. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1917. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1918. if (!ieee80211_is_action(mgmt->frame_control))
  1919. return RX_CONTINUE;
  1920. /*
  1921. * For AP mode, hostapd is responsible for handling any action
  1922. * frames that we didn't handle, including returning unknown
  1923. * ones. For all other modes we will return them to the sender,
  1924. * setting the 0x80 bit in the action category, as required by
  1925. * 802.11-2007 7.3.1.11.
  1926. * Newer versions of hostapd shall also use the management frame
  1927. * registration mechanisms, but older ones still use cooked
  1928. * monitor interfaces so push all frames there.
  1929. */
  1930. if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) &&
  1931. (sdata->vif.type == NL80211_IFTYPE_AP ||
  1932. sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
  1933. return RX_DROP_MONITOR;
  1934. /* do not return rejected action frames */
  1935. if (mgmt->u.action.category & 0x80)
  1936. return RX_DROP_UNUSABLE;
  1937. nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
  1938. GFP_ATOMIC);
  1939. if (nskb) {
  1940. struct ieee80211_mgmt *nmgmt = (void *)nskb->data;
  1941. nmgmt->u.action.category |= 0x80;
  1942. memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN);
  1943. memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
  1944. memset(nskb->cb, 0, sizeof(nskb->cb));
  1945. ieee80211_tx_skb(rx->sdata, nskb);
  1946. }
  1947. dev_kfree_skb(rx->skb);
  1948. return RX_QUEUED;
  1949. }
  1950. static ieee80211_rx_result debug_noinline
  1951. ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
  1952. {
  1953. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1954. ieee80211_rx_result rxs;
  1955. struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
  1956. __le16 stype;
  1957. rxs = ieee80211_work_rx_mgmt(rx->sdata, rx->skb);
  1958. if (rxs != RX_CONTINUE)
  1959. return rxs;
  1960. stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
  1961. if (!ieee80211_vif_is_mesh(&sdata->vif) &&
  1962. sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  1963. sdata->vif.type != NL80211_IFTYPE_STATION)
  1964. return RX_DROP_MONITOR;
  1965. switch (stype) {
  1966. case cpu_to_le16(IEEE80211_STYPE_BEACON):
  1967. case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
  1968. /* process for all: mesh, mlme, ibss */
  1969. break;
  1970. case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
  1971. case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
  1972. if (is_multicast_ether_addr(mgmt->da) &&
  1973. !is_broadcast_ether_addr(mgmt->da))
  1974. return RX_DROP_MONITOR;
  1975. /* process only for station */
  1976. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1977. return RX_DROP_MONITOR;
  1978. break;
  1979. case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
  1980. case cpu_to_le16(IEEE80211_STYPE_AUTH):
  1981. /* process only for ibss */
  1982. if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
  1983. return RX_DROP_MONITOR;
  1984. break;
  1985. default:
  1986. return RX_DROP_MONITOR;
  1987. }
  1988. /* queue up frame and kick off work to process it */
  1989. rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
  1990. skb_queue_tail(&sdata->skb_queue, rx->skb);
  1991. ieee80211_queue_work(&rx->local->hw, &sdata->work);
  1992. if (rx->sta)
  1993. rx->sta->rx_packets++;
  1994. return RX_QUEUED;
  1995. }
  1996. static void ieee80211_rx_michael_mic_report(struct ieee80211_hdr *hdr,
  1997. struct ieee80211_rx_data *rx)
  1998. {
  1999. int keyidx;
  2000. unsigned int hdrlen;
  2001. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  2002. if (rx->skb->len >= hdrlen + 4)
  2003. keyidx = rx->skb->data[hdrlen + 3] >> 6;
  2004. else
  2005. keyidx = -1;
  2006. if (!rx->sta) {
  2007. /*
  2008. * Some hardware seem to generate incorrect Michael MIC
  2009. * reports; ignore them to avoid triggering countermeasures.
  2010. */
  2011. return;
  2012. }
  2013. if (!ieee80211_has_protected(hdr->frame_control))
  2014. return;
  2015. if (rx->sdata->vif.type == NL80211_IFTYPE_AP && keyidx) {
  2016. /*
  2017. * APs with pairwise keys should never receive Michael MIC
  2018. * errors for non-zero keyidx because these are reserved for
  2019. * group keys and only the AP is sending real multicast
  2020. * frames in the BSS.
  2021. */
  2022. return;
  2023. }
  2024. if (!ieee80211_is_data(hdr->frame_control) &&
  2025. !ieee80211_is_auth(hdr->frame_control))
  2026. return;
  2027. mac80211_ev_michael_mic_failure(rx->sdata, keyidx, hdr, NULL,
  2028. GFP_ATOMIC);
  2029. }
  2030. /* TODO: use IEEE80211_RX_FRAGMENTED */
  2031. static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
  2032. struct ieee80211_rate *rate)
  2033. {
  2034. struct ieee80211_sub_if_data *sdata;
  2035. struct ieee80211_local *local = rx->local;
  2036. struct ieee80211_rtap_hdr {
  2037. struct ieee80211_radiotap_header hdr;
  2038. u8 flags;
  2039. u8 rate_or_pad;
  2040. __le16 chan_freq;
  2041. __le16 chan_flags;
  2042. } __packed *rthdr;
  2043. struct sk_buff *skb = rx->skb, *skb2;
  2044. struct net_device *prev_dev = NULL;
  2045. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2046. /*
  2047. * If cooked monitor has been processed already, then
  2048. * don't do it again. If not, set the flag.
  2049. */
  2050. if (rx->flags & IEEE80211_RX_CMNTR)
  2051. goto out_free_skb;
  2052. rx->flags |= IEEE80211_RX_CMNTR;
  2053. if (skb_headroom(skb) < sizeof(*rthdr) &&
  2054. pskb_expand_head(skb, sizeof(*rthdr), 0, GFP_ATOMIC))
  2055. goto out_free_skb;
  2056. rthdr = (void *)skb_push(skb, sizeof(*rthdr));
  2057. memset(rthdr, 0, sizeof(*rthdr));
  2058. rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
  2059. rthdr->hdr.it_present =
  2060. cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  2061. (1 << IEEE80211_RADIOTAP_CHANNEL));
  2062. if (rate) {
  2063. rthdr->rate_or_pad = rate->bitrate / 5;
  2064. rthdr->hdr.it_present |=
  2065. cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
  2066. }
  2067. rthdr->chan_freq = cpu_to_le16(status->freq);
  2068. if (status->band == IEEE80211_BAND_5GHZ)
  2069. rthdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_OFDM |
  2070. IEEE80211_CHAN_5GHZ);
  2071. else
  2072. rthdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_DYN |
  2073. IEEE80211_CHAN_2GHZ);
  2074. skb_set_mac_header(skb, 0);
  2075. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2076. skb->pkt_type = PACKET_OTHERHOST;
  2077. skb->protocol = htons(ETH_P_802_2);
  2078. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  2079. if (!ieee80211_sdata_running(sdata))
  2080. continue;
  2081. if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
  2082. !(sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES))
  2083. continue;
  2084. if (prev_dev) {
  2085. skb2 = skb_clone(skb, GFP_ATOMIC);
  2086. if (skb2) {
  2087. skb2->dev = prev_dev;
  2088. netif_receive_skb(skb2);
  2089. }
  2090. }
  2091. prev_dev = sdata->dev;
  2092. sdata->dev->stats.rx_packets++;
  2093. sdata->dev->stats.rx_bytes += skb->len;
  2094. }
  2095. if (prev_dev) {
  2096. skb->dev = prev_dev;
  2097. netif_receive_skb(skb);
  2098. return;
  2099. }
  2100. out_free_skb:
  2101. dev_kfree_skb(skb);
  2102. }
  2103. static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx,
  2104. ieee80211_rx_result res)
  2105. {
  2106. switch (res) {
  2107. case RX_DROP_MONITOR:
  2108. I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
  2109. if (rx->sta)
  2110. rx->sta->rx_dropped++;
  2111. /* fall through */
  2112. case RX_CONTINUE: {
  2113. struct ieee80211_rate *rate = NULL;
  2114. struct ieee80211_supported_band *sband;
  2115. struct ieee80211_rx_status *status;
  2116. status = IEEE80211_SKB_RXCB((rx->skb));
  2117. sband = rx->local->hw.wiphy->bands[status->band];
  2118. if (!(status->flag & RX_FLAG_HT))
  2119. rate = &sband->bitrates[status->rate_idx];
  2120. ieee80211_rx_cooked_monitor(rx, rate);
  2121. break;
  2122. }
  2123. case RX_DROP_UNUSABLE:
  2124. I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
  2125. if (rx->sta)
  2126. rx->sta->rx_dropped++;
  2127. dev_kfree_skb(rx->skb);
  2128. break;
  2129. case RX_QUEUED:
  2130. I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued);
  2131. break;
  2132. }
  2133. }
  2134. static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx)
  2135. {
  2136. ieee80211_rx_result res = RX_DROP_MONITOR;
  2137. struct sk_buff *skb;
  2138. #define CALL_RXH(rxh) \
  2139. do { \
  2140. res = rxh(rx); \
  2141. if (res != RX_CONTINUE) \
  2142. goto rxh_next; \
  2143. } while (0);
  2144. spin_lock(&rx->local->rx_skb_queue.lock);
  2145. if (rx->local->running_rx_handler)
  2146. goto unlock;
  2147. rx->local->running_rx_handler = true;
  2148. while ((skb = __skb_dequeue(&rx->local->rx_skb_queue))) {
  2149. spin_unlock(&rx->local->rx_skb_queue.lock);
  2150. /*
  2151. * all the other fields are valid across frames
  2152. * that belong to an aMPDU since they are on the
  2153. * same TID from the same station
  2154. */
  2155. rx->skb = skb;
  2156. CALL_RXH(ieee80211_rx_h_decrypt)
  2157. CALL_RXH(ieee80211_rx_h_check_more_data)
  2158. CALL_RXH(ieee80211_rx_h_sta_process)
  2159. CALL_RXH(ieee80211_rx_h_defragment)
  2160. CALL_RXH(ieee80211_rx_h_ps_poll)
  2161. CALL_RXH(ieee80211_rx_h_michael_mic_verify)
  2162. /* must be after MMIC verify so header is counted in MPDU mic */
  2163. CALL_RXH(ieee80211_rx_h_remove_qos_control)
  2164. CALL_RXH(ieee80211_rx_h_amsdu)
  2165. #ifdef CONFIG_MAC80211_MESH
  2166. if (ieee80211_vif_is_mesh(&rx->sdata->vif))
  2167. CALL_RXH(ieee80211_rx_h_mesh_fwding);
  2168. #endif
  2169. CALL_RXH(ieee80211_rx_h_data)
  2170. CALL_RXH(ieee80211_rx_h_ctrl);
  2171. CALL_RXH(ieee80211_rx_h_mgmt_check)
  2172. CALL_RXH(ieee80211_rx_h_action)
  2173. CALL_RXH(ieee80211_rx_h_userspace_mgmt)
  2174. CALL_RXH(ieee80211_rx_h_action_return)
  2175. CALL_RXH(ieee80211_rx_h_mgmt)
  2176. rxh_next:
  2177. ieee80211_rx_handlers_result(rx, res);
  2178. spin_lock(&rx->local->rx_skb_queue.lock);
  2179. #undef CALL_RXH
  2180. }
  2181. rx->local->running_rx_handler = false;
  2182. unlock:
  2183. spin_unlock(&rx->local->rx_skb_queue.lock);
  2184. }
  2185. static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
  2186. {
  2187. ieee80211_rx_result res = RX_DROP_MONITOR;
  2188. #define CALL_RXH(rxh) \
  2189. do { \
  2190. res = rxh(rx); \
  2191. if (res != RX_CONTINUE) \
  2192. goto rxh_next; \
  2193. } while (0);
  2194. CALL_RXH(ieee80211_rx_h_passive_scan)
  2195. CALL_RXH(ieee80211_rx_h_check)
  2196. ieee80211_rx_reorder_ampdu(rx);
  2197. ieee80211_rx_handlers(rx);
  2198. return;
  2199. rxh_next:
  2200. ieee80211_rx_handlers_result(rx, res);
  2201. #undef CALL_RXH
  2202. }
  2203. /*
  2204. * This function makes calls into the RX path, therefore
  2205. * it has to be invoked under RCU read lock.
  2206. */
  2207. void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
  2208. {
  2209. struct ieee80211_rx_data rx = {
  2210. .sta = sta,
  2211. .sdata = sta->sdata,
  2212. .local = sta->local,
  2213. .queue = tid,
  2214. .flags = 0,
  2215. };
  2216. struct tid_ampdu_rx *tid_agg_rx;
  2217. tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
  2218. if (!tid_agg_rx)
  2219. return;
  2220. spin_lock(&tid_agg_rx->reorder_lock);
  2221. ieee80211_sta_reorder_release(&sta->local->hw, tid_agg_rx);
  2222. spin_unlock(&tid_agg_rx->reorder_lock);
  2223. ieee80211_rx_handlers(&rx);
  2224. }
  2225. /* main receive path */
  2226. static int prepare_for_handlers(struct ieee80211_rx_data *rx,
  2227. struct ieee80211_hdr *hdr)
  2228. {
  2229. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2230. struct sk_buff *skb = rx->skb;
  2231. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2232. u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
  2233. int multicast = is_multicast_ether_addr(hdr->addr1);
  2234. switch (sdata->vif.type) {
  2235. case NL80211_IFTYPE_STATION:
  2236. if (!bssid && !sdata->u.mgd.use_4addr)
  2237. return 0;
  2238. if (!multicast &&
  2239. compare_ether_addr(sdata->vif.addr, hdr->addr1) != 0) {
  2240. if (!(sdata->dev->flags & IFF_PROMISC) ||
  2241. sdata->u.mgd.use_4addr)
  2242. return 0;
  2243. status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
  2244. }
  2245. break;
  2246. case NL80211_IFTYPE_ADHOC:
  2247. if (!bssid)
  2248. return 0;
  2249. if (ieee80211_is_beacon(hdr->frame_control)) {
  2250. return 1;
  2251. }
  2252. else if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid)) {
  2253. if (!(status->rx_flags & IEEE80211_RX_IN_SCAN))
  2254. return 0;
  2255. status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
  2256. } else if (!multicast &&
  2257. compare_ether_addr(sdata->vif.addr,
  2258. hdr->addr1) != 0) {
  2259. if (!(sdata->dev->flags & IFF_PROMISC))
  2260. return 0;
  2261. status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
  2262. } else if (!rx->sta) {
  2263. int rate_idx;
  2264. if (status->flag & RX_FLAG_HT)
  2265. rate_idx = 0; /* TODO: HT rates */
  2266. else
  2267. rate_idx = status->rate_idx;
  2268. rx->sta = ieee80211_ibss_add_sta(sdata, bssid,
  2269. hdr->addr2, BIT(rate_idx), GFP_ATOMIC);
  2270. }
  2271. break;
  2272. case NL80211_IFTYPE_MESH_POINT:
  2273. if (!multicast &&
  2274. compare_ether_addr(sdata->vif.addr,
  2275. hdr->addr1) != 0) {
  2276. if (!(sdata->dev->flags & IFF_PROMISC))
  2277. return 0;
  2278. status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
  2279. }
  2280. break;
  2281. case NL80211_IFTYPE_AP_VLAN:
  2282. case NL80211_IFTYPE_AP:
  2283. if (!bssid) {
  2284. if (compare_ether_addr(sdata->vif.addr,
  2285. hdr->addr1))
  2286. return 0;
  2287. } else if (!ieee80211_bssid_match(bssid,
  2288. sdata->vif.addr)) {
  2289. if (!(status->rx_flags & IEEE80211_RX_IN_SCAN) &&
  2290. !ieee80211_is_beacon(hdr->frame_control))
  2291. return 0;
  2292. status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
  2293. }
  2294. break;
  2295. case NL80211_IFTYPE_WDS:
  2296. if (bssid || !ieee80211_is_data(hdr->frame_control))
  2297. return 0;
  2298. if (compare_ether_addr(sdata->u.wds.remote_addr, hdr->addr2))
  2299. return 0;
  2300. break;
  2301. default:
  2302. /* should never get here */
  2303. WARN_ON(1);
  2304. break;
  2305. }
  2306. return 1;
  2307. }
  2308. /*
  2309. * This function returns whether or not the SKB
  2310. * was destined for RX processing or not, which,
  2311. * if consume is true, is equivalent to whether
  2312. * or not the skb was consumed.
  2313. */
  2314. static bool ieee80211_prepare_and_rx_handle(struct ieee80211_rx_data *rx,
  2315. struct sk_buff *skb, bool consume)
  2316. {
  2317. struct ieee80211_local *local = rx->local;
  2318. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2319. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2320. struct ieee80211_hdr *hdr = (void *)skb->data;
  2321. int prepares;
  2322. rx->skb = skb;
  2323. status->rx_flags |= IEEE80211_RX_RA_MATCH;
  2324. prepares = prepare_for_handlers(rx, hdr);
  2325. if (!prepares)
  2326. return false;
  2327. if (status->flag & RX_FLAG_MMIC_ERROR) {
  2328. if (status->rx_flags & IEEE80211_RX_RA_MATCH)
  2329. ieee80211_rx_michael_mic_report(hdr, rx);
  2330. return false;
  2331. }
  2332. if (!consume) {
  2333. skb = skb_copy(skb, GFP_ATOMIC);
  2334. if (!skb) {
  2335. if (net_ratelimit())
  2336. wiphy_debug(local->hw.wiphy,
  2337. "failed to copy skb for %s\n",
  2338. sdata->name);
  2339. return true;
  2340. }
  2341. rx->skb = skb;
  2342. }
  2343. ieee80211_invoke_rx_handlers(rx);
  2344. return true;
  2345. }
  2346. /*
  2347. * This is the actual Rx frames handler. as it blongs to Rx path it must
  2348. * be called with rcu_read_lock protection.
  2349. */
  2350. static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
  2351. struct sk_buff *skb)
  2352. {
  2353. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2354. struct ieee80211_local *local = hw_to_local(hw);
  2355. struct ieee80211_sub_if_data *sdata;
  2356. struct ieee80211_hdr *hdr;
  2357. __le16 fc;
  2358. struct ieee80211_rx_data rx;
  2359. struct ieee80211_sub_if_data *prev;
  2360. struct sta_info *sta, *tmp, *prev_sta;
  2361. int err = 0;
  2362. fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
  2363. memset(&rx, 0, sizeof(rx));
  2364. rx.skb = skb;
  2365. rx.local = local;
  2366. if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc))
  2367. local->dot11ReceivedFragmentCount++;
  2368. if (unlikely(test_bit(SCAN_HW_SCANNING, &local->scanning) ||
  2369. test_bit(SCAN_SW_SCANNING, &local->scanning)))
  2370. status->rx_flags |= IEEE80211_RX_IN_SCAN;
  2371. if (ieee80211_is_mgmt(fc))
  2372. err = skb_linearize(skb);
  2373. else
  2374. err = !pskb_may_pull(skb, ieee80211_hdrlen(fc));
  2375. if (err) {
  2376. dev_kfree_skb(skb);
  2377. return;
  2378. }
  2379. hdr = (struct ieee80211_hdr *)skb->data;
  2380. ieee80211_parse_qos(&rx);
  2381. ieee80211_verify_alignment(&rx);
  2382. if (ieee80211_is_data(fc)) {
  2383. prev_sta = NULL;
  2384. for_each_sta_info(local, hdr->addr2, sta, tmp) {
  2385. if (!prev_sta) {
  2386. prev_sta = sta;
  2387. continue;
  2388. }
  2389. rx.sta = prev_sta;
  2390. rx.sdata = prev_sta->sdata;
  2391. ieee80211_prepare_and_rx_handle(&rx, skb, false);
  2392. prev_sta = sta;
  2393. }
  2394. if (prev_sta) {
  2395. rx.sta = prev_sta;
  2396. rx.sdata = prev_sta->sdata;
  2397. if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
  2398. return;
  2399. goto out;
  2400. }
  2401. }
  2402. prev = NULL;
  2403. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  2404. if (!ieee80211_sdata_running(sdata))
  2405. continue;
  2406. if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
  2407. sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  2408. continue;
  2409. /*
  2410. * frame is destined for this interface, but if it's
  2411. * not also for the previous one we handle that after
  2412. * the loop to avoid copying the SKB once too much
  2413. */
  2414. if (!prev) {
  2415. prev = sdata;
  2416. continue;
  2417. }
  2418. rx.sta = sta_info_get_bss(prev, hdr->addr2);
  2419. rx.sdata = prev;
  2420. ieee80211_prepare_and_rx_handle(&rx, skb, false);
  2421. prev = sdata;
  2422. }
  2423. if (prev) {
  2424. rx.sta = sta_info_get_bss(prev, hdr->addr2);
  2425. rx.sdata = prev;
  2426. if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
  2427. return;
  2428. }
  2429. out:
  2430. dev_kfree_skb(skb);
  2431. }
  2432. /*
  2433. * This is the receive path handler. It is called by a low level driver when an
  2434. * 802.11 MPDU is received from the hardware.
  2435. */
  2436. void ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb)
  2437. {
  2438. struct ieee80211_local *local = hw_to_local(hw);
  2439. struct ieee80211_rate *rate = NULL;
  2440. struct ieee80211_supported_band *sband;
  2441. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2442. WARN_ON_ONCE(softirq_count() == 0);
  2443. if (WARN_ON(status->band < 0 ||
  2444. status->band >= IEEE80211_NUM_BANDS))
  2445. goto drop;
  2446. sband = local->hw.wiphy->bands[status->band];
  2447. if (WARN_ON(!sband))
  2448. goto drop;
  2449. /*
  2450. * If we're suspending, it is possible although not too likely
  2451. * that we'd be receiving frames after having already partially
  2452. * quiesced the stack. We can't process such frames then since
  2453. * that might, for example, cause stations to be added or other
  2454. * driver callbacks be invoked.
  2455. */
  2456. if (unlikely(local->quiescing || local->suspended))
  2457. goto drop;
  2458. /*
  2459. * The same happens when we're not even started,
  2460. * but that's worth a warning.
  2461. */
  2462. if (WARN_ON(!local->started))
  2463. goto drop;
  2464. if (likely(!(status->flag & RX_FLAG_FAILED_PLCP_CRC))) {
  2465. /*
  2466. * Validate the rate, unless a PLCP error means that
  2467. * we probably can't have a valid rate here anyway.
  2468. */
  2469. if (status->flag & RX_FLAG_HT) {
  2470. /*
  2471. * rate_idx is MCS index, which can be [0-76]
  2472. * as documented on:
  2473. *
  2474. * http://wireless.kernel.org/en/developers/Documentation/ieee80211/802.11n
  2475. *
  2476. * Anything else would be some sort of driver or
  2477. * hardware error. The driver should catch hardware
  2478. * errors.
  2479. */
  2480. if (WARN((status->rate_idx < 0 ||
  2481. status->rate_idx > 76),
  2482. "Rate marked as an HT rate but passed "
  2483. "status->rate_idx is not "
  2484. "an MCS index [0-76]: %d (0x%02x)\n",
  2485. status->rate_idx,
  2486. status->rate_idx))
  2487. goto drop;
  2488. } else {
  2489. if (WARN_ON(status->rate_idx < 0 ||
  2490. status->rate_idx >= sband->n_bitrates))
  2491. goto drop;
  2492. rate = &sband->bitrates[status->rate_idx];
  2493. }
  2494. }
  2495. status->rx_flags = 0;
  2496. /*
  2497. * key references and virtual interfaces are protected using RCU
  2498. * and this requires that we are in a read-side RCU section during
  2499. * receive processing
  2500. */
  2501. rcu_read_lock();
  2502. /*
  2503. * Frames with failed FCS/PLCP checksum are not returned,
  2504. * all other frames are returned without radiotap header
  2505. * if it was previously present.
  2506. * Also, frames with less than 16 bytes are dropped.
  2507. */
  2508. skb = ieee80211_rx_monitor(local, skb, rate);
  2509. if (!skb) {
  2510. rcu_read_unlock();
  2511. return;
  2512. }
  2513. ieee80211_tpt_led_trig_rx(local,
  2514. ((struct ieee80211_hdr *)skb->data)->frame_control,
  2515. skb->len);
  2516. __ieee80211_rx_handle_packet(hw, skb);
  2517. rcu_read_unlock();
  2518. return;
  2519. drop:
  2520. kfree_skb(skb);
  2521. }
  2522. EXPORT_SYMBOL(ieee80211_rx);
  2523. /* This is a version of the rx handler that can be called from hard irq
  2524. * context. Post the skb on the queue and schedule the tasklet */
  2525. void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
  2526. {
  2527. struct ieee80211_local *local = hw_to_local(hw);
  2528. BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
  2529. skb->pkt_type = IEEE80211_RX_MSG;
  2530. skb_queue_tail(&local->skb_queue, skb);
  2531. tasklet_schedule(&local->tasklet);
  2532. }
  2533. EXPORT_SYMBOL(ieee80211_rx_irqsafe);